android.app.UiAutomation.OnAccessibilityEventListener Java Examples

The following examples show how to use android.app.UiAutomation.OnAccessibilityEventListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UiDevice.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
public void initialize(ShellUiAutomatorBridge uiAutomatorBridge) {
	mUiAutomationBridge = uiAutomatorBridge;
	// 监听activity变化,用于getAct
	try {
		IActivityManager am = ActivityManagerNative.getDefault();
		am.setActivityController(new DummyActivityController());
	} catch (RemoteException e) {
	}
	UiAutomation uiAutomation = uiAutomatorBridge.getUiAutomation();
	uiAutomation
			.setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
				public void onAccessibilityEvent(AccessibilityEvent event) {
					synchronized (onAccessibilityEventListeners) {
						for (OnAccessibilityEventListener listener : onAccessibilityEventListeners) {
							listener.onAccessibilityEvent(event);
						}
					}
				}
			});
}
 
Example #2
Source File: NotificationListenerTests.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    toastText =  new ArrayList<>();
    toastText.add("toast text");

    originalAccessibilityEventListener = mock(OnAccessibilityEventListener.class);
    PowerMockito.mockStatic(InstrumentationRegistry.class);
    when(InstrumentationRegistry.getInstrumentation()).thenReturn(null);

    PowerMockito.mockStatic(UiDevice.class);
    when(UiDevice.getInstance(null)).thenReturn(mock(UiDevice.class));
    when(UiDevice.getInstance()).thenReturn(mock(UiDevice.class));

    PowerMockito.mockStatic(UiAutomatorBridge.class);
    when(UiAutomatorBridge.getInstance()).thenReturn(mock(UiAutomatorBridge.class));

    PowerMockito.mockStatic(UiAutomation.class);
    uiAutomation = mock(UiAutomation.class);
    when(UiAutomation.getInstance()).thenReturn(uiAutomation);
    when(uiAutomation.getOnAccessibilityEventListener())
            .thenReturn(originalAccessibilityEventListener);

    notificationListener = spy(new NotificationListener());
}
 
Example #3
Source File: QueryController.java    From za-Farmer with MIT License 5 votes vote down vote up
public QueryController(UiAutomatorBridge bridge) {
    mUiAutomatorBridge = bridge;
    bridge.setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            synchronized (mLock) {
                switch(event.getEventType()) {
                    case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                        // don't trust event.getText(), check for nulls
                        if (event.getText() != null && event.getText().size() > 0) {
                            if(event.getText().get(0) != null)
                                mLastActivityName = event.getText().get(0).toString();
                        }
                       break;
                    case AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY:
                        // don't trust event.getText(), check for nulls
                        if (event.getText() != null && event.getText().size() > 0)
                            if(event.getText().get(0) != null)
                                mLastTraversedText = event.getText().get(0).toString();
                        if (DEBUG)
                            Log.d(LOG_TAG, "Last text selection reported: " +
                                    mLastTraversedText);
                        break;
                }
                mLock.notifyAll();
            }
        }
    });
}
 
Example #4
Source File: QueryController.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public QueryController(UiAutomatorBridge bridge) {
	mUiAutomatorBridge = bridge;
	bridge.setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
		@Override
		public void onAccessibilityEvent(AccessibilityEvent event) {
			synchronized (mLock) {
				switch (event.getEventType()) {
				case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
					// don't trust event.getText(), check for nulls
					if (event.getText() != null
							&& event.getText().size() > 0) {
						if (event.getText().get(0) != null)
							mLastActivityName = event.getText().get(0)
									.toString();
					}
					break;
				case AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY:
					// don't trust event.getText(), check for nulls
					if (event.getText() != null
							&& event.getText().size() > 0)
						if (event.getText().get(0) != null)
							mLastTraversedText = event.getText().get(0)
									.toString();
					if (DEBUG)
						Log.d(LOG_TAG, "Last text selection reported: "
								+ mLastTraversedText);
					break;
				}
				mLock.notifyAll();
			}
		}
	});
}
 
Example #5
Source File: UiDevice.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 添加元素事件监听
 * 
 * @param onAccessibilityEventListener
 */
public void addAccessibilityEventListener(
		OnAccessibilityEventListener onAccessibilityEventListener) {
	synchronized (onAccessibilityEventListeners) {
		onAccessibilityEventListeners.add(onAccessibilityEventListener);
	}
}
 
