aboutsummaryrefslogtreecommitdiff
path: root/mediarouter
diff options
context:
space:
mode:
authorKyunglyul Hyun <klhyun@google.com>2018-10-29 10:59:38 +0900
committerKyunglyul Hyun <klhyun@google.com>2018-10-29 11:26:07 +0900
commit4fc40b0207cc5990b40af7b86e0e47b149f3281a (patch)
tree1ed117720492727526e88c16373ea84566677bf2 /mediarouter
parentf5cb1252f2cbee6aefaade3ffa4ffc72da2bb8ec (diff)
downloadsupport-4fc40b0207cc5990b40af7b86e0e47b149f3281a.tar.gz
MediaRouter: fix no default route
Fix IllegalStateException such that there is no default route. It happend when invalid route is provided with SystemMeidaRouteProvider A test is added to check that Bug: 63630303 Test: Ran MediaRouterInitializationTest Change-Id: Ib0f12a2c666cb079a8038232090fbf7e90d84921
Diffstat (limited to 'mediarouter')
-rw-r--r--mediarouter/src/androidTest/java/androidx/mediarouter/media/MediaRouterInitializationTest.java63
-rw-r--r--mediarouter/src/main/java/androidx/mediarouter/media/MediaRouter.java9
2 files changed, 71 insertions, 1 deletions
diff --git a/mediarouter/src/androidTest/java/androidx/mediarouter/media/MediaRouterInitializationTest.java b/mediarouter/src/androidTest/java/androidx/mediarouter/media/MediaRouterInitializationTest.java
new file mode 100644
index 00000000000..f3426b59ee7
--- /dev/null
+++ b/mediarouter/src/androidTest/java/androidx/mediarouter/media/MediaRouterInitializationTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.mediarouter.media;
+
+import static androidx.test.InstrumentationRegistry.getContext;
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import static org.junit.Assert.assertTrue;
+
+import android.content.Context;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Test proper initialization of {@link MediaRouter}.
+ */
+@RunWith(AndroidJUnit4.class)
+public class MediaRouterInitializationTest {
+ /**
+ * This test checks weather MediaRouter is initialized well if an empty route exists
+ */
+ @Test
+ @SmallTest
+ public void testEmptyUserRoute() throws Exception {
+ getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ final Context context = getContext();
+ android.media.MediaRouter router =
+ (android.media.MediaRouter) context.getSystemService(
+ Context.MEDIA_ROUTER_SERVICE);
+
+ // Add empty user route
+ android.media.MediaRouter.RouteCategory category =
+ router.createRouteCategory("", false);
+ android.media.MediaRouter.UserRouteInfo routeInfo =
+ router.createUserRoute(category);
+ router.addUserRoute(routeInfo);
+
+ MediaRouter mediaRouter = MediaRouter.getInstance(context);
+ assertTrue(mediaRouter.getDefaultRoute() != null);
+ }
+ });
+ }
+}
diff --git a/mediarouter/src/main/java/androidx/mediarouter/media/MediaRouter.java b/mediarouter/src/main/java/androidx/mediarouter/media/MediaRouter.java
index 007d6ddc3ce..373fefddd3a 100644
--- a/mediarouter/src/main/java/androidx/mediarouter/media/MediaRouter.java
+++ b/mediarouter/src/main/java/androidx/mediarouter/media/MediaRouter.java
@@ -2468,13 +2468,20 @@ public final class MediaRouter {
// the order of their descriptors.
int targetIndex = 0;
boolean selectedRouteDescriptorChanged = false;
- if (providerDescriptor != null && providerDescriptor.isValid()) {
+ if (providerDescriptor != null && (providerDescriptor.isValid()
+ || providerDescriptor == mSystemProvider.getDescriptor())) {
final List<MediaRouteDescriptor> routeDescriptors = providerDescriptor.getRoutes();
// Updating route group's contents requires all member routes' information.
// Add the groups to the lists and update them later.
List<Pair<RouteInfo, MediaRouteDescriptor>> addedGroups = new ArrayList<>();
List<Pair<RouteInfo, MediaRouteDescriptor>> updatedGroups = new ArrayList<>();
for (MediaRouteDescriptor routeDescriptor : routeDescriptors) {
+ // SystemMediaRouteProvider may have invalid routes
+ if (routeDescriptor == null || !routeDescriptor.isValid()) {
+ Log.w(TAG, "Ignoring invalid system route descriptor: "
+ + routeDescriptor);
+ continue;
+ }
final String id = routeDescriptor.getId();
final int sourceIndex = provider.findRouteIndexByDescriptorId(id);
boolean isGroup = !(routeDescriptor.getGroupMemberIds().isEmpty());