summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/util
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2017-07-31 10:59:52 -0700
committerSunny Goyal <sunnygoyal@google.com>2017-07-31 11:03:54 -0700
commit1d7f45d8f8fafc46f23963e3b43c95a2c2120079 (patch)
tree5e8185beb7b7678528ab486caa94868aa1e8e489 /tests/src/com/android/launcher3/util
parentf880ecca32b10299e3b657a446ce882e004118be (diff)
downloadandroid_packages_apps_Trebuchet-1d7f45d8f8fafc46f23963e3b43c95a2c2120079.tar.gz
android_packages_apps_Trebuchet-1d7f45d8f8fafc46f23963e3b43c95a2c2120079.tar.bz2
android_packages_apps_Trebuchet-1d7f45d8f8fafc46f23963e3b43c95a2c2120079.zip
Fixing various tests
> Migrating UI tests to AndroidJUnit4 for better support > Removing obsolete RotationPreference test > Fixing broken FileLogTest and AndroidJUnit4 > Removing InvariantDeviceProfileTest as it does not work well with resource overlays Change-Id: I0abb1df6765d76d86c1c6c84e8ac35eb9a6bcdaa
Diffstat (limited to 'tests/src/com/android/launcher3/util')
-rw-r--r--tests/src/com/android/launcher3/util/SimpleActivityMonitor.java65
-rw-r--r--tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java132
-rw-r--r--tests/src/com/android/launcher3/util/rule/ShellCommandRule.java90
3 files changed, 222 insertions, 65 deletions
diff --git a/tests/src/com/android/launcher3/util/SimpleActivityMonitor.java b/tests/src/com/android/launcher3/util/SimpleActivityMonitor.java
deleted file mode 100644
index 6154ab6c5..000000000
--- a/tests/src/com/android/launcher3/util/SimpleActivityMonitor.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * 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.android.launcher3.util;
-
-import android.app.Activity;
-import android.app.Application.*;
-import android.os.Bundle;
-
-import java.util.ArrayList;
-
-/**
- * Simple monitor to keep a list of active activities.
- */
-public class SimpleActivityMonitor implements ActivityLifecycleCallbacks {
-
- public final ArrayList<Activity> created = new ArrayList<>();
- public final ArrayList<Activity> started = new ArrayList<>();
- public final ArrayList<Activity> resumed = new ArrayList<>();
-
- @Override
- public void onActivityCreated(Activity activity, Bundle bundle) {
- created.add(activity);
- }
-
- @Override
- public void onActivityStarted(Activity activity) {
- started.add(activity);
- }
-
- @Override
- public void onActivityResumed(Activity activity) {
- resumed.add(activity);
- }
-
- @Override
- public void onActivityPaused(Activity activity) {
- resumed.remove(activity);
- }
-
- @Override
- public void onActivityStopped(Activity activity) {
- started.remove(activity);
- }
-
- @Override
- public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { }
-
- @Override
- public void onActivityDestroyed(Activity activity) {
- created.remove(activity);
- }
-}
diff --git a/tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java b/tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java
new file mode 100644
index 000000000..edd152a2c
--- /dev/null
+++ b/tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.android.launcher3.util.rule;
+
+import android.app.Activity;
+import android.app.Application;
+import android.app.Application.ActivityLifecycleCallbacks;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.test.InstrumentationRegistry;
+
+import com.android.launcher3.Launcher;
+import com.android.launcher3.Workspace.ItemOperator;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import java.util.concurrent.Callable;
+
+/**
+ * Test rule to get the current Launcher activity.
+ */
+public class LauncherActivityRule implements TestRule {
+
+ private Launcher mActivity;
+
+ @Override
+ public Statement apply(Statement base, Description description) {
+ return new MyStatement(base);
+ }
+
+ public Launcher getActivity() {
+ return mActivity;
+ }
+
+ public Callable<Boolean> itemExists(final ItemOperator op) {
+ return new Callable<Boolean>() {
+
+ @Override
+ public Boolean call() throws Exception {
+ Launcher launcher = getActivity();
+ if (launcher == null) {
+ return false;
+ }
+ return launcher.getWorkspace().getFirstMatch(op) != null;
+ }
+ };
+ }
+
+ /**
+ * Starts the launcher activity in the target package.
+ */
+ public void startLauncher() {
+ InstrumentationRegistry.getInstrumentation().startActivitySync(getHomeIntent());
+ }
+
+ public void returnToHome() {
+ InstrumentationRegistry.getTargetContext().startActivity(getHomeIntent());
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ }
+
+ public static Intent getHomeIntent() {
+ return new Intent(Intent.ACTION_MAIN)
+ .addCategory(Intent.CATEGORY_HOME)
+ .setPackage(InstrumentationRegistry.getTargetContext().getPackageName())
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ }
+
+ private class MyStatement extends Statement implements ActivityLifecycleCallbacks {
+
+ private final Statement mBase;
+
+ public MyStatement(Statement base) {
+ mBase = base;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ Application app = (Application)
+ InstrumentationRegistry.getTargetContext().getApplicationContext();
+ app.registerActivityLifecycleCallbacks(this);
+ try {
+ mBase.evaluate();
+ } finally {
+ app.unregisterActivityLifecycleCallbacks(this);
+ }
+ }
+
+ @Override
+ public void onActivityCreated(Activity activity, Bundle bundle) {
+ if (activity instanceof Launcher) {
+ mActivity = (Launcher) activity;
+ }
+ }
+
+ @Override
+ public void onActivityStarted(Activity activity) { }
+
+ @Override
+ public void onActivityResumed(Activity activity) { }
+
+ @Override
+ public void onActivityPaused(Activity activity) { }
+
+ @Override
+ public void onActivityStopped(Activity activity) { }
+
+ @Override
+ public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { }
+
+ @Override
+ public void onActivityDestroyed(Activity activity) {
+ if (activity == mActivity) {
+ mActivity = null;
+ }
+ }
+ }
+}
diff --git a/tests/src/com/android/launcher3/util/rule/ShellCommandRule.java b/tests/src/com/android/launcher3/util/rule/ShellCommandRule.java
new file mode 100644
index 000000000..dba2d7100
--- /dev/null
+++ b/tests/src/com/android/launcher3/util/rule/ShellCommandRule.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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.android.launcher3.util.rule;
+
+import android.content.ComponentName;
+import android.content.pm.ActivityInfo;
+import android.os.ParcelFileDescriptor;
+import android.support.test.InstrumentationRegistry;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+
+/**
+ * Test rule which executes a shell command at the start of the test.
+ */
+public class ShellCommandRule implements TestRule {
+
+ private final String mCmd;
+
+ public ShellCommandRule(String cmd) {
+ mCmd = cmd;
+ }
+
+ @Override
+ public Statement apply(Statement base, Description description) {
+ return new MyStatement(base, mCmd);
+ }
+
+ public static void runShellCommand(String command) throws IOException {
+ ParcelFileDescriptor pfd = InstrumentationRegistry.getInstrumentation().getUiAutomation()
+ .executeShellCommand(command);
+
+ // Read the input stream fully.
+ FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
+ while (fis.read() != -1);
+ fis.close();
+ }
+
+ private static class MyStatement extends Statement {
+ private final Statement mBase;
+ private final String mCmd;
+
+ public MyStatement(Statement base, String cmd) {
+ mBase = base;
+ mCmd = cmd;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ runShellCommand(mCmd);
+ mBase.evaluate();
+ }
+ }
+
+ /**
+ * Grants the launcher permission to bind widgets.
+ */
+ public static ShellCommandRule grandWidgetBind() {
+ return new ShellCommandRule("appwidget grantbind --package "
+ + InstrumentationRegistry.getTargetContext().getPackageName());
+ }
+
+ /**
+ * Sets the target launcher as default launcher.
+ */
+ public static ShellCommandRule setDefaultLauncher() {
+ ActivityInfo launcher = InstrumentationRegistry.getTargetContext().getPackageManager()
+ .queryIntentActivities(LauncherActivityRule.getHomeIntent(), 0).get(0)
+ .activityInfo;
+ return new ShellCommandRule("cmd package set-home-activity " +
+ new ComponentName(launcher.packageName, launcher.name).flattenToString());
+ }
+}