diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2022-03-30 22:09:03 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-03-30 22:09:03 +0000 |
commit | 4431586004f2b14e1c6bcdbc4a7dd807635ab040 (patch) | |
tree | db102b790c86f14a4abbbd4907b0407eae49da56 | |
parent | 4a8fce3f8ac1b427c821bb639c344aca1e60458b (diff) | |
parent | 350b2d819f0a95cd60a8d4fef4f4c331027675c4 (diff) | |
download | platform_testing-4431586004f2b14e1c6bcdbc4a7dd807635ab040.tar.gz |
Modify ScreenRecordCollector to work with ClassMetricRule. am: ab2caa2d7e am: 350b2d819f
Original change: https://googleplex-android-review.googlesource.com/c/platform/platform_testing/+/17509333
Change-Id: Icbced451c21bd374f4fa64ed9f43e03ac2a3b22a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
3 files changed, 110 insertions, 18 deletions
diff --git a/libraries/device-collectors/src/main/java/android/device/collectors/BaseMetricListener.java b/libraries/device-collectors/src/main/java/android/device/collectors/BaseMetricListener.java index 0663325ea..9d4a1323f 100644 --- a/libraries/device-collectors/src/main/java/android/device/collectors/BaseMetricListener.java +++ b/libraries/device-collectors/src/main/java/android/device/collectors/BaseMetricListener.java @@ -341,15 +341,18 @@ public class BaseMetricListener extends InstrumentationRunListener { } /** - * Create a directory inside external storage, and empty it. + * Create a directory inside external storage, and optionally empty it. * * @param dir full path to the dir to be created. + * @param empty whether to empty the new dirctory. * @return directory file created */ - public File createAndEmptyDirectory(String dir) { + public File createDirectory(String dir, boolean empty) { File rootDir = Environment.getExternalStorageDirectory(); File destDir = new File(rootDir, dir); - executeCommandBlocking("rm -rf " + destDir.getAbsolutePath()); + if (empty) { + executeCommandBlocking("rm -rf " + destDir.getAbsolutePath()); + } if (!destDir.exists() && !destDir.mkdirs()) { Log.e(getTag(), "Unable to create dir: " + destDir.getAbsolutePath()); return null; @@ -358,6 +361,16 @@ public class BaseMetricListener extends InstrumentationRunListener { } /** + * Create a directory inside external storage, and empty it. + * + * @param dir full path to the dir to be created. + * @return directory file created + */ + public File createAndEmptyDirectory(String dir) { + return createDirectory(dir, true); + } + + /** * Delete a directory and all the file inside. * * @param rootDir the {@link File} directory to delete. diff --git a/libraries/device-collectors/src/main/java/android/device/collectors/ScreenRecordCollector.java b/libraries/device-collectors/src/main/java/android/device/collectors/ScreenRecordCollector.java index 04ab1fe10..da9c98c71 100644 --- a/libraries/device-collectors/src/main/java/android/device/collectors/ScreenRecordCollector.java +++ b/libraries/device-collectors/src/main/java/android/device/collectors/ScreenRecordCollector.java @@ -47,6 +47,10 @@ public class ScreenRecordCollector extends BaseMetricListener { // * "low" is 1/8 the resolution. // * Otherwise, use the resolution. @VisibleForTesting static final String QUALITY_ARG = "video-quality"; + // Option for whether to empty the output directory before collecting. Defaults to true. Setting + // to false is useful when multiple test classes need recordings and recordings are pulled at + // the end of the test run. + @VisibleForTesting static final String EMPTY_OUTPUT_DIR_ARG = "empty-output-dir"; // Maximum parts per test (each part is <= 3min). @VisibleForTesting static final int MAX_RECORDING_PARTS = 5; private static final long VIDEO_TAIL_BUFFER = 500; @@ -59,6 +63,7 @@ public class ScreenRecordCollector extends BaseMetricListener { private RecordingThread mCurrentThread; private String mVideoDimensions; + private boolean mEmptyOutputDir; // Tracks the test iterations to ensure that each failure gets unique filenames. // Key: test description; value: number of iterations. @@ -76,11 +81,15 @@ public class ScreenRecordCollector extends BaseMetricListener { @Override public void onSetUp() { - mDestDir = createAndEmptyDirectory(OUTPUT_DIR); + mDestDir = createDirectory(OUTPUT_DIR, mEmptyOutputDir); } @Override public void setupAdditionalArgs() { + mEmptyOutputDir = + Boolean.parseBoolean( + getArgsBundle().getString(EMPTY_OUTPUT_DIR_ARG, String.valueOf(true))); + try { long scaleDown = 1; switch (getArgsBundle().getString(QUALITY_ARG, "default")) { @@ -166,18 +175,24 @@ public class ScreenRecordCollector extends BaseMetricListener { /** Returns the recording's name for part {@code part} of test {@code description}. */ private File getOutputFile(Description description, int part) { - final String baseName = - String.format("%s.%s", description.getClassName(), description.getMethodName()); - // Omit the iteration number for the first iteration. + StringBuilder builder = new StringBuilder(description.getClassName()); + if (description.getMethodName() != null) { + builder.append("."); + builder.append(description.getMethodName()); + } int iteration = mTestIterations.get(description.getDisplayName()); - final String fileName = - String.format( - "%s-video%s.mp4", - iteration == 1 - ? baseName - : String.join("-", baseName, String.valueOf(iteration)), - part == 1 ? "" : part); - return Paths.get(mDestDir.getAbsolutePath(), fileName).toFile(); + // Omit the iteration number for the first iteration. + if (iteration > 1) { + builder.append("-"); + builder.append(iteration); + } + builder.append("-video"); + // Omit the part number for the first part. + if (part > 1) { + builder.append(part); + } + builder.append(".mp4"); + return Paths.get(mDestDir.getAbsolutePath(), builder.toString()).toFile(); } /** Returns a buffer duration for the end of the video. */ diff --git a/libraries/device-collectors/src/test/java/android/device/collectors/ScreenRecordCollectorTest.java b/libraries/device-collectors/src/test/java/android/device/collectors/ScreenRecordCollectorTest.java index ce1bfa55c..71162f473 100644 --- a/libraries/device-collectors/src/test/java/android/device/collectors/ScreenRecordCollectorTest.java +++ b/libraries/device-collectors/src/test/java/android/device/collectors/ScreenRecordCollectorTest.java @@ -16,7 +16,10 @@ package android.device.collectors; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.AdditionalMatchers.not; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.endsWith; import static org.mockito.ArgumentMatchers.eq; @@ -95,7 +98,7 @@ public class ScreenRecordCollectorTest { listener = spy(new ScreenRecordCollector()); } listener.setInstrumentation(mInstrumentation); - doReturn(mLogDir).when(listener).createAndEmptyDirectory(anyString()); + doReturn(mLogDir).when(listener).createDirectory(anyString(), anyBoolean()); doReturn(0L).when(listener).getTailBuffer(); doReturn(mDevice).when(listener).getDevice(); doReturn("1234").when(mDevice).executeShellCommand(eq("pidof screenrecord")); @@ -113,7 +116,7 @@ public class ScreenRecordCollectorTest { // Verify output directories are created on test run start. mListener.testRunStarted(mRunDesc); - verify(mListener).createAndEmptyDirectory(ScreenRecordCollector.OUTPUT_DIR); + verify(mListener).createDirectory(ScreenRecordCollector.OUTPUT_DIR, true); // Walk through a number of test cases to simulate behavior. for (int i = 1; i <= NUM_TEST_CASE; i++) { @@ -156,7 +159,14 @@ public class ScreenRecordCollectorTest { int videoCount = 0; for (Bundle bundle : capturedBundle) { for (String key : bundle.keySet()) { - if (key.contains("mp4")) videoCount++; + if (key.contains("mp4")) { + videoCount++; + assertTrue(key.contains(mTestDesc.getClassName())); + assertTrue(key.contains(mTestDesc.getMethodName())); + String fileName = bundle.getString(key); + assertTrue(fileName.contains(mTestDesc.getClassName())); + assertTrue(fileName.contains(mTestDesc.getMethodName())); + } } } assertEquals(NUM_TEST_CASE * ScreenRecordCollector.MAX_RECORDING_PARTS, videoCount); @@ -280,4 +290,58 @@ public class ScreenRecordCollectorTest { verify(mDevice, atLeastOnce()) .executeShellCommand(not(matches("screenrecord .*video.mp4"))); } + + /** Test that the empty-output-dir works. */ + @Test + public void testEmptyrOutputDirOptionSetToFalse() throws Exception { + Bundle args = new Bundle(); + args.putString(ScreenRecordCollector.EMPTY_OUTPUT_DIR_ARG, "false"); + mListener = initListener(args); + + // Verify output directories are created on test run start. + mListener.testRunStarted(mRunDesc); + verify(mListener).createDirectory(ScreenRecordCollector.OUTPUT_DIR, false); + } + + /** + * Test that descriptions with null method names only result in class names in the video file + * names. + */ + @Test + public void testNullMethodNameDoesNotAppearInVideoName() throws Exception { + mListener = initListener(null); + + mListener.testRunStarted(mRunDesc); + + // mRunDesc does not have a method name. + mListener.testStarted(mRunDesc); + // Delay verification by 100 ms to ensure the thread was started. + SystemClock.sleep(100); + mListener.testFinished(mRunDesc); + mListener.testRunFinished(new Result()); + + Bundle resultBundle = new Bundle(); + mListener.instrumentationRunFinished(System.out, resultBundle, new Result()); + + ArgumentCaptor<Bundle> capture = ArgumentCaptor.forClass(Bundle.class); + Mockito.verify(mInstrumentation, times(1)) + .sendStatus( + Mockito.eq(SendToInstrumentation.INST_STATUS_IN_PROGRESS), + capture.capture()); + Bundle metrics = capture.getValue(); + // Ensure that we have recordings, and none of them have "null" in their file name or metric + // key. + boolean hasRecordings = false; + for (String key : metrics.keySet()) { + if (key.startsWith(mListener.getTag())) { + hasRecordings = true; + assertTrue(key.contains(mRunDesc.getClassName())); + assertFalse(key.contains("null")); + String fileName = metrics.getString(key); + assertTrue(fileName.contains(mRunDesc.getClassName())); + assertFalse(fileName.contains("null")); + } + } + assertTrue(hasRecordings); + } } |