@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");
      }
    }
  }
Ejemplo n.º 3
0
    @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);
            }
          }
        }

        if (AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED == type) {

          AccessibilityNodeInfo rootNode = node;
          rootNode = service.getRootInActiveWindow();

          List<AccessibilityNodeInfo> nodes =
              rootNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard");
          //                    nodes = rootNode.findAccessibilityNodeInfosByText("设置");
          boolean settingActivity = nodes != null && nodes.size() > 0;

          nodes = rootNode.findAccessibilityNodeInfosByText("开发者选项");
          boolean devActivity = nodes != null && nodes.size() == 1;

          settingActivity =
              new ComponentName("com.android.settings", "com.android.settings.Settings")
                  .equals(mLastComponentName);
          devActivity =
              new ComponentName("com.android.settings", "com.android.settings.SubSettings")
                  .equals(mLastComponentName);

          Log.d(TAG, "settingActivity: " + settingActivity + " devActivity: " + devActivity);
          if (settingActivity) {
            Log.d(TAG, "scroll to bottom");
            nodes =
                rootNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard");
            nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);

            nodes = rootNode.findAccessibilityNodeInfosByText("开发者选项");
            if (null != nodes && nodes.size() > 0) {
              Log.d(TAG, "open developer's setting");
              nodes.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
          } else if (devActivity) {
            nodes = rootNode.findAccessibilityNodeInfosByText("开启");

            AccessibilityNodeInfo open =
                AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("开启"));
            Log.d(TAG, "open: " + open);
            open = open.getParent().getChild(1);
            if (!open.isChecked()) {
              //                            open.setChecked(true);
              open.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
            } else {
              Log.i(TAG, "adb is open yet.");
            }

            AccessibilityNodeInfo usb =
                AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("USB调试"));
            if (null == usb) {
              AccessibilityNodeInfo listview =
                  AccessibilityNodeInfoFinder.find(
                      rootNode, new UiSelector().className("android.widget.ListView"));
              listview.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            }
            usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("USB调试"));
            Log.i(TAG, "usb: " + usb);
            Log.i(TAG, "p: " + usb.getParent());
            Log.i(TAG, "pp: " + usb.getParent().getParent());
            AccessibilityNodeInfo debug = usb.getParent().getParent().getChild(1).getChild(0);
            Log.i(TAG, "debug: " + debug);

            debug =
                AccessibilityNodeInfoFinder.find(
                    rootNode,
                    new UiSelector()
                        .className("android.widget.Switch")
                        .fromParent(new UiSelector().className("android.widget.LinearLayout")));
            Log.i(TAG, "debug: " + debug);

            usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("调试"));
            int index = 0;
            for (int i = 0; i < usb.getParent().getChildCount(); i++) {
              if ("调试".equals(usb.getParent().getChild(i).getText())) {
                index = i;
                break;
              }
            }
            usb = usb.getParent().getChild(index + 1);
            Log.i(TAG, "debug: " + debug);

            //                        if (!debug.isChecked()) {
            //                            debug.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            //                        } else {
            //                            Log.i(TAG, "adb debug is open yet.");
            //                        }

            //                        if (null != nodes && nodes.size() > 0) {
            //                            Log.d(TAG, nodes.size() + " node found.");
            //                            Log.d(TAG, "open developer's option");
            //
            // nodes.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
            //                        } else {
            //                            return;
            //                        }
          }
        }
      }

      //        node.recycle();
    }