Add a Builder for TransformationResult.

PiperOrigin-RevId: 421278099
This commit is contained in:
samrobinson 2022-01-12 14:05:49 +00:00 committed by Ian Baker
parent f8d84eec59
commit d18c572d24
2 changed files with 68 additions and 35 deletions

View File

@ -43,12 +43,45 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
/** Information about the result of successfully running a transformer. */ /** Information about the result of successfully running a transformer. */
public static final class TransformationResult { public static final class TransformationResult {
public final String testId;
public final long outputSizeBytes;
private TransformationResult(String testId, long outputSizeBytes) { /** A builder for {@link TransformationResult} instances. */
public static final class Builder {
private final String testId;
@Nullable private Long fileSizeBytes;
public Builder(String testId) {
this.testId = testId;
}
public Builder setFileSizeBytes(long fileSizeBytes) {
this.fileSizeBytes = fileSizeBytes;
return this;
}
public TransformationResult build() {
return new TransformationResult(testId, fileSizeBytes);
}
}
public final String testId;
@Nullable public final Long fileSizeBytes;
private TransformationResult(String testId, @Nullable Long fileSizeBytes) {
this.testId = testId; this.testId = testId;
this.outputSizeBytes = outputSizeBytes; this.fileSizeBytes = fileSizeBytes;
}
/**
* Returns all the analysis data from the test.
*
* <p>If a value was not generated, it will not be part of the return value.
*/
public String getFormattedAnalysis() {
String analysis = "test=" + testId;
if (fileSizeBytes != null) {
analysis += ", fileSizeBytes=" + fileSizeBytes;
}
return analysis;
} }
} }
@ -108,9 +141,10 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
if (exception != null) { if (exception != null) {
throw exception; throw exception;
} }
long outputSizeBytes = outputVideoFile.length();
TransformationResult result = new TransformationResult(testId, outputSizeBytes); TransformationResult result =
new TransformationResult.Builder(testId).setFileSizeBytes(outputVideoFile.length()).build();
writeTransformationResultToFile(context, result); writeTransformationResultToFile(context, result);
return result; return result;
} }
@ -121,16 +155,15 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
createExternalCacheFile(context, /* fileName= */ result.testId + "-result.txt"); createExternalCacheFile(context, /* fileName= */ result.testId + "-result.txt");
try (FileWriter fileWriter = new FileWriter(analysisFile)) { try (FileWriter fileWriter = new FileWriter(analysisFile)) {
String fileContents = String fileContents =
"test=" result.getFormattedAnalysis()
+ result.testId + ", deviceFingerprint="
+ Build.FINGERPRINT
+ ", deviceBrand=" + ", deviceBrand="
+ Build.MANUFACTURER + Build.MANUFACTURER
+ ", deviceModel=" + ", deviceModel="
+ Build.MODEL + Build.MODEL
+ ", sdkVersion=" + ", sdkVersion="
+ Build.VERSION.SDK_INT + Build.VERSION.SDK_INT;
+ ", outputSizeBytes="
+ result.outputSizeBytes;
fileWriter.write(fileContents); fileWriter.write(fileContents);
} }
} }

View File

