timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.settingslib.media; |
| 17 | |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 18 | import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP; |
| 19 | import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER; |
| 20 | import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER; |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 21 | import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES; |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 22 | |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 23 | import static com.google.common.truth.Truth.assertThat; |
| 24 | |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 25 | import static org.mockito.Mockito.mock; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.verify; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.bluetooth.BluetoothClass; |
| 30 | import android.bluetooth.BluetoothDevice; |
| 31 | import android.content.Context; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 32 | import android.media.MediaRoute2Info; |
| 33 | import android.media.MediaRouter2Manager; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 34 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 35 | import com.android.settingslib.bluetooth.A2dpProfile; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 36 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 37 | import com.android.settingslib.bluetooth.HearingAidProfile; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 38 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 39 | import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.mockito.Mock; |
| 45 | import org.mockito.MockitoAnnotations; |
| 46 | import org.robolectric.RobolectricTestRunner; |
| 47 | import org.robolectric.RuntimeEnvironment; |
| 48 | |
| 49 | import java.util.ArrayList; |
| 50 | import java.util.Collections; |
| 51 | import java.util.Comparator; |
| 52 | import java.util.List; |
| 53 | |
| 54 | @RunWith(RobolectricTestRunner.class) |
| 55 | public 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"; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 66 | private static final String TEST_PACKAGE_NAME = "com.test.playmusic"; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 67 | 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 |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 73 | 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 |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 87 | private MediaRoute2Info mRouteInfo1; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 88 | @Mock |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 89 | private MediaRoute2Info mRouteInfo2; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 90 | @Mock |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 91 | private MediaRoute2Info mRouteInfo3; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 92 | @Mock |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 93 | private MediaRoute2Info mBluetoothRouteInfo1; |
| 94 | @Mock |
| 95 | private MediaRoute2Info mBluetoothRouteInfo2; |
| 96 | @Mock |
| 97 | private MediaRoute2Info mBluetoothRouteInfo3; |
| 98 | @Mock |
| 99 | private MediaRoute2Info mPhoneRouteInfo; |
| 100 | @Mock |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 101 | private LocalBluetoothProfileManager mProfileManager; |
| 102 | @Mock |
| 103 | private HearingAidProfile mHapProfile; |
| 104 | @Mock |
| 105 | private A2dpProfile mA2dpProfile; |
| 106 | @Mock |
| 107 | private BluetoothDevice mDevice; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 108 | @Mock |
| 109 | private MediaRouter2Manager mMediaRouter2Manager; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 110 | |
| 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); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 135 | 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); |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 141 | when(mBluetoothRouteInfo1.getType()).thenReturn(TYPE_BLUETOOTH_A2DP); |
| 142 | when(mBluetoothRouteInfo2.getType()).thenReturn(TYPE_BLUETOOTH_A2DP); |
| 143 | when(mBluetoothRouteInfo3.getType()).thenReturn(TYPE_BLUETOOTH_A2DP); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 144 | 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); |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 150 | 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); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 154 | when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager); |
| 155 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 156 | when(mProfileManager.getHearingAidProfile()).thenReturn(mHapProfile); |
| 157 | when(mA2dpProfile.getActiveDevice()).thenReturn(mDevice); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 158 | |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 159 | mBluetoothMediaDevice1 = |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 160 | new BluetoothMediaDevice(mContext, mCachedDevice1, mMediaRouter2Manager, |
| 161 | mBluetoothRouteInfo1, TEST_PACKAGE_NAME); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 162 | mBluetoothMediaDevice2 = |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 163 | new BluetoothMediaDevice(mContext, mCachedDevice2, mMediaRouter2Manager, |
| 164 | mBluetoothRouteInfo2, TEST_PACKAGE_NAME); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 165 | mBluetoothMediaDevice3 = |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 166 | new BluetoothMediaDevice(mContext, mCachedDevice3, mMediaRouter2Manager, |
| 167 | mBluetoothRouteInfo3, TEST_PACKAGE_NAME); |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 168 | 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); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 174 | mPhoneMediaDevice = |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 175 | new PhoneMediaDevice(mContext, mMediaRouter2Manager, mPhoneRouteInfo, |
| 176 | TEST_PACKAGE_NAME); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 177 | } |
| 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 |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 203 | public void compareTo_carKit_phone_carKitFirst() { |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 204 | when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 205 | mMediaDevices.add(mPhoneMediaDevice); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 206 | mMediaDevices.add(mBluetoothMediaDevice1); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 207 | |
| 208 | assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 209 | Collections.sort(mMediaDevices, COMPARATOR); |
| 210 | assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 211 | } |
| 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); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 222 | 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 | } |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 236 | |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 237 | @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 |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 255 | 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 |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 274 | public void compareTo_info_bluetooth_bluetoothFirst() { |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 275 | mMediaDevices.add(mInfoMediaDevice1); |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 276 | mMediaDevices.add(mBluetoothMediaDevice1); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 277 | |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 278 | assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1); |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 279 | Collections.sort(mMediaDevices, COMPARATOR); |
| 280 | assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Test |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 284 | public void compareTo_bluetooth_phone_bluetoothFirst() { |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 285 | mMediaDevices.add(mPhoneMediaDevice); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 286 | 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); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 303 | |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 304 | assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 305 | Collections.sort(mMediaDevices, COMPARATOR); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 306 | 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); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 323 | } |
| 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 | |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 345 | @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 | |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 356 | // 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 |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 363 | // Order: 2 -> 5 -> 6 -> 1 -> 3 -> 4 -> 7 |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 364 | @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); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 385 | 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); |
hughchen | 9a36f80 | 2020-04-17 18:09:48 +0800 | [diff] [blame] | 389 | assertThat(mMediaDevices.get(4)).isEqualTo(mInfoMediaDevice2); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 390 | assertThat(mMediaDevices.get(5)).isEqualTo(mInfoMediaDevice3); |
| 391 | assertThat(mMediaDevices.get(6)).isEqualTo(mPhoneMediaDevice); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 392 | } |
| 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 |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 401 | // Order: 6 -> 1 -> 3 -> 4 -> 7 -> 2 -> 5 |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 402 | @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); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 427 | assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice3); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 428 | assertThat(mMediaDevices.get(1)).isEqualTo(mInfoMediaDevice1); |
| 429 | assertThat(mMediaDevices.get(2)).isEqualTo(mInfoMediaDevice2); |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 430 | assertThat(mMediaDevices.get(3)).isEqualTo(mInfoMediaDevice3); |
| 431 | assertThat(mMediaDevices.get(4)).isEqualTo(mPhoneMediaDevice); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 432 | assertThat(mMediaDevices.get(5)).isEqualTo(mBluetoothMediaDevice1); |
| 433 | assertThat(mMediaDevices.get(6)).isEqualTo(mBluetoothMediaDevice2); |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 434 | } |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 435 | |
| 436 | @Test |
| 437 | public void connect_shouldSelectRoute() { |
| 438 | mInfoMediaDevice1.connect(); |
| 439 | |
| 440 | verify(mMediaRouter2Manager).selectRoute(TEST_PACKAGE_NAME, mRouteInfo1); |
| 441 | } |
timhypeng | a109d3a | 2020-02-10 14:36:44 +0800 | [diff] [blame] | 442 | |
| 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 Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 450 | @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 | } |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 468 | } |