@Override
 public void setUp() throws Exception {
   super.setUp();
   mUiTestAutomationBridge = new UiTestAutomationBridge();
   mUiTestAutomationBridge.connect();
   mUiTestAutomationBridge.waitForIdle(
       IDLE_EVENT_TIME_DELTA_MILLIS, GLOBAL_IDLE_DETECTION_TIMEOUT_MILLIS);
   mUiTestAutomationBridge.executeCommandAndWaitForAccessibilityEvent(
       new Runnable() {
         // wait for the first accessibility event
         @Override
         public void run() {
           // bring up the activity
           getActivity();
         }
       },
       new Predicate<AccessibilityEvent>() {
         @Override
         public boolean apply(AccessibilityEvent event) {
           return (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
               && event.getPackageName().equals(getActivity().getPackageName()));
         }
       },
       TIMEOUT_PROPAGATE_ACCESSIBILITY_EVENT_MILLIS);
 }
  @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");
      }
    }
  }