aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Wickham <twickham@google.com>2013-06-20 00:43:21 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-06-20 00:43:22 +0000
commit886a46681a7d555ba0d847e7020a38e9c0114313 (patch)
tree868ab5411fe4a82a2aaf6879fffb3edc0dc3efe3
parent707bb4a4567d4691793fdf87de3c6a32046375d9 (diff)
parent115eb39036a7a4e500dc94b57975a5693861f654 (diff)
downloaddroiddriver-886a46681a7d555ba0d847e7020a38e9c0114313.tar.gz
Merge "Added getRunningActivity; Precautions in InstrumentationDriver.getRootElement"
-rw-r--r--src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java26
-rw-r--r--src/com/google/android/droiddriver/instrumentation/RootFinder.java1
-rw-r--r--src/com/google/android/droiddriver/runner/TestRunner.java15
-rw-r--r--src/com/google/android/droiddriver/util/ActivityUtils.java41
4 files changed, 81 insertions, 2 deletions
diff --git a/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java b/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java
index 63247fb..d793d5b 100644
--- a/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java
+++ b/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java
@@ -16,12 +16,17 @@
package com.google.android.droiddriver.instrumentation;
+import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
+import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.droiddriver.base.AbstractDroidDriver;
+import com.google.android.droiddriver.exceptions.DroidDriverException;
+import com.google.android.droiddriver.util.ActivityUtils;
+import com.google.android.droiddriver.util.Logs;
import java.util.ArrayList;
import java.util.List;
@@ -39,15 +44,32 @@ public class InstrumentationDriver extends AbstractDroidDriver {
@Override
public ViewElement getRootElement() {
+ Activity runningActivity = ActivityUtils.getRunningActivity();
+
+ if (runningActivity == null) {
+ // Maybe loop main thread and try again?
+ throw new DroidDriverException("The application under test has no foreground activity.");
+ }
+
View[] views = RootFinder.getRootViews();
+ if (views.length > 1) {
+ // We are assuming that there is only one root view.
+ Logs.log(Log.WARN, "There are more than one root views.");
+ }
+
+ // Note(twickham): This is no longer needed (will be deleted soon).
// Need to create a fake root to be able to traverse all the possible views.
- RootViewGroup root = new RootViewGroup(context.getInstrumentation().getTargetContext());
+ /* RootViewGroup root = new RootViewGroup(context.getInstrumentation().getTargetContext());
for (View view : views) {
root.addView(view);
- }
+ } */
+
+ // We assume getWindow().getDecorView() on the currently resumed activity is the sole root.
+ View root = runningActivity.getWindow().getDecorView();
return context.getUiElement(root);
}
+ // Note(twickham): This class is no longer in use (will be deleted soon).
private static class RootViewGroup extends ViewGroup {
private final List<View> children = new ArrayList<View>();
diff --git a/src/com/google/android/droiddriver/instrumentation/RootFinder.java b/src/com/google/android/droiddriver/instrumentation/RootFinder.java
index 8f67198..19b8387 100644
--- a/src/com/google/android/droiddriver/instrumentation/RootFinder.java
+++ b/src/com/google/android/droiddriver/instrumentation/RootFinder.java
@@ -29,6 +29,7 @@ import java.lang.reflect.Method;
/**
* Class to find the root view.
+ * Note(twickham): This class is no longer being used.
*/
public class RootFinder {
diff --git a/src/com/google/android/droiddriver/runner/TestRunner.java b/src/com/google/android/droiddriver/runner/TestRunner.java
index 0c84d44..3f34cb0 100644
--- a/src/com/google/android/droiddriver/runner/TestRunner.java
+++ b/src/com/google/android/droiddriver/runner/TestRunner.java
@@ -25,6 +25,7 @@ import android.test.suitebuilder.TestMethod;
import android.util.Log;
import com.android.internal.util.Predicate;
+import com.google.android.droiddriver.util.ActivityUtils;
import com.google.android.droiddriver.util.Logs;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@@ -129,4 +130,18 @@ public class TestRunner extends InstrumentationTestRunner {
super.callActivityOnCreate(activity, bundle);
activities.add(activity);
}
+
+ @Override
+ public void callActivityOnResume(Activity activity) {
+ super.callActivityOnResume(activity);
+ ActivityUtils.setRunningActivity(activity);
+ }
+
+ @Override
+ public void callActivityOnPause(Activity activity) {
+ super.callActivityOnPause(activity);
+ if (activity == ActivityUtils.getRunningActivity()) {
+ ActivityUtils.setRunningActivity(null);
+ }
+ }
}
diff --git a/src/com/google/android/droiddriver/util/ActivityUtils.java b/src/com/google/android/droiddriver/util/ActivityUtils.java
new file mode 100644
index 0000000..72a0239
--- /dev/null
+++ b/src/com/google/android/droiddriver/util/ActivityUtils.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2013 DroidDriver committers
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.android.droiddriver.util;
+
+import android.app.Activity;
+
+/**
+ * Static helper methods for retrieving activities.
+ */
+public class ActivityUtils {
+ private static Activity runningActivity;
+
+ /**
+ * Sets the running activity. Called from TestRunner.
+ */
+ public static void setRunningActivity(Activity activity) {
+ runningActivity = activity;
+ }
+
+ /**
+ * Gets the activity in the foreground of the screen. This activity must have user focus.
+ * @return the currently running activity, or null if no single activity has focus.
+ */
+ public static Activity getRunningActivity() {
+ return runningActivity;
+ }
+} \ No newline at end of file