From 44a29dd097350e3050e44a2f64aa4c75d26ec695 Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Mon, 3 Aug 2009 14:27:20 -0700 Subject: [PATCH] Adding "test context" to AndroidTestCase --- core/java/android/test/AndroidTestCase.java | 26 ++++++++++++++++--- .../android/test/AndroidTestRunner.java | 6 +++-- test-runner/android/test/TestRunner.java | 8 +++--- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/core/java/android/test/AndroidTestCase.java b/core/java/android/test/AndroidTestCase.java index de0587ab7366d..1015506ba227a 100644 --- a/core/java/android/test/AndroidTestCase.java +++ b/core/java/android/test/AndroidTestCase.java @@ -30,6 +30,7 @@ import java.lang.reflect.Field; public class AndroidTestCase extends TestCase { protected Context mContext; + private Context mTestContext; @Override protected void setUp() throws Exception { @@ -43,7 +44,7 @@ public class AndroidTestCase extends TestCase { public void testAndroidTestCaseSetupProperly() { assertNotNull("Context is null. setContext should be called before tests are run", - mContext); + mContext); } public void setContext(Context context) { @@ -54,6 +55,25 @@ public class AndroidTestCase extends TestCase { return mContext; } + /** + * Test context can be used to access resources from the test's own package + * as opposed to the resources from the test target package. Access to the + * latter is provided by the context set with the {@link #setContext} + * method. + * + * @hide + */ + public void setTestContext(Context context) { + mTestContext = context; + } + + /** + * @hide + */ + public Context getTestContext() { + return mTestContext; + } + /** * Asserts that launching a given activity is protected by a particular permission by * attempting to start the activity and validating that a {@link SecurityException} @@ -125,9 +145,9 @@ public class AndroidTestCase extends TestCase { * to scrub out any class variables. This protects against memory leaks in the case where a * test case creates a non-static inner class (thus referencing the test case) and gives it to * someone else to hold onto. - * + * * @param testCaseClass The class of the derived TestCase implementation. - * + * * @throws IllegalAccessException */ protected void scrubClass(final Class testCaseClass) diff --git a/test-runner/android/test/AndroidTestRunner.java b/test-runner/android/test/AndroidTestRunner.java index 79cedb009e8a8..00440b4350d91 100644 --- a/test-runner/android/test/AndroidTestRunner.java +++ b/test-runner/android/test/AndroidTestRunner.java @@ -158,16 +158,18 @@ public class AndroidTestRunner extends BaseTestRunner { mTestResult.addListener(testListener); } + Context testContext = mInstrumentation.getContext(); for (TestCase testCase : mTestCases) { - setContextIfAndroidTestCase(testCase, mContext); + setContextIfAndroidTestCase(testCase, mContext, testContext); setInstrumentationIfInstrumentationTestCase(testCase, mInstrumentation); testCase.run(mTestResult); } } - private void setContextIfAndroidTestCase(Test test, Context context) { + private void setContextIfAndroidTestCase(Test test, Context context, Context testContext) { if (AndroidTestCase.class.isAssignableFrom(test.getClass())) { ((AndroidTestCase) test).setContext(context); + ((AndroidTestCase) test).setTestContext(testContext); } } diff --git a/test-runner/android/test/TestRunner.java b/test-runner/android/test/TestRunner.java index efa24807ef08a..012df35010269 100644 --- a/test-runner/android/test/TestRunner.java +++ b/test-runner/android/test/TestRunner.java @@ -39,7 +39,7 @@ import com.google.android.collect.Lists; * and you probably will not need to instantiate, extend, or call this * class yourself. See the full {@link android.test} package description * to learn more about testing Android applications. - * + * * {@hide} Not needed for 1.0 SDK. */ public class TestRunner implements PerformanceTestCase.Intermediates { @@ -84,6 +84,7 @@ public class TestRunner implements PerformanceTestCase.Intermediates { super(); } + @Override public void run(TestResult result) { result.addListener(this); super.run(result); @@ -301,7 +302,7 @@ public class TestRunner implements PerformanceTestCase.Intermediates { if (mMode == PERFORMANCE) { runInPerformanceMode(test, className, false, className); } else if (mMode == PROFILING) { - //Need a way to mark a test to be run in profiling mode or not. + //Need a way to mark a test to be run in profiling mode or not. startProfiling(); test.run(); finishProfiling(); @@ -337,6 +338,7 @@ public class TestRunner implements PerformanceTestCase.Intermediates { AndroidTestCase testcase = (AndroidTestCase) test; try { testcase.setContext(mContext); + testcase.setTestContext(mContext); } catch (Exception ex) { Log.i("TestHarness", ex.toString()); } @@ -700,7 +702,7 @@ public class TestRunner implements PerformanceTestCase.Intermediates { } } catch (ClassNotFoundException e) { return 1; // this gets the count right, because either this test - // is missing, and it will fail when run or it is a single Junit test to be run. + // is missing, and it will fail when run or it is a single Junit test to be run. } return 0; }