@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(); }
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(); }
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; } } }
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()); } }
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); } }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Override public UiAutomation getUiAutomation() { return mBase.getUiAutomation(); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Override public UiAutomation getUiAutomation() { return base.getUiAutomation(); }
public UiAutomation getUiAutomation() { return mUiAutomation; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public UiAutomation getUiAutomation() { return mInstrumentation.getUiAutomation(); }
@TargetApi(Build.VERSION_CODES.N) @Override public UiAutomation getUiAutomation(int flags) { return mInstrumentation.getUiAutomation(flags); }
@Override @TargetApi(18) public UiAutomation getUiAutomation() { return this.mBase.getUiAutomation(); }
/** * 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(); } }
public UiAutomation getUiAutomation() { return this.mAutomation; }
public void setUiAutomation(UiAutomation uiauto) { this.mAutomation = uiauto; }
public UiAutomation getUiAutomation() { return (UiAutomation)getField(CLASS_UI_AUTOMATOR_BRIDGE, FIELD_UI_AUTOMATOR, uiAutomatorBridge); }
public KeyboardTypeAction(UiAutomation uiAutomation) { this.uiAutomation = uiAutomation; }
@Override @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public UiAutomation getUiAutomation() { return targetInstrumentation.getUiAutomation(); }
@Override public UiAutomation getUiAutomation() { // TODO Auto-generated method stub return mBase.getUiAutomation(); }
@TargetApi(18) public UiAutomation getUiAutomation() { return this.mBase.getUiAutomation(); }
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(); } } }
/** * 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); }
/** * 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); }
/** * 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); }
/** * Disables the sensors and freezes the device rotation at its * current rotation state. * @throws RemoteException */ public void freezeRotation() { mUiAutomatorBridge.setRotation(UiAutomation.ROTATION_FREEZE_CURRENT); }
/** * 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); }
/** * 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); }