Add method runner instrumentation
This can be used to run single static methods for setup and inspecting the app
state.
Test
Ran GoogleContactsTests
Change-Id: I132a7e9dc8f2ee2e14b8a1c583f3d5236ab548ce
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 6f50aab..472ee1c 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -98,14 +98,15 @@
android:label="Contacts app tests">
</instrumentation>
- <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
- android:targetPackage="com.android.contacts"
- android:label="Contacts app tests">
- </instrumentation>
-
<instrumentation android:name="com.android.contacts.ContactsLaunchPerformance"
android:targetPackage="com.android.contacts"
android:label="Contacts launch performance">
</instrumentation>
+ <instrumentation
+ android:name="com.android.contacts.RunMethodInstrumentation"
+ android:targetPackage="com.android.contacts"
+ android:label="Run Contacts Method">
+ </instrumentation>
+
</manifest>
diff --git a/tests/src/com/android/contacts/RunMethodInstrumentation.java b/tests/src/com/android/contacts/RunMethodInstrumentation.java
new file mode 100644
index 0000000..77f36ed
--- /dev/null
+++ b/tests/src/com/android/contacts/RunMethodInstrumentation.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2016 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.contacts;
+
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.test.InstrumentationRegistry;
+import android.util.Log;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Runs a single static method specified via the arguments.
+ *
+ * Useful for manipulating the app state during manual testing.
+ *
+ * Valid signatures: void f(Context, Bundle), void f(Context), void f()
+ *
+ * Example usage:
+ * $ adb shell am instrument -e class com.android.contacts.Foo -e method bar -e someArg someValue\
+ * -w com.google.android.contacts.tests/com.android.contacts.RunMethodInstrumentation
+ */
+public class RunMethodInstrumentation extends Instrumentation {
+
+ private static final String TAG = "RunMethod";
+
+ private String className;
+ private String methodName;
+ private Bundle args;
+
+ @Override
+ public void onCreate(Bundle arguments) {
+ super.onCreate(arguments);
+
+ InstrumentationRegistry.registerInstance(this, arguments);
+
+ className = arguments.getString("class");
+ methodName = arguments.getString("method");
+ args = arguments;
+
+ Log.d(TAG, "Running " + className + "." + methodName);
+ Log.d(TAG, "args=" + args);
+
+ start();
+ }
+
+ public void onStart() {
+ Log.d(TAG, "onStart");
+ super.onStart();
+
+ if (className == null || methodName == null) {
+ Log.e(TAG, "Must supply class and method");
+ finish(Activity.RESULT_CANCELED, null);
+ return;
+ }
+
+ try {
+ invokeMethod(args, className, methodName);
+ } catch (Exception e) {
+ e.printStackTrace();
+ finish(Activity.RESULT_CANCELED, null);
+ return;
+ }
+ // Maybe should let the method determine when this is called.
+ finish(Activity.RESULT_OK, null);
+ }
+
+ private void invokeMethod(Bundle args, String className, String methodName) throws
+ InvocationTargetException, IllegalAccessException, NoSuchMethodException,
+ ClassNotFoundException {
+ Context context;
+ Class<?> clazz = null;
+ try {
+ // Try to load from App's code
+ clazz = getTargetContext().getClassLoader().loadClass(className);
+ context = getTargetContext();
+ } catch (Exception e) {
+ // Try to load from Test App's code
+ clazz = getContext().getClassLoader().loadClass(className);
+ context = getContext();
+ }
+
+ Object[] methodArgs = null;
+ Method method = null;
+
+ try {
+ method = clazz.getMethod(methodName, Context.class, Bundle.class);
+ methodArgs = new Object[] { context, args };
+ } catch (NoSuchMethodException e) {
+ }
+
+ if (method != null) {
+ method.invoke(clazz, methodArgs);
+ return;
+ }
+
+ try {
+ method = clazz.getMethod(methodName, Context.class);
+ methodArgs = new Object[] { context };
+ } catch (NoSuchMethodException e) {
+ }
+
+ if (method != null) {
+ method.invoke(clazz, methodArgs);
+ return;
+ }
+
+ method = clazz.getMethod(methodName);
+ method.invoke(clazz);
+ }
+}
diff --git a/tests/src/com/android/contacts/tests/AccountsTestHelper.java b/tests/src/com/android/contacts/tests/AccountsTestHelper.java
index be826f7..11476b3 100644
--- a/tests/src/com/android/contacts/tests/AccountsTestHelper.java
+++ b/tests/src/com/android/contacts/tests/AccountsTestHelper.java
@@ -20,10 +20,12 @@
import android.content.ContentResolver;
import android.content.Context;
import android.os.Build;
+import android.os.Bundle;
import android.provider.ContactsContract.RawContacts;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.test.InstrumentationRegistry;
+import android.util.Log;
import com.android.contacts.common.model.account.AccountWithDataSet;
@@ -32,8 +34,12 @@
@SuppressWarnings("MissingPermission")
public class AccountsTestHelper {
+ private static final String TAG = "AccountsTestHelper";
+
public static final String TEST_ACCOUNT_TYPE = "com.android.contacts.tests.testauth.basic";
+ public static final String EXTRA_ACCOUNT_NAME = "accountName";
+
private final Context mContext;
private final AccountManager mAccountManager;
private final ContentResolver mResolver;
@@ -103,4 +109,32 @@
private AccountWithDataSet convertTestAccount() {
return new AccountWithDataSet(mTestAccount.name, mTestAccount.type, null);
}
+
+ /**
+ * Invoke from adb using the RunMethodInstrumentation:
+ * $ adb shell am instrument -e class com.android.contacts.tests.AccountTestHelper\
+ * -e method addTestAccount -e accountName fooAccount\
+ * -w com.google.android.contacts.tests/com.android.contacts.RunMethodInstrumentation
+ */
+ public static void addTestAccount(Context context, Bundle args) {
+ final String accountName = args.getString(EXTRA_ACCOUNT_NAME);
+ if (accountName == null) {
+ Log.e(TAG, "args must contain extra " + EXTRA_ACCOUNT_NAME);
+ return;
+ }
+
+ new AccountsTestHelper(context).addTestAccount(accountName);
+ }
+
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
+ public static void removeTestAccount(Context context, Bundle args) {
+ final String accountName = args.getString(EXTRA_ACCOUNT_NAME);
+ if (accountName == null) {
+ Log.e(TAG, "args must contain extra " + EXTRA_ACCOUNT_NAME);
+ return;
+ }
+
+ AccountWithDataSet account = new AccountWithDataSet(accountName, TEST_ACCOUNT_TYPE, null);
+ new AccountsTestHelper(context).removeTestAccount(account);
+ }
}