Skip to content

Commit ff498c4

Browse files
icbakercopybara-github
authored andcommitted
Fix proguard tests to use the instrumentation or app context as needed
The instrumentation context is needed to access assets bundled into the **instrumenting** test APK. Unfortunately the instrumentation context returns `null` from `getApplicationContext()`, so we have to carefully only use it for specifically loading the asset. PiperOrigin-RevId: 875124490
1 parent 3a3d000 commit ff498c4

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

libraries/test_proguard/src/androidTest/java/androidx/media3/test/proguard/ExtractorModuleProguardTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,24 @@
1515
*/
1616
package androidx.media3.test.proguard;
1717

18-
import androidx.test.core.app.ApplicationProvider;
1918
import androidx.test.ext.junit.runners.AndroidJUnit4;
20-
import org.junit.Ignore;
19+
import androidx.test.platform.app.InstrumentationRegistry;
2120
import org.junit.Test;
2221
import org.junit.runner.RunWith;
2322

2423
/** Unit test executing methods in {@link ExtractorModuleProguard}. */
2524
@RunWith(AndroidJUnit4.class)
26-
@Ignore("Fails on gradle: b/463675073")
2725
public final class ExtractorModuleProguardTest {
2826

2927
@Test
3028
public void defaultExtractorFactory_createExtensionFlacExtractor_succeeds() throws Exception {
3129
ExtractorModuleProguard.createLibFlacExtractorWithDefaultExtractorsFactory(
32-
ApplicationProvider.getApplicationContext());
30+
InstrumentationRegistry.getInstrumentation().getContext());
3331
}
3432

3533
@Test
3634
public void defaultExtractorFactory_createMidiExtractor_succeeds() throws Exception {
3735
ExtractorModuleProguard.createMidiExtractorWithDefaultExtractorsFactory(
38-
ApplicationProvider.getApplicationContext());
36+
InstrumentationRegistry.getInstrumentation().getContext());
3937
}
4038
}

libraries/test_proguard/src/androidTest/java/androidx/media3/test/proguard/UiModuleProguardTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
2020

2121
import androidx.test.ext.junit.runners.AndroidJUnit4;
22-
import org.junit.Ignore;
22+
import androidx.test.platform.app.InstrumentationRegistry;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525

@@ -46,10 +46,9 @@ public void playerView_inflateVideoDecoderGLSurfaceView_succeeds() {
4646
}
4747

4848
@Test
49-
@Ignore("Fails on gradle: b/463675073")
5049
public void playerControlView_scrubbingWithExoPlayer_succeeds() throws Exception {
5150
UiModuleProguard.scrubOnTimeBarWithExoPlayerAndCheckThatSuppressionReasonChangesAndSeeksHappen(
52-
getApplicationContext());
51+
InstrumentationRegistry.getInstrumentation().getContext(), getApplicationContext());
5352
}
5453

5554
@Test

libraries/test_proguard/src/main/java/androidx/media3/test/proguard/UiModuleProguard.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
import androidx.media3.common.TrackGroup;
3838
import androidx.media3.common.Tracks;
3939
import androidx.media3.common.util.ConditionVariable;
40+
import androidx.media3.datasource.AssetDataSource;
4041
import androidx.media3.exoplayer.BaseRenderer;
4142
import androidx.media3.exoplayer.ExoPlayer;
4243
import androidx.media3.exoplayer.Renderer;
4344
import androidx.media3.exoplayer.RendererCapabilities;
45+
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
4446
import androidx.media3.exoplayer.video.VideoDecoderGLSurfaceView;
4547
import androidx.media3.exoplayer.video.spherical.SphericalGLSurfaceView;
4648
import androidx.media3.transformer.Composition;
@@ -87,7 +89,7 @@ public static void inflatePlayerViewWithVideoDecoderGLSurfaceView(Context contex
8789
* the suppression reason change and seeks happen.
8890
*/
8991
public static void scrubOnTimeBarWithExoPlayerAndCheckThatSuppressionReasonChangesAndSeeksHappen(
90-
Context context) throws InterruptedException {
92+
Context instrumentationContext, Context applicationContext) throws InterruptedException {
9193
ConditionVariable playerReady = new ConditionVariable();
9294
List<@Player.PlaybackSuppressionReason Integer> playbackSuppressionReasons =
9395
Collections.synchronizedList(new ArrayList<>());
@@ -119,11 +121,20 @@ public void onPositionDiscontinuity(
119121
Handler mainHandler = new Handler(Looper.getMainLooper());
120122
mainHandler.post(
121123
() -> {
122-
ExoPlayer player = new ExoPlayer.Builder(context).build();
124+
ExoPlayer player =
125+
new ExoPlayer.Builder(applicationContext)
126+
.setMediaSourceFactory(
127+
new DefaultMediaSourceFactory(
128+
// The asset media URI used below is accessible to the instrumentation
129+
// context, not the app context (which needs to be used for all other
130+
// purposes).
131+
/* dataSourceFactory= */ () ->
132+
new AssetDataSource(instrumentationContext)))
133+
.build();
123134
player.addListener(listener);
124135
PlayerControlView playerControlView =
125136
(PlayerControlView)
126-
LayoutInflater.from(context)
137+
LayoutInflater.from(applicationContext)
127138
.inflate(
128139
R.layout.player_control_view_with_scrubbable_timebar, /* root= */ null);
129140
playerControlView.setPlayer(player);

0 commit comments

Comments
 (0)