blob: 47d4beb752c5716c55bd6e185f18f595c45b02a9 [file] [log] [blame]
timhypenga8b826c2018-11-14 15:33:53 +08001/*
2 * Copyright 2018 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 */
16package com.android.settingslib.media;
17
hughchen9a36f802020-04-17 18:09:48 +080018import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
19import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
20import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER;
hughchen31901c02020-04-22 19:31:28 +080021import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES;
hughchen9a36f802020-04-17 18:09:48 +080022
timhypenga8b826c2018-11-14 15:33:53 +080023import static com.google.common.truth.Truth.assertThat;
24
hughchen31901c02020-04-22 19:31:28 +080025import static org.mockito.Mockito.mock;
hughchen491b9fc2020-02-05 18:31:36 +080026import static org.mockito.Mockito.verify;
timhypenga8b826c2018-11-14 15:33:53 +080027import static org.mockito.Mockito.when;
28
29import android.bluetooth.BluetoothClass;
30import android.bluetooth.BluetoothDevice;
31import android.content.Context;
hughchene6a4f482019-10-18 17:34:15 +080032import android.media.MediaRoute2Info;
33import android.media.MediaRouter2Manager;
timhypenga8b826c2018-11-14 15:33:53 +080034
hughchenbd0dd492019-01-08 14:34:10 +080035import com.android.settingslib.bluetooth.A2dpProfile;
timhypenga8b826c2018-11-14 15:33:53 +080036import com.android.settingslib.bluetooth.CachedBluetoothDevice;
hughchenbd0dd492019-01-08 14:34:10 +080037import com.android.settingslib.bluetooth.HearingAidProfile;
timhypenga8b826c2018-11-14 15:33:53 +080038import com.android.settingslib.bluetooth.LocalBluetoothManager;
hughchenbd0dd492019-01-08 14:34:10 +080039import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
timhypenga8b826c2018-11-14 15:33:53 +080040
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44import org.mockito.Mock;
45import org.mockito.MockitoAnnotations;
46import org.robolectric.RobolectricTestRunner;
47import org.robolectric.RuntimeEnvironment;
48
49import java.util.ArrayList;
50import java.util.Collections;
51import java.util.Comparator;
52import java.util.List;
53
54@RunWith(RobolectricTestRunner.class)
55public class MediaDeviceTest {
56 private static final Comparator<MediaDevice> COMPARATOR = Comparator.naturalOrder();
57 private static final String DEVICE_ADDRESS_1 = "AA:BB:CC:DD:EE:11";
58 private static final String DEVICE_ADDRESS_2 = "AA:BB:CC:DD:EE:22";
59 private static final String DEVICE_ADDRESS_3 = "AA:BB:CC:DD:EE:33";
60 private static final String DEVICE_NAME_1 = "TestName_1";
61 private static final String DEVICE_NAME_2 = "TestName_2";
62 private static final String DEVICE_NAME_3 = "TestName_3";
63 private static final String ROUTER_ID_1 = "RouterId_1";
64 private static final String ROUTER_ID_2 = "RouterId_2";
65 private static final String ROUTER_ID_3 = "RouterId_3";
hughchene6a4f482019-10-18 17:34:15 +080066 private static final String TEST_PACKAGE_NAME = "com.test.playmusic";
timhypenga8b826c2018-11-14 15:33:53 +080067 private final BluetoothClass mHeadreeClass =
68 new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES);
69 private final BluetoothClass mCarkitClass =
70 new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO);
71
72 @Mock
timhypenga8b826c2018-11-14 15:33:53 +080073 private BluetoothDevice mDevice1;
74 @Mock
75 private BluetoothDevice mDevice2;
76 @Mock
77 private BluetoothDevice mDevice3;
78 @Mock
79 private CachedBluetoothDevice mCachedDevice1;
80 @Mock
81 private CachedBluetoothDevice mCachedDevice2;
82 @Mock
83 private CachedBluetoothDevice mCachedDevice3;
84 @Mock
85 private LocalBluetoothManager mLocalBluetoothManager;
86 @Mock
hughchene6a4f482019-10-18 17:34:15 +080087 private MediaRoute2Info mRouteInfo1;
timhypenga8b826c2018-11-14 15:33:53 +080088 @Mock
hughchene6a4f482019-10-18 17:34:15 +080089 private MediaRoute2Info mRouteInfo2;
timhypenga8b826c2018-11-14 15:33:53 +080090 @Mock
hughchene6a4f482019-10-18 17:34:15 +080091 private MediaRoute2Info mRouteInfo3;
hughchenbd0dd492019-01-08 14:34:10 +080092 @Mock
hughchen491b9fc2020-02-05 18:31:36 +080093 private MediaRoute2Info mBluetoothRouteInfo1;
94 @Mock
95 private MediaRoute2Info mBluetoothRouteInfo2;
96 @Mock
97 private MediaRoute2Info mBluetoothRouteInfo3;
98 @Mock
99 private MediaRoute2Info mPhoneRouteInfo;
100 @Mock
hughchenbd0dd492019-01-08 14:34:10 +0800101 private LocalBluetoothProfileManager mProfileManager;
102 @Mock
103 private HearingAidProfile mHapProfile;
104 @Mock
105 private A2dpProfile mA2dpProfile;
106 @Mock
107 private BluetoothDevice mDevice;
hughchen491b9fc2020-02-05 18:31:36 +0800108 @Mock
109 private MediaRouter2Manager mMediaRouter2Manager;
timhypenga8b826c2018-11-14 15:33:53 +0800110
111 private BluetoothMediaDevice mBluetoothMediaDevice1;
112 private BluetoothMediaDevice mBluetoothMediaDevice2;
113 private BluetoothMediaDevice mBluetoothMediaDevice3;
114 private Context mContext;
115 private InfoMediaDevice mInfoMediaDevice1;
116 private InfoMediaDevice mInfoMediaDevice2;
117 private InfoMediaDevice mInfoMediaDevice3;
118 private List<MediaDevice> mMediaDevices = new ArrayList<>();
119 private PhoneMediaDevice mPhoneMediaDevice;
120
121 @Before
122 public void setUp() {
123 MockitoAnnotations.initMocks(this);
124 mContext = RuntimeEnvironment.application;
125
126 when(mCachedDevice1.getAddress()).thenReturn(DEVICE_ADDRESS_1);
127 when(mCachedDevice2.getAddress()).thenReturn(DEVICE_ADDRESS_2);
128 when(mCachedDevice3.getAddress()).thenReturn(DEVICE_ADDRESS_3);
129 when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME_1);
130 when(mCachedDevice2.getName()).thenReturn(DEVICE_NAME_2);
131 when(mCachedDevice3.getName()).thenReturn(DEVICE_NAME_3);
132 when(mCachedDevice1.getDevice()).thenReturn(mDevice1);
133 when(mCachedDevice2.getDevice()).thenReturn(mDevice2);
134 when(mCachedDevice3.getDevice()).thenReturn(mDevice3);
hughchenc0c11632019-03-07 16:21:17 +0800135 when(mCachedDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
136 when(mCachedDevice1.isConnected()).thenReturn(true);
137 when(mCachedDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
138 when(mCachedDevice2.isConnected()).thenReturn(true);
139 when(mCachedDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
140 when(mCachedDevice3.isConnected()).thenReturn(true);
hughchen9a36f802020-04-17 18:09:48 +0800141 when(mBluetoothRouteInfo1.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
142 when(mBluetoothRouteInfo2.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
143 when(mBluetoothRouteInfo3.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
timhypenga8b826c2018-11-14 15:33:53 +0800144 when(mRouteInfo1.getId()).thenReturn(ROUTER_ID_1);
145 when(mRouteInfo2.getId()).thenReturn(ROUTER_ID_2);
146 when(mRouteInfo3.getId()).thenReturn(ROUTER_ID_3);
147 when(mRouteInfo1.getName()).thenReturn(DEVICE_NAME_1);
148 when(mRouteInfo2.getName()).thenReturn(DEVICE_NAME_2);
149 when(mRouteInfo3.getName()).thenReturn(DEVICE_NAME_3);
hughchen9a36f802020-04-17 18:09:48 +0800150 when(mRouteInfo1.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
151 when(mRouteInfo2.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
152 when(mRouteInfo3.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
153 when(mPhoneRouteInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
hughchenbd0dd492019-01-08 14:34:10 +0800154 when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
155 when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
156 when(mProfileManager.getHearingAidProfile()).thenReturn(mHapProfile);
157 when(mA2dpProfile.getActiveDevice()).thenReturn(mDevice);
timhypenga8b826c2018-11-14 15:33:53 +0800158
hughchendf7b8382020-02-03 17:48:49 +0800159 mBluetoothMediaDevice1 =
hughchen491b9fc2020-02-05 18:31:36 +0800160 new BluetoothMediaDevice(mContext, mCachedDevice1, mMediaRouter2Manager,
161 mBluetoothRouteInfo1, TEST_PACKAGE_NAME);
hughchendf7b8382020-02-03 17:48:49 +0800162 mBluetoothMediaDevice2 =
hughchen491b9fc2020-02-05 18:31:36 +0800163 new BluetoothMediaDevice(mContext, mCachedDevice2, mMediaRouter2Manager,
164 mBluetoothRouteInfo2, TEST_PACKAGE_NAME);
hughchendf7b8382020-02-03 17:48:49 +0800165 mBluetoothMediaDevice3 =
hughchen491b9fc2020-02-05 18:31:36 +0800166 new BluetoothMediaDevice(mContext, mCachedDevice3, mMediaRouter2Manager,
167 mBluetoothRouteInfo3, TEST_PACKAGE_NAME);
hughchene6a4f482019-10-18 17:34:15 +0800168 mInfoMediaDevice1 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo1,
169 TEST_PACKAGE_NAME);
170 mInfoMediaDevice2 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo2,
171 TEST_PACKAGE_NAME);
172 mInfoMediaDevice3 = new InfoMediaDevice(mContext, mMediaRouter2Manager, mRouteInfo3,
173 TEST_PACKAGE_NAME);
hughchendf7b8382020-02-03 17:48:49 +0800174 mPhoneMediaDevice =
hughchen491b9fc2020-02-05 18:31:36 +0800175 new PhoneMediaDevice(mContext, mMediaRouter2Manager, mPhoneRouteInfo,
176 TEST_PACKAGE_NAME);
timhypenga8b826c2018-11-14 15:33:53 +0800177 }
178
179 @Test
180 public void compareTo_carKit_nonCarKitBluetooth_carKitFirst() {
181 when(mDevice1.getBluetoothClass()).thenReturn(mHeadreeClass);
182 when(mDevice2.getBluetoothClass()).thenReturn(mCarkitClass);
183 mMediaDevices.add(mBluetoothMediaDevice1);
184 mMediaDevices.add(mBluetoothMediaDevice2);
185
186 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
187 Collections.sort(mMediaDevices, COMPARATOR);
188 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
189 }
190
191 @Test
192 public void compareTo_carKit_info_carKitFirst() {
193 when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
194 mMediaDevices.add(mInfoMediaDevice1);
195 mMediaDevices.add(mBluetoothMediaDevice1);
196
197 assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
198 Collections.sort(mMediaDevices, COMPARATOR);
199 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
200 }
201
202 @Test
hughchen31901c02020-04-22 19:31:28 +0800203 public void compareTo_carKit_phone_carKitFirst() {
timhypenga8b826c2018-11-14 15:33:53 +0800204 when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
hughchenc0c11632019-03-07 16:21:17 +0800205 mMediaDevices.add(mPhoneMediaDevice);
hughchen31901c02020-04-22 19:31:28 +0800206 mMediaDevices.add(mBluetoothMediaDevice1);
timhypenga8b826c2018-11-14 15:33:53 +0800207
208 assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
hughchen31901c02020-04-22 19:31:28 +0800209 Collections.sort(mMediaDevices, COMPARATOR);
210 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
hughchenc0c11632019-03-07 16:21:17 +0800211 }
212
213 @Test
214 public void compareTo_carKitIsDisConnected_nonCarKitBluetooth_nonCarKitBluetoothFirst() {
215 when(mDevice1.getBluetoothClass()).thenReturn(mHeadreeClass);
216 when(mDevice2.getBluetoothClass()).thenReturn(mCarkitClass);
217 when(mCachedDevice2.isConnected()).thenReturn(false);
218 mMediaDevices.add(mBluetoothMediaDevice1);
219 mMediaDevices.add(mBluetoothMediaDevice2);
220
221 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
timhypenga8b826c2018-11-14 15:33:53 +0800222 Collections.sort(mMediaDevices, COMPARATOR);
223 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
224 }
225
226 @Test
227 public void compareTo_lastSelected_others_lastSelectedFirst() {
228 mMediaDevices.add(mBluetoothMediaDevice1);
229 mMediaDevices.add(mBluetoothMediaDevice2);
230 mBluetoothMediaDevice2.connect();
231
232 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
233 Collections.sort(mMediaDevices, COMPARATOR);
234 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
235 }
hughchenc0c11632019-03-07 16:21:17 +0800236
timhypenga8b826c2018-11-14 15:33:53 +0800237 @Test
238 public void compareTo_connectionRecord_sortByRecord() {
239 mMediaDevices.add(mBluetoothMediaDevice1);
240 mMediaDevices.add(mBluetoothMediaDevice2);
241 mBluetoothMediaDevice1.connect();
242 mBluetoothMediaDevice2.connect();
243 mBluetoothMediaDevice2.connect();
244 // Reset last selected record
245 ConnectionRecordManager.getInstance().setConnectionRecord(mContext, null, 0);
246
247 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
248 assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
249 Collections.sort(mMediaDevices, COMPARATOR);
250 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
251 assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice1);
252 }
253
254 @Test
hughchenc0c11632019-03-07 16:21:17 +0800255 public void compareTo_sortByRecord_connectedDeviceFirst() {
256 mMediaDevices.add(mBluetoothMediaDevice1);
257 mMediaDevices.add(mBluetoothMediaDevice2);
258 when(mCachedDevice2.isConnected()).thenReturn(false);
259
260 mBluetoothMediaDevice1.connect();
261 mBluetoothMediaDevice2.connect();
262 mBluetoothMediaDevice2.connect();
263 // Reset last selected record
264 ConnectionRecordManager.getInstance().setConnectionRecord(mContext, null, 0);
265
266 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
267 assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
268 Collections.sort(mMediaDevices, COMPARATOR);
269 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
270 assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
271 }
272
273 @Test
hughchen31901c02020-04-22 19:31:28 +0800274 public void compareTo_info_bluetooth_bluetoothFirst() {
timhypenga8b826c2018-11-14 15:33:53 +0800275 mMediaDevices.add(mInfoMediaDevice1);
hughchen9a36f802020-04-17 18:09:48 +0800276 mMediaDevices.add(mBluetoothMediaDevice1);
timhypenga8b826c2018-11-14 15:33:53 +0800277
timhypenga8b826c2018-11-14 15:33:53 +0800278 assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
hughchen9a36f802020-04-17 18:09:48 +0800279 Collections.sort(mMediaDevices, COMPARATOR);
280 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
timhypenga8b826c2018-11-14 15:33:53 +0800281 }
282
283 @Test
hughchen31901c02020-04-22 19:31:28 +0800284 public void compareTo_bluetooth_phone_bluetoothFirst() {
hughchenc0c11632019-03-07 16:21:17 +0800285 mMediaDevices.add(mPhoneMediaDevice);
hughchen31901c02020-04-22 19:31:28 +0800286 mMediaDevices.add(mBluetoothMediaDevice1);
287
288 assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
289 Collections.sort(mMediaDevices, COMPARATOR);
290 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
291 }
292
293 @Test
294 public void compareTo_bluetooth_wiredHeadset_wiredHeadsetFirst() {
295 final MediaRoute2Info phoneRouteInfo = mock(MediaRoute2Info.class);
296 when(phoneRouteInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
297
298 final PhoneMediaDevice phoneMediaDevice = new PhoneMediaDevice(mContext,
299 mMediaRouter2Manager, phoneRouteInfo, TEST_PACKAGE_NAME);
300
301 mMediaDevices.add(mBluetoothMediaDevice1);
302 mMediaDevices.add(phoneMediaDevice);
timhypenga8b826c2018-11-14 15:33:53 +0800303
timhypenga8b826c2018-11-14 15:33:53 +0800304 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
hughchenc0c11632019-03-07 16:21:17 +0800305 Collections.sort(mMediaDevices, COMPARATOR);
hughchen31901c02020-04-22 19:31:28 +0800306 assertThat(mMediaDevices.get(0)).isEqualTo(phoneMediaDevice);
307 }
308
309 @Test
310 public void compareTo_info_wiredHeadset_wiredHeadsetFirst() {
311 final MediaRoute2Info phoneRouteInfo = mock(MediaRoute2Info.class);
312 when(phoneRouteInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
313
314 final PhoneMediaDevice phoneMediaDevice = new PhoneMediaDevice(mContext,
315 mMediaRouter2Manager, phoneRouteInfo, TEST_PACKAGE_NAME);
316
317 mMediaDevices.add(mInfoMediaDevice1);
318 mMediaDevices.add(phoneMediaDevice);
319
320 assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
321 Collections.sort(mMediaDevices, COMPARATOR);
322 assertThat(mMediaDevices.get(0)).isEqualTo(phoneMediaDevice);
timhypenga8b826c2018-11-14 15:33:53 +0800323 }
324
325 @Test
326 public void compareTo_twoInfo_sortByAlphabet() {
327 mMediaDevices.add(mInfoMediaDevice2);
328 mMediaDevices.add(mInfoMediaDevice1);
329
330 assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice2);
331 Collections.sort(mMediaDevices, COMPARATOR);
332 assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
333 }
334
335 @Test
336 public void compareTo_twoBluetooth_sortByAlphabet() {
337 mMediaDevices.add(mBluetoothMediaDevice2);
338 mMediaDevices.add(mBluetoothMediaDevice1);
339
340 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
341 Collections.sort(mMediaDevices, COMPARATOR);
342 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
343 }
344
hughchenc0c11632019-03-07 16:21:17 +0800345 @Test
346 public void compareTo_sortByAlphabet_connectDeviceFirst() {
347 mMediaDevices.add(mBluetoothMediaDevice2);
348 mMediaDevices.add(mBluetoothMediaDevice1);
349 when(mCachedDevice1.isConnected()).thenReturn(false);
350
351 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
352 Collections.sort(mMediaDevices, COMPARATOR);
353 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
354 }
355
timhypenga8b826c2018-11-14 15:33:53 +0800356 // 1.mInfoMediaDevice1: Last Selected device
357 // 2.mBluetoothMediaDevice1: CarKit device
358 // 3.mInfoMediaDevice2: * 2 times usage
359 // 4.mInfoMediaDevice3: * 1 time usage
360 // 5.mBluetoothMediaDevice2: * 2 times usage
361 // 6.mBluetoothMediaDevice3: * 1 time usage
362 // 7.mPhoneMediaDevice: * 0 time usage
hughchen31901c02020-04-22 19:31:28 +0800363 // Order: 2 -> 5 -> 6 -> 1 -> 3 -> 4 -> 7
timhypenga8b826c2018-11-14 15:33:53 +0800364 @Test
365 public void compareTo_mixedDevices_carKitFirst() {
366 when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
367 when(mDevice2.getBluetoothClass()).thenReturn(mHeadreeClass);
368 when(mDevice3.getBluetoothClass()).thenReturn(mHeadreeClass);
369 mMediaDevices.add(mBluetoothMediaDevice1);
370 mMediaDevices.add(mBluetoothMediaDevice2);
371 mMediaDevices.add(mBluetoothMediaDevice3);
372 mMediaDevices.add(mInfoMediaDevice1);
373 mMediaDevices.add(mInfoMediaDevice2);
374 mMediaDevices.add(mInfoMediaDevice3);
375 mMediaDevices.add(mPhoneMediaDevice);
376 mBluetoothMediaDevice3.connect();
377 mBluetoothMediaDevice2.connect();
378 mBluetoothMediaDevice2.connect();
379 mInfoMediaDevice3.connect();
380 mInfoMediaDevice2.connect();
381 mInfoMediaDevice2.connect();
382 mInfoMediaDevice1.connect();
383
384 Collections.sort(mMediaDevices, COMPARATOR);
hughchen31901c02020-04-22 19:31:28 +0800385 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
386 assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
387 assertThat(mMediaDevices.get(2)).isEqualTo(mBluetoothMediaDevice3);
388 assertThat(mMediaDevices.get(3)).isEqualTo(mInfoMediaDevice1);
hughchen9a36f802020-04-17 18:09:48 +0800389 assertThat(mMediaDevices.get(4)).isEqualTo(mInfoMediaDevice2);
hughchen31901c02020-04-22 19:31:28 +0800390 assertThat(mMediaDevices.get(5)).isEqualTo(mInfoMediaDevice3);
391 assertThat(mMediaDevices.get(6)).isEqualTo(mPhoneMediaDevice);
hughchenc0c11632019-03-07 16:21:17 +0800392 }
393
394 // 1.mInfoMediaDevice1: Last Selected device
395 // 2.mBluetoothMediaDevice1: CarKit device not connected
396 // 3.mInfoMediaDevice2: * 2 times usage
397 // 4.mInfoMediaDevice3: * 1 time usage
398 // 5.mBluetoothMediaDevice2: * 4 times usage not connected
399 // 6.mBluetoothMediaDevice3: * 1 time usage
400 // 7.mPhoneMediaDevice: * 0 time usage
hughchen31901c02020-04-22 19:31:28 +0800401 // Order: 6 -> 1 -> 3 -> 4 -> 7 -> 2 -> 5
hughchenc0c11632019-03-07 16:21:17 +0800402 @Test
403 public void compareTo_mixedDevices_connectDeviceFirst() {
404 when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
405 when(mDevice2.getBluetoothClass()).thenReturn(mHeadreeClass);
406 when(mDevice3.getBluetoothClass()).thenReturn(mHeadreeClass);
407 when(mCachedDevice1.isConnected()).thenReturn(false);
408 when(mCachedDevice2.isConnected()).thenReturn(false);
409 mMediaDevices.add(mBluetoothMediaDevice1);
410 mMediaDevices.add(mBluetoothMediaDevice2);
411 mMediaDevices.add(mBluetoothMediaDevice3);
412 mMediaDevices.add(mInfoMediaDevice1);
413 mMediaDevices.add(mInfoMediaDevice2);
414 mMediaDevices.add(mInfoMediaDevice3);
415 mMediaDevices.add(mPhoneMediaDevice);
416 mBluetoothMediaDevice3.connect();
417 mBluetoothMediaDevice2.connect();
418 mBluetoothMediaDevice2.connect();
419 mBluetoothMediaDevice2.connect();
420 mBluetoothMediaDevice2.connect();
421 mInfoMediaDevice3.connect();
422 mInfoMediaDevice2.connect();
423 mInfoMediaDevice2.connect();
424 mInfoMediaDevice1.connect();
425
426 Collections.sort(mMediaDevices, COMPARATOR);
hughchen31901c02020-04-22 19:31:28 +0800427 assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice3);
timhypenga8b826c2018-11-14 15:33:53 +0800428 assertThat(mMediaDevices.get(1)).isEqualTo(mInfoMediaDevice1);
429 assertThat(mMediaDevices.get(2)).isEqualTo(mInfoMediaDevice2);
hughchen31901c02020-04-22 19:31:28 +0800430 assertThat(mMediaDevices.get(3)).isEqualTo(mInfoMediaDevice3);
431 assertThat(mMediaDevices.get(4)).isEqualTo(mPhoneMediaDevice);
hughchenc0c11632019-03-07 16:21:17 +0800432 assertThat(mMediaDevices.get(5)).isEqualTo(mBluetoothMediaDevice1);
433 assertThat(mMediaDevices.get(6)).isEqualTo(mBluetoothMediaDevice2);
timhypenga8b826c2018-11-14 15:33:53 +0800434 }
hughchen491b9fc2020-02-05 18:31:36 +0800435
436 @Test
437 public void connect_shouldSelectRoute() {
438 mInfoMediaDevice1.connect();
439
440 verify(mMediaRouter2Manager).selectRoute(TEST_PACKAGE_NAME, mRouteInfo1);
441 }
timhypenga109d3a2020-02-10 14:36:44 +0800442
443 @Test
444 public void getClientPackageName_returnPackageName() {
445 when(mRouteInfo1.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
446
447 assertThat(mInfoMediaDevice1.getClientPackageName()).isEqualTo(TEST_PACKAGE_NAME);
448 }
449
Tim Pengdbdfaef2020-03-11 11:30:01 +0800450 @Test
451 public void setState_verifyGetState() {
452 mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTED);
453 assertThat(mInfoMediaDevice1.getState()).isEqualTo(
454 LocalMediaManager.MediaDeviceState.STATE_CONNECTED);
455
456 mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING);
457 assertThat(mInfoMediaDevice1.getState()).isEqualTo(
458 LocalMediaManager.MediaDeviceState.STATE_CONNECTING);
459
460 mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED);
461 assertThat(mInfoMediaDevice1.getState()).isEqualTo(
462 LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED);
463
464 mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING_FAILED);
465 assertThat(mInfoMediaDevice1.getState()).isEqualTo(
466 LocalMediaManager.MediaDeviceState.STATE_CONNECTING_FAILED);
467 }
timhypenga8b826c2018-11-14 15:33:53 +0800468}