diff --git a/libraries/transformer/build.gradle b/libraries/transformer/build.gradle index 67ad6ceadd..a70db44205 100644 --- a/libraries/transformer/build.gradle +++ b/libraries/transformer/build.gradle @@ -12,15 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. apply from: "$gradle.ext.androidxMediaSettingsDir/common_library_config.gradle" - android { + + defaultConfig { + // The following argument makes the Android Test Orchestrator run its + // "pm clear" command after each test invocation. This command ensures + // that the app's state is completely cleared between tests. + testInstrumentationRunnerArguments clearPackageData: 'true' + multiDexEnabled true + } + buildTypes { debug { testCoverageEnabled = true } } - sourceSets.test.assets.srcDir '../test_data/src/test/assets/' + sourceSets { + androidTest.assets.srcDir '../test_data/src/test/assets/' //copybara:media3-only + test.assets.srcDir '../test_data/src/test/assets/' + } } dependencies { @@ -33,6 +44,10 @@ dependencies { testImplementation project(modulePrefix + 'test-utils') testImplementation project(modulePrefix + 'test-data') testImplementation 'org.robolectric:robolectric:' + robolectricVersion + testImplementation 'com.google.truth:truth:' + truthVersion + androidTestImplementation 'junit:junit:' + junitVersion + androidTestImplementation 'androidx.test:runner:' + androidxTestRunnerVersion + androidTestImplementation project(modulePrefix + 'test-utils') } ext { diff --git a/libraries/transformer/src/androidTest/AndroidManifest.xml b/libraries/transformer/src/androidTest/AndroidManifest.xml new file mode 100644 index 0000000000..d383c6e76e --- /dev/null +++ b/libraries/transformer/src/androidTest/AndroidManifest.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java new file mode 100644 index 0000000000..78018417d1 --- /dev/null +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/AndroidTestUtil.java @@ -0,0 +1,91 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.transformer; + +import android.content.Context; +import android.net.Uri; +import androidx.annotation.Nullable; +import androidx.media3.common.MediaItem; +import androidx.media3.common.util.Assertions; +import androidx.test.platform.app.InstrumentationRegistry; +import java.io.File; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReference; +import org.checkerframework.checker.nullness.compatqual.NullableType; + +/** Utility methods for instrumentation tests. */ +/* package */ final class AndroidTestUtil { + public static final String MP4_ASSET_URI = "asset:///media/mp4/sample.mp4"; + public static final String SEF_ASSET_URI = "asset:///media/mp4/sample_sef_slow_motion.mp4"; + + /** Transforms the {@code uriString} with the {@link Transformer}. */ + public static void runTransformer(Context context, Transformer transformer, String uriString) + throws Exception { + AtomicReference<@NullableType Exception> exceptionReference = new AtomicReference<>(); + CountDownLatch countDownLatch = new CountDownLatch(1); + + Transformer testTransformer = + transformer + .buildUpon() + .setListener( + new Transformer.Listener() { + @Override + public void onTransformationCompleted(MediaItem inputMediaItem) { + countDownLatch.countDown(); + } + + @Override + public void onTransformationError(MediaItem inputMediaItem, Exception exception) { + exceptionReference.set(exception); + countDownLatch.countDown(); + } + }) + .build(); + + Uri uri = Uri.parse(uriString); + File externalCacheFile = createExternalCacheFile(uri, context); + try { + InstrumentationRegistry.getInstrumentation() + .runOnMainSync( + () -> { + try { + testTransformer.startTransformation( + MediaItem.fromUri(uri), externalCacheFile.getAbsolutePath()); + } catch (IOException e) { + exceptionReference.set(e); + } + }); + countDownLatch.await(); + @Nullable Exception exception = exceptionReference.get(); + if (exception != null) { + throw exception; + } + } finally { + externalCacheFile.delete(); + } + } + + private static File createExternalCacheFile(Uri uri, Context context) throws IOException { + File file = new File(context.getExternalCacheDir(), "transformer-" + uri.hashCode()); + Assertions.checkState( + !file.exists() || file.delete(), "Could not delete the previous transformer output file"); + Assertions.checkState(file.createNewFile(), "Could not create the transformer output file"); + return file; + } + + private AndroidTestUtil() {} +} diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/RemoveAudioTransformationTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/RemoveAudioTransformationTest.java new file mode 100644 index 0000000000..fc8565d4a3 --- /dev/null +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/RemoveAudioTransformationTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.transformer; + +import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_URI; +import static androidx.media3.transformer.AndroidTestUtil.runTransformer; + +import android.content.Context; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** {@link Transformer} instrumentation test for removing audio. */ +@RunWith(AndroidJUnit4.class) +public class RemoveAudioTransformationTest { + @Test + public void removeAudioTransform() throws Exception { + Context context = ApplicationProvider.getApplicationContext(); + Transformer transformer = + new Transformer.Builder().setContext(context).setRemoveAudio(true).build(); + runTransformer(context, transformer, MP4_ASSET_URI); + } +} diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/RemoveVideoTransformationTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/RemoveVideoTransformationTest.java new file mode 100644 index 0000000000..c3ad13a7ae --- /dev/null +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/RemoveVideoTransformationTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.transformer; + +import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_URI; +import static androidx.media3.transformer.AndroidTestUtil.runTransformer; + +import android.content.Context; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** {@link Transformer} instrumentation test for removing video. */ +@RunWith(AndroidJUnit4.class) +public class RemoveVideoTransformationTest { + @Test + public void removeVideoTransform() throws Exception { + Context context = ApplicationProvider.getApplicationContext(); + Transformer transformer = + new Transformer.Builder().setContext(context).setRemoveVideo(true).build(); + runTransformer(context, transformer, MP4_ASSET_URI); + } +} diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/SefTransformationTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/SefTransformationTest.java new file mode 100644 index 0000000000..1172aab34b --- /dev/null +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/SefTransformationTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.transformer; + +import static androidx.media3.transformer.AndroidTestUtil.SEF_ASSET_URI; +import static androidx.media3.transformer.AndroidTestUtil.runTransformer; + +import android.content.Context; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** {@link Transformer} instrumentation test for SEF. */ +@RunWith(AndroidJUnit4.class) +public class SefTransformationTest { + @Test + public void sefTransform() throws Exception { + Context context = ApplicationProvider.getApplicationContext(); + Transformer transformer = + new Transformer.Builder().setContext(context).setFlattenForSlowMotion(true).build(); + runTransformer(context, transformer, SEF_ASSET_URI); + } +} diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTest.java new file mode 100644 index 0000000000..19a1882814 --- /dev/null +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/TransformationTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.transformer; + +import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_URI; +import static androidx.media3.transformer.AndroidTestUtil.runTransformer; + +import android.content.Context; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** {@link Transformer} instrumentation test. */ +@RunWith(AndroidJUnit4.class) +public class TransformationTest { + @Test + public void transform() throws Exception { + Context context = ApplicationProvider.getApplicationContext(); + Transformer transformer = new Transformer.Builder().setContext(context).build(); + runTransformer(context, transformer, MP4_ASSET_URI); + } +}