| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.test; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Bundle; |
| 21 | import android.test.mock.MockContext; |
| 22 | import android.test.suitebuilder.ListTestCaseNames; |
| 23 | import android.test.suitebuilder.ListTestCaseNames.TestDescriptor; |
| 24 | import android.test.suitebuilder.annotation.SmallTest; |
| 25 | |
| 26 | import junit.framework.Test; |
| 27 | import junit.framework.TestCase; |
| 28 | import junit.framework.TestSuite; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Tests for {@link InstrumentationTestRunner} |
| 34 | */ |
| 35 | @SmallTest |
| 36 | public class InstrumentationTestRunnerTest extends TestCase { |
| 37 | private StubInstrumentationTestRunner mInstrumentationTestRunner; |
| 38 | private StubAndroidTestRunner mStubAndroidTestRunner; |
| 39 | private String mTargetContextPackageName; |
| 40 | |
| 41 | protected void setUp() throws Exception { |
| 42 | super.setUp(); |
| 43 | mStubAndroidTestRunner = new StubAndroidTestRunner(); |
| 44 | mTargetContextPackageName = "android.test.suitebuilder.examples"; |
| 45 | mInstrumentationTestRunner = new StubInstrumentationTestRunner( |
| 46 | new StubContext("com.google.foo.tests"), |
| 47 | new StubContext(mTargetContextPackageName), mStubAndroidTestRunner); |
| 48 | } |
| 49 | |
| 50 | public void testOverrideTestToRunWithClassArgument() throws Exception { |
| 51 | String expectedTestClassName = PlaceHolderTest.class.getName(); |
| 52 | mInstrumentationTestRunner.onCreate(createBundle( |
| 53 | InstrumentationTestRunner.ARGUMENT_TEST_CLASS, expectedTestClassName)); |
| 54 | |
| 55 | assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, "testPlaceHolder"); |
| 56 | } |
| 57 | |
| 58 | public void testOverrideTestToRunWithClassAndMethodArgument() throws Exception { |
| 59 | String expectedTestClassName = PlaceHolderTest.class.getName(); |
| 60 | String expectedTestMethodName = "testPlaceHolder"; |
| 61 | String classAndMethod = expectedTestClassName + "#" + expectedTestMethodName; |
| 62 | mInstrumentationTestRunner.onCreate(createBundle( |
| 63 | InstrumentationTestRunner.ARGUMENT_TEST_CLASS, classAndMethod)); |
| 64 | |
| 65 | assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, |
| 66 | expectedTestMethodName); |
| 67 | } |
| 68 | |
| 69 | public void testUseSelfAsTestSuiteProviderWhenNoMetaDataOrClassArgument() throws Exception { |
| 70 | TestSuite testSuite = new TestSuite(); |
| 71 | testSuite.addTestSuite(PlaceHolderTest.class); |
| 72 | mInstrumentationTestRunner.setAllTestsSuite(testSuite); |
| 73 | mInstrumentationTestRunner.onCreate(null); |
| 74 | assertTestRunnerCalledWithExpectedParameters( |
| 75 | PlaceHolderTest.class.getName(), "testPlaceHolder"); |
| 76 | } |
| 77 | |
| 78 | public void testMultipleTestClass() throws Exception { |
| 79 | String classArg = PlaceHolderTest.class.getName() + "," + |
| 80 | PlaceHolderTest2.class.getName(); |
| 81 | mInstrumentationTestRunner.onCreate(createBundle( |
| 82 | InstrumentationTestRunner.ARGUMENT_TEST_CLASS, classArg)); |
| 83 | |
| 84 | Test test = mStubAndroidTestRunner.getTest(); |
| 85 | |
| 86 | assertContentsInOrder(ListTestCaseNames.getTestNames((TestSuite) test), |
| 87 | new TestDescriptor(PlaceHolderTest.class.getName(), "testPlaceHolder"), |
| 88 | new TestDescriptor(PlaceHolderTest2.class.getName(), "testPlaceHolder2")); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | public void testDelayParameter() throws Exception { |
| 93 | int delayMsec = 1000; |
| 94 | Bundle args = new Bundle(); |
| 95 | args.putInt(InstrumentationTestRunner.ARGUMENT_DELAY_MSEC, delayMsec); |
| 96 | args.putString(InstrumentationTestRunner.ARGUMENT_TEST_CLASS, |
| 97 | PlaceHolderTest.class.getName() + "," + |
| 98 | PlaceHolderTest2.class.getName()); |
| 99 | mInstrumentationTestRunner.onCreate(args); |
| 100 | Thread t = new Thread() { public void run() { mInstrumentationTestRunner.onStart(); } }; |
| 101 | |
| 102 | // Should delay three times: before, between, and after the two tests. |
| 103 | long beforeTest = System.currentTimeMillis(); |
| 104 | t.start(); |
| 105 | t.join(); |
| 106 | assertTrue(System.currentTimeMillis() > beforeTest + delayMsec * 3); |
| 107 | assertTrue(mInstrumentationTestRunner.isStarted()); |
| 108 | assertTrue(mInstrumentationTestRunner.isFinished()); |
| 109 | assertTrue(mStubAndroidTestRunner.isRun()); |
| 110 | } |
| 111 | |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame^] | 112 | /** |
| 113 | * Test that the -e {@link InstrumentationTestRunner.ARGUMENT_ANNOTATION} parameter properly |
| 114 | * selects tests. |
| 115 | */ |
| 116 | public void testAnnotationParameter() throws Exception { |
| 117 | String expectedTestClassName = AnnotationTest.class.getName(); |
| 118 | Bundle args = new Bundle(); |
| 119 | args.putString(InstrumentationTestRunner.ARGUMENT_TEST_CLASS, expectedTestClassName); |
| 120 | args.putString(InstrumentationTestRunner.ARGUMENT_ANNOTATION, FlakyTest.class.getName()); |
| 121 | mInstrumentationTestRunner.onCreate(args); |
| 122 | assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, "testAnnotated"); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Test that the -e {@link InstrumentationTestRunner.ARGUMENT_NOT_ANNOTATION} parameter |
| 127 | * properly excludes tests. |
| 128 | */ |
| 129 | public void testNotAnnotationParameter() throws Exception { |
| 130 | String expectedTestClassName = AnnotationTest.class.getName(); |
| 131 | Bundle args = new Bundle(); |
| 132 | args.putString(InstrumentationTestRunner.ARGUMENT_TEST_CLASS, expectedTestClassName); |
| 133 | args.putString(InstrumentationTestRunner.ARGUMENT_NOT_ANNOTATION, |
| 134 | FlakyTest.class.getName()); |
| 135 | mInstrumentationTestRunner.onCreate(args); |
| 136 | assertTestRunnerCalledWithExpectedParameters(expectedTestClassName, "testNotAnnotated"); |
| 137 | } |
| 138 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | private void assertContentsInOrder(List<TestDescriptor> actual, TestDescriptor... source) { |
| 140 | TestDescriptor[] clonedSource = source.clone(); |
| 141 | assertEquals("Unexpected number of items.", clonedSource.length, actual.size()); |
| 142 | for (int i = 0; i < actual.size(); i++) { |
| 143 | TestDescriptor actualItem = actual.get(i); |
| 144 | TestDescriptor sourceItem = clonedSource[i]; |
| 145 | assertEquals("Unexpected item. Index: " + i, sourceItem, actualItem); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private void assertTestRunnerCalledWithExpectedParameters( |
| 150 | String expectedTestClassName, String expectedTestMethodName) { |
| 151 | Test test = mStubAndroidTestRunner.getTest(); |
| 152 | assertContentsInOrder(ListTestCaseNames.getTestNames((TestSuite) test), |
| 153 | new TestDescriptor(expectedTestClassName, expectedTestMethodName)); |
| 154 | assertTrue(mInstrumentationTestRunner.isStarted()); |
| 155 | assertFalse(mInstrumentationTestRunner.isFinished()); |
| 156 | } |
| 157 | |
| 158 | private Bundle createBundle(String key, String value) { |
| 159 | Bundle bundle = new Bundle(); |
| 160 | bundle.putString(key, value); |
| 161 | return bundle; |
| 162 | } |
| 163 | |
| 164 | private static class StubInstrumentationTestRunner extends InstrumentationTestRunner { |
| 165 | private Context mContext; |
| 166 | private Context mTargetContext; |
| 167 | private boolean mStarted; |
| 168 | private boolean mFinished; |
| 169 | private AndroidTestRunner mAndroidTestRunner; |
| 170 | private TestSuite mTestSuite; |
| 171 | private TestSuite mDefaultTestSuite; |
| 172 | private String mPackageNameForDefaultTests; |
| 173 | |
| 174 | public StubInstrumentationTestRunner(Context context, Context targetContext, |
| 175 | AndroidTestRunner androidTestRunner) { |
| 176 | this.mContext = context; |
| 177 | this.mTargetContext = targetContext; |
| 178 | this.mAndroidTestRunner = androidTestRunner; |
| 179 | } |
| 180 | |
| 181 | public Context getContext() { |
| 182 | return mContext; |
| 183 | } |
| 184 | |
| 185 | public TestSuite getAllTests() { |
| 186 | return mTestSuite; |
| 187 | } |
| 188 | |
| 189 | public Context getTargetContext() { |
| 190 | return mTargetContext; |
| 191 | } |
| 192 | |
| 193 | protected AndroidTestRunner getAndroidTestRunner() { |
| 194 | return mAndroidTestRunner; |
| 195 | } |
| 196 | |
| 197 | public void start() { |
| 198 | mStarted = true; |
| 199 | } |
| 200 | |
| 201 | public void finish(int resultCode, Bundle results) { |
| 202 | mFinished = true; |
| 203 | } |
| 204 | |
| 205 | public boolean isStarted() { |
| 206 | return mStarted; |
| 207 | } |
| 208 | |
| 209 | public boolean isFinished() { |
| 210 | return mFinished; |
| 211 | } |
| 212 | |
| 213 | public void setAllTestsSuite(TestSuite testSuite) { |
| 214 | mTestSuite = testSuite; |
| 215 | } |
| 216 | |
| 217 | public void setDefaultTestsSuite(TestSuite testSuite) { |
| 218 | mDefaultTestSuite = testSuite; |
| 219 | } |
| 220 | |
| 221 | public String getPackageNameForDefaultTests() { |
| 222 | return mPackageNameForDefaultTests; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private static class StubContext extends MockContext { |
| 227 | private String mPackageName; |
| 228 | |
| 229 | public StubContext(String packageName) { |
| 230 | this.mPackageName = packageName; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public String getPackageCodePath() { |
| 235 | return mPackageName; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public String getPackageName() { |
| 240 | return mPackageName; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public ClassLoader getClassLoader() { |
| 245 | return getClass().getClassLoader(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private static class StubAndroidTestRunner extends AndroidTestRunner { |
| 250 | private Test mTest; |
| 251 | private boolean mRun; |
| 252 | |
| 253 | public boolean isRun() { |
| 254 | return mRun; |
| 255 | } |
| 256 | |
| 257 | public void setTest(Test test) { |
| 258 | super.setTest(test); |
| 259 | mTest = test; |
| 260 | } |
| 261 | |
| 262 | public Test getTest() { |
| 263 | return mTest; |
| 264 | } |
| 265 | |
| 266 | public void runTest() { |
| 267 | super.runTest(); |
| 268 | mRun = true; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Empty test used for validation |
| 274 | */ |
| 275 | public static class PlaceHolderTest extends TestCase { |
| 276 | |
| 277 | public PlaceHolderTest() { |
| 278 | super("testPlaceHolder"); |
| 279 | } |
| 280 | |
| 281 | public void testPlaceHolder() throws Exception { |
| 282 | |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Empty test used for validation |
| 288 | */ |
| 289 | public static class PlaceHolderTest2 extends TestCase { |
| 290 | |
| 291 | public PlaceHolderTest2() { |
| 292 | super("testPlaceHolder2"); |
| 293 | } |
| 294 | |
| 295 | public void testPlaceHolder2() throws Exception { |
| 296 | |
| 297 | } |
| 298 | } |
| Brett Chabot | 88e03a9 | 2010-02-19 09:57:11 -0800 | [diff] [blame^] | 299 | |
| 300 | /** |
| 301 | * Annotated test used for validation. |
| 302 | */ |
| 303 | public static class AnnotationTest extends TestCase { |
| 304 | |
| 305 | public void testNotAnnotated() throws Exception { |
| 306 | } |
| 307 | |
| 308 | @FlakyTest |
| 309 | public void testAnnotated() throws Exception { |
| 310 | } |
| 311 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |