Java 类android.app.UiAutomation 实例源码

项目:kgb    文件:KeyboardSwitcher.java   
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static void injectTap(int x, int y, UiAutomation uiAutomation, boolean sync) {
  long downTime = System.currentTimeMillis();
  MotionEvent eventDown = MotionEvent.obtain(downTime,
      downTime, MotionEvent.ACTION_DOWN,
      x, y, 0);
  eventDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
  Log.d(TAG, "Injecting " + eventDown);

  if (!uiAutomation.injectInputEvent(eventDown, sync)) {
    Log.d(TAG, "Injection failed");
  }

  MotionEvent eventUp = MotionEvent.obtain(eventDown);
  eventUp.setAction(MotionEvent.ACTION_UP);

  Log.d(TAG, "Injecting " + eventUp);
  if (!uiAutomation.injectInputEvent(eventUp, sync)) {
    Log.d(TAG, "Injection failed");
  }

  eventDown.recycle();
  eventUp.recycle();
}
项目:Brevent    文件:HideApiOverrideU.java   
public void connect() {
    if (mHandlerThread.isAlive()) {
        throw new IllegalStateException("Already connected!");
    }
    mHandlerThread.start();
    mUiAutomation = new UiAutomation(mHandlerThread.getLooper(), new UiAutomationConnection());
    try {
        mUiAutomation.disconnect();
    } catch (RuntimeException e) {
        // do nothing
    }
    mUiAutomation.connect();
}
项目:appium-uiautomator2-server    文件:NotificationListener.java   
public void run() {
    while (true) {
        AccessibilityEvent accessibilityEvent = null;
        toastMessages = init();

        //return true if the AccessibilityEvent type is NOTIFICATION type
        UiAutomation.AccessibilityEventFilter eventFilter = new UiAutomation.AccessibilityEventFilter() {
            @Override
            public boolean accept(AccessibilityEvent event) {
                return event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
            }
        };
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                // Not performing any event.
            }
        };

        try {
            //wait for AccessibilityEvent filter
            accessibilityEvent = UiAutomatorBridge.getInstance().getUiAutomation()
                    .executeAndWaitForEvent(runnable /*executable event*/, eventFilter /* event to filter*/, 500 /*time out in ms*/);
        } catch (Exception ignore) {}

        if (accessibilityEvent != null) {
            toastMessages = accessibilityEvent.getText();
            previousTime = currentTimeMillis();
        }
        if(stopLooping){
            break;
        }
    }
}
项目:permissive    文件:PermissiveTestRule.java   
private void grantAllPermissions() {
  Context context = InstrumentationRegistry.getTargetContext();
  try {
    PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
    UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
    for (int i = 0; i < packageInfo.requestedPermissions.length; ++i) {
      if ((packageInfo.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
        grantReal(uiAutomation, packageInfo.requestedPermissions[i]);
      }
    }
  } catch (PackageManager.NameNotFoundException e) {
    Log.w(TAG, "packageInfo not found for: " + context.getPackageName());
  }
}
项目:permissive    文件:PermissiveTestRule.java   
private static void grantReal(UiAutomation automation, String permission) {
  try {
    String targetPackageName = InstrumentationRegistry.getTargetContext().getPackageName();
    ParcelFileDescriptor pfn = automation.executeShellCommand("pm grant " + targetPackageName + " " + permission);
    pfn.close();
  } catch (IOException e) {
    Log.w(TAG, e);
  }
}
项目:atlas    文件:InstrumentationHook.java   
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public UiAutomation getUiAutomation() {
    return mBase.getUiAutomation();
}
项目:VirtualHook    文件:InstrumentationDelegate.java   
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public UiAutomation getUiAutomation() {
    return base.getUiAutomation();
}
项目:Brevent    文件:HideApiOverrideU.java   
public UiAutomation getUiAutomation() {
    return mUiAutomation;
}
项目:TPlayer    文件:InstrumentationDelegate.java   
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public UiAutomation getUiAutomation() {
    return base.getUiAutomation();
}
项目:container    文件:InstrumentationDelegate.java   
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public UiAutomation getUiAutomation() {
    return base.getUiAutomation();
}
项目:android-apkbox    文件:HookActivityInstrumentationHnadler.java   
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public UiAutomation getUiAutomation() {
    return mInstrumentation.getUiAutomation();
}
项目:android-apkbox    文件:HookActivityInstrumentationHnadler.java   
@TargetApi(Build.VERSION_CODES.N)
@Override
public UiAutomation getUiAutomation(int flags) {
    return mInstrumentation.getUiAutomation(flags);
}
项目:ACDD    文件:InstrumentationHook.java   
@Override
@TargetApi(18)
public UiAutomation getUiAutomation() {
    return this.mBase.getUiAutomation();
}
项目:EnhancedPUMA    文件:MyUiAutomatorTestRunner.java   
/**
 * Called after all test classes are in place, ready to test
 */
protected void start() {
    TestCaseCollector collector = getTestCaseCollector(this.getClass().getClassLoader());
    try {
        collector.addTestClasses(mTestClasses);
    } catch (ClassNotFoundException e) {
        // will be caught by uncaught handler
        throw new RuntimeException(e.getMessage(), e);
    }
    if (mDebug) {
        Debug.waitForDebugger();
    }
    mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME);
    mHandlerThread.setDaemon(true);
    mHandlerThread.start();
    UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
    automationWrapper.connect();

    long startTime = SystemClock.uptimeMillis();
    TestResult testRunResult = new TestResult();
    ResultReporter resultPrinter;
    String outputFormat = mParams.getString("outputFormat");
    List<TestCase> testCases = collector.getTestCases();
    Bundle testRunOutput = new Bundle();
    if ("simple".equals(outputFormat)) {
        resultPrinter = new SimpleResultPrinter(System.out, true);
    } else {
        resultPrinter = new WatcherResultPrinter(testCases.size());
    }
    try {
        automationWrapper.setRunAsMonkey(mMonkey);
        mUiDevice = MyUiDevice.getInstance();
        UiAutomation uiAutomation = automationWrapper.getUiAutomation();
        mUiDevice.initialize(new ShellUiAutomatorBridge(uiAutomation));
        mUiDevice.setUiAutomation(uiAutomation);

        String traceType = mParams.getString("traceOutputMode");
        if (traceType != null) {
            Tracer.Mode mode = Tracer.Mode.valueOf(Tracer.Mode.class, traceType);
            if (mode == Tracer.Mode.FILE || mode == Tracer.Mode.ALL) {
                String filename = mParams.getString("traceLogFilename");
                if (filename == null) {
                    throw new RuntimeException("Name of log file not specified. " + "Please specify it using traceLogFilename parameter");
                }
                Tracer.getInstance().setOutputFilename(filename);
            }
            Tracer.getInstance().setOutputMode(mode);
        }

        // add test listeners
        testRunResult.addListener(resultPrinter);
        // add all custom listeners
        for (TestListener listener : mTestListeners) {
            testRunResult.addListener(listener);
        }

        // run tests for realz!
        for (TestCase testCase : testCases) {
            prepareTestCase(testCase);
            testCase.run(testRunResult);
        }
    } catch (Throwable t) {
        // catch all exceptions so a more verbose error message can be outputted
        resultPrinter.printUnexpectedError(t);
    } finally {
        long runTime = SystemClock.uptimeMillis() - startTime;
        resultPrinter.print(testRunResult, runTime, testRunOutput);
        automationWrapper.disconnect();
        automationWrapper.setRunAsMonkey(false);
        mHandlerThread.quit();
    }
}
项目:EnhancedPUMA    文件:MyUiDevice.java   
public UiAutomation getUiAutomation() {
    return this.mAutomation;
}
项目:EnhancedPUMA    文件:MyUiDevice.java   
public void setUiAutomation(UiAutomation uiauto) {
    this.mAutomation = uiauto;
}
项目:appium-uiautomator2-server    文件:UiAutomatorBridge.java   
public UiAutomation getUiAutomation() {
    return (UiAutomation)getField(CLASS_UI_AUTOMATOR_BRIDGE, FIELD_UI_AUTOMATOR, uiAutomatorBridge);
}
项目:kgb    文件:KeyboardTypeAction.java   
public KeyboardTypeAction(UiAutomation uiAutomation) {
  this.uiAutomation = uiAutomation;
}
项目:AndroidSkinChange    文件:SkinInstrumentation.java   
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public UiAutomation getUiAutomation() {
    return targetInstrumentation.getUiAutomation();
}
项目:ApkLauncher    文件:InstrumentationWrapper.java   
@Override
public UiAutomation getUiAutomation() {
    // TODO Auto-generated method stub
    return mBase.getUiAutomation();
}
项目:AtlasForAndroid    文件:InstrumentationHook.java   
@TargetApi(18)
public UiAutomation getUiAutomation() {
    return this.mBase.getUiAutomation();
}
项目:PUMA    文件:MyUiAutomatorTestRunner.java   
/**
 * Called after all test classes are in place, ready to test
 */
