Merge "Clear routes when media router manager has no callback" into rvc-dev am: 1bb914e8ec

Change-Id: I25f8e242156911e3cd33181d7be63b0eeb17d11f
This commit is contained in:
Kyunglyul Hyun
2020-04-21 11:15:37 +00:00
committed by Automerger Merge Worker
4 changed files with 62 additions and 12 deletions

View File

@@ -237,9 +237,9 @@ public final class MediaRouter2 {
} catch (RemoteException ex) { } catch (RemoteException ex) {
Log.e(TAG, "Unable to unregister media router.", ex); Log.e(TAG, "Unable to unregister media router.", ex);
} }
mStub = null;
} }
mShouldUpdateRoutes = true; mShouldUpdateRoutes = true;
mStub = null;
} }
} }

View File

@@ -147,14 +147,16 @@ public final class MediaRouter2Manager {
} }
synchronized (sLock) { synchronized (sLock) {
if (mCallbackRecords.size() == 0 && mClient != null) { if (mCallbackRecords.size() == 0) {
try { if (mClient != null) {
mMediaRouterService.unregisterManager(mClient); try {
} catch (RemoteException ex) { mMediaRouterService.unregisterManager(mClient);
Log.e(TAG, "Unable to unregister media router manager", ex); } catch (RemoteException ex) {
Log.e(TAG, "Unable to unregister media router manager", ex);
}
mClient = null;
} }
//TODO: clear mRoutes? mRoutes.clear();
mClient = null;
mPreferredFeaturesMap.clear(); mPreferredFeaturesMap.clear();
} }
} }

View File

@@ -36,6 +36,7 @@ import static com.android.mediaroutertest.StubMediaRoute2ProviderService.VOLUME_
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import android.content.Context; import android.content.Context;
@@ -160,6 +161,7 @@ public class MediaRouter2ManagerTest {
}); });
MediaRoute2Info routeToRemove = routes.get(ROUTE_ID2); MediaRoute2Info routeToRemove = routes.get(ROUTE_ID2);
assertNotNull(routeToRemove);
StubMediaRoute2ProviderService sInstance = StubMediaRoute2ProviderService sInstance =
StubMediaRoute2ProviderService.getInstance(); StubMediaRoute2ProviderService.getInstance();
@@ -171,6 +173,52 @@ public class MediaRouter2ManagerTest {
assertTrue(addedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); assertTrue(addedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
} }
@Test
public void testGetRoutes_removedRoute_returnsCorrectRoutes() throws Exception {
CountDownLatch addedLatch = new CountDownLatch(1);
CountDownLatch removedLatch = new CountDownLatch(1);
RouteCallback routeCallback = new RouteCallback() {
// Used to ensure the removed route is added.
@Override
public void onRoutesAdded(List<MediaRoute2Info> routes) {
if (removedLatch.getCount() > 0) {
return;
}
addedLatch.countDown();
}
@Override
public void onRoutesRemoved(List<MediaRoute2Info> routes) {
removedLatch.countDown();
}
};
mRouter2.registerRouteCallback(mExecutor, routeCallback,
new RouteDiscoveryPreference.Builder(FEATURES_ALL, true).build());
mRouteCallbacks.add(routeCallback);
Map<String, MediaRoute2Info> routes = waitAndGetRoutesWithManager(FEATURES_ALL);
MediaRoute2Info routeToRemove = routes.get(ROUTE_ID2);
assertNotNull(routeToRemove);
StubMediaRoute2ProviderService sInstance =
StubMediaRoute2ProviderService.getInstance();
assertNotNull(sInstance);
sInstance.removeRoute(ROUTE_ID2);
// Wait until the route is removed.
assertTrue(removedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
Map<String, MediaRoute2Info> newRoutes = waitAndGetRoutesWithManager(FEATURES_ALL);
assertNull(newRoutes.get(ROUTE_ID2));
// Revert the removal.
sInstance.addRoute(routeToRemove);
assertTrue(addedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
mRouter2.unregisterRouteCallback(routeCallback);
}
/** /**
* Tests if we get proper routes for application that has special route feature. * Tests if we get proper routes for application that has special route feature.
*/ */
@@ -475,8 +523,8 @@ public class MediaRouter2ManagerTest {
MediaRouter2Manager.Callback managerCallback = new MediaRouter2Manager.Callback() { MediaRouter2Manager.Callback managerCallback = new MediaRouter2Manager.Callback() {
@Override @Override
public void onRoutesAdded(List<MediaRoute2Info> routes) { public void onRoutesAdded(List<MediaRoute2Info> routes) {
for (int i = 0; i < routes.size(); i++) { for (MediaRoute2Info route : routes) {
if (!routes.get(i).isSystemRoute()) { if (!route.isSystemRoute()) {
addedLatch.countDown(); addedLatch.countDown();
break; break;
} }

View File

@@ -65,9 +65,9 @@ public class StubMediaRoute2ProviderService extends MediaRoute2ProviderService {
public static final String ROUTE_NAME_VARIABLE_VOLUME = "Variable Volume Route"; public static final String ROUTE_NAME_VARIABLE_VOLUME = "Variable Volume Route";
public static final String FEATURE_SAMPLE = public static final String FEATURE_SAMPLE =
"com.android.mediarouteprovider.FEATURE_SAMPLE"; "com.android.mediaroutertest.FEATURE_SAMPLE";
public static final String FEATURE_SPECIAL = public static final String FEATURE_SPECIAL =
"com.android.mediarouteprovider.FEATURE_SPECIAL"; "com.android.mediaroutertest..FEATURE_SPECIAL";
Map<String, MediaRoute2Info> mRoutes = new HashMap<>(); Map<String, MediaRoute2Info> mRoutes = new HashMap<>();
Map<String, String> mRouteIdToSessionId = new HashMap<>(); Map<String, String> mRouteIdToSessionId = new HashMap<>();