hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settingslib.media; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.content.Context; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 26 | import android.media.MediaRoute2Info; |
| 27 | import android.media.MediaRouter2Manager; |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 28 | import android.media.RoutingSessionInfo; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 29 | |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame^] | 30 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
| 31 | |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.mockito.Mock; |
| 36 | import org.mockito.MockitoAnnotations; |
| 37 | import org.robolectric.RobolectricTestRunner; |
| 38 | import org.robolectric.RuntimeEnvironment; |
| 39 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 43 | @RunWith(RobolectricTestRunner.class) |
| 44 | public class InfoMediaManagerTest { |
| 45 | |
| 46 | private static final String TEST_PACKAGE_NAME = "com.test.packagename"; |
| 47 | private static final String TEST_ID = "test_id"; |
| 48 | |
| 49 | @Mock |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 50 | private MediaRouter2Manager mRouterManager; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame^] | 51 | @Mock |
| 52 | private LocalBluetoothManager mLocalBluetoothManager; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 53 | |
| 54 | private InfoMediaManager mInfoMediaManager; |
| 55 | private Context mContext; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | MockitoAnnotations.initMocks(this); |
| 60 | mContext = RuntimeEnvironment.application; |
| 61 | |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame^] | 62 | mInfoMediaManager = |
| 63 | new InfoMediaManager(mContext, TEST_PACKAGE_NAME, null, mLocalBluetoothManager); |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 64 | mInfoMediaManager.mRouterManager = mRouterManager; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void stopScan_shouldRemoveCallback() { |
| 69 | mInfoMediaManager.stopScan(); |
| 70 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 71 | verify(mRouterManager).unregisterCallback(mInfoMediaManager.mMediaRouterCallback); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void startScan_shouldAddCallback() { |
| 76 | mInfoMediaManager.startScan(); |
| 77 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 78 | verify(mRouterManager).registerCallback(mInfoMediaManager.mExecutor, |
| 79 | mInfoMediaManager.mMediaRouterCallback); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 83 | public void onRouteAdded_getAvailableRoutes_shouldAddMediaDevice() { |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 84 | final MediaRoute2Info info = mock(MediaRoute2Info.class); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 85 | when(info.getId()).thenReturn(TEST_ID); |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 86 | when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME); |
| 87 | |
| 88 | final List<MediaRoute2Info> routes = new ArrayList<>(); |
| 89 | routes.add(info); |
| 90 | when(mRouterManager.getAvailableRoutes(TEST_PACKAGE_NAME)).thenReturn(routes); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 91 | |
| 92 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 93 | assertThat(mediaDevice).isNull(); |
| 94 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 95 | mInfoMediaManager.mMediaRouterCallback.onRoutesAdded(routes); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 96 | |
| 97 | final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0); |
| 98 | assertThat(infoDevice.getId()).isEqualTo(TEST_ID); |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 99 | assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice); |
| 100 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size()); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 104 | public void onRouteAdded_buildAllRoutes_shouldAddMediaDevice() { |
| 105 | final MediaRoute2Info info = mock(MediaRoute2Info.class); |
| 106 | when(info.getId()).thenReturn(TEST_ID); |
| 107 | when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME); |
| 108 | |
| 109 | final List<MediaRoute2Info> routes = new ArrayList<>(); |
| 110 | routes.add(info); |
| 111 | when(mRouterManager.getAllRoutes()).thenReturn(routes); |
| 112 | |
| 113 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 114 | assertThat(mediaDevice).isNull(); |
| 115 | |
| 116 | mInfoMediaManager.mPackageName = ""; |
| 117 | mInfoMediaManager.mMediaRouterCallback.onRoutesAdded(routes); |
| 118 | |
| 119 | final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0); |
| 120 | assertThat(infoDevice.getId()).isEqualTo(TEST_ID); |
| 121 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size()); |
| 122 | } |
| 123 | |
| 124 | @Test |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 125 | public void onControlCategoriesChanged_samePackageName_shouldAddMediaDevice() { |
| 126 | final MediaRoute2Info info = mock(MediaRoute2Info.class); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 127 | when(info.getId()).thenReturn(TEST_ID); |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 128 | when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME); |
| 129 | |
| 130 | final List<MediaRoute2Info> routes = new ArrayList<>(); |
| 131 | routes.add(info); |
| 132 | when(mRouterManager.getAvailableRoutes(TEST_PACKAGE_NAME)).thenReturn(routes); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 133 | |
| 134 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 135 | assertThat(mediaDevice).isNull(); |
| 136 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 137 | mInfoMediaManager.mMediaRouterCallback.onControlCategoriesChanged(TEST_PACKAGE_NAME, null); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 138 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 139 | final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0); |
| 140 | assertThat(infoDevice.getId()).isEqualTo(TEST_ID); |
| 141 | assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice); |
| 142 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size()); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void onControlCategoriesChanged_differentPackageName_doNothing() { |
| 147 | mInfoMediaManager.mMediaRouterCallback.onControlCategoriesChanged("com.fake.play", null); |
| 148 | |
| 149 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(0); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 150 | } |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 151 | |
| 152 | @Test |
| 153 | public void onRoutesChanged_getAvailableRoutes_shouldAddMediaDevice() { |
| 154 | final MediaRoute2Info info = mock(MediaRoute2Info.class); |
| 155 | when(info.getId()).thenReturn(TEST_ID); |
| 156 | when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME); |
| 157 | |
| 158 | final List<MediaRoute2Info> routes = new ArrayList<>(); |
| 159 | routes.add(info); |
| 160 | when(mRouterManager.getAvailableRoutes(TEST_PACKAGE_NAME)).thenReturn(routes); |
| 161 | |
| 162 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 163 | assertThat(mediaDevice).isNull(); |
| 164 | |
| 165 | mInfoMediaManager.mMediaRouterCallback.onRoutesChanged(routes); |
| 166 | |
| 167 | final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0); |
| 168 | assertThat(infoDevice.getId()).isEqualTo(TEST_ID); |
| 169 | assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice); |
| 170 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size()); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void onRoutesChanged_buildAllRoutes_shouldAddMediaDevice() { |
| 175 | final MediaRoute2Info info = mock(MediaRoute2Info.class); |
| 176 | when(info.getId()).thenReturn(TEST_ID); |
| 177 | when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME); |
| 178 | |
| 179 | final List<MediaRoute2Info> routes = new ArrayList<>(); |
| 180 | routes.add(info); |
| 181 | when(mRouterManager.getAllRoutes()).thenReturn(routes); |
| 182 | |
| 183 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 184 | assertThat(mediaDevice).isNull(); |
| 185 | |
| 186 | mInfoMediaManager.mPackageName = ""; |
| 187 | mInfoMediaManager.mMediaRouterCallback.onRoutesChanged(routes); |
| 188 | |
| 189 | final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0); |
| 190 | assertThat(infoDevice.getId()).isEqualTo(TEST_ID); |
| 191 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size()); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void connectDeviceWithoutPackageName_noSession_returnFalse() { |
| 196 | final MediaRoute2Info info = mock(MediaRoute2Info.class); |
| 197 | final MediaDevice device = new InfoMediaDevice(mContext, mRouterManager, info, |
| 198 | TEST_PACKAGE_NAME); |
| 199 | |
| 200 | final List<RoutingSessionInfo> infos = new ArrayList<>(); |
| 201 | |
| 202 | when(mRouterManager.getActiveSessions()).thenReturn(infos); |
| 203 | |
| 204 | assertThat(mInfoMediaManager.connectDeviceWithoutPackageName(device)).isFalse(); |
| 205 | } |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 206 | } |