aboutsummaryrefslogtreecommitdiff
path: root/src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java
diff options
context:
space:
mode:
authorKevin Jin <kjin@google.com>2014-05-29 15:35:31 -0700
committerKevin Jin <kjin@google.com>2014-06-05 14:01:34 -0700
commita738fe74f57f48dde2dd7a28479bab3f5441dadb (patch)
tree6bcb449c5545660d5057a6323e0e95c5eda0cf5c /src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java
parentb4e825291041d963c5bda0349638565949d999f6 (diff)
downloaddroiddriver-a738fe74f57f48dde2dd7a28479bab3f5441dadb.tar.gz
add AccessibilityDriver for testing Accessibility
This is the first cut with known issues, e.g. needs to handle click on EditText differently, etc. Thic cl sets up the architecture of AccessibilityDriver; details will be filled in follow-up cls. Change-Id: I2881b28075eba478a5aad9d7e945b5d55e78da89
Diffstat (limited to 'src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java')
-rw-r--r--src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java106
1 files changed, 7 insertions, 99 deletions
diff --git a/src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java b/src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java
index d465816..9291f13 100644
--- a/src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java
+++ b/src/com/google/android/droiddriver/uiautomation/UiAutomationDriver.java
@@ -17,112 +17,20 @@
package com.google.android.droiddriver.uiautomation;
import android.app.Instrumentation;
-import android.app.UiAutomation;
-import android.content.Context;
-import android.os.SystemClock;
-import android.view.accessibility.AccessibilityEvent;
-import android.view.accessibility.AccessibilityManager;
-import android.view.accessibility.AccessibilityNodeInfo;
-import com.google.android.droiddriver.base.BaseDroidDriver;
-import com.google.android.droiddriver.exceptions.TimeoutException;
-import com.google.android.droiddriver.uiautomation.UiAutomationContext.UiAutomationCallable;
-import com.google.android.droiddriver.util.Logs;
+import com.google.android.droiddriver.uiautomation.base.BaseUiAutomationDriver;
/**
- * Implementation of DroidDriver that is driven via the accessibility layer.
+ * Implementation of DroidDriver that gets attributes via the Accessibility API
+ * and is acted upon via synthesized events.
*/
-public class UiAutomationDriver extends BaseDroidDriver {
- // TODO: magic const from UiAutomator, but may not be useful
- /**
- * This value has the greatest bearing on the appearance of test execution
- * speeds. This value is used as the minimum time to wait before considering
- * the UI idle after each action.
- */
- private static final long QUIET_TIME_TO_BE_CONSIDERD_IDLE_STATE = 500;// ms
-
- private final UiAutomationContext context;
- private final UiAutomationUiDevice uiDevice;
-
+public class UiAutomationDriver extends BaseUiAutomationDriver<UiAutomationElement> {
public UiAutomationDriver(Instrumentation instrumentation) {
- this.context = new UiAutomationContext(instrumentation, this);
- this.uiDevice = new UiAutomationUiDevice(context);
- }
-
- @Override
- protected UiAutomationElement getNewRootElement() {
- return context.getUiElement(getRootNode(), null /* parent */);
- }
-
- @Override
- protected UiAutomationContext getContext() {
- return context;
- }
-
- private AccessibilityNodeInfo getRootNode() {
- final long timeoutMillis = getPoller().getTimeoutMillis();
- context.callUiAutomation(new UiAutomationCallable<Void>() {
- @Override
- public Void call(UiAutomation uiAutomation) {
- try {
- uiAutomation.waitForIdle(QUIET_TIME_TO_BE_CONSIDERD_IDLE_STATE, timeoutMillis);
- return null;
- } catch (java.util.concurrent.TimeoutException e) {
- throw new TimeoutException(e);
- }
- }
- });
-
- long end = SystemClock.uptimeMillis() + timeoutMillis;
- while (true) {
- AccessibilityNodeInfo root =
- context.callUiAutomation(new UiAutomationCallable<AccessibilityNodeInfo>() {
- @Override
- public AccessibilityNodeInfo call(UiAutomation uiAutomation) {
- return uiAutomation.getRootInActiveWindow();
- }
- });
- if (root != null) {
- return root;
- }
- long remainingMillis = end - SystemClock.uptimeMillis();
- if (remainingMillis < 0) {
- throw new TimeoutException(
- String.format("Timed out after %d milliseconds waiting for root AccessibilityNodeInfo",
- timeoutMillis));
- }
- SystemClock.sleep(Math.min(250, remainingMillis));
- }
- }
-
- /**
- * Some widgets fail to trigger some AccessibilityEvent's after actions,
- * resulting in stale AccessibilityNodeInfo's. As a work-around, force to
- * clear the AccessibilityNodeInfoCache.
- */
- public void clearAccessibilityNodeInfoCache() {
- Logs.call(this, "clearAccessibilityNodeInfoCache");
- uiDevice.sleep();
- uiDevice.wakeUp();
- }
-
- /**
- * {@link #clearAccessibilityNodeInfoCache} causes the screen to blink. This
- * method clears the cache without blinking by employing an implementation
- * detail of AccessibilityNodeInfoCache. This is a hack; use it at your own
- * discretion.
- */
- public void clearAccessibilityNodeInfoCacheHack() {
- Logs.call(this, "clearAccessibilityNodeInfoCacheHack");
- AccessibilityManager accessibilityManager =
- (AccessibilityManager) context.getInstrumentation().getTargetContext()
- .getSystemService(Context.ACCESSIBILITY_SERVICE);
- accessibilityManager.sendAccessibilityEvent(AccessibilityEvent
- .obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED));
+ super(instrumentation);
}
@Override
- public UiAutomationUiDevice getUiDevice() {
- return uiDevice;
+ protected UiAutomationContext newContext(Instrumentation instrumentation) {
+ return new UiAutomationContext(instrumentation, this);
}
}