From 9fca474027a4c96e9fd7b0a2c981df745c8688b5 Mon Sep 17 00:00:00 2001 From: samrobinson Date: Fri, 1 Apr 2022 14:17:58 +0100 Subject: [PATCH] Add file logging for skipping instrumentation tests. PiperOrigin-RevId: 438808231 --- .../transformer/AndroidTestUtil.java | 60 +++++++++++++++++-- .../TransformerAndroidTestRunner.java | 24 ++------ .../transformer/mh/TransformationTest.java | 9 ++- 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/AndroidTestUtil.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/AndroidTestUtil.java index 206687d246..b853519c79 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/AndroidTestUtil.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/AndroidTestUtil.java @@ -22,6 +22,7 @@ import android.os.Build; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.util.Log; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.json.JSONException; @@ -42,12 +43,22 @@ public final class AndroidTestUtil { public static final String MP4_REMOTE_4K60_PORTRAIT_URI_STRING = "https://storage.googleapis.com/exoplayer-test-media-1/mp4/portrait_4k60.mp4"; - /* package */ static File createExternalCacheFile(Context context, String fileName) - throws IOException { - File file = new File(context.getExternalCacheDir(), fileName); - checkState(!file.exists() || file.delete(), "Could not delete file: " + file.getAbsolutePath()); - checkState(file.createNewFile(), "Could not create file: " + file.getAbsolutePath()); - return file; + + /** + * Log in logcat and in an analysis file that this test was skipped. + * + *

Analysis file is a JSON summarising the test, saved to the application cache. + * + *

The analysis json will contain a {@code skipReason} key, with the reason for skipping the + * test case. + */ + public static void recordTestSkipped(Context context, String testId, String reason) + throws JSONException, IOException { + Log.i(testId, reason); + JSONObject testJson = new JSONObject(); + testJson.put("skipReason", reason); + + writeTestSummaryToFile(context, testId, testJson); } /** @@ -106,5 +117,42 @@ public final class AndroidTestUtil { return exceptionJson; } + /** + * Writes the summary of a test run to the application cache file. + * + *

The cache filename follows the pattern {@code -result.txt}. + * + * @param context The {@link Context}. + * @param testId A unique identifier for the transformer test run. + * @param testJson A {@link JSONObject} containing a summary of the test run. + */ + /* package */ static void writeTestSummaryToFile( + Context context, String testId, JSONObject testJson) throws IOException, JSONException { + testJson.put("testId", testId).put("device", getDeviceDetailsAsJsonObject()); + + String analysisContents = testJson.toString(/* indentSpaces= */ 2); + + // Log contents as well as writing to file, for easier visibility on individual device testing. + Log.i(testId, analysisContents); + + File analysisFile = createExternalCacheFile(context, /* fileName= */ testId + "-result.txt"); + try (FileWriter fileWriter = new FileWriter(analysisFile)) { + fileWriter.write(analysisContents); + } + } + + /** + * Creates a {@link File} of the {@code fileName} in the application cache directory. + * + *

If a file of that name already exists, it is overwritten. + */ + /* package */ static File createExternalCacheFile(Context context, String fileName) + throws IOException { + File file = new File(context.getExternalCacheDir(), fileName); + checkState(!file.exists() || file.delete(), "Could not delete file: " + file.getAbsolutePath()); + checkState(file.createNewFile(), "Could not create file: " + file.getAbsolutePath()); + return file; + } + private AndroidTestUtil() {} } diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/TransformerAndroidTestRunner.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/TransformerAndroidTestRunner.java index f9bf033b2a..77977da7a0 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/TransformerAndroidTestRunner.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/TransformerAndroidTestRunner.java @@ -26,14 +26,12 @@ import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.SystemClock; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; import org.checkerframework.checker.nullness.compatqual.NullableType; -import org.json.JSONException; import org.json.JSONObject; /** An android instrumentation test runner for {@link Transformer}. */ @@ -174,7 +172,9 @@ public class TransformerAndroidTestRunner { */ public TransformationTestResult run(String testId, String uriString) throws Exception { JSONObject resultJson = new JSONObject(); - resultJson.put("inputValues", JSONObject.wrap(inputValues)); + if (inputValues != null) { + resultJson.put("inputValues", JSONObject.wrap(inputValues)); + } try { TransformationTestResult transformationTestResult = runInternal(testId, uriString); resultJson.put("transformationResult", transformationTestResult.asJsonObject()); @@ -186,7 +186,7 @@ public class TransformerAndroidTestRunner { resultJson.put("exception", AndroidTestUtil.exceptionAsJsonObject(e)); throw e; } finally { - writeTestSummaryToFile(context, testId, resultJson); + AndroidTestUtil.writeTestSummaryToFile(context, testId, resultJson); } } @@ -303,20 +303,4 @@ public class TransformerAndroidTestRunner { return resultBuilder.build(); } - - private static void writeTestSummaryToFile(Context context, String testId, JSONObject resultJson) - throws IOException, JSONException { - resultJson.put("testId", testId).put("device", AndroidTestUtil.getDeviceDetailsAsJsonObject()); - - String analysisContents = resultJson.toString(/* indentSpaces= */ 2); - - // Log contents as well as writing to file, for easier visibility on individual device testing. - Log.i(TAG_PREFIX + testId, analysisContents); - - File analysisFile = - AndroidTestUtil.createExternalCacheFile(context, /* fileName= */ testId + "-result.txt"); - try (FileWriter fileWriter = new FileWriter(analysisFile)) { - fileWriter.write(analysisContents); - } - } } diff --git a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/mh/TransformationTest.java b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/mh/TransformationTest.java index 51ea018124..f09fab7b7e 100644 --- a/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/mh/TransformationTest.java +++ b/library/transformer/src/androidTest/java/com/google/android/exoplayer2/transformer/mh/TransformationTest.java @@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSE import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_URI_STRING; import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_WITH_INCREASING_TIMESTAMPS_URI_STRING; import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_REMOTE_4K60_PORTRAIT_URI_STRING; +import static com.google.android.exoplayer2.transformer.AndroidTestUtil.recordTestSkipped; import android.content.Context; import androidx.test.core.app.ApplicationProvider; @@ -30,7 +31,6 @@ import com.google.android.exoplayer2.transformer.TransformationRequest; import com.google.android.exoplayer2.transformer.Transformer; import com.google.android.exoplayer2.transformer.TransformerAndroidTestRunner; import com.google.android.exoplayer2.transformer.VideoEncoderSettings; -import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; import org.junit.Test; import org.junit.runner.RunWith; @@ -120,14 +120,17 @@ public class TransformationTest { @Test public void transformSef() throws Exception { String testId = TAG + "_transformSef"; + Context context = ApplicationProvider.getApplicationContext(); if (Util.SDK_INT < 25) { // TODO(b/210593256): Remove test skipping after removing the MediaMuxer dependency. - Log.i(testId, "Skipping on this API version due to lack of muxing support"); + recordTestSkipped( + context, + testId, + /* reason= */ "Skipping on this API version due to lack of muxing support"); return; } - Context context = ApplicationProvider.getApplicationContext(); Transformer transformer = new Transformer.Builder(context) .setTransformationRequest(