Fix MediaControllerSurfaceSizeChangeTest

There are multiple problems in the test:
 1. The controller must run on the main thread due to the Player
    interface requirement to add surfaces on the thread their
    callbacks will be called on.
 2. This test made assertions about an expected callback that
    aren't true. Setting an anonymous surface will not result in
    a size change callback because the size stays unknown.
    But clearing a surface (even if never set) ensures the returned
    size is guaranteed to be zero.

PiperOrigin-RevId: 559080905
This commit is contained in:
tonihei 2023-08-22 13:33:03 +01:00 committed by Julia Bibik
parent 2309fc5edb
commit a00bef9e51

View File

@ -22,6 +22,7 @@ import static androidx.media3.test.session.common.TestUtils.TIMEOUT_MS;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.os.Looper;
import android.os.RemoteException; import android.os.RemoteException;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@ -32,14 +33,14 @@ import android.view.ViewGroup.LayoutParams;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.Player; import androidx.media3.common.Player;
import androidx.media3.test.session.common.HandlerThreadTestRule; import androidx.media3.test.session.common.MainLooperTestRule;
import androidx.media3.test.session.common.PollingCheck; import androidx.media3.test.session.common.PollingCheck;
import androidx.media3.test.session.common.SurfaceActivity; import androidx.media3.test.session.common.SurfaceActivity;
import androidx.media3.test.session.common.TestUtils; import androidx.media3.test.session.common.TestUtils;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule; import androidx.test.rule.ActivityTestRule;
import java.util.Arrays; import com.google.common.collect.ImmutableList;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -48,8 +49,6 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -61,8 +60,6 @@ import org.junit.runners.Parameterized;
@LargeTest @LargeTest
public class MediaControllerSurfaceSizeChangeTest { public class MediaControllerSurfaceSizeChangeTest {
private static final String TAG = "MCSurfaceSizeChangeTest";
private static final String SET_VIDEO_SURFACE = "setVideoSurface"; private static final String SET_VIDEO_SURFACE = "setVideoSurface";
private static final String SET_VIDEO_SURFACE_HOLDER = "setVideoSurfaceHolder"; private static final String SET_VIDEO_SURFACE_HOLDER = "setVideoSurfaceHolder";
private static final String SET_VIDEO_SURFACE_VIEW = "setVideoSurfaceView"; private static final String SET_VIDEO_SURFACE_VIEW = "setVideoSurfaceView";
@ -81,11 +78,11 @@ public class MediaControllerSurfaceSizeChangeTest {
@Parameterized.Parameters( @Parameterized.Parameters(
name = "action:{0}, precondition:{1}, sizeCondition:{2}, callbackShouldBeCalled:{3}") name = "action:{0}, precondition:{1}, sizeCondition:{2}, callbackShouldBeCalled:{3}")
public static List<Object[]> params() { public static List<Object[]> params() {
return Arrays.asList( return ImmutableList.of(
// Major use cases // Major use cases
new Object[] {SET_VIDEO_SURFACE, SET_VIDEO_SURFACE, SIZE_SAME, false}, new Object[] {SET_VIDEO_SURFACE, SET_VIDEO_SURFACE, SIZE_SAME, false},
new Object[] {SET_VIDEO_SURFACE, SET_VIDEO_SURFACE, SIZE_DIFFERENT, false}, new Object[] {SET_VIDEO_SURFACE, SET_VIDEO_SURFACE, SIZE_DIFFERENT, false},
new Object[] {SET_VIDEO_SURFACE, NO_PRECONDITION, SIZE_NOT_APPLICABLE, true}, new Object[] {SET_VIDEO_SURFACE, NO_PRECONDITION, SIZE_NOT_APPLICABLE, false},
new Object[] {SET_VIDEO_SURFACE_HOLDER, SET_VIDEO_SURFACE_HOLDER, SIZE_SAME, false}, new Object[] {SET_VIDEO_SURFACE_HOLDER, SET_VIDEO_SURFACE_HOLDER, SIZE_SAME, false},
new Object[] {SET_VIDEO_SURFACE_HOLDER, SET_VIDEO_SURFACE_HOLDER, SIZE_DIFFERENT, true}, new Object[] {SET_VIDEO_SURFACE_HOLDER, SET_VIDEO_SURFACE_HOLDER, SIZE_DIFFERENT, true},
new Object[] {SET_VIDEO_SURFACE_HOLDER, NO_PRECONDITION, SIZE_NOT_APPLICABLE, true}, new Object[] {SET_VIDEO_SURFACE_HOLDER, NO_PRECONDITION, SIZE_NOT_APPLICABLE, true},
@ -125,15 +122,10 @@ public class MediaControllerSurfaceSizeChangeTest {
new Object[] {SET_VIDEO_TEXTURE_VIEW, SET_VIDEO_SURFACE_HOLDER, SIZE_DIFFERENT, true}, new Object[] {SET_VIDEO_TEXTURE_VIEW, SET_VIDEO_SURFACE_HOLDER, SIZE_DIFFERENT, true},
new Object[] {SET_VIDEO_TEXTURE_VIEW, SET_VIDEO_SURFACE_VIEW, SIZE_SAME, false}, new Object[] {SET_VIDEO_TEXTURE_VIEW, SET_VIDEO_SURFACE_VIEW, SIZE_SAME, false},
new Object[] {SET_VIDEO_TEXTURE_VIEW, SET_VIDEO_SURFACE_VIEW, SIZE_DIFFERENT, true}, new Object[] {SET_VIDEO_TEXTURE_VIEW, SET_VIDEO_SURFACE_VIEW, SIZE_DIFFERENT, true},
new Object[] {CLEAR_VIDEO_SURFACE, NO_PRECONDITION, SIZE_NOT_APPLICABLE, false}); new Object[] {CLEAR_VIDEO_SURFACE, NO_PRECONDITION, SIZE_NOT_APPLICABLE, true});
} }
private final HandlerThreadTestRule threadTestRule = new HandlerThreadTestRule(TAG); @Rule public final MainLooperTestRule mainLooperTestRule = new MainLooperTestRule();
private final MediaControllerTestRule controllerTestRule =
new MediaControllerTestRule(threadTestRule);
@Rule
public final TestRule chain = RuleChain.outerRule(threadTestRule).around(controllerTestRule);
private final String action; private final String action;
private final String precondition; private final String precondition;
@ -150,7 +142,6 @@ public class MediaControllerSurfaceSizeChangeTest {
private SurfaceActivity activity; private SurfaceActivity activity;
private RemoteMediaSession remoteSession; private RemoteMediaSession remoteSession;
private MediaController controller;
@Nullable private View viewForPrecondition; @Nullable private View viewForPrecondition;
@Nullable private View viewForAction; @Nullable private View viewForAction;
@ -163,7 +154,6 @@ public class MediaControllerSurfaceSizeChangeTest {
remoteSession = remoteSession =
new RemoteMediaSession( new RemoteMediaSession(
DEFAULT_TEST_NAME, ApplicationProvider.getApplicationContext(), null); DEFAULT_TEST_NAME, ApplicationProvider.getApplicationContext(), null);
controller = controllerTestRule.createController(remoteSession.getToken());
} }
@After @After
@ -189,49 +179,49 @@ public class MediaControllerSurfaceSizeChangeTest {
@Test @Test
public void test() throws Throwable { public void test() throws Throwable {
setPrecondition(); MediaController controller =
new MediaController.Builder(activity, remoteSession.getToken())
.setApplicationLooper(Looper.getMainLooper())
.buildAsync()
.get();
setPrecondition(controller);
setViewForAction(); setViewForAction();
matchSizeOfViewForAction(); matchSizeOfViewForAction();
setExpectedWidthAndHeightFromCallback(); setExpectedWidthAndHeightFromCallback();
doAction(); doAction(controller);
waitCallbackAndAssert(); waitCallbackAndAssert();
} }
private void setPrecondition() throws Exception { private void setPrecondition(MediaController controller) throws Exception {
switch (precondition) { switch (precondition) {
case SET_VIDEO_SURFACE: case SET_VIDEO_SURFACE:
{ {
Surface testSurface = activity.getFirstSurfaceHolder().getSurface(); Surface testSurface = activity.getFirstSurfaceHolder().getSurface();
threadTestRule.getHandler().postAndSync(() -> controller.setVideoSurface(testSurface)); MainLooperTestRule.runOnMainSync(() -> controller.setVideoSurface(testSurface));
viewForPrecondition = activity.getFirstSurfaceView(); viewForPrecondition = activity.getFirstSurfaceView();
break; break;
} }
case SET_VIDEO_SURFACE_HOLDER: case SET_VIDEO_SURFACE_HOLDER:
{ {
SurfaceHolder testSurfaceHolder = activity.getFirstSurfaceHolder(); SurfaceHolder testSurfaceHolder = activity.getFirstSurfaceHolder();
threadTestRule MainLooperTestRule.runOnMainSync(
.getHandler() () -> controller.setVideoSurfaceHolder(testSurfaceHolder));
.postAndSync(() -> controller.setVideoSurfaceHolder(testSurfaceHolder));
viewForPrecondition = activity.getFirstSurfaceView(); viewForPrecondition = activity.getFirstSurfaceView();
break; break;
} }
case SET_VIDEO_SURFACE_VIEW: case SET_VIDEO_SURFACE_VIEW:
{ {
SurfaceView testSurfaceView = activity.getFirstSurfaceView(); SurfaceView testSurfaceView = activity.getFirstSurfaceView();
threadTestRule MainLooperTestRule.runOnMainSync(() -> controller.setVideoSurfaceView(testSurfaceView));
.getHandler()
.postAndSync(() -> controller.setVideoSurfaceView(testSurfaceView));
viewForPrecondition = activity.getFirstSurfaceView(); viewForPrecondition = activity.getFirstSurfaceView();
break; break;
} }
case SET_VIDEO_TEXTURE_VIEW: case SET_VIDEO_TEXTURE_VIEW:
{ {
TextureView testTextureView = activity.getFirstTextureView(); TextureView testTextureView = activity.getFirstTextureView();
threadTestRule MainLooperTestRule.runOnMainSync(() -> controller.setVideoTextureView(testTextureView));
.getHandler()
.postAndSync(() -> controller.setVideoTextureView(testTextureView));
viewForPrecondition = activity.getFirstTextureView(); viewForPrecondition = activity.getFirstTextureView();
break; break;
} }
@ -241,9 +231,7 @@ public class MediaControllerSurfaceSizeChangeTest {
default: default:
throw new AssertionError(precondition + " is not an allowed precondition."); throw new AssertionError(precondition + " is not an allowed precondition.");
} }
threadTestRule MainLooperTestRule.runOnMainSync(
.getHandler()
.postAndSync(
() -> () ->
controller.addListener( controller.addListener(
new Player.Listener() { new Player.Listener() {
@ -321,43 +309,38 @@ public class MediaControllerSurfaceSizeChangeTest {
} }
} }
private void doAction() throws Exception { private void doAction(MediaController controller) throws Exception {
switch (action) { switch (action) {
case SET_VIDEO_SURFACE: case SET_VIDEO_SURFACE:
{ {
SurfaceView testSurfaceView = (SurfaceView) viewForAction; SurfaceView testSurfaceView = (SurfaceView) viewForAction;
Surface surface = testSurfaceView.getHolder().getSurface(); Surface surface = testSurfaceView.getHolder().getSurface();
threadTestRule.getHandler().postAndSync(() -> controller.setVideoSurface(surface)); MainLooperTestRule.runOnMainSync(() -> controller.setVideoSurface(surface));
break; break;
} }
case SET_VIDEO_SURFACE_HOLDER: case SET_VIDEO_SURFACE_HOLDER:
{ {
SurfaceView testSurfaceView = (SurfaceView) viewForAction; SurfaceView testSurfaceView = (SurfaceView) viewForAction;
SurfaceHolder testSurfaceHolder = testSurfaceView.getHolder(); SurfaceHolder testSurfaceHolder = testSurfaceView.getHolder();
threadTestRule MainLooperTestRule.runOnMainSync(
.getHandler() () -> controller.setVideoSurfaceHolder(testSurfaceHolder));
.postAndSync(() -> controller.setVideoSurfaceHolder(testSurfaceHolder));
break; break;
} }
case SET_VIDEO_SURFACE_VIEW: case SET_VIDEO_SURFACE_VIEW:
{ {
SurfaceView testSurfaceView = (SurfaceView) viewForAction; SurfaceView testSurfaceView = (SurfaceView) viewForAction;
threadTestRule MainLooperTestRule.runOnMainSync(() -> controller.setVideoSurfaceView(testSurfaceView));
.getHandler()
.postAndSync(() -> controller.setVideoSurfaceView(testSurfaceView));
break; break;
} }
case SET_VIDEO_TEXTURE_VIEW: case SET_VIDEO_TEXTURE_VIEW:
{ {
TextureView testTextureView = (TextureView) viewForAction; TextureView testTextureView = (TextureView) viewForAction;
threadTestRule MainLooperTestRule.runOnMainSync(() -> controller.setVideoTextureView(testTextureView));
.getHandler()
.postAndSync(() -> controller.setVideoTextureView(testTextureView));
break; break;
} }
case CLEAR_VIDEO_SURFACE: case CLEAR_VIDEO_SURFACE:
{ {
threadTestRule.getHandler().postAndSync(() -> controller.clearVideoSurface()); MainLooperTestRule.runOnMainSync(controller::clearVideoSurface);
break; break;
} }
default: default: