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