Java Code Examples for android.app.UiAutomation#setOnAccessibilityEventListener()

The following examples show how to use android.app.UiAutomation#setOnAccessibilityEventListener() . 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: 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();
    }
  }
}