Commit be98509e by jaewan Committed by Oliver Woodman

Workaround test failure with stable release of media2.session

MediaSession requires prepared Looper in 1.0.3-stable release,
which is the latest stable release for now.

The requirement would be removed in 1.1.0-stable, but workaround
is needed meanwhile.

PiperOrigin-RevId: 327220042
parent d0d5e8ec
...@@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; ...@@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.content.Context; import android.content.Context;
import android.os.Looper;
import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
...@@ -46,13 +45,6 @@ public class MediaSessionUtilTest { ...@@ -46,13 +45,6 @@ public class MediaSessionUtilTest {
@Test @Test
public void getSessionCompatToken_withMediaControllerCompat_returnsValidToken() throws Exception { public void getSessionCompatToken_withMediaControllerCompat_returnsValidToken() throws Exception {
// Workaround to instantiate MediaSession with public androidx.media dependency.
// TODO(b/146536708): Remove this workaround when the relevant change is released via
// androidx.media 1.2.0.
if (Looper.myLooper() == null) {
Looper.prepare();
}
Context context = ApplicationProvider.getApplicationContext(); Context context = ApplicationProvider.getApplicationContext();
SessionPlayerConnector sessionPlayerConnector = playerTestRule.getSessionPlayerConnector(); SessionPlayerConnector sessionPlayerConnector = playerTestRule.getSessionPlayerConnector();
......
...@@ -55,15 +55,23 @@ import org.junit.rules.ExternalResource; ...@@ -55,15 +55,23 @@ import org.junit.rules.ExternalResource;
@Override @Override
protected void before() { protected void before() {
// Workaround limitation in androidx.media2.session:1.0.3 which session can only be instantiated
// on thread with prepared Looper.
// TODO: Remove when androidx.media2.session:1.1.0 is released without the limitation
// [Internal: b/146536708]
if (Looper.myLooper() == null) {
Looper.prepare();
}
context = ApplicationProvider.getApplicationContext(); context = ApplicationProvider.getApplicationContext();
executor = Executors.newFixedThreadPool(1); executor = Executors.newFixedThreadPool(1);
InstrumentationRegistry.getInstrumentation() InstrumentationRegistry.getInstrumentation()
.runOnMainSync( .runOnMainSync(
() -> { () -> {
// Initialize AudioManager on the main thread to workaround b/78617702 that // Initialize AudioManager on the main thread to workaround that
// audio focus listener is called on the thread where the AudioManager was // audio focus listener is called on the thread where the AudioManager was
// originally initialized. // originally initialized. [Internal: b/78617702]
// Without posting this, audio focus listeners wouldn't be called because the // Without posting this, audio focus listeners wouldn't be called because the
// listeners would be posted to the test thread (here) where it waits until the // listeners would be posted to the test thread (here) where it waits until the
// tests are finished. // tests are finished.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment