Java 类android.app.UiAutomation.OnAccessibilityEventListener 实例源码
项目:EnhancedPUMA
文件:LaunchApp.java
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);
}
项目:EnhancedPUMA
文件:EnhancedPUMALauncher.java
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);
}
项目:PUMA
文件:LaunchApp.java
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);
}
项目:effective_android_sample
文件:MainActivityTest.java
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();
}
}
}