Example #6
Source File: UiAutomation.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Nullable
public OnAccessibilityEventListener getOnAccessibilityEventListener() {
    try {
        return (OnAccessibilityEventListener) getField(android.app.UiAutomation.class,
                FIELD_ON_ACCESSIBILITY_EVENT_LISTENER, uiAutomation);
    } catch (Exception e) {
        /* mOnAccessibilityEventListener is no longer accessible on Android P */
        return null;
    }
}
 
Example #7
Source File: NotificationListenerTests.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingOnStopIfListenerIsAlreadyStopped() {
    doReturn(false).when(notificationListener).isListening();
    notificationListener.stop();
    verify(uiAutomation, never())
            .setOnAccessibilityEventListener((OnAccessibilityEventListener) any());
}
 
Example #8
Source File: NotificationListenerTests.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingOnStartIfListenerIsAlreadyStarted() {
    doReturn(true).when(notificationListener).isListening();
    notificationListener.start();
    verify(uiAutomation, never())
            .setOnAccessibilityEventListener((OnAccessibilityEventListener) any());
}
 
Example #9
Source File: NotificationListenerTests.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRestoreOriginalListener() {
    ArgumentCaptor<OnAccessibilityEventListener> argumentCaptor =
            ArgumentCaptor.forClass(OnAccessibilityEventListener.class);
    doNothing().when(uiAutomation).setOnAccessibilityEventListener(argumentCaptor.capture());
    doReturn(false).when(notificationListener).isListening();
    notificationListener.start();
    doReturn(true).when(notificationListener).isListening();
    notificationListener.stop();

    assertEquals(notificationListener, argumentCaptor.getAllValues().get(0));
    assertEquals(originalAccessibilityEventListener, argumentCaptor.getAllValues().get(1));
}
 
Example #10
Source File: UiAutomatorBridge.java    From za-Farmer with MIT License 4 votes vote down vote up
public void setOnAccessibilityEventListener(OnAccessibilityEventListener listener) {
    mUiAutomation.setOnAccessibilityEventListener(listener);
}
 
Example #11
Source File: UiAutomatorBridge.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public void setOnAccessibilityEventListener(
		OnAccessibilityEventListener listener) {
	mUiAutomation.setOnAccessibilityEventListener(listener);
}
 
Example #12
Source File: UiDevice.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public void removeAccessibilityEventListener(
		OnAccessibilityEventListener onAccessibilityEventListener) {
	synchronized (onAccessibilityEventListeners) {
		onAccessibilityEventListeners.remove(onAccessibilityEventListener);
	}
}
 
Example #13
Source File: UiAutomation.java    From appium-uiautomator2-server with Apache License 2.0 4 votes vote down vote up
public void setOnAccessibilityEventListener(OnAccessibilityEventListener listener) {
    uiAutomation.setOnAccessibilityEventListener(listener);
}
 
Example #14
Source File: LaunchApp.java    From PUMA with Apache License 2.0 4 votes vote down vote up
public void _testPassiveMonkey() {
	dev = getMyUiDevice();
	bridge = dev.getUiAutomatorBridge();

	final String appPackageName = "com.chenio.android.sixpark";
	final String fn = "/sdcard/haos/events.log";
	Util.openFile(fn, true);

	// intercept all events 
	dev.getUiAutomation().setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
		public void onAccessibilityEvent(AccessibilityEvent event) {
			CharSequence pack_name = event.getPackageName();
			// only process related events
			if (pack_name != null && pack_name.equals(appPackageName)) {
				// log events
				Util.log2File(event);
			} else {
				// ignore the rest: e.g. notification etc
				// Util.err("UNKNOWN EVENT " + pack_name);
			}
		}
	});

	Thread t = new Thread() {
		public void run() {
			while (true) {
				dev.waitForIdle();

				AccessibilityNodeInfo rootNode = bridge.getRootInActiveWindow();
				if (rootNode.getPackageName().equals(appPackageName)) {
					String timestamp = new SimpleDateFormat("MM-dd'T'HH-mm-ss-SSS").format(new Date());

					boolean status = dev.takeScreenshot(new File("/sdcard/haos/" + timestamp + ".png"), 0.1f, 90);
					Util.log("Dumped screenshot: " + status);

					dev.dumpWindowHierarchy(timestamp + ".xml"); // default location "/data/local/tmp/local/tmp/*.xml"
					Util.log("Dumped view tree");
				}

				SystemClock.sleep(2000);
			}
		}
	};
	t.start();

	SystemClock.sleep(30000); // 30s

	Util.closeFile(fn);
}
 
Example #15
Source File: MainActivityTest.java    From effective_android_sample with Apache License 2.0 4 votes vote down vote up
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();
    }
  }
}