Android Open Source - PreCTS Pre C T S Automate






From Project

Back to project page PreCTS.

License

The source code is released under:

Apache License

If you think the Android project PreCTS listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.androidhuman.ctsprepare.automator;
/*from w w w  .  j  a v a  2 s.  co  m*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.json.JSONException;

import android.os.Build;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.uiautomator.core.Configurator;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class PreCTSAutomate extends UiAutomatorTestCase{
  
  private static final int MAX_RETRY = 5;

  public void testActivateDeviceAdmin() throws UiObjectNotFoundException {
    // Get installed device admin listview
    
    UiScrollable deviceAdminList = null;
    
    // Try double-column case first
    deviceAdminList = new UiScrollable(new UiSelector().className(ListView.class).instance(1));
    if(!deviceAdminList.exists()){
      deviceAdminList = new UiScrollable(new UiSelector().className("android.widget.ListView"));
      System.out.println("Single column mode detected");
    }
    
    // First device admin receiver
    UiObject receiver1 = 
        deviceAdminList.getChildByText(
            new UiSelector().className("android.widget.LinearLayout"), 
            "android.deviceadmin.cts.CtsDeviceAdminReceiver");
    UiObject checkbox1 = receiver1.getChild(new UiSelector().className("android.widget.CheckBox"));
    // If not checked
    if(!checkbox1.isChecked()){
      receiver1.click();
      (new UiObject(new UiSelector().text("Activate"))).clickAndWaitForNewWindow();
    }
    
    // Second device admin receiver
    UiObject receiver2 = 
        deviceAdminList.getChildByText(
            new UiSelector().className("android.widget.LinearLayout"), 
            "android.deviceadmin.cts.CtsDeviceAdminReceiver2");
    UiObject checkbox2 = receiver2.getChild(new UiSelector().className("android.widget.CheckBox"));
    // If not checked
    if(!checkbox2.isChecked()){
      receiver2.click();
      (new UiObject(new UiSelector().text("Activate"))).clickAndWaitForNewWindow();
    }
    
    UiDevice.getInstance().pressHome();
  }
  
  public void testActivateWifi() throws UiObjectNotFoundException {
    if(Build.VERSION.SDK_INT >= 18){
      Configurator config = Configurator.getInstance();
      config.setWaitForSelectorTimeout(2000); // Set UI wait timeout for 2 seconds
    }
    int numRetry = 0;
    
    File file = new File("/data/local/tmp/wifi.info");
    
    BufferedReader reader = null;
    WifiAp apData = null;
    try{
      reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();
      apData = WifiAp.fromJson(line);
    }catch(IOException e){
      fail(e.getMessage());
    }catch(JSONException e){
      fail(e.getMessage());
    }
    
    numRetry=0;
    while(true){
      try{
        UiObject wifiSwitch = new UiObject(new UiSelector().className("android.widget.Switch"));
        if(!wifiSwitch.isChecked()){
          wifiSwitch.clickAndWaitForNewWindow();
        }
        break;
      }catch(UiObjectNotFoundException e){
        if(numRetry>2){
          break; // Fallthroungh to next level
        }
        skipAdditionalDialogsForWifi();
      }
    }
    UiScrollable wifiList = null;
    
    // Try double column mode first
    wifiList = new UiScrollable(new UiSelector().className(ListView.class).instance(1));
    
    if(!wifiList.exists()){
      // Single column mode
      wifiList = new UiScrollable(new UiSelector().className(ListView.class));
    }
    
    numRetry=0;
    while(true){
      try{
        System.out.println("Wifi AP count : "+wifiList.getChildCount());
        break;
      }catch(Exception e){
        if(numRetry>MAX_RETRY){
          fail();
        }
        System.out.println("Skipping dialog...");
        skipAdditionalDialogsForWifi();
      }
    }
    UiObject ap = null;
    
    numRetry=0;
    while(true){
      try{
        // Select AP
        System.out.println("Trying to find AP : "+apData.apName);
        ap = wifiList.getChildByText(
            new UiSelector().className(LinearLayout.class), apData.apName);
        break;
      }catch(UiObjectNotFoundException e){
        if(numRetry>MAX_RETRY){
          fail("Can't find AP. Cancel Wi-Fi setting.");
        }
        UiDevice.getInstance().waitForWindowUpdate("com.android.settings", 1500);
      }
      skipAdditionalDialogsForWifi();
      numRetry++;
    }
    ap.clickAndWaitForNewWindow();
    
    numRetry = 0;
    while(true){
      try{
        UiObject editText = new UiObject(new UiSelector().className("android.widget.EditText"));
        editText.click();
        editText.setText(apData.password); // Enter password
        break;
      }catch(UiObjectNotFoundException e){  
        if(numRetry>MAX_RETRY){
          fail("Could not find authentication dialog.");
        }
      }
      skipAdditionalDialogs();
      numRetry++;
    }
    
    numRetry = 0;
    while(true){
      try{
        UiObject connectButton = new UiObject(new UiSelector().text("Connect"));
        connectButton.clickAndWaitForNewWindow();
        break;
      }catch(UiObjectNotFoundException e){
        if(numRetry>MAX_RETRY){
          fail("Could not find Connect button.");
        }
        skipAdditionalDialogs();
        numRetry++;
      }
    }
    
    UiDevice.getInstance().pressHome();
  }
  
  public void testSetTimeout() throws UiObjectNotFoundException{
    UiScrollable settingsList = null;
    
    // Try double column case first
    settingsList = new UiScrollable(new UiSelector().className(ListView.class).instance(1));
    if(!settingsList.exists()){
      // If current layout is single-column case
      settingsList = new UiScrollable(new UiSelector().className("android.widget.ListView"));
    }
    
    UiObject timeout = 
        settingsList.getChildByText(
            new UiSelector().className(LinearLayout.class), "Screen timeout");
    
    // Select display timeout
    timeout.clickAndWaitForNewWindow();
    
    // Select '10 minutes' for timeout
    UiObject timeoutItem = new UiObject(new UiSelector().textContains("10 min"));
    timeoutItem.clickAndWaitForNewWindow();
    
    UiDevice.getInstance().pressHome();
  }
  
  public void testAddGoogleAccount() throws UiObjectNotFoundException{
    if(Build.VERSION.SDK_INT >= 18){
      Configurator config = Configurator.getInstance();
      config.setWaitForSelectorTimeout(2000); // Set UI wait timeout for 2 seconds
    }
    
    File file = new File("/data/local/tmp/account.info");
    
    BufferedReader reader = null;
    GoogleAccount account = null;
    try{
      reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();
      account = GoogleAccount.fromJson(line);
    }catch(IOException e){
      fail(e.getMessage());
    }catch(JSONException e){
      fail(e.getMessage());
    }
    
    UiScrollable accountTypeList = new UiScrollable(new UiSelector().className("android.widget.ListView"));
    UiObject google = accountTypeList.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "Google");
    
    // Select 'Google'
    google.clickAndWaitForNewWindow();
    
    // Find 'Existing' button
    UiObject existingButton = new UiObject(new UiSelector().text("Existing"));
    existingButton.clickAndWaitForNewWindow();
    
    int count = 0;
    while(true){
      try{
        // Find 'Email' field
        UiObject email = new UiObject(new UiSelector().textContains("Email"));
        email.click();
        email.setText(account.email);
        
        // Find 'Password' Field
        UiObject password = new UiObject(new UiSelector().className(android.widget.EditText.class).instance(1));
        password.click();
        password.setText(account.password);
        
        break;
      }catch(UiObjectNotFoundException e){
        count++;
        if(count>3){
          fail("Could not find Google account auth screen.");
        }
        System.out.println("Dialog found. trying to dismiss...");
        
      }
      
      skipAdditionalDialogs();
    }
    
    UiObject nextBtn = new UiObject(new UiSelector().description("Next"));
    nextBtn.clickAndWaitForNewWindow();
    
    // Find terms of service 'OK' button
    UiObject tosOk = new UiObject(new UiSelector().text("OK"));
    tosOk.clickAndWaitForNewWindow();
    
    // Skip Google+ signup
    try{
      UiObject skipGp = new UiObject(new UiSelector().text("Not now"));
      skipGp.clickAndWaitForNewWindow(1500);
    }catch(UiObjectNotFoundException e){
      System.out.println("Google+ signup has already done");
    }
    
    // Proceed to next (Google Services)
    try{
      UiObject skipGs = new UiObject(new UiSelector().description("Next"));
      skipGs.clickAndWaitForNewWindow(1500);
    }catch(UiObjectNotFoundException e){
      System.out.println("Google services screen not found");
    }
    
    // Skip Entertainment
    try{
      UiObject skipEnt = new UiObject(new UiSelector().text("Not now"));
      skipEnt.clickAndWaitForNewWindow(1500);
    }catch(UiObjectNotFoundException e){
      System.out.println("Payment setup has already done");
    }
    
    // Proceed final step (Account sign-in successful)
    try{
      UiObject finalNext = new UiObject(new UiSelector().description("Next"));
      finalNext.clickAndWaitForNewWindow(1500);
    }catch(UiObjectNotFoundException e){
      System.out.println("Account setup confirmation screen not found");
    }
    
    // Finished!
    UiDevice.getInstance().pressHome();
  }
  
  public void testSetInternetAsDefault() throws UiObjectNotFoundException {
    if(Build.VERSION.SDK_INT >= 18){
      Configurator config = Configurator.getInstance();
      config.setWaitForSelectorTimeout(2000); // Set UI wait timeout for 2 seconds
    }
    
    // Get installed app list
    UiScrollable deviceAdminList = 
        new UiScrollable(new UiSelector().className("android.widget.GridView"));
    
    // First device admin receiver
    UiObject internet = 
        deviceAdminList.getChildByText(
            new UiSelector().className("android.widget.LinearLayout"), 
            "Internet");
    
    // select internet
    internet.click();
    
    UiObject alwaysBtn = new UiObject(new UiSelector().text("Always"));
    alwaysBtn.click();
    
    UiObject okBtn = new UiObject(new UiSelector().text("OK"));
    okBtn.click();
    
    // Done!
    UiDevice.getInstance().pressHome();
    
  }
  
  private void skipAdditionalDialogs(){
    UiObject dismissDlgBtn = null;
    System.out.println("Trying to dismiss dialogs..");
    try{
      // If input method dialog popup showed up
      dismissDlgBtn = new UiObject(new UiSelector().text("OK"));
      dismissDlgBtn.clickAndWaitForNewWindow(1000);
    }catch(UiObjectNotFoundException e){}
    
    try{
      // If swype instruction dialog popup showed up
      dismissDlgBtn = new UiObject(new UiSelector().text("Dismiss"));
      dismissDlgBtn.clickAndWaitForNewWindow(1000);
    }catch(UiObjectNotFoundException e){}
  
    try{
      // Wi-Fi calling
      dismissDlgBtn = new UiObject(new UiSelector().text("Skip"));
      dismissDlgBtn.clickAndWaitForNewWindow(1000);
    }catch(UiObjectNotFoundException e){}
  }
  
  private void skipAdditionalDialogsForWifi(){
    UiObject dismissDlgBtn = null;
    
    try{
      // Wi-Fi calling
      dismissDlgBtn = new UiObject(new UiSelector().text("Skip"));
      dismissDlgBtn.clickAndWaitForNewWindow(1000);
    }catch(UiObjectNotFoundException e){}
    
    try{
      // If input method dialog popup showed up
      dismissDlgBtn = new UiObject(new UiSelector().text("OK"));
      dismissDlgBtn.clickAndWaitForNewWindow(1000);
    }catch(UiObjectNotFoundException e){}
    
    try{
      // If swype instruction dialog popup showed up
      dismissDlgBtn = new UiObject(new UiSelector().text("Dismiss"));
      dismissDlgBtn.clickAndWaitForNewWindow(1000);
    }catch(UiObjectNotFoundException e){}
    
  }
  
}




Java Source Code List

com.androidhuman.ctsprepare.automator.GoogleAccount.java
com.androidhuman.ctsprepare.automator.PreCTSAutomate.java
com.androidhuman.ctsprepare.automator.WifiAp.java
com.androidhuman.ctsprepare.data.BasicDeviceInfo.java
com.androidhuman.ctsprepare.data.Event.java
com.androidhuman.ctsprepare.data.GoogleAccount.java
com.androidhuman.ctsprepare.data.Model.java
com.androidhuman.ctsprepare.data.ProxySettings.java
com.androidhuman.ctsprepare.data.Task.java
com.androidhuman.ctsprepare.data.Waiver.java
com.androidhuman.ctsprepare.data.WifiAp.java
com.androidhuman.ctsprepare.dialog.CtsMediaDownloadDialog.java
com.androidhuman.ctsprepare.dialog.EditGoogleAccountDialog.java
com.androidhuman.ctsprepare.dialog.EditMacroDialog.java
com.androidhuman.ctsprepare.dialog.EditProxyDialog.java
com.androidhuman.ctsprepare.dialog.EditSdkPathDialog.java
com.androidhuman.ctsprepare.dialog.EditWifiApDialog.java
com.androidhuman.ctsprepare.dialog.TargetSelectionDialog.java
com.androidhuman.ctsprepare.shell.ConfigureMacroShell.java
com.androidhuman.ctsprepare.shell.Main.java
com.androidhuman.ctsprepare.util.AdbCommand.java
com.androidhuman.ctsprepare.util.AdbWrapper.java
com.androidhuman.ctsprepare.util.AsyncTask.java
com.androidhuman.ctsprepare.util.Download.java
com.androidhuman.ctsprepare.util.Utils.java
com.androidhuman.ctsprepare.util.Worker.java
com.androidhuman.prectsandroidsettingsapp.EnableSyncActivity.java
com.androidhuman.prectsandroidsettingsapp.FinishHandler.java
com.androidhuman.prectsandroidsettingsapp.MainActivity.java