Use new media router2 api to get info media device list
1. Use new media router2 api to get info device list
2. Use new media router2 api to tranfer media to info device
Bug: 144535188
Test: make -j50 RunSettingsLibRoboTests
Change-Id: I682dc53f659180cdef434b2063668ee0fcd1f9c3
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaDeviceTest.java
new file mode 100644
index 0000000..c9db0d1
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaDeviceTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2019 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 com.android.settingslib.media;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.media.MediaRoute2Info;
+import android.media.MediaRouter2Manager;
+
+import com.android.settingslib.R;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class InfoMediaDeviceTest {
+
+ private static final String TEST_PACKAGE_NAME = "com.test.packagename";
+ private static final String TEST_ID = "test_id";
+ private static final String TEST_NAME = "test_name";
+
+ @Mock
+ private MediaRouter2Manager mRouterManager;
+ @Mock
+ private MediaRoute2Info mRouteInfo;
+
+
+ private Context mContext;
+ private InfoMediaDevice mInfoMediaDevice;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
+
+ mInfoMediaDevice = new InfoMediaDevice(mContext, mRouterManager, mRouteInfo,
+ TEST_PACKAGE_NAME);
+ }
+
+ @Test
+ public void getName_shouldReturnName() {
+ when(mRouteInfo.getName()).thenReturn(TEST_NAME);
+
+ assertThat(mInfoMediaDevice.getName()).isEqualTo(TEST_NAME);
+ }
+
+ @Test
+ public void getSummary_clientPackageNameIsNull_returnNull() {
+ when(mRouteInfo.getClientPackageName()).thenReturn(null);
+
+ assertThat(mInfoMediaDevice.getSummary()).isEqualTo(null);
+ }
+
+ @Test
+ public void getSummary_clientPackageNameIsNotNull_returnActive() {
+ when(mRouteInfo.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
+
+ assertThat(mInfoMediaDevice.getSummary())
+ .isEqualTo(mContext.getString(R.string.bluetooth_active_no_battery_level));
+ }
+
+ @Test
+ public void getId_shouldReturnId() {
+ when(mRouteInfo.getId()).thenReturn(TEST_ID);
+
+ assertThat(mInfoMediaDevice.getId()).isEqualTo(TEST_ID);
+ }
+
+ @Test
+ public void connect_shouldSelectRoute() {
+ mInfoMediaDevice.connect();
+
+ verify(mRouterManager).selectRoute(TEST_PACKAGE_NAME, mRouteInfo);
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java
index b11cf69..67f6dd90 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java
@@ -23,9 +23,8 @@
import static org.mockito.Mockito.when;
import android.content.Context;
-
-import androidx.mediarouter.media.MediaRouteSelector;
-import androidx.mediarouter.media.MediaRouter;
+import android.media.MediaRoute2Info;
+import android.media.MediaRouter2Manager;
import org.junit.Before;
import org.junit.Test;
@@ -35,6 +34,9 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
+import java.util.ArrayList;
+import java.util.List;
+
@RunWith(RobolectricTestRunner.class)
public class InfoMediaManagerTest {
@@ -42,9 +44,7 @@
private static final String TEST_ID = "test_id";
@Mock
- private MediaRouter mMediaRouter;
- @Mock
- private MediaRouteSelector mSelector;
+ private MediaRouter2Manager mRouterManager;
private InfoMediaManager mInfoMediaManager;
private Context mContext;
@@ -55,82 +55,70 @@
mContext = RuntimeEnvironment.application;
mInfoMediaManager = new InfoMediaManager(mContext, TEST_PACKAGE_NAME, null);
- mInfoMediaManager.mMediaRouter = mMediaRouter;
- mInfoMediaManager.mSelector = mSelector;
+ mInfoMediaManager.mRouterManager = mRouterManager;
}
@Test
public void stopScan_shouldRemoveCallback() {
mInfoMediaManager.stopScan();
- verify(mMediaRouter).removeCallback(mInfoMediaManager.mMediaRouterCallback);
+ verify(mRouterManager).unregisterCallback(mInfoMediaManager.mMediaRouterCallback);
}
@Test
public void startScan_shouldAddCallback() {
mInfoMediaManager.startScan();
- verify(mMediaRouter).addCallback(mSelector, mInfoMediaManager.mMediaRouterCallback,
- MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
+ verify(mRouterManager).registerCallback(mInfoMediaManager.mExecutor,
+ mInfoMediaManager.mMediaRouterCallback);
}
@Test
- public void onRouteAdded_mediaDeviceNotExistInList_addMediaDevice() {
- final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class);
+ public void onRouteAdded_shouldAddMediaDevice() {
+ final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
+ when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
+
+ final List<MediaRoute2Info> routes = new ArrayList<>();
+ routes.add(info);
+ when(mRouterManager.getAvailableRoutes(TEST_PACKAGE_NAME)).thenReturn(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
- mInfoMediaManager.mMediaRouterCallback.onRouteAdded(mMediaRouter, info);
+ mInfoMediaManager.mMediaRouterCallback.onRoutesAdded(routes);
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
+ assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
+ assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
}
@Test
- public void onRouteAdded_mediaDeviceExistInList_doNothing() {
- final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class);
+ public void onControlCategoriesChanged_samePackageName_shouldAddMediaDevice() {
+ final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
- final InfoMediaDevice infoDevice = new InfoMediaDevice(mContext, info);
- mInfoMediaManager.mMediaDevices.add(infoDevice);
+ when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
+
+ final List<MediaRoute2Info> routes = new ArrayList<>();
+ routes.add(info);
+ when(mRouterManager.getAvailableRoutes(TEST_PACKAGE_NAME)).thenReturn(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
- final int size = mInfoMediaManager.mMediaDevices.size();
- assertThat(mediaDevice).isNotNull();
-
- mInfoMediaManager.mMediaRouterCallback.onRouteAdded(mMediaRouter, info);
-
- assertThat(mInfoMediaManager.mMediaDevices).hasSize(size);
- }
-
- @Test
- public void onRouteRemoved_mediaDeviceExistInList_removeMediaDevice() {
- final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class);
- when(info.getId()).thenReturn(TEST_ID);
- final InfoMediaDevice infoDevice = new InfoMediaDevice(mContext, info);
- mInfoMediaManager.mMediaDevices.add(infoDevice);
-
- final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
- assertThat(mediaDevice).isNotNull();
- assertThat(mInfoMediaManager.mMediaDevices).hasSize(1);
-
- mInfoMediaManager.mMediaRouterCallback.onRouteRemoved(mMediaRouter, info);
-
- assertThat(mInfoMediaManager.mMediaDevices).isEmpty();
- }
-
- @Test
- public void onRouteRemoved_mediaDeviceNotExistInList_doNothing() {
- final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class);
- when(info.getId()).thenReturn(TEST_ID);
-
- final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
- final int size = mInfoMediaManager.mMediaDevices.size();
assertThat(mediaDevice).isNull();
- mInfoMediaManager.mMediaRouterCallback.onRouteRemoved(mMediaRouter, info);
+ mInfoMediaManager.mMediaRouterCallback.onControlCategoriesChanged(TEST_PACKAGE_NAME, null);
- assertThat(mInfoMediaManager.mMediaDevices).hasSize(size);
+ final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
+ assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
+ assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
+ assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
+ }
+
+ @Test
+ public void onControlCategoriesChanged_differentPackageName_doNothing() {
+ mInfoMediaManager.mMediaRouterCallback.onControlCategoriesChanged("com.fake.play", null);
+
+ assertThat(mInfoMediaManager.mMediaDevices).hasSize(0);
}
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java
index 23d2c74..02cb83e 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java
@@ -22,8 +22,8 @@
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
-
-import androidx.mediarouter.media.MediaRouter;
+import android.media.MediaRoute2Info;
+import android.media.MediaRouter2Manager;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -56,6 +56,7 @@
private static final String ROUTER_ID_1 = "RouterId_1";
private static final String ROUTER_ID_2 = "RouterId_2";
private static final String ROUTER_ID_3 = "RouterId_3";
+ private static final String TEST_PACKAGE_NAME = "com.test.playmusic";
private final BluetoothClass mHeadreeClass =
new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES);
private final BluetoothClass mCarkitClass =
@@ -76,11 +77,11 @@
@Mock
private LocalBluetoothManager mLocalBluetoothManager;
@Mock
- private MediaRouter.RouteInfo mRouteInfo1;
+ private MediaRoute2Info mRouteInfo1;
@Mock
- private MediaRouter.RouteInfo mRouteInfo2;
+ private MediaRoute2Info mRouteInfo2;
@Mock
- private MediaRouter.RouteInfo mRouteInfo3;
+ private MediaRoute2Info mRouteInfo3;
@Mock
private LocalBluetoothProfileManager mProfileManager;
@Mock
@@ -99,6 +100,7 @@
private InfoMediaDevice mInfoMediaDevice3;
private List<MediaDevice> mMediaDevices = new ArrayList<>();
private PhoneMediaDevice mPhoneMediaDevice;
+ private MediaRouter2Manager mMediaRouter2Manager;
@Before
public void setUp() {
@@ -134,9 +136,13 @@
mBluetoothMediaDevice1 = new BluetoothMediaDevice(mContext, mCachedDevice1);
mBluetoothMediaDevice2 = new BluetoothMediaDevice(mContext, mCachedDevice2);
mBluetoothMediaDevice3 = new BluetoothMediaDevice(mContext, mCachedDevice3);
- mInfoMediaDevice1 = new InfoMediaDevice(mContext, mRouteInfo1);
- mInfoMediaDevice2 = new InfoMediaDevice(mContext, mRouteInfo2);
- mInfoMediaDevice3 = new InfoMediaDevice(mContext, mRouteInfo3);
+ mMediaRouter2Manager = MediaRouter2Manager.getInstance(mContext);
+ mInfoMediaDevice1 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo1,
+ TEST_PACKAGE_NAME);
+ mInfoMediaDevice2 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo2,
+ TEST_PACKAGE_NAME);
+ mInfoMediaDevice3 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo3,
+ TEST_PACKAGE_NAME);
mPhoneMediaDevice = new PhoneMediaDevice(mContext, mLocalBluetoothManager);
}
@@ -364,5 +370,4 @@
assertThat(mMediaDevices.get(5)).isEqualTo(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(6)).isEqualTo(mBluetoothMediaDevice2);
}
-
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java
index 1e5545f..30a6ad2 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java
@@ -21,8 +21,7 @@
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothDevice;
-
-import androidx.mediarouter.media.MediaRouter;
+import android.media.MediaRoute2Info;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -44,7 +43,7 @@
@Mock
private BluetoothDevice mBluetoothDevice;
@Mock
- private MediaRouter.RouteInfo mRouteInfo;
+ private MediaRoute2Info mRouteInfo;
@Before
public void setUp() {