Skip to content

Commit

Permalink
Move Json helper methods to AndroidTestUtil.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 438253138
  • Loading branch information
Samrobbo authored and icbaker committed Apr 6, 2022
1 parent 3ac7e0e commit 01c24e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 55 deletions.
Expand Up @@ -18,8 +18,12 @@
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 {
Expand All @@ -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() {}
}
Expand Up @@ -17,6 +17,8 @@

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 {
Expand Down Expand Up @@ -111,11 +113,38 @@ public TransformationTestResult build() {
/** 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,
Expand Down
Expand Up @@ -20,9 +20,7 @@

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;
Expand Down Expand Up @@ -179,13 +177,13 @@ public TransformationTestResult run(String testId, String uriString) throws Exce
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);
Expand Down Expand Up @@ -308,7 +306,7 @@ public void onTransformationError(

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);

Expand All @@ -321,53 +319,4 @@ private static void writeTestSummaryToFile(Context context, String testId, JSONO
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;
}
}

0 comments on commit 01c24e4

Please sign in to comment.