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 | |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 19 | import static android.bluetooth.BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES; |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 20 | import static android.media.MediaRoute2ProviderService.REASON_UNKNOWN_ERROR; |
| 21 | |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
| 24 | import static org.mockito.ArgumentMatchers.any; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.never; |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.spy; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.when; |
| 30 | |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 31 | import android.bluetooth.BluetoothAdapter; |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 32 | import android.bluetooth.BluetoothClass; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 33 | import android.bluetooth.BluetoothDevice; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 34 | import android.content.Context; |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 35 | import android.media.MediaRoute2Info; |
| 36 | import android.media.MediaRouter2Manager; |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 37 | import android.media.RoutingSessionInfo; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 38 | |
| 39 | import com.android.settingslib.bluetooth.A2dpProfile; |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 40 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 41 | import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 42 | import com.android.settingslib.bluetooth.HearingAidProfile; |
| 43 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 44 | import com.android.settingslib.bluetooth.LocalBluetoothProfile; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 45 | import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 46 | import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 47 | |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | import org.robolectric.RobolectricTestRunner; |
| 54 | import org.robolectric.RuntimeEnvironment; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 55 | import org.robolectric.annotation.Config; |
| 56 | import org.robolectric.shadow.api.Shadow; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 57 | |
| 58 | import java.util.ArrayList; |
| 59 | import java.util.List; |
| 60 | |
| 61 | @RunWith(RobolectricTestRunner.class) |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 62 | @Config(shadows = {ShadowBluetoothAdapter.class}) |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 63 | public class LocalMediaManagerTest { |
| 64 | |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 65 | private static final String TEST_DEVICE_NAME_1 = "device_name_1"; |
| 66 | private static final String TEST_DEVICE_NAME_2 = "device_name_2"; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 67 | private static final String TEST_DEVICE_ID_1 = "device_id_1"; |
| 68 | private static final String TEST_DEVICE_ID_2 = "device_id_2"; |
| 69 | private static final String TEST_DEVICE_ID_3 = "device_id_3"; |
| 70 | private static final String TEST_CURRENT_DEVICE_ID = "currentDevice_id"; |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 71 | private static final String TEST_PACKAGE_NAME = "com.test.playmusic"; |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 72 | private static final String TEST_SESSION_ID = "session_id"; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 73 | |
| 74 | @Mock |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 75 | private InfoMediaManager mInfoMediaManager; |
| 76 | @Mock |
| 77 | private LocalBluetoothManager mLocalBluetoothManager; |
| 78 | @Mock |
| 79 | private LocalMediaManager.DeviceCallback mCallback; |
| 80 | @Mock |
| 81 | private HearingAidProfile mHapProfile; |
| 82 | @Mock |
| 83 | private A2dpProfile mA2dpProfile; |
| 84 | @Mock |
| 85 | private LocalBluetoothProfileManager mLocalProfileManager; |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 86 | @Mock |
| 87 | private MediaRouter2Manager mMediaRouter2Manager; |
| 88 | @Mock |
| 89 | private MediaRoute2Info mRouteInfo1; |
| 90 | @Mock |
| 91 | private MediaRoute2Info mRouteInfo2; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 92 | |
| 93 | private Context mContext; |
| 94 | private LocalMediaManager mLocalMediaManager; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 95 | private ShadowBluetoothAdapter mShadowBluetoothAdapter; |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 96 | private InfoMediaDevice mInfoMediaDevice1; |
| 97 | private InfoMediaDevice mInfoMediaDevice2; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 98 | |
| 99 | @Before |
| 100 | public void setUp() { |
| 101 | MockitoAnnotations.initMocks(this); |
| 102 | mContext = RuntimeEnvironment.application; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 103 | final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); |
| 104 | mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); |
| 105 | mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(bluetoothDevices); |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 106 | when(mRouteInfo1.getName()).thenReturn(TEST_DEVICE_NAME_1); |
| 107 | when(mRouteInfo1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 108 | when(mRouteInfo2.getName()).thenReturn(TEST_DEVICE_NAME_2); |
| 109 | when(mRouteInfo2.getId()).thenReturn(TEST_DEVICE_ID_2); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 110 | when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager); |
| 111 | when(mLocalProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 112 | when(mLocalProfileManager.getHearingAidProfile()).thenReturn(mHapProfile); |
| 113 | |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 114 | mInfoMediaDevice1 = spy(new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo1, |
| 115 | TEST_PACKAGE_NAME)); |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 116 | mInfoMediaDevice2 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo2, |
| 117 | TEST_PACKAGE_NAME); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 118 | mLocalMediaManager = new LocalMediaManager(mContext, mLocalBluetoothManager, |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 119 | mInfoMediaManager, "com.test.packagename"); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void startScan_mediaDevicesListShouldBeClear() { |
| 124 | final MediaDevice device = mock(MediaDevice.class); |
| 125 | mLocalMediaManager.mMediaDevices.add(device); |
| 126 | |
| 127 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(1); |
| 128 | mLocalMediaManager.startScan(); |
| 129 | assertThat(mLocalMediaManager.mMediaDevices).isEmpty(); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void connectDevice_deviceNotEqualCurrentConnectedDevice_connectDevice() { |
| 134 | final MediaDevice currentDevice = mock(MediaDevice.class); |
| 135 | final MediaDevice device = mock(MediaDevice.class); |
| 136 | mLocalMediaManager.mMediaDevices.add(currentDevice); |
| 137 | mLocalMediaManager.mMediaDevices.add(device); |
| 138 | mLocalMediaManager.mCurrentConnectedDevice = currentDevice; |
| 139 | |
| 140 | when(device.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 141 | when(currentDevice.getId()).thenReturn(TEST_CURRENT_DEVICE_ID); |
| 142 | |
| 143 | mLocalMediaManager.registerCallback(mCallback); |
| 144 | mLocalMediaManager.connectDevice(device); |
| 145 | |
| 146 | verify(currentDevice).disconnect(); |
| 147 | verify(device).connect(); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Test |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 151 | public void connectDevice_deviceNotEqualCurrentConnectedDevice_isConnectingState() { |
| 152 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1); |
| 153 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2); |
| 154 | mLocalMediaManager.mCurrentConnectedDevice = mInfoMediaDevice1; |
| 155 | |
| 156 | mLocalMediaManager.registerCallback(mCallback); |
| 157 | mLocalMediaManager.connectDevice(mInfoMediaDevice2); |
| 158 | |
| 159 | assertThat(mInfoMediaDevice2.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 160 | .STATE_CONNECTING); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void connectDevice_deviceEqualCurrentConnectedDevice_notConnectingState() { |
| 165 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1); |
| 166 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2); |
| 167 | mLocalMediaManager.mCurrentConnectedDevice = mInfoMediaDevice1; |
| 168 | |
| 169 | mLocalMediaManager.registerCallback(mCallback); |
| 170 | mLocalMediaManager.connectDevice(mInfoMediaDevice1); |
| 171 | |
| 172 | assertThat(mInfoMediaDevice1.getState()).isNotEqualTo(LocalMediaManager.MediaDeviceState |
| 173 | .STATE_CONNECTING); |
| 174 | } |
| 175 | |
| 176 | @Test |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 177 | public void connectDevice_bluetoothDeviceNotConnected_connectBluetoothDevice() { |
| 178 | final MediaDevice device = mock(BluetoothMediaDevice.class); |
| 179 | final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class); |
| 180 | mLocalMediaManager.mMediaDevices.add(device); |
| 181 | |
| 182 | when(device.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 183 | when(((BluetoothMediaDevice) device).getCachedDevice()).thenReturn(cachedDevice); |
| 184 | when(cachedDevice.isConnected()).thenReturn(false); |
| 185 | when(cachedDevice.isBusy()).thenReturn(false); |
| 186 | |
| 187 | mLocalMediaManager.registerCallback(mCallback); |
| 188 | mLocalMediaManager.connectDevice(device); |
| 189 | |
hughchen | 4ae2186 | 2020-01-14 15:55:29 +0800 | [diff] [blame] | 190 | verify(cachedDevice).connect(); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Test |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 194 | public void getMediaDeviceById_idExist_shouldReturnMediaDevice() { |
| 195 | final MediaDevice device1 = mock(MediaDevice.class); |
| 196 | final MediaDevice device2 = mock(MediaDevice.class); |
| 197 | mLocalMediaManager.mMediaDevices.add(device1); |
| 198 | mLocalMediaManager.mMediaDevices.add(device2); |
| 199 | |
| 200 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 201 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 202 | |
| 203 | final MediaDevice device = mLocalMediaManager |
| 204 | .getMediaDeviceById(mLocalMediaManager.mMediaDevices, TEST_DEVICE_ID_2); |
| 205 | |
| 206 | assertThat(device.getId()).isEqualTo(TEST_DEVICE_ID_2); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void getMediaDeviceById_idNotExist_shouldReturnNull() { |
| 211 | final MediaDevice device1 = mock(MediaDevice.class); |
| 212 | final MediaDevice device2 = mock(MediaDevice.class); |
| 213 | mLocalMediaManager.mMediaDevices.add(device1); |
| 214 | mLocalMediaManager.mMediaDevices.add(device2); |
| 215 | |
| 216 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 217 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 218 | |
| 219 | final MediaDevice device = mLocalMediaManager |
| 220 | .getMediaDeviceById(mLocalMediaManager.mMediaDevices, TEST_CURRENT_DEVICE_ID); |
| 221 | |
| 222 | assertThat(device).isNull(); |
| 223 | } |
| 224 | |
| 225 | @Test |
hughchen | 4e6e524 | 2020-04-06 11:42:33 +0800 | [diff] [blame] | 226 | public void getMediaDeviceById_idIsNull_shouldReturnNull() { |
| 227 | final MediaDevice device1 = mock(MediaDevice.class); |
| 228 | final MediaDevice device2 = mock(MediaDevice.class); |
| 229 | mLocalMediaManager.mMediaDevices.add(device1); |
| 230 | mLocalMediaManager.mMediaDevices.add(device2); |
| 231 | |
| 232 | when(device1.getId()).thenReturn(null); |
| 233 | when(device2.getId()).thenReturn(null); |
| 234 | |
| 235 | MediaDevice device = mLocalMediaManager |
| 236 | .getMediaDeviceById(mLocalMediaManager.mMediaDevices, TEST_CURRENT_DEVICE_ID); |
| 237 | |
| 238 | assertThat(device).isNull(); |
| 239 | |
| 240 | device = mLocalMediaManager.getMediaDeviceById(TEST_CURRENT_DEVICE_ID); |
| 241 | |
| 242 | assertThat(device).isNull(); |
| 243 | } |
| 244 | |
| 245 | @Test |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 246 | public void onDeviceAdded_addDevice() { |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 247 | final MediaDevice device = mock(MediaDevice.class); |
| 248 | |
| 249 | assertThat(mLocalMediaManager.mMediaDevices).isEmpty(); |
| 250 | mLocalMediaManager.registerCallback(mCallback); |
| 251 | mLocalMediaManager.mMediaDeviceCallback.onDeviceAdded(device); |
| 252 | |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 253 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(1); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 254 | verify(mCallback).onDeviceListUpdate(any()); |
| 255 | } |
| 256 | |
| 257 | @Test |
| 258 | public void onDeviceAdded_mediaDeviceNotExistAndPhoneDeviceExistInList_addMediaDevice() { |
| 259 | final MediaDevice device1 = mock(MediaDevice.class); |
| 260 | final MediaDevice device2 = mock(MediaDevice.class); |
| 261 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 262 | mLocalMediaManager.mMediaDevices.add(device1); |
| 263 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 264 | |
| 265 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 266 | mLocalMediaManager.registerCallback(mCallback); |
| 267 | mLocalMediaManager.mMediaDeviceCallback.onDeviceAdded(device2); |
| 268 | |
| 269 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(3); |
| 270 | verify(mCallback).onDeviceListUpdate(any()); |
| 271 | } |
| 272 | |
| 273 | @Test |
| 274 | public void onDeviceAdded_mediaDeviceAndPhoneDeviceExistInList_doNothing() { |
| 275 | final MediaDevice device1 = mock(MediaDevice.class); |
| 276 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 277 | mLocalMediaManager.mMediaDevices.add(device1); |
| 278 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 279 | |
| 280 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 281 | mLocalMediaManager.registerCallback(mCallback); |
| 282 | mLocalMediaManager.mMediaDeviceCallback.onDeviceAdded(device1); |
| 283 | |
| 284 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 285 | verify(mCallback, never()).onDeviceListUpdate(any()); |
| 286 | } |
| 287 | |
| 288 | @Test |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 289 | public void onDeviceListAdded_addDevicesList() { |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 290 | final List<MediaDevice> devices = new ArrayList<>(); |
| 291 | final MediaDevice device1 = mock(MediaDevice.class); |
| 292 | final MediaDevice device2 = mock(MediaDevice.class); |
| 293 | devices.add(device1); |
| 294 | devices.add(device2); |
| 295 | |
| 296 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 297 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 298 | |
| 299 | assertThat(mLocalMediaManager.mMediaDevices).isEmpty(); |
| 300 | mLocalMediaManager.registerCallback(mCallback); |
| 301 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices); |
| 302 | |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 303 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 304 | verify(mCallback).onDeviceListUpdate(any()); |
| 305 | } |
| 306 | |
| 307 | @Test |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 308 | public void onDeviceListAdded_addDeviceList() { |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 309 | final List<MediaDevice> devices = new ArrayList<>(); |
| 310 | final MediaDevice device1 = mock(MediaDevice.class); |
| 311 | final MediaDevice device2 = mock(MediaDevice.class); |
| 312 | final MediaDevice device3 = mock(MediaDevice.class); |
| 313 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 314 | devices.add(device1); |
| 315 | devices.add(device2); |
| 316 | mLocalMediaManager.mMediaDevices.add(device3); |
| 317 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 318 | |
| 319 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 320 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 321 | when(device3.getId()).thenReturn(TEST_DEVICE_ID_3); |
| 322 | when(mLocalMediaManager.mPhoneDevice.getId()).thenReturn("test_phone_id"); |
| 323 | |
| 324 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 325 | mLocalMediaManager.registerCallback(mCallback); |
| 326 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices); |
| 327 | |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 328 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 329 | verify(mCallback).onDeviceListUpdate(any()); |
| 330 | } |
| 331 | |
| 332 | @Test |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 333 | public void onDeviceRemoved_removeDevice() { |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 334 | final MediaDevice device1 = mock(MediaDevice.class); |
| 335 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 336 | mLocalMediaManager.mMediaDevices.add(device1); |
| 337 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 338 | |
| 339 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 340 | mLocalMediaManager.registerCallback(mCallback); |
| 341 | mLocalMediaManager.mMediaDeviceCallback.onDeviceRemoved(device1); |
| 342 | |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 343 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(1); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 344 | verify(mCallback).onDeviceListUpdate(any()); |
| 345 | } |
| 346 | |
| 347 | @Test |
| 348 | public void onDeviceRemoved_phoneDeviceNotLastDeviceAfterRemoveMediaDevice_removeMediaDevice() { |
| 349 | final MediaDevice device1 = mock(MediaDevice.class); |
| 350 | final MediaDevice device2 = mock(MediaDevice.class); |
| 351 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 352 | mLocalMediaManager.mMediaDevices.add(device1); |
| 353 | mLocalMediaManager.mMediaDevices.add(device2); |
| 354 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 355 | |
| 356 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(3); |
| 357 | mLocalMediaManager.registerCallback(mCallback); |
| 358 | mLocalMediaManager.mMediaDeviceCallback.onDeviceRemoved(device2); |
| 359 | |
| 360 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 361 | verify(mCallback).onDeviceListUpdate(any()); |
| 362 | } |
| 363 | |
| 364 | @Test |
| 365 | public void onDeviceRemoved_removeMediaDeviceNotInList_doNothing() { |
| 366 | final MediaDevice device1 = mock(MediaDevice.class); |
| 367 | final MediaDevice device2 = mock(MediaDevice.class); |
| 368 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 369 | mLocalMediaManager.mMediaDevices.add(device2); |
| 370 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 371 | |
| 372 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 373 | mLocalMediaManager.registerCallback(mCallback); |
| 374 | mLocalMediaManager.mMediaDeviceCallback.onDeviceRemoved(device1); |
| 375 | |
| 376 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 377 | verify(mCallback, never()).onDeviceListUpdate(any()); |
| 378 | } |
| 379 | |
| 380 | @Test |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 381 | public void onDeviceListRemoved_removeAll() { |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 382 | final List<MediaDevice> devices = new ArrayList<>(); |
| 383 | final MediaDevice device1 = mock(MediaDevice.class); |
| 384 | final MediaDevice device2 = mock(MediaDevice.class); |
| 385 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 386 | devices.add(device1); |
| 387 | devices.add(device2); |
| 388 | mLocalMediaManager.mMediaDevices.add(device1); |
| 389 | mLocalMediaManager.mMediaDevices.add(device2); |
| 390 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 391 | |
| 392 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(3); |
| 393 | mLocalMediaManager.registerCallback(mCallback); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 394 | mLocalMediaManager.mMediaDeviceCallback |
| 395 | .onDeviceListRemoved(mLocalMediaManager.mMediaDevices); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 396 | |
| 397 | assertThat(mLocalMediaManager.mMediaDevices).isEmpty(); |
| 398 | verify(mCallback).onDeviceListUpdate(any()); |
| 399 | } |
| 400 | |
| 401 | @Test |
| 402 | public void onDeviceListRemoved_phoneDeviceNotLastDeviceAfterRemoveDeviceList_removeList() { |
| 403 | final List<MediaDevice> devices = new ArrayList<>(); |
| 404 | final MediaDevice device1 = mock(MediaDevice.class); |
| 405 | final MediaDevice device2 = mock(MediaDevice.class); |
| 406 | final MediaDevice device3 = mock(MediaDevice.class); |
| 407 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 408 | devices.add(device1); |
| 409 | devices.add(device3); |
| 410 | mLocalMediaManager.mMediaDevices.add(device1); |
| 411 | mLocalMediaManager.mMediaDevices.add(device2); |
| 412 | mLocalMediaManager.mMediaDevices.add(device3); |
| 413 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 414 | |
| 415 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(4); |
| 416 | mLocalMediaManager.registerCallback(mCallback); |
| 417 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListRemoved(devices); |
| 418 | |
| 419 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 420 | verify(mCallback).onDeviceListUpdate(any()); |
| 421 | } |
| 422 | |
| 423 | @Test |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 424 | public void onConnectedDeviceChanged_connectedAndCurrentDeviceAreDifferent_notifyThemChanged() { |
| 425 | final MediaDevice device1 = mock(MediaDevice.class); |
| 426 | final MediaDevice device2 = mock(MediaDevice.class); |
| 427 | |
| 428 | mLocalMediaManager.mMediaDevices.add(device1); |
| 429 | mLocalMediaManager.mMediaDevices.add(device2); |
| 430 | mLocalMediaManager.mCurrentConnectedDevice = device1; |
| 431 | |
| 432 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 433 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 434 | |
| 435 | mLocalMediaManager.registerCallback(mCallback); |
| 436 | mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_2); |
| 437 | |
| 438 | assertThat(mLocalMediaManager.getCurrentConnectedDevice()).isEqualTo(device2); |
hughchen | 29a8a18 | 2020-03-11 11:28:49 +0800 | [diff] [blame] | 439 | verify(mCallback).onSelectedDeviceStateChanged(device2, |
| 440 | LocalMediaManager.MediaDeviceState.STATE_CONNECTED); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Test |
| 444 | public void onConnectedDeviceChanged_connectedAndCurrentDeviceAreSame_doNothing() { |
| 445 | final MediaDevice device1 = mock(MediaDevice.class); |
| 446 | final MediaDevice device2 = mock(MediaDevice.class); |
| 447 | |
| 448 | mLocalMediaManager.mMediaDevices.add(device1); |
| 449 | mLocalMediaManager.mMediaDevices.add(device2); |
| 450 | mLocalMediaManager.mCurrentConnectedDevice = device1; |
| 451 | |
| 452 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 453 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 454 | |
| 455 | mLocalMediaManager.registerCallback(mCallback); |
| 456 | mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_1); |
| 457 | |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 458 | verify(mCallback, never()).onDeviceAttributesChanged(); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 459 | } |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 460 | |
| 461 | @Test |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 462 | public void onConnectedDeviceChanged_isConnectedState() { |
| 463 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1); |
| 464 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2); |
| 465 | mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED); |
| 466 | |
| 467 | assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 468 | .STATE_DISCONNECTED); |
| 469 | mLocalMediaManager.registerCallback(mCallback); |
| 470 | mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_1); |
| 471 | |
| 472 | assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 473 | .STATE_CONNECTED); |
| 474 | } |
| 475 | |
| 476 | @Test |
Tim Peng | 2c98fc4 | 2020-03-23 10:31:39 +0800 | [diff] [blame] | 477 | public void onConnectedDeviceChanged_nullConnectedDevice_noException() { |
| 478 | mLocalMediaManager.registerCallback(mCallback); |
| 479 | mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_2); |
| 480 | } |
| 481 | |
| 482 | @Test |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 483 | public void onDeviceAttributesChanged_shouldDispatchDeviceListUpdate() { |
| 484 | mLocalMediaManager.registerCallback(mCallback); |
| 485 | |
| 486 | mLocalMediaManager.mMediaDeviceCallback.onDeviceAttributesChanged(); |
| 487 | |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 488 | verify(mCallback).onDeviceAttributesChanged(); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 489 | } |
timhypeng | a812e88 | 2020-02-04 17:15:48 +0800 | [diff] [blame] | 490 | |
| 491 | @Test |
Tim Peng | 92fb219 | 2020-03-31 11:34:20 +0800 | [diff] [blame] | 492 | public void onDeviceAttributesChanged_failingTransferring_shouldResetState() { |
| 493 | final MediaDevice currentDevice = mock(MediaDevice.class); |
| 494 | final MediaDevice device = mock(BluetoothMediaDevice.class); |
| 495 | final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class); |
| 496 | mLocalMediaManager.mMediaDevices.add(device); |
| 497 | mLocalMediaManager.mMediaDevices.add(currentDevice); |
| 498 | when(device.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 499 | when(currentDevice.getId()).thenReturn(TEST_CURRENT_DEVICE_ID); |
| 500 | when(((BluetoothMediaDevice) device).getCachedDevice()).thenReturn(cachedDevice); |
| 501 | when(cachedDevice.isConnected()).thenReturn(false); |
| 502 | when(cachedDevice.isBusy()).thenReturn(false); |
| 503 | |
| 504 | mLocalMediaManager.registerCallback(mCallback); |
| 505 | mLocalMediaManager.connectDevice(device); |
| 506 | |
| 507 | mLocalMediaManager.mDeviceAttributeChangeCallback.onDeviceAttributesChanged(); |
| 508 | verify(device).setState(LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED); |
| 509 | } |
| 510 | |
| 511 | @Test |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 512 | public void onRequestFailed_checkDevicesState() { |
| 513 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1); |
| 514 | mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2); |
| 515 | mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING); |
| 516 | mInfoMediaDevice2.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTED); |
| 517 | |
| 518 | assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 519 | .STATE_CONNECTING); |
| 520 | assertThat(mInfoMediaDevice2.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 521 | .STATE_CONNECTED); |
| 522 | mLocalMediaManager.registerCallback(mCallback); |
| 523 | mLocalMediaManager.mMediaDeviceCallback.onRequestFailed(REASON_UNKNOWN_ERROR); |
| 524 | |
| 525 | assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 526 | .STATE_CONNECTING_FAILED); |
| 527 | assertThat(mInfoMediaDevice2.getState()).isEqualTo(LocalMediaManager.MediaDeviceState |
| 528 | .STATE_CONNECTED); |
| 529 | } |
| 530 | |
| 531 | @Test |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 532 | public void onDeviceAttributesChanged_shouldBeCalled() { |
| 533 | mLocalMediaManager.registerCallback(mCallback); |
| 534 | |
| 535 | mLocalMediaManager.mDeviceAttributeChangeCallback.onDeviceAttributesChanged(); |
| 536 | |
| 537 | verify(mCallback).onDeviceAttributesChanged(); |
| 538 | } |
| 539 | |
| 540 | @Test |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 541 | public void getActiveMediaSession_verifyCorrectSession() { |
| 542 | final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>(); |
| 543 | final RoutingSessionInfo info = mock(RoutingSessionInfo.class); |
| 544 | when(info.getId()).thenReturn(TEST_SESSION_ID); |
| 545 | routingSessionInfos.add(info); |
| 546 | when(mInfoMediaManager.getActiveMediaSession()).thenReturn(routingSessionInfos); |
| 547 | |
| 548 | assertThat(mLocalMediaManager.getActiveMediaSession().get(0).getId()) |
| 549 | .matches(TEST_SESSION_ID); |
| 550 | } |
| 551 | |
| 552 | @Test |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 553 | public void onDeviceListAdded_haveDisconnectedDevice_addDisconnectedDevice() { |
| 554 | final List<MediaDevice> devices = new ArrayList<>(); |
| 555 | final MediaDevice device1 = mock(MediaDevice.class); |
| 556 | final MediaDevice device2 = mock(MediaDevice.class); |
| 557 | final MediaDevice device3 = mock(MediaDevice.class); |
| 558 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 559 | devices.add(device1); |
| 560 | devices.add(device2); |
| 561 | mLocalMediaManager.mMediaDevices.add(device3); |
| 562 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 563 | |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 564 | final List<LocalBluetoothProfile> profiles = new ArrayList<>(); |
| 565 | final A2dpProfile a2dpProfile = mock(A2dpProfile.class); |
| 566 | profiles.add(a2dpProfile); |
| 567 | |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 568 | final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); |
| 569 | final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); |
| 570 | final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class); |
| 571 | final CachedBluetoothDeviceManager cachedManager = mock(CachedBluetoothDeviceManager.class); |
| 572 | bluetoothDevices.add(bluetoothDevice); |
| 573 | mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(bluetoothDevices); |
| 574 | |
| 575 | when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(cachedManager); |
| 576 | when(cachedManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice); |
| 577 | when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); |
| 578 | when(cachedDevice.isConnected()).thenReturn(false); |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 579 | when(cachedDevice.getConnectableProfiles()).thenReturn(profiles); |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 580 | |
| 581 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 582 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 583 | when(device3.getId()).thenReturn(TEST_DEVICE_ID_3); |
| 584 | when(mLocalMediaManager.mPhoneDevice.getId()).thenReturn("test_phone_id"); |
| 585 | |
| 586 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 587 | mLocalMediaManager.registerCallback(mCallback); |
| 588 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices); |
| 589 | |
| 590 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(3); |
| 591 | verify(mCallback).onDeviceListUpdate(any()); |
| 592 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 593 | |
| 594 | @Test |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 595 | public void onDeviceListAdded_transferToDisconnectedBluetooth_verifyConnectDevice() { |
| 596 | final List<MediaDevice> devices = new ArrayList<>(); |
| 597 | final MediaDevice currentDevice = mock(MediaDevice.class); |
| 598 | final MediaDevice device = mock(BluetoothMediaDevice.class); |
| 599 | final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class); |
| 600 | mLocalMediaManager.mMediaDevices.add(device); |
| 601 | mLocalMediaManager.mMediaDevices.add(currentDevice); |
| 602 | |
| 603 | when(device.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 604 | when(currentDevice.getId()).thenReturn(TEST_CURRENT_DEVICE_ID); |
| 605 | when(((BluetoothMediaDevice) device).getCachedDevice()).thenReturn(cachedDevice); |
| 606 | when(cachedDevice.isConnected()).thenReturn(false); |
| 607 | when(cachedDevice.isBusy()).thenReturn(false); |
| 608 | |
| 609 | mLocalMediaManager.registerCallback(mCallback); |
| 610 | mLocalMediaManager.connectDevice(device); |
| 611 | |
| 612 | verify(cachedDevice).connect(); |
| 613 | when(device.isConnected()).thenReturn(true); |
| 614 | mLocalMediaManager.mCurrentConnectedDevice = currentDevice; |
| 615 | devices.add(mInfoMediaDevice1); |
| 616 | devices.add(currentDevice); |
| 617 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices); |
| 618 | |
| 619 | verify(mInfoMediaDevice1).connect(); |
| 620 | } |
| 621 | |
| 622 | @Test |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 623 | public void onRequestFailed_shouldDispatchOnRequestFailed() { |
| 624 | mLocalMediaManager.registerCallback(mCallback); |
| 625 | |
| 626 | mLocalMediaManager.mMediaDeviceCallback.onRequestFailed(1); |
| 627 | |
| 628 | verify(mCallback).onRequestFailed(1); |
| 629 | } |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 630 | |
| 631 | @Test |
| 632 | public void onDeviceListAdded_haveDisconnectedDevice_list5DisconnectedDevice() { |
| 633 | final List<MediaDevice> devices = new ArrayList<>(); |
| 634 | final MediaDevice device1 = mock(MediaDevice.class); |
| 635 | final MediaDevice device2 = mock(MediaDevice.class); |
| 636 | final MediaDevice device3 = mock(MediaDevice.class); |
| 637 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 638 | devices.add(device1); |
| 639 | devices.add(device2); |
| 640 | mLocalMediaManager.mMediaDevices.add(device3); |
| 641 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 642 | |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 643 | final List<LocalBluetoothProfile> profiles = new ArrayList<>(); |
| 644 | final A2dpProfile a2dpProfile = mock(A2dpProfile.class); |
| 645 | profiles.add(a2dpProfile); |
| 646 | |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 647 | final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); |
| 648 | final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); |
| 649 | final BluetoothDevice bluetoothDevice2 = mock(BluetoothDevice.class); |
| 650 | final BluetoothDevice bluetoothDevice3 = mock(BluetoothDevice.class); |
| 651 | final BluetoothDevice bluetoothDevice4 = mock(BluetoothDevice.class); |
| 652 | final BluetoothDevice bluetoothDevice5 = mock(BluetoothDevice.class); |
| 653 | final BluetoothDevice bluetoothDevice6 = mock(BluetoothDevice.class); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 654 | final BluetoothClass bluetoothClass = mock(BluetoothClass.class); |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 655 | final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class); |
| 656 | final CachedBluetoothDeviceManager cachedManager = mock(CachedBluetoothDeviceManager.class); |
| 657 | bluetoothDevices.add(bluetoothDevice); |
| 658 | bluetoothDevices.add(bluetoothDevice2); |
| 659 | bluetoothDevices.add(bluetoothDevice3); |
| 660 | bluetoothDevices.add(bluetoothDevice4); |
| 661 | bluetoothDevices.add(bluetoothDevice5); |
| 662 | bluetoothDevices.add(bluetoothDevice6); |
| 663 | mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(bluetoothDevices); |
| 664 | |
| 665 | when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(cachedManager); |
| 666 | when(cachedManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice); |
| 667 | when(cachedManager.findDevice(bluetoothDevice2)).thenReturn(cachedDevice); |
| 668 | when(cachedManager.findDevice(bluetoothDevice3)).thenReturn(cachedDevice); |
| 669 | when(cachedManager.findDevice(bluetoothDevice4)).thenReturn(cachedDevice); |
| 670 | when(cachedManager.findDevice(bluetoothDevice5)).thenReturn(cachedDevice); |
| 671 | when(cachedManager.findDevice(bluetoothDevice6)).thenReturn(cachedDevice); |
| 672 | when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); |
| 673 | when(cachedDevice.isConnected()).thenReturn(false); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 674 | when(cachedDevice.getDevice()).thenReturn(bluetoothDevice); |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 675 | when(cachedDevice.getConnectableProfiles()).thenReturn(profiles); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 676 | when(bluetoothDevice.getBluetoothClass()).thenReturn(bluetoothClass); |
| 677 | when(bluetoothClass.getDeviceClass()).thenReturn(AUDIO_VIDEO_HEADPHONES); |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 678 | |
| 679 | when(device1.getId()).thenReturn(TEST_DEVICE_ID_1); |
| 680 | when(device2.getId()).thenReturn(TEST_DEVICE_ID_2); |
| 681 | when(device3.getId()).thenReturn(TEST_DEVICE_ID_3); |
| 682 | when(mLocalMediaManager.mPhoneDevice.getId()).thenReturn("test_phone_id"); |
| 683 | |
| 684 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 685 | mLocalMediaManager.registerCallback(mCallback); |
| 686 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices); |
| 687 | |
| 688 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(7); |
| 689 | verify(mCallback).onDeviceListUpdate(any()); |
| 690 | } |
hughchen | be51400 | 2020-04-14 11:50:55 +0800 | [diff] [blame] | 691 | |
| 692 | @Test |
| 693 | public void onDeviceListAdded_bluetoothAdapterIsNull_noDisconnectedDeviceAdded() { |
| 694 | final List<MediaDevice> devices = new ArrayList<>(); |
| 695 | final MediaDevice device1 = mock(MediaDevice.class); |
| 696 | final MediaDevice device2 = mock(MediaDevice.class); |
| 697 | final MediaDevice device3 = mock(MediaDevice.class); |
| 698 | mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class); |
| 699 | devices.add(device1); |
| 700 | devices.add(device2); |
| 701 | mLocalMediaManager.mMediaDevices.add(device3); |
| 702 | mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice); |
| 703 | |
| 704 | mShadowBluetoothAdapter = null; |
| 705 | |
| 706 | when(mLocalMediaManager.mPhoneDevice.getId()).thenReturn("test_phone_id"); |
| 707 | |
| 708 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 709 | mLocalMediaManager.registerCallback(mCallback); |
| 710 | mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices); |
| 711 | |
| 712 | assertThat(mLocalMediaManager.mMediaDevices).hasSize(2); |
| 713 | verify(mCallback).onDeviceListUpdate(any()); |
| 714 | } |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 715 | |
| 716 | @Test |
| 717 | public void adjustSessionVolume_verifyCorrectSessionVolume() { |
| 718 | final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>(); |
| 719 | final RoutingSessionInfo info = mock(RoutingSessionInfo.class); |
| 720 | when(info.getId()).thenReturn(TEST_SESSION_ID); |
| 721 | routingSessionInfos.add(info); |
| 722 | when(mInfoMediaManager.getActiveMediaSession()).thenReturn(routingSessionInfos); |
| 723 | |
| 724 | mLocalMediaManager.adjustSessionVolume(TEST_SESSION_ID, 10); |
| 725 | |
| 726 | verify(mInfoMediaManager).adjustSessionVolume(info, 10); |
| 727 | } |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 728 | } |