@ -24,16 +24,16 @@ import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.transformer.TransformationRequest; import com.google.android.exoplayer2.transformer.TransformationRequest;
import com.google.android.exoplayer2.transformer.Transformer; import com.google.android.exoplayer2.transformer.Transformer;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
/** Tests repeated transcoding operations (as a stress test and to help reproduce flakiness). */ /** Tests repeated transcoding operations (as a stress test and to help reproduce flakiness). */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@Ignore("Internal - b/206917996") // @Ignore("Internal - b/206917996")
public final class RepeatedTranscodeTransformationTest { public final class RepeatedTranscodeTransformationTest {
private static final int TRANSCODE_COUNT = 10; private static final int TRANSCODE_COUNT = 10;
@ -55,14 +55,14 @@ public final class RepeatedTranscodeTransformationTest {
Set<Long> differentOutputSizesBytes = new HashSet<>(); Set<Long> differentOutputSizesBytes = new HashSet<>();
for (int i = 0; i < TRANSCODE_COUNT; i++) { for (int i = 0; i < TRANSCODE_COUNT; i++) {
// Use a long video in case an error occurs a while after the start of the video. // Use a long video in case an error occurs a while after the start of the video.
differentOutputSizesBytes.add( AndroidTestUtil.TransformationResult result =
runTransformer( runTransformer(
context, context,
/* testId= */ "repeatedTranscode_givesConsistentLengthOutput", /* testId= */ "repeatedTranscode_givesConsistentLengthOutput_" + i,
transformer, transformer,
AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING, AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING,
/* timeoutSeconds= */ 120) /* timeoutSeconds= */ 120);
.outputSizeBytes); differentOutputSizesBytes.add(Assertions.checkNotNull(result.fileSizeBytes));
} }
assertWithMessage( assertWithMessage(
@ -89,14 +89,14 @@ public final class RepeatedTranscodeTransformationTest {
Set<Long> differentOutputSizesBytes = new HashSet<>(); Set<Long> differentOutputSizesBytes = new HashSet<>();
for (int i = 0; i < TRANSCODE_COUNT; i++) { for (int i = 0; i < TRANSCODE_COUNT; i++) {
// Use a long video in case an error occurs a while after the start of the video. // Use a long video in case an error occurs a while after the start of the video.
differentOutputSizesBytes.add( AndroidTestUtil.TransformationResult result =
runTransformer( runTransformer(
context, context,
/* testId= */ "repeatedTranscodeNoAudio_givesConsistentLengthOutput", /* testId= */ "repeatedTranscodeNoAudio_givesConsistentLengthOutput_" + i,
transformer, transformer,
AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING, AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING,
/* timeoutSeconds= */ 120) /* timeoutSeconds= */ 120);
.outputSizeBytes); differentOutputSizesBytes.add(Assertions.checkNotNull(result.fileSizeBytes));
} }
assertWithMessage( assertWithMessage(
@ -108,7 +108,7 @@ public final class RepeatedTranscodeTransformationTest {
@Test @Test
public void repeatedTranscodeNoVideo_givesConsistentLengthOutput() throws Exception { public void repeatedTranscodeNoVideo_givesConsistentLengthOutput() throws Exception {
Context context = ApplicationProvider.getApplicationContext(); Context context = ApplicationProvider.getApplicationContext();
Transformer transcodingTransformer = Transformer transformer =
new Transformer.Builder(context) new Transformer.Builder(context)
.setRemoveVideo(true) .setRemoveVideo(true)
.setTransformationRequest( .setTransformationRequest(
@ -120,14 +120,14 @@ public final class RepeatedTranscodeTransformationTest {
Set<Long> differentOutputSizesBytes = new HashSet<>(); Set<Long> differentOutputSizesBytes = new HashSet<>();
for (int i = 0; i < TRANSCODE_COUNT; i++) { for (int i = 0; i < TRANSCODE_COUNT; i++) {
// Use a long video in case an error occurs a while after the start of the video. // Use a long video in case an error occurs a while after the start of the video.
differentOutputSizesBytes.add( AndroidTestUtil.TransformationResult result =
runTransformer( runTransformer(
context, context,
/* testId= */ "repeatedTranscodeNoVideo_givesConsistentLengthOutput", /* testId= */ "repeatedTranscodeNoVideo_givesConsistentLengthOutput_" + i,
transcodingTransformer, transformer,
AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING, AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING,
/* timeoutSeconds= */ 120) /* timeoutSeconds= */ 120);
.outputSizeBytes); differentOutputSizesBytes.add(Assertions.checkNotNull(result.fileSizeBytes));
} }
assertWithMessage( assertWithMessage(