首页 > 代码库 > [uiautomator篇][11]wifi

[uiautomator篇][11]wifi

package com.softwinner.network.wifi;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.util.Log;


import java.io.IOException;

import static android.support.test.InstrumentationRegistry.getArguments;

import static android.support.test.InstrumentationRegistry.getContext;
import static org.junit.Assert.assertTrue;

/**
 * @author liuzhipeng
 * Created by Administrator on 2017/6/27.
 */

public class wifiBaseClass {
    private String packageName = "com.example.black.wifiswitch";
    private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    private String ssid ;
    private String password ;
    private String mLogTag ;
    private WifiManager mWifiManager;
//    = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);


    public wifiBaseClass(Context context, UiDevice device, String SSID, String passwd, String logTag, String packName){
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mDevice = device;
        ssid = SSID;
        password = passwd;
        mLogTag = logTag;
        packageName = packName;
    }

    /**
     * after connect wifi, check the network is available
     * @author liuzhipeng
     * @throws UiObjectNotFoundException
     * @throws InterruptedException
     */
    public void connectWifiAndCheckNetwork() throws UiObjectNotFoundException, InterruptedException {

        final String ssidStr = "com.example.black.wifiswitch:id/ssid";
        final String passwdIdStr = "com.example.black.wifiswitch:id/password";
        final String connectIdStr = "com.example.black.wifiswitch:id/Connect" ;
        Log.i(mLogTag,"trigger on wifi");
        triggerOnWifi();
        Log.i(mLogTag,"open wifiswitch apk");
        openApplication(packageName);
        Thread.sleep(5000);
        try {GetWiFiParameters();} catch (RemoteException e) {e.printStackTrace();}
        Log.i(mLogTag,"connect wifi: " + ssid);
        wakeupScreen();
        UiObject ssidObj = mDevice.findObject(new UiSelector().resourceId(ssidStr));
        ssidObj.setText(ssid);
        wakeupScreen();
        UiObject passwordObj = mDevice.findObject(new UiSelector().resourceId(passwdIdStr));
        passwordObj.setText(password);
        wakeupScreen();
        UiObject connectObj = mDevice.findObject(new UiSelector().resourceId(connectIdStr));
        connectObj.click();
        Thread.sleep(5000);
        assertTrue("wifi state not enabled", checkWifiState() == 3);
        Log.i(mLogTag, "check network available?");
        assertTrue("wifi network unavailable", isNetworkAvailable());
        Log.i(mLogTag, "network available");
    }

    /**
     * open third application:
     * @author liuzhipeng
     * @param packageNameStr
     */
    public void openApplication(String packageNameStr){

        try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
        UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        /* Start from the home screen*/
        mDevice.pressHome();

//        final String launcherPackage = mDevice.getLauncherPackageName();
//        assertThat(launcherPackage,notNullValue());
//        try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
//        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
//                5000);

        // launch the app
        Context context = InstrumentationRegistry.getContext();
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(packageNameStr);
        // Clear out any previous instances
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(packageNameStr).depth(0)),
                5000);
    }

    /** get wifi parameters: ssid and password
     * @author liuzhipeng
     * @throws RemoteException
     */
    private void GetWiFiParameters() throws RemoteException {

        Bundle bundle = getArguments();
        if (bundle.getString("ssid") != null) {
            ssid = bundle.getString("ssid");
            if (bundle.getString("password") != null) {
                password = bundle.getString("password");
            } else {
                password = null;
            }
        }
    }

    /**
     * trigger on wifi
     * @author liuzhipeng
     */
    public void triggerOnWifi(){

//        WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
        checkWifiState();
    }

    /**
     * trigger off wifi
     * @author liuzhipeng
     */
    public void triggerOffWifi(){

//        WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
        checkWifiState();
    }

    /**
     * check wifi state
     * @author liuzhipeng
     * @return wifiState
     */
    public int checkWifiState(){

//        WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
        int tempInt =  mWifiManager.getWifiState();
        switch (tempInt){
            case 0:
                Log.i(mLogTag, "wifi state disabling");
                break;
            case 1:
                Log.i(mLogTag, "wifi state disabled");
                break;
            case 2:
                Log.i(mLogTag, "wifi state enabling");
                break;
            case 3:
                Log.i(mLogTag, "wifi state enabled");
                break;
            case 4:
                Log.i(mLogTag, "wifi state unknown");
                break;
            default:
                break;
        }
        return tempInt;

    }

    /**
     * @author liuzhipeng
     * check network is available
     * @return true if networkAviabile else false
     */
    public static boolean isNetworkAvailable(){

        ConnectivityManager connectivityManager = (ConnectivityManager) InstrumentationRegistry.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return (info != null && info.isConnected() && (info.getType() == ConnectivityManager.TYPE_WIFI));
    }

    /**
     * wakeup screen
     * @author liuzhipeng
     */
    public void wakeupScreen(){
        Context context = InstrumentationRegistry.getContext();
        PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
        wl.acquire();
        wl.release();
    }

    public void quitApplication(String packageNameStr)
    {
        try {
            mDevice.executeShellCommand("am force-stop "+ packageNameStr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void goToSleep(){
        Context context = InstrumentationRegistry.getContext();
        PowerManager pm =(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//        pm.goTosleep()
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ON_AFTER_RELEASE| PowerManager.PARTIAL_WAKE_LOCK,"wakeLockUtil");
        wl.acquire();
        wl.release();

    }
}

 

[uiautomator篇][11]wifi