1 /* 2 * Copyright (C) 2019 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 package com.android.compatibility.testtype; 17 18 import com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.invoker.TestInformation; 21 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 22 import com.android.tradefed.result.ITestInvocationListener; 23 import com.android.tradefed.result.TestDescription; 24 import com.android.tradefed.testtype.InstrumentationTest; 25 import com.android.tradefed.util.CommandResult; 26 import com.android.tradefed.util.CommandStatus; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 import static org.mockito.ArgumentMatchers.any; 32 import static org.mockito.ArgumentMatchers.anyInt; 33 import static org.mockito.ArgumentMatchers.anyLong; 34 import static org.mockito.ArgumentMatchers.anyObject; 35 import static org.mockito.ArgumentMatchers.anyString; 36 import static org.mockito.Mockito.inOrder; 37 import static org.mockito.Mockito.mock; 38 import static org.mockito.Mockito.never; 39 import static org.mockito.Mockito.times; 40 import static org.mockito.Mockito.when; 41 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.junit.runners.JUnit4; 45 import org.mockito.InOrder; 46 47 import java.util.HashMap; 48 import java.util.HashSet; 49 import java.util.Map; 50 import java.util.Set; 51 52 @RunWith(JUnit4.class) 53 public final class AppLaunchTestTest { 54 55 private final ITestInvocationListener mMockListener = mock(ITestInvocationListener.class); 56 private static final String TEST_PACKAGE_NAME = "package_name"; 57 private static final TestInformation NULL_TEST_INFORMATION = null; 58 59 @Test run_testFailed()60 public void run_testFailed() throws DeviceNotAvailableException { 61 InstrumentationTest instrumentationTest = createFailingInstrumentationTest(); 62 AppLaunchTest appLaunchTest = createLaunchTestWithInstrumentation(instrumentationTest); 63 64 appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); 65 66 verifyFailedAndEndedCall(mMockListener); 67 } 68 69 @Test run_testPassed()70 public void run_testPassed() throws DeviceNotAvailableException { 71 InstrumentationTest instrumentationTest = createPassingInstrumentationTest(); 72 AppLaunchTest appLaunchTest = createLaunchTestWithInstrumentation(instrumentationTest); 73 74 appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); 75 76 verifyPassedAndEndedCall(mMockListener); 77 } 78 79 @Test run_packageResetSuccess()80 public void run_packageResetSuccess() throws DeviceNotAvailableException { 81 ITestDevice mMockDevice = mock(ITestDevice.class); 82 when(mMockDevice.executeShellV2Command(String.format("pm clear %s", TEST_PACKAGE_NAME))) 83 .thenReturn(new CommandResult(CommandStatus.SUCCESS)); 84 AppLaunchTest appLaunchTest = createLaunchTestWithMockDevice(mMockDevice); 85 86 appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); 87 88 verifyPassedAndEndedCall(mMockListener); 89 } 90 91 @Test run_packageResetError()92 public void run_packageResetError() throws DeviceNotAvailableException { 93 ITestDevice mMockDevice = mock(ITestDevice.class); 94 when(mMockDevice.executeShellV2Command(String.format("pm clear %s", TEST_PACKAGE_NAME))) 95 .thenReturn(new CommandResult(CommandStatus.FAILED)); 96 AppLaunchTest appLaunchTest = createLaunchTestWithMockDevice(mMockDevice); 97 98 appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); 99 100 verifyFailedAndEndedCall(mMockListener); 101 } 102 103 @Test run_testRetry_passedAfterTwoFailings()104 public void run_testRetry_passedAfterTwoFailings() throws Exception { 105 InstrumentationTest instrumentationTest = createPassingInstrumentationTestAfterFailing(2); 106 AppLaunchTest appLaunchTest = createLaunchTestWithRetry(instrumentationTest, 2); 107 108 appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); 109 110 verifyPassedAndEndedCall(mMockListener); 111 } 112 113 @Test run_testRetry_failedAfterThreeFailings()114 public void run_testRetry_failedAfterThreeFailings() throws Exception { 115 InstrumentationTest instrumentationTest = createPassingInstrumentationTestAfterFailing(3); 116 AppLaunchTest appLaunchTest = createLaunchTestWithRetry(instrumentationTest, 2); 117 118 appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); 119 120 verifyFailedAndEndedCall(mMockListener); 121 } 122 123 @Test(expected = IllegalArgumentException.class) addIncludeFilter_nullIncludeFilter_throwsException()124 public void addIncludeFilter_nullIncludeFilter_throwsException() { 125 AppLaunchTest sut = new AppLaunchTest(); 126 127 sut.addIncludeFilter(null); 128 } 129 130 @Test(expected = IllegalArgumentException.class) addIncludeFilter_emptyIncludeFilter_throwsException()131 public void addIncludeFilter_emptyIncludeFilter_throwsException() { 132 AppLaunchTest sut = new AppLaunchTest(); 133 134 sut.addIncludeFilter(""); 135 } 136 137 @Test addIncludeFilter_validIncludeFilter()138 public void addIncludeFilter_validIncludeFilter() { 139 AppLaunchTest sut = new AppLaunchTest(); 140 141 sut.addIncludeFilter("test_filter"); 142 143 assertTrue(sut.mIncludeFilters.contains("test_filter")); 144 } 145 146 @Test(expected = NullPointerException.class) addAllIncludeFilters_nullIncludeFilter_throwsException()147 public void addAllIncludeFilters_nullIncludeFilter_throwsException() { 148 AppLaunchTest sut = new AppLaunchTest(); 149 150 sut.addAllIncludeFilters(null); 151 } 152 153 @Test addAllIncludeFilters_validIncludeFilters()154 public void addAllIncludeFilters_validIncludeFilters() { 155 AppLaunchTest sut = new AppLaunchTest(); 156 Set<String> test_filters = new HashSet<>(); 157 test_filters.add("filter_one"); 158 test_filters.add("filter_two"); 159 160 sut.addAllIncludeFilters(test_filters); 161 162 assertTrue(sut.mIncludeFilters.contains("filter_one")); 163 assertTrue(sut.mIncludeFilters.contains("filter_two")); 164 } 165 166 @Test clearIncludeFilters()167 public void clearIncludeFilters() { 168 AppLaunchTest sut = new AppLaunchTest(); 169 sut.addIncludeFilter("filter_test"); 170 171 sut.clearIncludeFilters(); 172 173 assertTrue(sut.mIncludeFilters.isEmpty()); 174 } 175 176 @Test getIncludeFilters()177 public void getIncludeFilters() { 178 AppLaunchTest sut = new AppLaunchTest(); 179 sut.addIncludeFilter("filter_test"); 180 181 assertEquals(sut.mIncludeFilters, sut.getIncludeFilters()); 182 } 183 184 @Test(expected = IllegalArgumentException.class) addExcludeFilter_nullExcludeFilter_throwsException()185 public void addExcludeFilter_nullExcludeFilter_throwsException() { 186 AppLaunchTest sut = new AppLaunchTest(); 187 188 sut.addExcludeFilter(null); 189 } 190 191 @Test(expected = IllegalArgumentException.class) addExcludeFilter_emptyExcludeFilter_throwsException()192 public void addExcludeFilter_emptyExcludeFilter_throwsException() { 193 AppLaunchTest sut = new AppLaunchTest(); 194 195 sut.addExcludeFilter(""); 196 } 197 198 @Test addExcludeFilter_validExcludeFilter()199 public void addExcludeFilter_validExcludeFilter() { 200 AppLaunchTest sut = new AppLaunchTest(); 201 202 sut.addExcludeFilter("test_filter"); 203 204 assertTrue(sut.mExcludeFilters.contains("test_filter")); 205 } 206 207 @Test(expected = NullPointerException.class) addAllExcludeFilters_nullExcludeFilters_throwsException()208 public void addAllExcludeFilters_nullExcludeFilters_throwsException() { 209 AppLaunchTest sut = new AppLaunchTest(); 210 211 sut.addAllExcludeFilters(null); 212 } 213 214 @Test addAllExcludeFilters_validExcludeFilters()215 public void addAllExcludeFilters_validExcludeFilters() { 216 AppLaunchTest sut = new AppLaunchTest(); 217 Set<String> test_filters = new HashSet<>(); 218 test_filters.add("filter_one"); 219 test_filters.add("filter_two"); 220 221 sut.addAllExcludeFilters(test_filters); 222 223 assertTrue(sut.mExcludeFilters.contains("filter_one")); 224 assertTrue(sut.mExcludeFilters.contains("filter_two")); 225 } 226 227 @Test clearExcludeFilters()228 public void clearExcludeFilters() { 229 AppLaunchTest sut = new AppLaunchTest(); 230 sut.addExcludeFilter("filter_test"); 231 232 sut.clearExcludeFilters(); 233 234 assertTrue(sut.mExcludeFilters.isEmpty()); 235 } 236 237 @Test getExcludeFilters()238 public void getExcludeFilters() { 239 AppLaunchTest sut = new AppLaunchTest(); 240 241 sut.addExcludeFilter("filter_test"); 242 243 assertEquals(sut.mExcludeFilters, sut.getExcludeFilters()); 244 } 245 246 @Test inFilter_withEmptyFilter()247 public void inFilter_withEmptyFilter() { 248 AppLaunchTest sut = new AppLaunchTest(); 249 250 assertTrue(sut.inFilter("filter_one")); 251 } 252 253 @Test inFilter_withRelatedIncludeFilter()254 public void inFilter_withRelatedIncludeFilter() { 255 AppLaunchTest sut = new AppLaunchTest(); 256 257 sut.addIncludeFilter("filter_one"); 258 259 assertTrue(sut.inFilter("filter_one")); 260 } 261 262 @Test inFilter_withUnrelatedIncludeFilter()263 public void inFilter_withUnrelatedIncludeFilter() { 264 AppLaunchTest sut = new AppLaunchTest(); 265 266 sut.addIncludeFilter("filter_two"); 267 268 assertFalse(sut.inFilter("filter_one")); 269 } 270 271 @Test inFilter_withRelatedExcludeFilter()272 public void inFilter_withRelatedExcludeFilter() { 273 AppLaunchTest sut = new AppLaunchTest(); 274 275 sut.addExcludeFilter("filter_one"); 276 277 assertFalse(sut.inFilter("filter_one")); 278 } 279 280 @Test inFilter_withUnrelatedExcludeFilter()281 public void inFilter_withUnrelatedExcludeFilter() { 282 AppLaunchTest sut = new AppLaunchTest(); 283 284 sut.addExcludeFilter("filter_two"); 285 286 assertTrue(sut.inFilter("filter_one")); 287 } 288 289 @Test inFilter_withSameIncludeAndExcludeFilters()290 public void inFilter_withSameIncludeAndExcludeFilters() { 291 AppLaunchTest sut = new AppLaunchTest(); 292 293 sut.addIncludeFilter("filter_one"); 294 sut.addExcludeFilter("filter_one"); 295 296 assertFalse(sut.inFilter("filter_one")); 297 } 298 299 @Test inFilter_withUnrelatedIncludeFilterAndRelatedExcludeFilter()300 public void inFilter_withUnrelatedIncludeFilterAndRelatedExcludeFilter() { 301 AppLaunchTest sut = new AppLaunchTest(); 302 303 sut.addIncludeFilter("filter_one"); 304 sut.addExcludeFilter("filter_two"); 305 306 assertFalse(sut.inFilter("filter_two")); 307 } 308 309 @Test inFilter_withRelatedIncludeFilterAndUnrelatedExcludeFilter()310 public void inFilter_withRelatedIncludeFilterAndUnrelatedExcludeFilter() { 311 AppLaunchTest sut = new AppLaunchTest(); 312 313 sut.addIncludeFilter("filter_one"); 314 sut.addExcludeFilter("filter_two"); 315 316 assertTrue(sut.inFilter("filter_one")); 317 } 318 319 @Test inFilter_withUnrelatedIncludeFilterAndUnrelatedExcludeFilter()320 public void inFilter_withUnrelatedIncludeFilterAndUnrelatedExcludeFilter() { 321 AppLaunchTest sut = new AppLaunchTest(); 322 323 sut.addIncludeFilter("filter_one"); 324 sut.addExcludeFilter("filter_two"); 325 326 assertFalse(sut.inFilter("filter_three")); 327 } 328 createFailingInstrumentationTest()329 private InstrumentationTest createFailingInstrumentationTest() { 330 InstrumentationTest instrumentation = 331 new InstrumentationTest() { 332 @Override 333 public void run( 334 final TestInformation testInfo, final ITestInvocationListener listener) 335 throws DeviceNotAvailableException { 336 listener.testFailed(new TestDescription("", ""), "test failed"); 337 } 338 }; 339 return instrumentation; 340 } 341 createPassingInstrumentationTest()342 private InstrumentationTest createPassingInstrumentationTest() { 343 InstrumentationTest instrumentation = 344 new InstrumentationTest() { 345 @Override 346 public void run( 347 final TestInformation testInfo, final ITestInvocationListener listener) 348 throws DeviceNotAvailableException {} 349 }; 350 return instrumentation; 351 } 352 createPassingInstrumentationTestAfterFailing(int failedCount)353 private InstrumentationTest createPassingInstrumentationTestAfterFailing(int failedCount) { 354 InstrumentationTest instrumentation = 355 new InstrumentationTest() { 356 private int mRetryCount = 0; 357 358 @Override 359 public void run( 360 final TestInformation testInfo, final ITestInvocationListener listener) 361 throws DeviceNotAvailableException { 362 if (mRetryCount < failedCount) { 363 listener.testFailed(new TestDescription("", ""), "test failed"); 364 } 365 mRetryCount++; 366 } 367 }; 368 return instrumentation; 369 } 370 createLaunchTestWithInstrumentation(InstrumentationTest instrumentation)371 private AppLaunchTest createLaunchTestWithInstrumentation(InstrumentationTest instrumentation) { 372 AppLaunchTest appLaunchTest = 373 new AppLaunchTest(TEST_PACKAGE_NAME) { 374 @Override 375 protected InstrumentationTest createInstrumentationTest( 376 String packageBeingTested) { 377 return instrumentation; 378 } 379 380 @Override 381 protected CommandResult resetPackage() throws DeviceNotAvailableException { 382 return new CommandResult(CommandStatus.SUCCESS); 383 } 384 }; 385 appLaunchTest.setDevice(mock(ITestDevice.class)); 386 return appLaunchTest; 387 } 388 createLaunchTestWithRetry( InstrumentationTest instrumentation, int retryCount)389 private AppLaunchTest createLaunchTestWithRetry( 390 InstrumentationTest instrumentation, int retryCount) { 391 AppLaunchTest appLaunchTest = 392 new AppLaunchTest(TEST_PACKAGE_NAME, retryCount) { 393 @Override 394 protected InstrumentationTest createInstrumentationTest( 395 String packageBeingTested) { 396 return instrumentation; 397 } 398 399 @Override 400 protected CommandResult resetPackage() throws DeviceNotAvailableException { 401 return new CommandResult(CommandStatus.SUCCESS); 402 } 403 }; 404 appLaunchTest.setDevice(mock(ITestDevice.class)); 405 return appLaunchTest; 406 } 407 createLaunchTestWithMockDevice(ITestDevice device)408 private AppLaunchTest createLaunchTestWithMockDevice(ITestDevice device) { 409 AppLaunchTest appLaunchTest = new AppLaunchTest(TEST_PACKAGE_NAME); 410 appLaunchTest.setDevice(device); 411 return appLaunchTest; 412 } 413 verifyFailedAndEndedCall(ITestInvocationListener listener)414 private static void verifyFailedAndEndedCall(ITestInvocationListener listener) { 415 InOrder inOrder = inOrder(listener); 416 inOrder.verify(listener, times(1)).testRunStarted(anyString(), anyInt()); 417 inOrder.verify(listener, times(1)).testStarted(anyObject(), anyLong()); 418 inOrder.verify(listener, times(1)).testFailed(any(), anyString()); 419 inOrder.verify(listener, times(1)) 420 .testEnded(anyObject(), anyLong(), (Map<String, String>) any()); 421 inOrder.verify(listener, times(1)).testRunEnded(anyLong(), (HashMap<String, Metric>) any()); 422 } 423 verifyPassedAndEndedCall(ITestInvocationListener listener)424 private static void verifyPassedAndEndedCall(ITestInvocationListener listener) { 425 InOrder inOrder = inOrder(listener); 426 inOrder.verify(listener, times(1)).testRunStarted(anyString(), anyInt()); 427 inOrder.verify(listener, times(1)).testStarted(anyObject(), anyLong()); 428 inOrder.verify(listener, never()).testFailed(any(), anyString()); 429 inOrder.verify(listener, times(1)) 430 .testEnded(anyObject(), anyLong(), (Map<String, String>) any()); 431 inOrder.verify(listener, times(1)).testRunEnded(anyLong(), (HashMap<String, Metric>) any()); 432 } 433 } 434