diff options
author | Kailin Luo <karenluo@google.com> | 2019-12-17 18:38:27 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-12-17 18:38:27 +0000 |
commit | d69bbd3a50a544b5555f7a4f16da1a6c88c823ec (patch) | |
tree | 81d091bdc498ad8a0793a91c77adcfd264bf14e7 | |
parent | 30e5120fc6ef86627bcd5079e455aaacbadc0710 (diff) | |
parent | c2a0dec88d20faa46807b3286c3d0e3294761c27 (diff) | |
download | csuite-d69bbd3a50a544b5555f7a4f16da1a6c88c823ec.tar.gz |
Merge "Clear package data before launching the app"
-rw-r--r-- | harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java | 22 | ||||
-rw-r--r-- | harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java | 47 |
2 files changed, 66 insertions, 3 deletions
diff --git a/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java b/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java index 0124017..72afc2a 100644 --- a/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java +++ b/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java @@ -39,6 +39,8 @@ import com.android.tradefed.testtype.IDeviceTest; import com.android.tradefed.testtype.IRemoteTest; import com.android.tradefed.testtype.InstrumentationTest; import com.android.tradefed.testtype.ITestFilterReceiver; +import com.android.tradefed.util.CommandResult; +import com.android.tradefed.util.CommandStatus; import com.android.tradefed.util.StreamUtil; import com.google.common.annotations.VisibleForTesting; @@ -102,7 +104,13 @@ public class AppLaunchTest } @VisibleForTesting - public AppLaunchTest(int retryCount) { + public AppLaunchTest(String packageName) { + this.mPackageName = packageName; + } + + @VisibleForTesting + public AppLaunchTest(String packageName, int retryCount) { + this.mPackageName = packageName; this.mRetryCount = retryCount; } @@ -214,6 +222,13 @@ public class AppLaunchTest throws DeviceNotAvailableException { CLog.d("Launching package: %s.", result.packageName); + CommandResult resetResult = resetPackage(); + if (resetResult.getStatus() != CommandStatus.SUCCESS){ + result.status = CompatibilityTestResult.STATUS_ERROR; + result.message = resetResult.getStatus() + resetResult.getStderr(); + return; + } + InstrumentationTest instrTest = createInstrumentationTest(result.packageName); FailureCollectingListener failureListener = createFailureListener(); @@ -294,6 +309,11 @@ public class AppLaunchTest return false; } + protected CommandResult resetPackage() throws DeviceNotAvailableException { + return mDevice.executeShellV2Command( + String.format("pm clear %s", mPackageName)); + } + private void stopPackage() throws DeviceNotAvailableException { mDevice.executeShellCommand( String.format("am force-stop %s", mPackageName)); diff --git a/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java b/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java index 956a447..a3f5abd 100644 --- a/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java +++ b/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java @@ -38,6 +38,8 @@ import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; import com.android.tradefed.result.ITestInvocationListener; import com.android.tradefed.result.TestDescription; import com.android.tradefed.testtype.InstrumentationTest; +import com.android.tradefed.util.CommandResult; +import com.android.tradefed.util.CommandStatus; import java.util.Map; import java.util.HashMap; @@ -54,6 +56,7 @@ import org.junit.runners.JUnit4; public final class AppLaunchTestTest { private final ITestInvocationListener mMockListener = mock(ITestInvocationListener.class); + private static final String TEST_PACKAGE_NAME = "package_name"; @Test public void run_testFailed() throws DeviceNotAvailableException { @@ -76,6 +79,30 @@ public final class AppLaunchTestTest { } @Test + public void run_packageResetSuccess() throws DeviceNotAvailableException { + ITestDevice mMockDevice = mock(ITestDevice.class); + when(mMockDevice.executeShellV2Command(String.format("pm clear %s", TEST_PACKAGE_NAME))) + .thenReturn(new CommandResult(CommandStatus.SUCCESS)); + AppLaunchTest appLaunchTest = createLaunchTestWithMockDevice(mMockDevice); + + appLaunchTest.run(mMockListener); + + verifyPassedAndEndedCall(mMockListener); + } + + @Test + public void run_packageResetError() throws DeviceNotAvailableException { + ITestDevice mMockDevice = mock(ITestDevice.class); + when(mMockDevice.executeShellV2Command(String.format("pm clear %s", TEST_PACKAGE_NAME))) + .thenReturn(new CommandResult(CommandStatus.FAILED)); + AppLaunchTest appLaunchTest = createLaunchTestWithMockDevice(mMockDevice); + + appLaunchTest.run(mMockListener); + + verifyFailedAndEndedCall(mMockListener); + } + + @Test public void run_testRetry_passedAfterTwoFailings() throws Exception { InstrumentationTest instrumentationTest = createPassingInstrumentationTestAfterFailing(2); @@ -337,11 +364,16 @@ public final class AppLaunchTestTest { } private AppLaunchTest createLaunchTestWithInstrumentation(InstrumentationTest instrumentation) { - AppLaunchTest appLaunchTest = new AppLaunchTest(){ + AppLaunchTest appLaunchTest = new AppLaunchTest(TEST_PACKAGE_NAME) { @Override protected InstrumentationTest createInstrumentationTest(String packageBeingTested) { return instrumentation; } + + @Override + protected CommandResult resetPackage() throws DeviceNotAvailableException { + return new CommandResult(CommandStatus.SUCCESS); + } }; appLaunchTest.setDevice(mock(ITestDevice.class)); return appLaunchTest; @@ -349,16 +381,27 @@ public final class AppLaunchTestTest { private AppLaunchTest createLaunchTestWithRetry (InstrumentationTest instrumentation, int retryCount) { - AppLaunchTest appLaunchTest = new AppLaunchTest(retryCount){ + AppLaunchTest appLaunchTest = new AppLaunchTest(TEST_PACKAGE_NAME, retryCount) { @Override protected InstrumentationTest createInstrumentationTest(String packageBeingTested) { return instrumentation; } + + @Override + protected CommandResult resetPackage() throws DeviceNotAvailableException { + return new CommandResult(CommandStatus.SUCCESS); + } }; appLaunchTest.setDevice(mock(ITestDevice.class)); return appLaunchTest; } + private AppLaunchTest createLaunchTestWithMockDevice(ITestDevice device) { + AppLaunchTest appLaunchTest = new AppLaunchTest(TEST_PACKAGE_NAME); + appLaunchTest.setDevice(device); + return appLaunchTest; + } + private static void verifyFailedAndEndedCall(ITestInvocationListener listener) { InOrder inOrder = inOrder(listener); inOrder.verify(listener, times(1)).testRunStarted(anyString(), anyInt()); |