protected void start() {
    TestCaseCollector collector = getTestCaseCollector(this.getClass().getClassLoader());
    try {
        collector.addTestClasses(mTestClasses);
    } catch (ClassNotFoundException e) {
        // will be caught by uncaught handler
        throw new RuntimeException(e.getMessage(), e);
    }
    if (mDebug) {
        Debug.waitForDebugger();
    }
    mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME);
    mHandlerThread.setDaemon(true);
    mHandlerThread.start();
    UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
    automationWrapper.connect();

    long startTime = SystemClock.uptimeMillis();
    TestResult testRunResult = new TestResult();
    ResultReporter resultPrinter;
    String outputFormat = mParams.getString("outputFormat");
    List<TestCase> testCases = collector.getTestCases();
    Bundle testRunOutput = new Bundle();
    if ("simple".equals(outputFormat)) {
        resultPrinter = new SimpleResultPrinter(System.out, true);
    } else {
        resultPrinter = new WatcherResultPrinter(testCases.size());
    }
    try {
        automationWrapper.setRunAsMonkey(mMonkey);
        mUiDevice = MyUiDevice.getInstance();
        UiAutomation uiAutomation = automationWrapper.getUiAutomation();
        mUiDevice.initialize(new ShellUiAutomatorBridge(uiAutomation));
        mUiDevice.setUiAutomation(uiAutomation);

        String traceType = mParams.getString("traceOutputMode");
        if (traceType != null) {
            Tracer.Mode mode = Tracer.Mode.valueOf(Tracer.Mode.class, traceType);
            if (mode == Tracer.Mode.FILE || mode == Tracer.Mode.ALL) {
                String filename = mParams.getString("traceLogFilename");
                if (filename == null) {
                    throw new RuntimeException("Name of log file not specified. " + "Please specify it using traceLogFilename parameter");
                }
                Tracer.getInstance().setOutputFilename(filename);
            }
            Tracer.getInstance().setOutputMode(mode);
        }

        // add test listeners
        testRunResult.addListener(resultPrinter);
        // add all custom listeners
        for (TestListener listener : mTestListeners) {
            testRunResult.addListener(listener);
        }

        // run tests for realz!
        for (TestCase testCase : testCases) {
            prepareTestCase(testCase);
            testCase.run(testRunResult);
        }
    } catch (Throwable t) {
        // catch all exceptions so a more verbose error message can be outputted
        resultPrinter.printUnexpectedError(t);
    } finally {
        long runTime = SystemClock.uptimeMillis() - startTime;
        resultPrinter.print(testRunResult, runTime, testRunOutput);
        automationWrapper.disconnect();
        automationWrapper.setRunAsMonkey(false);
        mHandlerThread.quit();
    }
}
项目:PUMA    文件:MyUiDevice.java   
public UiAutomation getUiAutomation() {
    return this.mAutomation;
}
项目:PUMA    文件:MyUiDevice.java   
public void setUiAutomation(UiAutomation uiauto) {
    this.mAutomation = uiauto;
}
项目:effective_android_sample    文件:MainActivityTest.java   
public void testAirplaneModeToOn() {
  UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
  // Activityの起動を監視するリスナーをセット
  mMainLaunched = false;
  uiAutomation
      .setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
          if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
            // ウィンドウのコンテンツが変わった
            if (TARGET_PKG.equals(event.getPackageName())) {
              // MainActivityが起動した
              mMainLaunched = true;
            }
          }
        }
      });

  // MainActivity起動
  Activity target = launchActivity(TARGET_PKG, MainActivity.class, null);
  try {
    // MainActivity起動待ち
    do {
      Thread.sleep(1000);
    } while (!mMainLaunched);

    // 機内モードをOnにする
    // Settingsの起動
    Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getInstrumentation().getContext().startActivity(intent);

    // Settingsの起動待ち
    AccessibilityNodeInfo root;
    while (true) {
      root = uiAutomation.getRootInActiveWindow();
      if (root != null && SETTINGS_PKG.equals(root.getPackageName())) {
        break;
      } else {
        Thread.sleep(1000);
      }
    }

    // ボタンを押す
    List<AccessibilityNodeInfo> list = root
        .findAccessibilityNodeInfosByViewId("android:id/list");
    AccessibilityNodeInfo listViewInfo = list.get(0);
    AccessibilityNodeInfo airplaneModeView = listViewInfo.getChild(0);
    List<AccessibilityNodeInfo> checkList = airplaneModeView
        .findAccessibilityNodeInfosByViewId("android:id/checkbox");
    AccessibilityNodeInfo airplaneModeCheck = checkList.get(0);
    if (!airplaneModeCheck.isChecked()) {
      airplaneModeView.performAction(AccessibilityNodeInfo.ACTION_CLICK);
    }

    // Backキーを押してSettingsの終了
    uiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);

    // 機内モード反映待ち
    Thread.sleep(10000);

    // TextViewの文字列検証
    String expected = target
        .getString(org.techbooster.uiautomationsample.R.string.airplane_mode_off);
    TextView textView = (TextView) target
        .findViewById(org.techbooster.uiautomationsample.R.id.text_view);
    assertEquals(expected, textView.getText().toString());

  } catch (Exception e) {
    fail(e.getMessage());
    e.printStackTrace();
  } finally {
    if (target != null) {
      target.finish();
    }
  }
}
项目:EnhancedPUMA    文件:MyInteractionController.java   
/**
 * Rotates right and also freezes rotation in that position by
 * disabling the sensors. If you want to un-freeze the rotation
 * and re-enable the sensors see {@link #unfreezeRotation()}. Note
 * that doing so may cause the screen contents to rotate
 * depending on the current physical position of the test device.
 * @throws RemoteException
 */
