diff options
| author | Yuexi Ma <yuexima@google.com> | 2021-06-22 01:13:49 -0700 |
|---|---|---|
| committer | Yuexi Ma <yuexima@google.com> | 2021-06-23 11:08:59 -0700 |
| commit | 2e7b54eaafd5144d10f4dd27a2886283d9711f93 (patch) | |
| tree | b6481a8d193c75c6d50c72d0fce1440b4ebf2649 | |
| parent | 8eb30b706460a763aee3865e2c1ddae0ce3cb333 (diff) | |
| download | platform_test_app_compat_csuite-2e7b54eaafd5144d10f4dd27a2886283d9711f93.tar.gz platform_test_app_compat_csuite-2e7b54eaafd5144d10f4dd27a2886283d9711f93.tar.bz2 platform_test_app_compat_csuite-2e7b54eaafd5144d10f4dd27a2886283d9711f93.zip | |
Collect app version information in launch tests
Add an option to collect app version name and version code in launch tests.
Test: atest csuite-harness-tests
Change-Id: I009732c29aeabba53d02ef708db183f2b0f84b8c
| -rw-r--r-- | harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java | 76 | ||||
| -rw-r--r-- | harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java | 30 |
2 files changed, 102 insertions, 4 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 7a52735..46e2844 100644 --- a/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java +++ b/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java @@ -60,12 +60,20 @@ import java.util.Set; public class AppLaunchTest implements IDeviceTest, IRemoteTest, IConfigurationReceiver, ITestFilterReceiver { @VisibleForTesting static final String SCREENSHOT_AFTER_LAUNCH = "screenshot-after-launch"; + @VisibleForTesting static final String COLLECT_APP_VERSION = "collect-app-version"; @Option( name = SCREENSHOT_AFTER_LAUNCH, description = "Whether to take a screenshost after a package is launched.") private boolean mScreenshotAfterLaunch; + @Option( + name = COLLECT_APP_VERSION, + description = + "Whether to collect package version information and store the information in" + + " test log files.") + private boolean mCollectAppVersion; + @Option(name = "package-name", description = "Package name of testing app.") private String mPackageName; @@ -201,6 +209,22 @@ public class AppLaunchTest result.packageName = mPackageName; try { + if (mCollectAppVersion) { + String versionCode = DeviceUtils.getPackageVersionCode(mDevice, mPackageName); + String versionName = DeviceUtils.getPackageVersionName(mDevice, mPackageName); + CLog.i( + "Testing package %s versionCode=%s, versionName=%s", + mPackageName, versionCode, versionName); + listener.testLog( + String.format("%s_[versionCode=%s]", mPackageName, versionCode), + LogDataType.TEXT, + new ByteArrayInputStreamSource(versionCode.getBytes())); + listener.testLog( + String.format("%s_[versionName=%s]", mPackageName, versionName), + LogDataType.TEXT, + new ByteArrayInputStreamSource(versionName.getBytes())); + } + for (int i = 0; i <= mRetryCount; i++) { result.status = null; result.message = null; @@ -446,4 +470,56 @@ public class AppLaunchTest public Set<String> getExcludeFilters() { return Collections.unmodifiableSet(mExcludeFilters); } + + private static final class DeviceUtils { + @VisibleForTesting static final String UNKNOWN = "Unknown"; + + /** + * Gets the version name of a package installed on the device. + * + * @param packageName The full package name to query + * @return The package version name, or 'Unknown' if the package doesn't exist or the adb + * command failed. + * @throws DeviceNotAvailableException + */ + static String getPackageVersionName(ITestDevice device, String packageName) + throws DeviceNotAvailableException { + CommandResult cmdResult = + device.executeShellV2Command( + String.format("dumpsys package %s | grep versionName", packageName)); + + String prefix = "versionName="; + + if (cmdResult.getStatus() != CommandStatus.SUCCESS + || !cmdResult.getStdout().contains(prefix)) { + return UNKNOWN; + } + + return cmdResult.getStdout().trim().substring(prefix.length()); + } + + /** + * Gets the version code of a package installed on the device. + * + * @param packageName The full package name to query + * @return The package version code, or 'Unknown' if the package doesn't exist or the adb + * command failed. + * @throws DeviceNotAvailableException + */ + static String getPackageVersionCode(ITestDevice device, String packageName) + throws DeviceNotAvailableException { + CommandResult cmdResult = + device.executeShellV2Command( + String.format("dumpsys package %s | grep versionCode", packageName)); + + String prefix = "versionCode="; + + if (cmdResult.getStatus() != CommandStatus.SUCCESS + || !cmdResult.getStdout().contains(prefix)) { + return UNKNOWN; + } + + return cmdResult.getStdout().trim().split(" ")[0].substring(prefix.length()); + } + } } 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 0c23cc3..de60a7b 100644 --- a/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java +++ b/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java @@ -90,11 +90,11 @@ public final class AppLaunchTestTest { AppLaunchTest appLaunchTest = createLaunchTestWithInstrumentation(instrumentationTest); new OptionSetter(appLaunchTest) .setOptionValue(AppLaunchTest.SCREENSHOT_AFTER_LAUNCH, "true"); - ITestDevice mMockDevice = mock(ITestDevice.class); - appLaunchTest.setDevice(mMockDevice); + ITestDevice mockDevice = mock(ITestDevice.class); + appLaunchTest.setDevice(mockDevice); InputStreamSource screenshotData = new FileInputStreamSource(tempFolder.newFile()); - when(mMockDevice.getScreenshot()).thenReturn(screenshotData); - when(mMockDevice.getSerialNumber()).thenReturn("SERIAL"); + when(mockDevice.getScreenshot()).thenReturn(screenshotData); + when(mockDevice.getSerialNumber()).thenReturn("SERIAL"); appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); @@ -103,6 +103,24 @@ public final class AppLaunchTestTest { } @Test + public void run_collectAppVersion_savesToTestLog() throws Exception { + InstrumentationTest instrumentationTest = createPassingInstrumentationTest(); + AppLaunchTest appLaunchTest = createLaunchTestWithInstrumentation(instrumentationTest); + new OptionSetter(appLaunchTest).setOptionValue(AppLaunchTest.COLLECT_APP_VERSION, "true"); + ITestDevice mockDevice = mock(ITestDevice.class); + appLaunchTest.setDevice(mockDevice); + when(mockDevice.executeShellV2Command(Mockito.startsWith("dumpsys package"))) + .thenReturn(createSuccessfulCommandResult()); + + appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener); + + Mockito.verify(mMockListener, times(1)) + .testLog(Mockito.contains("versionCode"), Mockito.any(), Mockito.any()); + Mockito.verify(mMockListener, times(1)) + .testLog(Mockito.contains("versionName"), Mockito.any(), Mockito.any()); + } + + @Test public void run_packageResetSuccess() throws DeviceNotAvailableException { ITestDevice mMockDevice = mock(ITestDevice.class); when(mMockDevice.executeShellV2Command(String.format("pm clear %s", TEST_PACKAGE_NAME))) @@ -446,12 +464,16 @@ public final class AppLaunchTestTest { private CommandResult createSuccessfulCommandResult() { CommandResult commandResult = new CommandResult(CommandStatus.SUCCESS); commandResult.setExitCode(0); + commandResult.setStdout(""); + commandResult.setStderr(""); return commandResult; } private CommandResult createFailedCommandResult() { CommandResult commandResult = new CommandResult(CommandStatus.FAILED); commandResult.setExitCode(1); + commandResult.setStdout(""); + commandResult.setStderr("error"); return commandResult; } } |
