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
This commit is contained in:
jaewan 2020-08-18 14:59:20 +01:00 committed by Oliver Woodman
parent d0d5e8ec8f
commit be98509e03
2 changed files with 10 additions and 10 deletions

View file

@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.content.Context;
import android.os.Looper;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import androidx.annotation.NonNull;
@ -46,13 +45,6 @@ public class MediaSessionUtilTest {
@Test
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();
SessionPlayerConnector sessionPlayerConnector = playerTestRule.getSessionPlayerConnector();

View file

@ -55,15 +55,23 @@ import org.junit.rules.ExternalResource;
@Override
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();
executor = Executors.newFixedThreadPool(1);
InstrumentationRegistry.getInstrumentation()
.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
// originally initialized.
// originally initialized. [Internal: b/78617702]
// 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
// tests are finished.