public void setRotationRight() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_270);
}
项目:EnhancedPUMA    文件:MyInteractionController.java   
/**
 * Rotates left and also freezes rotation in that position by
 * disabling the sensors. If you want to un-freeze the rotation
 * and re-enable the sensors see {@link #unfreezeRotation()}. Note
 * that doing so may cause the screen contents to rotate
 * depending on the current physical position of the test device.
 * @throws RemoteException
 */
public void setRotationLeft() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_90);
}
项目:EnhancedPUMA    文件:MyInteractionController.java   
/**
 * Rotates up and also freezes rotation in that position by
 * disabling the sensors. If you want to un-freeze the rotation
 * and re-enable the sensors see {@link #unfreezeRotation()}. Note
 * that doing so may cause the screen contents to rotate
 * depending on the current physical position of the test device.
 * @throws RemoteException
 */
public void setRotationNatural() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_0);
}
项目:EnhancedPUMA    文件:MyInteractionController.java   
/**
 * Disables the sensors and freezes the device rotation at its
 * current rotation state.
 * @throws RemoteException
 */
public void freezeRotation() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_CURRENT);
}
项目:EnhancedPUMA    文件:MyInteractionController.java   
/**
 * Re-enables the sensors and un-freezes the device rotation
 * allowing its contents to rotate with the device physical rotation.
 * @throws RemoteException
 */
