From 01c24e4de88382fc2bb86cf211f030fc57463d99 Mon Sep 17 00:00:00 2001 From: samrobinson Date: Wed, 30 Mar 2022 11:04:08 +0100 Subject: [PATCH] Move Json helper methods to AndroidTestUtil. PiperOrigin-RevId: 438253138 --- .../media3/transformer/AndroidTestUtil.java | 32 +++++++++++ .../transformer/TransformationTestResult.java | 31 +++++++++- .../TransformerAndroidTestRunner.java | 57 +------------------ 3 files changed, 65 insertions(+), 55 deletions(-) diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java index 9560be07b7..5740f31cb7 100644 --- a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java @@ -18,8 +18,12 @@ package androidx.media3.transformer; import static androidx.media3.common.util.Assertions.checkState; import android.content.Context; +import android.os.Build; +import androidx.media3.common.util.Log; import java.io.File; import java.io.IOException; +import org.json.JSONException; +import org.json.JSONObject; /** Utilities for instrumentation tests. */ public final class AndroidTestUtil { @@ -44,5 +48,33 @@ public final class AndroidTestUtil { return file; } + /** + * Returns a {@link JSONObject} containing device specific details from {@link Build}, including + * manufacturer, model, SDK version and build fingerprint. + */ + public static JSONObject getDeviceDetailsAsJsonObject() throws JSONException { + return new JSONObject() + .put("manufacturer", Build.MANUFACTURER) + .put("model", Build.MODEL) + .put("sdkVersion", Build.VERSION.SDK_INT) + .put("fingerprint", Build.FINGERPRINT); + } + + /** + * Converts an exception to a {@link JSONObject}. + * + *

If the exception is a {@link TransformationException}, {@code errorCode} is included. + */ + public static JSONObject exceptionAsJsonObject(Exception exception) throws JSONException { + JSONObject exceptionJson = new JSONObject(); + exceptionJson.put("message", exception.getMessage()); + exceptionJson.put("type", exception.getClass()); + if (exception instanceof TransformationException) { + exceptionJson.put("errorCode", ((TransformationException) exception).errorCode); + } + exceptionJson.put("stackTrace", Log.getThrowableString(exception)); + return exceptionJson; + } + private AndroidTestUtil() {} } diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTestResult.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTestResult.java index 06cd6f27ce..d74e5484d9 100644 --- a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTestResult.java +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTestResult.java @@ -17,6 +17,8 @@ package androidx.media3.transformer; import androidx.annotation.Nullable; import androidx.media3.common.C; +import org.json.JSONException; +import org.json.JSONObject; /** A test only class for holding the details of a test transformation. */ public class TransformationTestResult { @@ -111,11 +113,38 @@ public class TransformationTestResult { /** The SSIM score of the transformation, {@link #SSIM_UNSET} if unavailable. */ public final double ssim; /** - * The {@link Exception} that was thrown during post-tranformation analysis, or {@code null} if + * The {@link Exception} that was thrown during post-transformation analysis, or {@code null} if * nothing was thrown. */ @Nullable public final Exception analysisException; + /** Returns a {@link JSONObject} representing all the values in {@code this}. */ + public JSONObject asJsonObject() throws JSONException { + JSONObject jsonObject = new JSONObject(); + if (transformationResult.durationMs != C.LENGTH_UNSET) { + jsonObject.put("durationMs", transformationResult.durationMs); + } + if (transformationResult.fileSizeBytes != C.LENGTH_UNSET) { + jsonObject.put("fileSizeBytes", transformationResult.fileSizeBytes); + } + if (transformationResult.averageAudioBitrate != C.RATE_UNSET_INT) { + jsonObject.put("averageAudioBitrate", transformationResult.averageAudioBitrate); + } + if (transformationResult.averageVideoBitrate != C.RATE_UNSET_INT) { + jsonObject.put("averageVideoBitrate", transformationResult.averageVideoBitrate); + } + if (elapsedTimeMs != C.TIME_UNSET) { + jsonObject.put("elapsedTimeMs", elapsedTimeMs); + } + if (ssim != TransformationTestResult.SSIM_UNSET) { + jsonObject.put("ssim", ssim); + } + if (analysisException != null) { + jsonObject.put("analysisException", AndroidTestUtil.exceptionAsJsonObject(analysisException)); + } + return jsonObject; + } + private TransformationTestResult( TransformationResult transformationResult, @Nullable String filePath, diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformerAndroidTestRunner.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformerAndroidTestRunner.java index 9211149761..043eaa2b84 100644 --- a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformerAndroidTestRunner.java +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformerAndroidTestRunner.java @@ -20,9 +20,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; import android.content.Context; import android.net.Uri; -import android.os.Build; import androidx.annotation.Nullable; -import androidx.media3.common.C; import androidx.media3.common.MediaItem; import androidx.media3.common.util.Log; import androidx.media3.common.util.SystemClock; @@ -179,13 +177,13 @@ public class TransformerAndroidTestRunner { resultJson.put("inputValues", JSONObject.wrap(inputValues)); try { TransformationTestResult transformationTestResult = runInternal(testId, uriString); - resultJson.put("transformationResult", getTestResultJson(transformationTestResult)); + resultJson.put("transformationResult", transformationTestResult.asJsonObject()); if (!suppressAnalysisExceptions && transformationTestResult.analysisException != null) { throw transformationTestResult.analysisException; } return transformationTestResult; } catch (Exception e) { - resultJson.put("exception", getExceptionJson(e)); + resultJson.put("exception", AndroidTestUtil.exceptionAsJsonObject(e)); throw e; } finally { writeTestSummaryToFile(context, testId, resultJson); @@ -308,7 +306,7 @@ public class TransformerAndroidTestRunner { private static void writeTestSummaryToFile(Context context, String testId, JSONObject resultJson) throws IOException, JSONException { - resultJson.put("testId", testId).put("device", getDeviceJson()); + resultJson.put("testId", testId).put("device", AndroidTestUtil.getDeviceDetailsAsJsonObject()); String analysisContents = resultJson.toString(/* indentSpaces= */ 2); @@ -321,53 +319,4 @@ public class TransformerAndroidTestRunner { fileWriter.write(analysisContents); } } - - private static JSONObject getDeviceJson() throws JSONException { - return new JSONObject() - .put("manufacturer", Build.MANUFACTURER) - .put("model", Build.MODEL) - .put("sdkVersion", Build.VERSION.SDK_INT) - .put("fingerprint", Build.FINGERPRINT); - } - - private static JSONObject getTestResultJson(TransformationTestResult testResult) - throws JSONException { - TransformationResult transformationResult = testResult.transformationResult; - - JSONObject transformationResultJson = new JSONObject(); - if (transformationResult.durationMs != C.LENGTH_UNSET) { - transformationResultJson.put("durationMs", transformationResult.durationMs); - } - if (transformationResult.fileSizeBytes != C.LENGTH_UNSET) { - transformationResultJson.put("fileSizeBytes", transformationResult.fileSizeBytes); - } - if (transformationResult.averageAudioBitrate != C.RATE_UNSET_INT) { - transformationResultJson.put("averageAudioBitrate", transformationResult.averageAudioBitrate); - } - if (transformationResult.averageVideoBitrate != C.RATE_UNSET_INT) { - transformationResultJson.put("averageVideoBitrate", transformationResult.averageVideoBitrate); - } - if (testResult.elapsedTimeMs != C.TIME_UNSET) { - transformationResultJson.put("elapsedTimeMs", testResult.elapsedTimeMs); - } - if (testResult.ssim != TransformationTestResult.SSIM_UNSET) { - transformationResultJson.put("ssim", testResult.ssim); - } - if (testResult.analysisException != null) { - transformationResultJson.put( - "analysisException", getExceptionJson(testResult.analysisException)); - } - return transformationResultJson; - } - - private static JSONObject getExceptionJson(Exception exception) throws JSONException { - JSONObject exceptionJson = new JSONObject(); - exceptionJson.put("message", exception.getMessage()); - exceptionJson.put("type", exception.getClass()); - if (exception instanceof TransformationException) { - exceptionJson.put("errorCode", ((TransformationException) exception).errorCode); - } - exceptionJson.put("stackTrace", Log.getThrowableString(exception)); - return exceptionJson; - } }