Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.bluetooth; |
| 17 | |
| 18 | import static com.google.common.truth.Truth.assertThat; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 19 | import static org.mockito.Matchers.any; |
| 20 | import static org.mockito.Matchers.anyString; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.doAnswer; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.never; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.spy; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.verify; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.bluetooth.BluetoothAdapter; |
| 28 | import android.bluetooth.BluetoothDevice; |
| 29 | import android.bluetooth.BluetoothProfile; |
| 30 | import android.content.Context; |
| 31 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.mockito.Mock; |
| 36 | import org.mockito.MockitoAnnotations; |
| 37 | import org.robolectric.RobolectricTestRunner; |
| 38 | import org.robolectric.RuntimeEnvironment; |
| 39 | import org.robolectric.annotation.Config; |
| 40 | |
| 41 | @RunWith(RobolectricTestRunner.class) |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 42 | @Config(resourceDir = "../../res") |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 43 | public class CachedBluetoothDeviceTest { |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 44 | private final static String DEVICE_NAME = "TestName"; |
| 45 | private final static String DEVICE_ALIAS = "TestAlias"; |
| 46 | private final static String DEVICE_ADDRESS = "AA:BB:CC:DD:EE:FF"; |
| 47 | private final static String DEVICE_ALIAS_NEW = "TestAliasNew"; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 48 | @Mock |
| 49 | private LocalBluetoothAdapter mAdapter; |
| 50 | @Mock |
| 51 | private LocalBluetoothProfileManager mProfileManager; |
| 52 | @Mock |
| 53 | private HeadsetProfile mHfpProfile; |
| 54 | @Mock |
| 55 | private A2dpProfile mA2dpProfile; |
| 56 | @Mock |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 57 | private PanProfile mPanProfile; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 58 | @Mock |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 59 | private HearingAidProfile mHearingAidProfile; |
| 60 | @Mock |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 61 | private BluetoothDevice mDevice; |
| 62 | private CachedBluetoothDevice mCachedDevice; |
| 63 | private Context mContext; |
| 64 | private int mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() { |
| 68 | MockitoAnnotations.initMocks(this); |
| 69 | mContext = RuntimeEnvironment.application; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 70 | when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 71 | when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); |
| 72 | when(mHfpProfile.isProfileReady()).thenReturn(true); |
| 73 | when(mA2dpProfile.isProfileReady()).thenReturn(true); |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 74 | when(mPanProfile.isProfileReady()).thenReturn(true); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 75 | when(mHearingAidProfile.isProfileReady()).thenReturn(true); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 76 | mCachedDevice = spy( |
| 77 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice)); |
| 78 | doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel(); |
| 79 | } |
| 80 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 81 | @Test |
| 82 | public void testGetConnectionSummary_testSingleProfileConnectDisconnect() { |
| 83 | // Test without battery level |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 84 | // Set PAN profile to be connected and test connection state summary |
| 85 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 86 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 87 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 88 | // Set PAN profile to be disconnected and test connection state summary |
| 89 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 90 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 91 | |
| 92 | // Test with battery level |
| 93 | mBatteryLevel = 10; |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 94 | // Set PAN profile to be connected and test connection state summary |
| 95 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 96 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, battery 10%"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 97 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 98 | // Set PAN profile to be disconnected and test connection state summary |
| 99 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 100 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 101 | |
| 102 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 103 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 104 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 105 | // Set PAN profile to be connected and test connection state summary |
| 106 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 107 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 108 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 109 | // Set PAN profile to be disconnected and test connection state summary |
| 110 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 111 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testGetConnectionSummary_testMultipleProfileConnectDisconnect() { |
| 116 | mBatteryLevel = 10; |
| 117 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 118 | // Set HFP, A2DP and PAN profile to be connected and test connection state summary |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 119 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 120 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 121 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 122 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, battery 10%"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 123 | |
| 124 | // Disconnect HFP only and test connection state summary |
| 125 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 126 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 127 | "Connected (no phone), battery 10%"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 128 | |
| 129 | // Disconnect A2DP only and test connection state summary |
| 130 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 131 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 132 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 133 | "Connected (no media), battery 10%"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 134 | |
| 135 | // Disconnect both HFP and A2DP and test connection state summary |
| 136 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 137 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 138 | "Connected (no phone or media), battery 10%"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 139 | |
| 140 | // Disconnect all profiles and test connection state summary |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 141 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 142 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 143 | } |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 144 | |
| 145 | @Test |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 146 | public void testGetConnectionSummary_testSingleProfileActiveDeviceA2dp() { |
| 147 | // Test without battery level |
| 148 | // Set A2DP profile to be connected and test connection state summary |
| 149 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 150 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected"); |
| 151 | |
| 152 | // Set device as Active for A2DP and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 153 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 154 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active(media)"); |
| 155 | |
| 156 | // Test with battery level |
| 157 | mBatteryLevel = 10; |
| 158 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 159 | "Connected, battery 10%, active(media)"); |
| 160 | |
| 161 | // Set A2DP profile to be disconnected and test connection state summary |
| 162 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 163 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 164 | |
| 165 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 166 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 167 | // Set A2DP profile to be connected, Active and test connection state summary |
| 168 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 169 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 170 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active(media)"); |
| 171 | |
| 172 | // Set A2DP profile to be disconnected and test connection state summary |
| 173 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 174 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testGetConnectionSummary_testSingleProfileActiveDeviceHfp() { |
| 179 | // Test without battery level |
| 180 | // Set HFP profile to be connected and test connection state summary |
| 181 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 182 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected"); |
| 183 | |
| 184 | // Set device as Active for HFP and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 185 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 186 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active(phone)"); |
| 187 | |
| 188 | // Test with battery level |
| 189 | mBatteryLevel = 10; |
| 190 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 191 | "Connected, battery 10%, active(phone)"); |
| 192 | |
| 193 | // Set HFP profile to be disconnected and test connection state summary |
| 194 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 195 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 196 | |
| 197 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 198 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 199 | // Set HFP profile to be connected, Active and test connection state summary |
| 200 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 201 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 202 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active(phone)"); |
| 203 | |
| 204 | // Set HFP profile to be disconnected and test connection state summary |
| 205 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 206 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 207 | } |
| 208 | |
| 209 | @Test |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 210 | public void testGetConnectionSummary_testSingleProfileActiveDeviceHearingAid() { |
| 211 | // Test without battery level |
| 212 | // Set Hearing Aid profile to be connected and test connection state summary |
| 213 | mCachedDevice.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); |
| 214 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected"); |
| 215 | |
| 216 | // Set device as Active for Hearing Aid and test connection state summary |
| 217 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID); |
| 218 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active"); |
| 219 | |
| 220 | // Set Hearing Aid profile to be disconnected and test connection state summary |
| 221 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEARING_AID); |
| 222 | mCachedDevice.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 223 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 224 | } |
| 225 | |
| 226 | @Test |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 227 | public void testGetConnectionSummary_testMultipleProfilesActiveDevice() { |
| 228 | // Test without battery level |
| 229 | // Set A2DP and HFP profiles to be connected and test connection state summary |
| 230 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 231 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 232 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected"); |
| 233 | |
| 234 | // Set device as Active for A2DP and HFP and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 235 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 236 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 237 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active"); |
| 238 | |
| 239 | // Test with battery level |
| 240 | mBatteryLevel = 10; |
| 241 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 242 | "Connected, battery 10%, active"); |
| 243 | |
| 244 | // Disconnect A2DP only and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 245 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.A2DP); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 246 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 247 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 248 | "Connected (no media), battery 10%, active(phone)"); |
| 249 | |
| 250 | // Disconnect HFP only and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 251 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEADSET); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 252 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 253 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 254 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 255 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
| 256 | "Connected (no phone), battery 10%, active(media)"); |
| 257 | |
| 258 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 259 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 260 | // Set A2DP and HFP profiles to be connected, Active and test connection state summary |
| 261 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 262 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 263 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 264 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 265 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Connected, active"); |
| 266 | |
| 267 | // Set A2DP and HFP profiles to be disconnected and test connection state summary |
| 268 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 269 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 270 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 271 | } |
| 272 | |
| 273 | @Test |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 274 | public void testDeviceName_testAliasNameAvailable() { |
| 275 | when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS); |
| 276 | when(mDevice.getName()).thenReturn(DEVICE_NAME); |
| 277 | CachedBluetoothDevice cachedBluetoothDevice = |
| 278 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); |
| 279 | // Verify alias is returned on getName |
| 280 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS); |
| 281 | // Verify device is visible |
| 282 | assertThat(cachedBluetoothDevice.hasHumanReadableName()).isTrue(); |
| 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public void testDeviceName_testNameNotAvailable() { |
| 287 | CachedBluetoothDevice cachedBluetoothDevice = |
| 288 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); |
| 289 | // Verify device address is returned on getName |
| 290 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ADDRESS); |
| 291 | // Verify device is not visible |
| 292 | assertThat(cachedBluetoothDevice.hasHumanReadableName()).isFalse(); |
| 293 | } |
| 294 | |
| 295 | @Test |
| 296 | public void testDeviceName_testRenameDevice() { |
| 297 | final String[] alias = {DEVICE_ALIAS}; |
| 298 | doAnswer(invocation -> alias[0]).when(mDevice).getAliasName(); |
| 299 | doAnswer(invocation -> { |
| 300 | alias[0] = (String) invocation.getArguments()[0]; |
| 301 | return true; |
| 302 | }).when(mDevice).setAlias(anyString()); |
| 303 | when(mDevice.getName()).thenReturn(DEVICE_NAME); |
| 304 | CachedBluetoothDevice cachedBluetoothDevice = |
| 305 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); |
| 306 | // Verify alias is returned on getName |
| 307 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS); |
| 308 | // Verify null name does not get set |
| 309 | cachedBluetoothDevice.setName(null); |
| 310 | verify(mDevice, never()).setAlias(any()); |
| 311 | // Verify new name is set properly |
| 312 | cachedBluetoothDevice.setName(DEVICE_ALIAS_NEW); |
| 313 | verify(mDevice).setAlias(DEVICE_ALIAS_NEW); |
| 314 | // Verify new alias is returned on getName |
| 315 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS_NEW); |
| 316 | } |
Hansong Zhang | 6a41632 | 2018-03-19 18:20:38 -0700 | [diff] [blame] | 317 | |
| 318 | @Test |
| 319 | public void testSetActive() { |
| 320 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 321 | when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); |
| 322 | when(mA2dpProfile.setActiveDevice(any(BluetoothDevice.class))).thenReturn(true); |
| 323 | when(mHfpProfile.setActiveDevice(any(BluetoothDevice.class))).thenReturn(true); |
| 324 | |
| 325 | assertThat(mCachedDevice.setActive()).isFalse(); |
| 326 | |
| 327 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 328 | assertThat(mCachedDevice.setActive()).isTrue(); |
| 329 | |
| 330 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 331 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 332 | assertThat(mCachedDevice.setActive()).isTrue(); |
| 333 | |
| 334 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 335 | assertThat(mCachedDevice.setActive()).isFalse(); |
| 336 | } |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 337 | |
| 338 | @Test |
| 339 | public void testIsA2dpDevice_isA2dpDevice() { |
| 340 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 341 | when(mA2dpProfile.getConnectionStatus(mDevice)). |
| 342 | thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 343 | |
| 344 | assertThat(mCachedDevice.isA2dpDevice()).isTrue(); |
| 345 | } |
| 346 | |
| 347 | @Test |
| 348 | public void testIsA2dpDevice_isNotA2dpDevice() { |
| 349 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 350 | when(mA2dpProfile.getConnectionStatus(mDevice)). |
| 351 | thenReturn(BluetoothProfile.STATE_DISCONNECTING); |
| 352 | |
| 353 | assertThat(mCachedDevice.isA2dpDevice()).isFalse(); |
| 354 | } |
| 355 | |
| 356 | @Test |
| 357 | public void testIsHfpDevice_isHfpDevice() { |
| 358 | when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); |
| 359 | when(mHfpProfile.getConnectionStatus(mDevice)). |
| 360 | thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 361 | |
| 362 | assertThat(mCachedDevice.isHfpDevice()).isTrue(); |
| 363 | } |
| 364 | |
| 365 | @Test |
| 366 | public void testIsHfpDevice_isNotHfpDevice() { |
| 367 | when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); |
| 368 | when(mHfpProfile.getConnectionStatus(mDevice)). |
| 369 | thenReturn(BluetoothProfile.STATE_DISCONNECTING); |
| 370 | |
| 371 | assertThat(mCachedDevice.isHfpDevice()).isFalse(); |
| 372 | } |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 373 | } |