@LargeTest public void testFindAccessibilityNodeInfoByViewId() throws Exception { final long startTimeMillis = SystemClock.uptimeMillis(); try { AccessibilityNodeInfo button = mUiTestAutomationBridge.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.button5); assertNotNull(button); assertEquals(0, button.getChildCount()); // bounds Rect bounds = new Rect(); button.getBoundsInParent(bounds); assertEquals(0, bounds.left); assertEquals(0, bounds.top); assertEquals(160, bounds.right); assertEquals(100, bounds.bottom); // char sequence attributes assertEquals("com.android.frameworks.coretests", button.getPackageName()); assertEquals("android.widget.Button", button.getClassName()); assertEquals("Button5", button.getText()); assertNull(button.getContentDescription()); // boolean attributes assertTrue(button.isFocusable()); assertTrue(button.isClickable()); assertTrue(button.isEnabled()); assertFalse(button.isFocused()); assertTrue(button.isClickable()); assertFalse(button.isPassword()); assertFalse(button.isSelected()); assertFalse(button.isCheckable()); assertFalse(button.isChecked()); // actions assertEquals(ACTION_FOCUS | ACTION_SELECT | ACTION_CLEAR_SELECTION, button.getActions()); } finally { if (DEBUG) { final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; Log.i(LOG_TAG, "testFindAccessibilityNodeInfoByViewId: " + elapsedTimeMillis + "ms"); } } }
@LargeTest public void testAccessibilityEventGetSource() throws Exception { final long startTimeMillis = SystemClock.uptimeMillis(); try { // find a view and make sure it is not focused final AccessibilityNodeInfo button = mUiTestAutomationBridge.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.button5); assertFalse(button.isFocused()); AccessibilityEvent event = mUiTestAutomationBridge.executeCommandAndWaitForAccessibilityEvent( new Runnable() { @Override public void run() { // focus the view assertTrue(button.performAction(ACTION_FOCUS)); } }, new Predicate<AccessibilityEvent>() { @Override public boolean apply(AccessibilityEvent event) { return (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && event.getPackageName().equals(getActivity().getPackageName()) && event.getText().get(0).equals(button.getText())); } }, TIMEOUT_PROPAGATE_ACCESSIBILITY_EVENT_MILLIS); // check the last event assertNotNull(event); // check that last event source AccessibilityNodeInfo source = event.getSource(); assertNotNull(source); // bounds Rect buttonBounds = new Rect(); button.getBoundsInParent(buttonBounds); Rect sourceBounds = new Rect(); source.getBoundsInParent(sourceBounds); assertEquals(buttonBounds.left, sourceBounds.left); assertEquals(buttonBounds.right, sourceBounds.right); assertEquals(buttonBounds.top, sourceBounds.top); assertEquals(buttonBounds.bottom, sourceBounds.bottom); // char sequence attributes assertEquals(button.getPackageName(), source.getPackageName()); assertEquals(button.getClassName(), source.getClassName()); assertEquals(button.getText(), source.getText()); assertSame(button.getContentDescription(), source.getContentDescription()); // boolean attributes assertSame(button.isFocusable(), source.isFocusable()); assertSame(button.isClickable(), source.isClickable()); assertSame(button.isEnabled(), source.isEnabled()); assertNotSame(button.isFocused(), source.isFocused()); assertSame(button.isLongClickable(), source.isLongClickable()); assertSame(button.isPassword(), source.isPassword()); assertSame(button.isSelected(), source.isSelected()); assertSame(button.isCheckable(), source.isCheckable()); assertSame(button.isChecked(), source.isChecked()); } finally { if (DEBUG) { final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; Log.i(LOG_TAG, "testAccessibilityEventGetSource: " + elapsedTimeMillis + "ms"); } } }
@Override public void onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) { Log.d(TAG, "event: " + event); // Log.d(TAG, "event: " + toString(event)); AccessibilityNodeInfo node = event.getSource(); CharSequence packageName = event.getPackageName(); int type = event.getEventType(); int windowId = event.getWindowId(); if (null == node) { Log.w(TAG, "node is null, ^~^"); return; } if ("com.android.settings".equals(packageName)) { // http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { mLastComponentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString()); Log.i(TAG, "last activity: " + mLastComponentName); // from QueryController if (event.getText() != null && event.getText().size() > 0) { if (event.getText().get(0) != null) { String lastActivityName = event.getText().get(0).toString(); // Log.i(TAG, "last activity name: " + lastActivityName); } } boolean appDetail = new ComponentName( "com.android.settings", "com.android.settings.applications.InstalledAppDetailsTop") .equals(mLastComponentName); boolean stopConfirm = new ComponentName("com.android.settings", "android.app.AlertDialog") .equals(mLastComponentName); Log.d(TAG, "appDetail: " + appDetail + " stopConfirm: " + stopConfirm); if (appDetail) { AccessibilityNodeInfo stop = AccessibilityNodeInfoFinder.find( service.getRootInActiveWindow(), new UiSelector().text("强行停止")); if (stop.isEnabled()) { stop.performAction(AccessibilityNodeInfo.ACTION_CLICK); } else { Log.i(TAG, "app has stop yet."); } } if (stopConfirm) { AccessibilityNodeInfo confirm = AccessibilityNodeInfoFinder.find( service.getRootInActiveWindow(), new UiSelector().text("确定")); confirm.performAction(AccessibilityNodeInfo.ACTION_CLICK); Intent self = new Intent(service, MainActivity.class); self.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); service.startActivity(self); } } } }