public void unfreezeRotation() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_UNFREEZE);
}
项目:kgb    文件:KeyboardSwitcher.java   
/**
 * When created manually as an accessibility event delegate
 *
 * @param baseContext the context this class will use
 * @param automation the automation instance use to inject taps
 */
public KeyboardSwitcher(Context baseContext, UiAutomation automation) {
  mAutomation = automation;
  attachBaseContext(baseContext);
}
项目:PUMA    文件:MyInteractionController.java   
/**
 * Rotates right and also freezes rotation in that position by
 * disabling the sensors. If you want to un-freeze the rotation
 * and re-enable the sensors see {@link #unfreezeRotation()}. Note
 * that doing so may cause the screen contents to rotate
 * depending on the current physical position of the test device.
 * @throws RemoteException
 */
public void setRotationRight() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_270);
}
项目:PUMA    文件:MyInteractionController.java   
/**
 * Rotates left and also freezes rotation in that position by
 * disabling the sensors. If you want to un-freeze the rotation
 * and re-enable the sensors see {@link #unfreezeRotation()}. Note
 * that doing so may cause the screen contents to rotate
 * depending on the current physical position of the test device.
 * @throws RemoteException
 */
public void setRotationLeft() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_90);
}
项目:PUMA    文件:MyInteractionController.java   
/**
 * Rotates up and also freezes rotation in that position by
 * disabling the sensors. If you want to un-freeze the rotation
 * and re-enable the sensors see {@link #unfreezeRotation()}. Note
 * that doing so may cause the screen contents to rotate
 * depending on the current physical position of the test device.
 * @throws RemoteException
 */
public void setRotationNatural() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_0);
}
项目:PUMA    文件:MyInteractionController.java   
/**
 * Disables the sensors and freezes the device rotation at its
 * current rotation state.
 * @throws RemoteException
 */
public void freezeRotation() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_CURRENT);
}
项目:PUMA    文件:MyInteractionController.java   
/**
 * Re-enables the sensors and un-freezes the device rotation
 * allowing its contents to rotate with the device physical rotation.
 * @throws RemoteException
 */
public void unfreezeRotation() {
    mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_UNFREEZE);
}