Merge "Add ability to receive command line args in JUnit3 tests."
diff --git a/androidtestlib/src/com/android/test/BundleTest.java b/androidtestlib/src/com/android/test/BundleTest.java
new file mode 100644
index 0000000..f989312
--- /dev/null
+++ b/androidtestlib/src/com/android/test/BundleTest.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2013 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.test;
+
+import android.os.Bundle;
+
+import junit.framework.TestCase;
+
+/**
+ * Implement this interface to receive a {@link Bundle} containing the command line arguments
+ * passed to the test runner into your JUnit3 test.
+ * <p/>
+ * The test runner will call {@link #injectBundle(Bundle)} after
+ * object construction but before any {@link TestCase#setUp()} methods are called.
+ * Note the order in which injectBundle is called vs other inject methods is not defined.
+ * <p/>
+ * Declaring this in a JUnit4 test will have no effect. Use {@link InjectBundle} instead.
+ */
+public interface BundleTest {
+
+ /**
+ * Called by Android test runner to pass in Bundle containing command line arguments.
+ */
+ public void injectBundle(Bundle bundle);
+}
diff --git a/androidtestlib/src/com/android/test/InjectBundle.java b/androidtestlib/src/com/android/test/InjectBundle.java
index f3c2399..ee5e58e 100644
--- a/androidtestlib/src/com/android/test/InjectBundle.java
+++ b/androidtestlib/src/com/android/test/InjectBundle.java
@@ -38,6 +38,7 @@
* object construction but before any {@link Before} methods are called.
* <p/>
* Declaring this in a JUnit3 test (ie a class that is a {@link Test}) will have no effect.
+ * Use {@link BundleTest} instead.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
diff --git a/androidtestlib/src/com/android/test/runner/AndroidRunnerBuilder.java b/androidtestlib/src/com/android/test/runner/AndroidRunnerBuilder.java
index 328d413..e8c52a6 100644
--- a/androidtestlib/src/com/android/test/runner/AndroidRunnerBuilder.java
+++ b/androidtestlib/src/com/android/test/runner/AndroidRunnerBuilder.java
@@ -36,7 +36,7 @@
public AndroidRunnerBuilder(boolean canUseSuiteMethod, Instrumentation instr, Bundle bundle,
boolean skipExecution) {
super(canUseSuiteMethod);
- mAndroidJUnit3Builder = new AndroidJUnit3Builder(instr, skipExecution);
+ mAndroidJUnit3Builder = new AndroidJUnit3Builder(instr, bundle, skipExecution);
mAndroidJUnit4Builder = new AndroidJUnit4Builder(instr, bundle, skipExecution);
}
diff --git a/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3Builder.java b/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3Builder.java
index c6b4408..e20f28a 100644
--- a/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3Builder.java
+++ b/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3Builder.java
@@ -16,6 +16,7 @@
package com.android.test.runner.junit3;
import android.app.Instrumentation;
+import android.os.Bundle;
import junit.framework.TestCase;
@@ -30,9 +31,11 @@
private Instrumentation mInstr;
private boolean mSkipExecution;
+ private final Bundle mBundle;
- public AndroidJUnit3Builder(Instrumentation instr, boolean skipExecution) {
+ public AndroidJUnit3Builder(Instrumentation instr, Bundle bundle, boolean skipExecution) {
mInstr = instr;
+ mBundle = bundle;
mSkipExecution = skipExecution;
}
@@ -41,9 +44,11 @@
if (mSkipExecution && isJUnit3TestCase(testClass)) {
return new NonExecutingJUnit3ClassRunner(testClass);
} else if (isAndroidTestCase(testClass)) {
- return new AndroidJUnit3ClassRunner(testClass, mInstr);
+ return new AndroidJUnit3ClassRunner(testClass, mBundle, mInstr);
} else if (isInstrumentationTestCase(testClass)) {
- return new AndroidJUnit3ClassRunner(testClass, mInstr);
+ return new AndroidJUnit3ClassRunner(testClass, mBundle, mInstr);
+ } else if (isBundleTest(testClass)) {
+ return new AndroidJUnit3ClassRunner(testClass, mBundle, mInstr);
}
return null;
}
@@ -59,4 +64,9 @@
boolean isInstrumentationTestCase(Class<?> testClass) {
return android.test.InstrumentationTestCase.class.isAssignableFrom(testClass);
}
+
+ boolean isBundleTest(Class<?> testClass) {
+ return com.android.test.BundleTest.class.isAssignableFrom(testClass);
+ }
+
}
diff --git a/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3ClassRunner.java b/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3ClassRunner.java
index 91ddc62..418a80b 100644
--- a/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3ClassRunner.java
+++ b/androidtestlib/src/com/android/test/runner/junit3/AndroidJUnit3ClassRunner.java
@@ -16,6 +16,7 @@
package com.android.test.runner.junit3;
import android.app.Instrumentation;
+import android.os.Bundle;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -27,18 +28,15 @@
*/
class AndroidJUnit3ClassRunner extends JUnit38ClassRunner {
- /**
- * @param klass
- */
- public AndroidJUnit3ClassRunner(Class<?> klass, Instrumentation instr) {
- super(new AndroidTestSuite(klass.asSubclass(TestCase.class), instr));
+ public AndroidJUnit3ClassRunner(Class<?> klass, Bundle bundle, Instrumentation instr) {
+ super(new AndroidTestSuite(klass.asSubclass(TestCase.class), bundle, instr));
}
@Override
protected TestSuite createCopyOfSuite(TestSuite s) {
if (s instanceof AndroidTestSuite) {
AndroidTestSuite a = (AndroidTestSuite)s;
- return new AndroidTestSuite(a.getName(), a.getInstrumentation());
+ return new AndroidTestSuite(a.getName(), a.getBundle(), a.getInstrumentation());
} else {
return super.createCopyOfSuite(s);
}
diff --git a/androidtestlib/src/com/android/test/runner/junit3/AndroidTestSuite.java b/androidtestlib/src/com/android/test/runner/junit3/AndroidTestSuite.java
index 01d8103..e1a90c3 100644
--- a/androidtestlib/src/com/android/test/runner/junit3/AndroidTestSuite.java
+++ b/androidtestlib/src/com/android/test/runner/junit3/AndroidTestSuite.java
@@ -15,17 +15,20 @@
*/
package com.android.test.runner.junit3;
-import android.app.Instrumentation;
-import android.content.Context;
-import android.test.AndroidTestCase;
-import android.test.InstrumentationTestCase;
-
import junit.framework.Test;
import junit.framework.TestResult;
import junit.framework.TestSuite;
import org.junit.Ignore;
+import android.app.Instrumentation;
+import android.content.Context;
+import android.os.Bundle;
+import android.test.AndroidTestCase;
+import android.test.InstrumentationTestCase;
+
+import com.android.test.BundleTest;
+
/**
* A {@link TestSuite} used to pass {@link Context} and {@link Instrumentation} references to child
* tests.
@@ -34,14 +37,17 @@
class AndroidTestSuite extends TestSuite {
private final Instrumentation mInstr;
+ private final Bundle mBundle;
- AndroidTestSuite(Class<?> clazz, Instrumentation instrumentation) {
+ AndroidTestSuite(Class<?> clazz, Bundle bundle, Instrumentation instrumentation) {
super(clazz);
+ mBundle = bundle;
mInstr = instrumentation;
}
- AndroidTestSuite(String name, Instrumentation instrumentation) {
+ AndroidTestSuite(String name, Bundle bundle, Instrumentation instrumentation) {
super(name);
+ mBundle = bundle;
mInstr = instrumentation;
}
@@ -53,10 +59,17 @@
if (test instanceof InstrumentationTestCase) {
((InstrumentationTestCase)test).injectInstrumentation(mInstr);
}
+ if (test instanceof BundleTest) {
+ ((BundleTest)test).injectBundle(mBundle);
+ }
super.runTest(test, result);
}
Instrumentation getInstrumentation() {
return mInstr;
}
+
+ Bundle getBundle() {
+ return mBundle;
+ }
}
diff --git a/androidtestlib/src/com/android/test/runner/junit4/AndroidJUnit4ClassRunner.java b/androidtestlib/src/com/android/test/runner/junit4/AndroidJUnit4ClassRunner.java
index 4b6e00c..92bac8a 100644
--- a/androidtestlib/src/com/android/test/runner/junit4/AndroidJUnit4ClassRunner.java
+++ b/androidtestlib/src/com/android/test/runner/junit4/AndroidJUnit4ClassRunner.java
@@ -83,7 +83,7 @@
List<FrameworkField> bundleFields = getTestClass().getAnnotatedFields(
InjectBundle.class);
for (FrameworkField bundleField : bundleFields) {
- validateInjectField(errors, bundleField, Context.class);
+ validateInjectField(errors, bundleField, Bundle.class);
}
}
diff --git a/androidtestlib/tests/src/com/android/test/MyBundleTestCase.java b/androidtestlib/tests/src/com/android/test/MyBundleTestCase.java
new file mode 100644
index 0000000..7ed0fd4
--- /dev/null
+++ b/androidtestlib/tests/src/com/android/test/MyBundleTestCase.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2013 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.test;
+
+import junit.framework.TestCase;
+import android.os.Bundle;
+import android.util.Log;
+
+/**
+ * Placeholder test to verify {@link Bundle} gets injected to {@link BundleTest}.
+ */
+public class MyBundleTestCase extends TestCase implements BundleTest {
+
+ private Bundle mBundle = null;
+
+ public MyBundleTestCase() {
+ Log.i("MyBundleTestCase", "I'm created");
+ }
+
+ @Override
+ public void injectBundle(Bundle bundle) {
+ mBundle = bundle;
+ }
+
+ public void testInjectBundleCalled() {
+ assertNotNull(mBundle);
+ }
+}