aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuexi Ma <yuexima@google.com>2021-05-05 22:05:06 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-05-05 22:05:06 +0000
commit3aa98b8591e0e5b3659db2457eda17103cc0247e (patch)
tree13ff98e650cd3c8e0fdee0e6d88b15ef88300fe2
parentdd3803873343cce1e563a92fbbaa2bc469701a6e (diff)
parent8215a7d9bd136a1f43a67c4766e5c31fbb2c9ee2 (diff)
downloadcsuite-3aa98b8591e0e5b3659db2457eda17103cc0247e.tar.gz
Add an option to take screenshot after app launch am: cf17e849bb am: 8215a7d9bd
Original change: https://android-review.googlesource.com/c/platform/test/app_compat/csuite/+/1684286 Change-Id: I59d73b158fd33ccdbe331592c5ae87fa12fb0ac8
-rw-r--r--harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java20
-rw-r--r--harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java27
2 files changed, 46 insertions, 1 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 e15f960..7a52735 100644
--- a/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java
+++ b/harness/src/main/java/com/android/compatibility/testtype/AppLaunchTest.java
@@ -59,6 +59,12 @@ import java.util.Set;
/** A test that verifies that a single app can be successfully launched. */
public class AppLaunchTest
implements IDeviceTest, IRemoteTest, IConfigurationReceiver, ITestFilterReceiver {
+ @VisibleForTesting static final String SCREENSHOT_AFTER_LAUNCH = "screenshot-after-launch";
+
+ @Option(
+ name = SCREENSHOT_AFTER_LAUNCH,
+ description = "Whether to take a screenshost after a package is launched.")
+ private boolean mScreenshotAfterLaunch;
@Option(name = "package-name", description = "Package name of testing app.")
private String mPackageName;
@@ -204,6 +210,20 @@ public class AppLaunchTest
break;
}
}
+
+ if (mScreenshotAfterLaunch) {
+ try (InputStreamSource screenSource = mDevice.getScreenshot()) {
+ listener.testLog(
+ mPackageName + "_screenshot_" + mDevice.getSerialNumber(),
+ LogDataType.PNG,
+ screenSource);
+ } catch (DeviceNotAvailableException e) {
+ CLog.e(
+ "Device %s became unavailable while capturing screenshot, %s",
+ mDevice.getSerialNumber(), e.toString());
+ throw e;
+ }
+ }
} finally {
reportResult(listener, testDescription, result);
stopPackage();
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 070cf85..0c23cc3 100644
--- a/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java
+++ b/harness/src/test/java/com/android/compatibility/testtype/AppLaunchTestTest.java
@@ -15,16 +15,20 @@
*/
package com.android.compatibility.testtype;
+import com.android.tradefed.config.OptionSetter;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.invoker.TestInformation;
import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
+import com.android.tradefed.result.FileInputStreamSource;
import com.android.tradefed.result.ITestInvocationListener;
+import com.android.tradefed.result.InputStreamSource;
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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -38,12 +42,14 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
-import static org.testng.Assert.assertThrows;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.InOrder;
+import org.mockito.Mockito;
import java.util.HashMap;
import java.util.HashSet;
@@ -56,6 +62,7 @@ public final class AppLaunchTestTest {
private final ITestInvocationListener mMockListener = mock(ITestInvocationListener.class);
private static final String TEST_PACKAGE_NAME = "package_name";
private static final TestInformation NULL_TEST_INFORMATION = null;
+ @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void run_testFailed() throws DeviceNotAvailableException {
@@ -78,6 +85,24 @@ public final class AppLaunchTestTest {
}
@Test
+ public void run_takeScreenShot_savesToTestLog() throws Exception {
+ InstrumentationTest instrumentationTest = createPassingInstrumentationTest();
+ AppLaunchTest appLaunchTest = createLaunchTestWithInstrumentation(instrumentationTest);
+ new OptionSetter(appLaunchTest)
+ .setOptionValue(AppLaunchTest.SCREENSHOT_AFTER_LAUNCH, "true");
+ ITestDevice mMockDevice = mock(ITestDevice.class);
+ appLaunchTest.setDevice(mMockDevice);
+ InputStreamSource screenshotData = new FileInputStreamSource(tempFolder.newFile());
+ when(mMockDevice.getScreenshot()).thenReturn(screenshotData);
+ when(mMockDevice.getSerialNumber()).thenReturn("SERIAL");
+
+ appLaunchTest.run(NULL_TEST_INFORMATION, mMockListener);
+
+ Mockito.verify(mMockListener, times(1))
+ .testLog(Mockito.contains("screenshot"), Mockito.any(), Mockito.eq(screenshotData));
+ }
+
+ @Test
public void run_packageResetSuccess() throws DeviceNotAvailableException {
ITestDevice mMockDevice = mock(ITestDevice.class);
when(mMockDevice.executeShellV2Command(String.format("pm clear %s", TEST_PACKAGE_NAME)))