Commit 3b185fd8 authored by Zhiqiang Zhang's avatar Zhiqiang Zhang Committed by Commit Bot

Unit tests for CafBaseMediaRouteProvider

This CL adds unit tests for CafBaseMediaRouteProvider. Some methods are
exposed for easier testing.

Also, the CL includes several minor fixes to CafBaseMRP.

Bug: 711860
Change-Id: I1675e122406a787ccdb25a32c337c50f60d71872
Reviewed-on: https://chromium-review.googlesource.com/c/1333117
Commit-Queue: Zhiqiang Zhang <zqzhang@chromium.org>
Reviewed-by: default avatarThomas Guilbert <tguilbert@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607811}
parent 7f687518
...@@ -129,7 +129,7 @@ public abstract class CafBaseMediaRouteProvider ...@@ -129,7 +129,7 @@ public abstract class CafBaseMediaRouteProvider
MediaSource source = getSourceFromId(sourceId); MediaSource source = getSourceFromId(sourceId);
if (source == null) return; if (source == null) return;
// No-op, if already monitoring the application for this source. // No-op, if not monitoring the application for this source.
String applicationId = source.getApplicationId(); String applicationId = source.getApplicationId();
DiscoveryCallback callback = mDiscoveryCallbacks.get(applicationId); DiscoveryCallback callback = mDiscoveryCallbacks.get(applicationId);
if (callback == null) return; if (callback == null) return;
...@@ -154,12 +154,9 @@ public abstract class CafBaseMediaRouteProvider ...@@ -154,12 +154,9 @@ public abstract class CafBaseMediaRouteProvider
handleSessionEnd(); handleSessionEnd();
} }
if (mPendingCreateRouteRequestInfo != null) { if (mPendingCreateRouteRequestInfo != null) {
cancelPendingRequest("Request repaced"); cancelPendingRequest("Request replaced");
} }
CastUtils.getCastContext().getSessionManager().addSessionManagerListener(
this, CastSession.class);
MediaSink sink = MediaSink.fromSinkId(sinkId, mAndroidMediaRouter); MediaSink sink = MediaSink.fromSinkId(sinkId, mAndroidMediaRouter);
if (sink == null) { if (sink == null) {
mManager.onRouteRequestError("No sink", nativeRequestId); mManager.onRouteRequestError("No sink", nativeRequestId);
...@@ -183,6 +180,9 @@ public abstract class CafBaseMediaRouteProvider ...@@ -183,6 +180,9 @@ public abstract class CafBaseMediaRouteProvider
mManager.onRouteRequestError("The sink does not exist", nativeRequestId); mManager.onRouteRequestError("The sink does not exist", nativeRequestId);
} }
CastUtils.getCastContext().getSessionManager().addSessionManagerListener(
this, CastSession.class);
mPendingCreateRouteRequestInfo = new CreateRouteRequestInfo(source, sink, presentationId, mPendingCreateRouteRequestInfo = new CreateRouteRequestInfo(source, sink, presentationId,
origin, tabId, isIncognito, nativeRequestId, targetRouteInfo); origin, tabId, isIncognito, nativeRequestId, targetRouteInfo);
...@@ -306,8 +306,7 @@ public abstract class CafBaseMediaRouteProvider ...@@ -306,8 +306,7 @@ public abstract class CafBaseMediaRouteProvider
protected void addRoute( protected void addRoute(
MediaRoute route, String origin, int tabId, int nativeRequestId, boolean wasLaunched) { MediaRoute route, String origin, int tabId, int nativeRequestId, boolean wasLaunched) {
mRoutes.put(route.id, route); mRoutes.put(route.id, route);
mManager.onRouteCreated(route.id, route.sinkId, mManager.onRouteCreated(route.id, route.sinkId, nativeRequestId, this, wasLaunched);
sessionController().getRouteCreationInfo().nativeRequestId, this, wasLaunched);
} }
/** /**
...@@ -316,7 +315,7 @@ public abstract class CafBaseMediaRouteProvider ...@@ -316,7 +315,7 @@ public abstract class CafBaseMediaRouteProvider
* *
* @param error the reason for the route close, {@code null} indicates no error. * @param error the reason for the route close, {@code null} indicates no error.
*/ */
protected final void removeRoute(String routeId, @Nullable String error) { protected void removeRoute(String routeId, @Nullable String error) {
removeRouteFromRecord(routeId); removeRouteFromRecord(routeId);
mManager.onRouteClosed(routeId, error); mManager.onRouteClosed(routeId, error);
} }
...@@ -327,7 +326,7 @@ public abstract class CafBaseMediaRouteProvider ...@@ -327,7 +326,7 @@ public abstract class CafBaseMediaRouteProvider
* *
* @param error the reason for the route close, {@code null} indicates no error. * @param error the reason for the route close, {@code null} indicates no error.
*/ */
protected final void removeAllRoutes(@Nullable String error) { protected void removeAllRoutes(@Nullable String error) {
Set<String> routeIds = new HashSet<>(mRoutes.keySet()); Set<String> routeIds = new HashSet<>(mRoutes.keySet());
for (String routeId : routeIds) { for (String routeId : routeIds) {
removeRoute(routeId, error); removeRoute(routeId, error);
...@@ -338,7 +337,7 @@ public abstract class CafBaseMediaRouteProvider ...@@ -338,7 +337,7 @@ public abstract class CafBaseMediaRouteProvider
* Removes all routes for bookkeeping. This should be called whenever the receiver app is * Removes all routes for bookkeeping. This should be called whenever the receiver app is
* terminated. * terminated.
*/ */
protected final void terminateAllRoutes() { protected void terminateAllRoutes() {
Set<String> routeIds = new HashSet<>(mRoutes.keySet()); Set<String> routeIds = new HashSet<>(mRoutes.keySet());
for (String routeId : routeIds) { for (String routeId : routeIds) {
removeRouteFromRecord(routeId); removeRouteFromRecord(routeId);
......
...@@ -2360,6 +2360,7 @@ chrome_junit_test_java_sources = [ ...@@ -2360,6 +2360,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/media/router/caf/ShadowCastContext.java", "junit/src/org/chromium/chrome/browser/media/router/caf/ShadowCastContext.java",
"junit/src/org/chromium/chrome/browser/media/router/caf/BaseSessionControllerTest.java", "junit/src/org/chromium/chrome/browser/media/router/caf/BaseSessionControllerTest.java",
"junit/src/org/chromium/chrome/browser/media/router/caf/CastSessionControllerTest.java", "junit/src/org/chromium/chrome/browser/media/router/caf/CastSessionControllerTest.java",
"junit/src/org/chromium/chrome/browser/media/router/caf/CafBaseMediaRouteProviderTest.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaImageManagerTest.java", "junit/src/org/chromium/chrome/browser/media/ui/MediaImageManagerTest.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationActionsUpdatedTest.java", "junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationActionsUpdatedTest.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationButtonComputationTest.java", "junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationButtonComputationTest.java",
......
...@@ -15,6 +15,9 @@ import org.mockito.MockitoAnnotations; ...@@ -15,6 +15,9 @@ import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import java.util.ArrayList;
import java.util.List;
/** Test helper for MediaRouter-related functionalities. */ /** Test helper for MediaRouter-related functionalities. */
public class MediaRouterTestHelper { public class MediaRouterTestHelper {
@Mock @Mock
...@@ -40,6 +43,11 @@ public class MediaRouterTestHelper { ...@@ -40,6 +43,11 @@ public class MediaRouterTestHelper {
.when(mShadowMediaRouter) .when(mShadowMediaRouter)
.unselect(anyInt()); .unselect(anyInt());
doReturn(mDefaultRoute).when(mShadowMediaRouter).getDefaultRoute(); doReturn(mDefaultRoute).when(mShadowMediaRouter).getDefaultRoute();
List<MediaRouter.RouteInfo> allRoutes = new ArrayList<>();
allRoutes.add(mDefaultRoute);
allRoutes.add(mCastRoute);
allRoutes.add(mOtherCastRoute);
doReturn(allRoutes).when(mShadowMediaRouter).getRoutes();
} }
public ShadowMediaRouter.ShadowImplementation getShadowImpl() { public ShadowMediaRouter.ShadowImplementation getShadowImpl() {
......
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 to comment