Move Json helper methods to AndroidTestUtil.

PiperOrigin-RevId: 438253138
This commit is contained in:
samrobinson 2022-03-30 11:04:08 +01:00 committed by Ian Baker
parent 3ac7e0e84e
commit 01c24e4de8
3 changed files with 65 additions and 55 deletions

View file

@ -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}.
*
* <p>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() {}
}

View file

@ -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,

View file

@ -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;
}
}