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; |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 19 | |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 20 | import static org.mockito.Matchers.any; |
| 21 | import static org.mockito.Matchers.anyString; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.doAnswer; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.never; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.spy; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.verify; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.when; |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 27 | import static org.robolectric.Shadows.shadowOf; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 28 | |
| 29 | import android.bluetooth.BluetoothAdapter; |
| 30 | import android.bluetooth.BluetoothDevice; |
Stanley Tng | c1d5c4f | 2018-09-30 22:43:06 -0700 | [diff] [blame] | 31 | import android.bluetooth.BluetoothHearingAid; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 32 | import android.bluetooth.BluetoothProfile; |
| 33 | import android.content.Context; |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 34 | import android.media.AudioManager; |
| 35 | |
| 36 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 37 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 43 | import org.robolectric.RuntimeEnvironment; |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 44 | import org.robolectric.shadows.ShadowAudioManager; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 45 | |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 46 | @RunWith(SettingsLibRobolectricTestRunner.class) |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 47 | public class CachedBluetoothDeviceTest { |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 48 | private final static String DEVICE_NAME = "TestName"; |
| 49 | private final static String DEVICE_ALIAS = "TestAlias"; |
| 50 | private final static String DEVICE_ADDRESS = "AA:BB:CC:DD:EE:FF"; |
| 51 | private final static String DEVICE_ALIAS_NEW = "TestAliasNew"; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 52 | @Mock |
| 53 | private LocalBluetoothAdapter mAdapter; |
| 54 | @Mock |
| 55 | private LocalBluetoothProfileManager mProfileManager; |
| 56 | @Mock |
| 57 | private HeadsetProfile mHfpProfile; |
| 58 | @Mock |
| 59 | private A2dpProfile mA2dpProfile; |
| 60 | @Mock |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 61 | private PanProfile mPanProfile; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 62 | @Mock |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 63 | private HearingAidProfile mHearingAidProfile; |
| 64 | @Mock |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 65 | private BluetoothDevice mDevice; |
| 66 | private CachedBluetoothDevice mCachedDevice; |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 67 | private ShadowAudioManager mShadowAudioManager; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 68 | private Context mContext; |
| 69 | private int mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 70 | |
| 71 | @Before |
| 72 | public void setUp() { |
| 73 | MockitoAnnotations.initMocks(this); |
| 74 | mContext = RuntimeEnvironment.application; |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 75 | mShadowAudioManager = shadowOf(mContext.getSystemService(AudioManager.class)); |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 76 | when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 77 | when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); |
| 78 | when(mHfpProfile.isProfileReady()).thenReturn(true); |
| 79 | when(mA2dpProfile.isProfileReady()).thenReturn(true); |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 80 | when(mPanProfile.isProfileReady()).thenReturn(true); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 81 | when(mHearingAidProfile.isProfileReady()).thenReturn(true); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 82 | mCachedDevice = spy( |
| 83 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice)); |
| 84 | doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel(); |
| 85 | } |
| 86 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 87 | @Test |
| 88 | public void testGetConnectionSummary_testSingleProfileConnectDisconnect() { |
| 89 | // Test without battery level |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 90 | // Set PAN profile to be connected and test connection state summary |
| 91 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 92 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 93 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 94 | // Set PAN profile to be disconnected and test connection state summary |
| 95 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 96 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 97 | |
| 98 | // Test with battery level |
| 99 | mBatteryLevel = 10; |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 100 | // Set PAN profile to be connected and test connection state summary |
| 101 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 102 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 103 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 104 | // Set PAN profile to be disconnected and test connection state summary |
| 105 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 106 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 107 | |
| 108 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 109 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 110 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 111 | // Set PAN profile to be connected and test connection state summary |
| 112 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 113 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 114 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 115 | // Set PAN profile to be disconnected and test connection state summary |
| 116 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 117 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testGetConnectionSummary_testMultipleProfileConnectDisconnect() { |
| 122 | mBatteryLevel = 10; |
| 123 | |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 124 | // 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] | 125 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 126 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 127 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 128 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 129 | |
| 130 | // Disconnect HFP only and test connection state summary |
| 131 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 132 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 133 | "10% battery"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 134 | |
| 135 | // Disconnect A2DP only and test connection state summary |
| 136 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 137 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 138 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 139 | "10% battery"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 140 | |
| 141 | // Disconnect both HFP and A2DP and test connection state summary |
| 142 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 143 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 144 | "10% battery"); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 145 | |
| 146 | // Disconnect all profiles and test connection state summary |
Hansong Zhang | 1c1bc25 | 2017-10-30 16:38:16 -0700 | [diff] [blame] | 147 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 148 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 149 | } |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 150 | |
| 151 | @Test |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 152 | public void testGetConnectionSummary_testSingleProfileActiveDeviceA2dp() { |
| 153 | // Test without battery level |
| 154 | // Set A2DP profile to be connected and test connection state summary |
| 155 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 156 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 157 | |
| 158 | // Set device as Active for A2DP and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 159 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 160 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 161 | |
| 162 | // Test with battery level |
| 163 | mBatteryLevel = 10; |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 164 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 165 | "Active, 10% battery"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 166 | |
| 167 | // Set A2DP profile to be disconnected and test connection state summary |
| 168 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 169 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 170 | |
| 171 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 172 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 173 | // Set A2DP profile to be connected, Active and test connection state summary |
| 174 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 175 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 176 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 177 | |
| 178 | // Set A2DP profile to be disconnected and test connection state summary |
| 179 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 180 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testGetConnectionSummary_testSingleProfileActiveDeviceHfp() { |
| 185 | // Test without battery level |
| 186 | // Set HFP profile to be connected and test connection state summary |
| 187 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 188 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 189 | |
| 190 | // Set device as Active for HFP and test connection state summary |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 191 | mCachedDevice.onAudioModeChanged(); |
| 192 | mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 193 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 194 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 195 | |
| 196 | // Test with battery level |
| 197 | mBatteryLevel = 10; |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 198 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 199 | "Active, 10% battery"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 200 | |
| 201 | // Set HFP profile to be disconnected and test connection state summary |
| 202 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 203 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 204 | |
| 205 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 206 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 207 | // Set HFP profile to be connected, Active and test connection state summary |
| 208 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 209 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 210 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 211 | |
| 212 | // Set HFP profile to be disconnected and test connection state summary |
| 213 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 214 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 215 | } |
| 216 | |
| 217 | @Test |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 218 | public void testGetConnectionSummary_testSingleProfileActiveDeviceHearingAid() { |
| 219 | // Test without battery level |
| 220 | // Set Hearing Aid profile to be connected and test connection state summary |
| 221 | mCachedDevice.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 222 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 223 | |
| 224 | // Set device as Active for Hearing Aid and test connection state summary |
| 225 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 226 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 227 | |
| 228 | // Set Hearing Aid profile to be disconnected and test connection state summary |
| 229 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEARING_AID); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 230 | mCachedDevice. |
| 231 | onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_DISCONNECTED); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 232 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 233 | } |
| 234 | |
| 235 | @Test |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 236 | public void testGetConnectionSummary_testMultipleProfilesActiveDevice() { |
| 237 | // Test without battery level |
| 238 | // Set A2DP and HFP profiles to be connected and test connection state summary |
| 239 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 240 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 241 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 242 | |
| 243 | // 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] | 244 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 245 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 246 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 247 | |
| 248 | // Test with battery level |
| 249 | mBatteryLevel = 10; |
| 250 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 251 | "Active, 10% battery"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 252 | |
| 253 | // Disconnect A2DP only and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 254 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.A2DP); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 255 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 256 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 257 | "10% battery"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 258 | |
| 259 | // Disconnect HFP only and test connection state summary |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 260 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEADSET); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 261 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 262 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 263 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 264 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 265 | "Active, 10% battery"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 266 | |
| 267 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 268 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 269 | // Set A2DP and HFP profiles to be connected, Active and test connection state summary |
| 270 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 271 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 272 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 273 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 274 | assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active"); |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 275 | |
| 276 | // Set A2DP and HFP profiles to be disconnected and test connection state summary |
| 277 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 278 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 279 | assertThat(mCachedDevice.getConnectionSummary()).isNull(); |
| 280 | } |
| 281 | |
| 282 | @Test |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 283 | public void getCarConnectionSummary_singleProfileConnectDisconnect() { |
| 284 | // Test without battery level |
| 285 | // Set PAN profile to be connected and test connection state summary |
| 286 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
| 287 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); |
| 288 | |
| 289 | // Set PAN profile to be disconnected and test connection state summary |
| 290 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 291 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 292 | |
| 293 | // Test with battery level |
| 294 | mBatteryLevel = 10; |
| 295 | // Set PAN profile to be connected and test connection state summary |
| 296 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
| 297 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, battery 10%"); |
| 298 | |
| 299 | // Set PAN profile to be disconnected and test connection state summary |
| 300 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 301 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 302 | |
| 303 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 304 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 305 | |
| 306 | // Set PAN profile to be connected and test connection state summary |
| 307 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
| 308 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); |
| 309 | |
| 310 | // Set PAN profile to be disconnected and test connection state summary |
| 311 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 312 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 313 | } |
| 314 | |
| 315 | @Test |
| 316 | public void getCarConnectionSummary_multipleProfileConnectDisconnect() { |
| 317 | mBatteryLevel = 10; |
| 318 | |
| 319 | // Set HFP, A2DP and PAN profile to be connected and test connection state summary |
| 320 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 321 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 322 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); |
| 323 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, battery 10%"); |
| 324 | |
| 325 | // Disconnect HFP only and test connection state summary |
| 326 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 327 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 328 | "Connected (no phone), battery 10%"); |
| 329 | |
| 330 | // Disconnect A2DP only and test connection state summary |
| 331 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 332 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 333 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 334 | "Connected (no media), battery 10%"); |
| 335 | |
| 336 | // Disconnect both HFP and A2DP and test connection state summary |
| 337 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 338 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 339 | "Connected (no phone or media), battery 10%"); |
| 340 | |
| 341 | // Disconnect all profiles and test connection state summary |
| 342 | mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 343 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 344 | } |
| 345 | |
| 346 | @Test |
| 347 | public void getCarConnectionSummary_singleProfileActiveDeviceA2dp() { |
| 348 | // Test without battery level |
| 349 | // Set A2DP profile to be connected and test connection state summary |
| 350 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 351 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); |
| 352 | |
| 353 | // Set device as Active for A2DP and test connection state summary |
| 354 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 355 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (media)"); |
| 356 | |
| 357 | // Test with battery level |
| 358 | mBatteryLevel = 10; |
| 359 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 360 | "Connected, battery 10%, active (media)"); |
| 361 | |
| 362 | // Set A2DP profile to be disconnected and test connection state summary |
| 363 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 364 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 365 | |
| 366 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 367 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 368 | // Set A2DP profile to be connected, Active and test connection state summary |
| 369 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 370 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 371 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (media)"); |
| 372 | |
| 373 | // Set A2DP profile to be disconnected and test connection state summary |
| 374 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 375 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void getCarConnectionSummary_singleProfileActiveDeviceHfp() { |
| 380 | // Test without battery level |
| 381 | // Set HFP profile to be connected and test connection state summary |
| 382 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 383 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); |
| 384 | |
| 385 | // Set device as Active for HFP and test connection state summary |
| 386 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
| 387 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (phone)"); |
| 388 | |
| 389 | // Test with battery level |
| 390 | mBatteryLevel = 10; |
| 391 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 392 | "Connected, battery 10%, active (phone)"); |
| 393 | |
| 394 | // Set HFP profile to be disconnected and test connection state summary |
| 395 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 396 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 397 | |
| 398 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 399 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 400 | // Set HFP profile to be connected, Active and test connection state summary |
| 401 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 402 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
| 403 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (phone)"); |
| 404 | |
| 405 | // Set HFP profile to be disconnected and test connection state summary |
| 406 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 407 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 408 | } |
| 409 | |
| 410 | @Test |
| 411 | public void getCarConnectionSummary_singleProfileActiveDeviceHearingAid() { |
| 412 | // Test without battery level |
| 413 | // Set Hearing Aid profile to be connected and test connection state summary |
| 414 | mCachedDevice.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); |
| 415 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); |
| 416 | |
| 417 | // Set device as Active for Hearing Aid and test connection state summary |
| 418 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID); |
| 419 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active"); |
| 420 | |
| 421 | // Set Hearing Aid profile to be disconnected and test connection state summary |
| 422 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEARING_AID); |
| 423 | mCachedDevice.onProfileStateChanged(mHearingAidProfile, |
| 424 | BluetoothProfile.STATE_DISCONNECTED); |
| 425 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 426 | } |
| 427 | |
| 428 | @Test |
| 429 | public void getCarConnectionSummary_multipleProfilesActiveDevice() { |
| 430 | // Test without battery level |
| 431 | // Set A2DP and HFP profiles to be connected and test connection state summary |
| 432 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 433 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 434 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); |
| 435 | |
| 436 | // Set device as Active for A2DP and HFP and test connection state summary |
| 437 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 438 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
| 439 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active"); |
| 440 | |
| 441 | // Test with battery level |
| 442 | mBatteryLevel = 10; |
| 443 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 444 | "Connected, battery 10%, active"); |
| 445 | |
| 446 | // Disconnect A2DP only and test connection state summary |
| 447 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.A2DP); |
| 448 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 449 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 450 | "Connected (no media), battery 10%, active (phone)"); |
| 451 | |
| 452 | // Disconnect HFP only and test connection state summary |
| 453 | mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEADSET); |
| 454 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 455 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 456 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 457 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( |
| 458 | "Connected (no phone), battery 10%, active (media)"); |
| 459 | |
| 460 | // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level |
| 461 | mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; |
| 462 | // Set A2DP and HFP profiles to be connected, Active and test connection state summary |
| 463 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 464 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 465 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); |
| 466 | mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); |
| 467 | assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active"); |
| 468 | |
| 469 | // Set A2DP and HFP profiles to be disconnected and test connection state summary |
| 470 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 471 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 472 | assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | @Test |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 477 | public void testDeviceName_testAliasNameAvailable() { |
| 478 | when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS); |
| 479 | when(mDevice.getName()).thenReturn(DEVICE_NAME); |
| 480 | CachedBluetoothDevice cachedBluetoothDevice = |
| 481 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); |
| 482 | // Verify alias is returned on getName |
| 483 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS); |
| 484 | // Verify device is visible |
| 485 | assertThat(cachedBluetoothDevice.hasHumanReadableName()).isTrue(); |
| 486 | } |
| 487 | |
| 488 | @Test |
| 489 | public void testDeviceName_testNameNotAvailable() { |
| 490 | CachedBluetoothDevice cachedBluetoothDevice = |
| 491 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); |
| 492 | // Verify device address is returned on getName |
| 493 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ADDRESS); |
| 494 | // Verify device is not visible |
| 495 | assertThat(cachedBluetoothDevice.hasHumanReadableName()).isFalse(); |
| 496 | } |
| 497 | |
| 498 | @Test |
| 499 | public void testDeviceName_testRenameDevice() { |
| 500 | final String[] alias = {DEVICE_ALIAS}; |
| 501 | doAnswer(invocation -> alias[0]).when(mDevice).getAliasName(); |
| 502 | doAnswer(invocation -> { |
| 503 | alias[0] = (String) invocation.getArguments()[0]; |
| 504 | return true; |
| 505 | }).when(mDevice).setAlias(anyString()); |
| 506 | when(mDevice.getName()).thenReturn(DEVICE_NAME); |
| 507 | CachedBluetoothDevice cachedBluetoothDevice = |
| 508 | new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); |
| 509 | // Verify alias is returned on getName |
| 510 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS); |
| 511 | // Verify null name does not get set |
| 512 | cachedBluetoothDevice.setName(null); |
| 513 | verify(mDevice, never()).setAlias(any()); |
| 514 | // Verify new name is set properly |
| 515 | cachedBluetoothDevice.setName(DEVICE_ALIAS_NEW); |
| 516 | verify(mDevice).setAlias(DEVICE_ALIAS_NEW); |
| 517 | // Verify new alias is returned on getName |
| 518 | assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS_NEW); |
| 519 | } |
Hansong Zhang | 6a41632 | 2018-03-19 18:20:38 -0700 | [diff] [blame] | 520 | |
| 521 | @Test |
| 522 | public void testSetActive() { |
| 523 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 524 | when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); |
| 525 | when(mA2dpProfile.setActiveDevice(any(BluetoothDevice.class))).thenReturn(true); |
| 526 | when(mHfpProfile.setActiveDevice(any(BluetoothDevice.class))).thenReturn(true); |
| 527 | |
| 528 | assertThat(mCachedDevice.setActive()).isFalse(); |
| 529 | |
| 530 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); |
| 531 | assertThat(mCachedDevice.setActive()).isTrue(); |
| 532 | |
| 533 | mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 534 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); |
| 535 | assertThat(mCachedDevice.setActive()).isTrue(); |
| 536 | |
| 537 | mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); |
| 538 | assertThat(mCachedDevice.setActive()).isFalse(); |
| 539 | } |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 540 | |
| 541 | @Test |
| 542 | public void testIsA2dpDevice_isA2dpDevice() { |
| 543 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 544 | when(mA2dpProfile.getConnectionStatus(mDevice)). |
| 545 | thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 546 | |
| 547 | assertThat(mCachedDevice.isA2dpDevice()).isTrue(); |
| 548 | } |
| 549 | |
| 550 | @Test |
| 551 | public void testIsA2dpDevice_isNotA2dpDevice() { |
| 552 | when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); |
| 553 | when(mA2dpProfile.getConnectionStatus(mDevice)). |
| 554 | thenReturn(BluetoothProfile.STATE_DISCONNECTING); |
| 555 | |
| 556 | assertThat(mCachedDevice.isA2dpDevice()).isFalse(); |
| 557 | } |
| 558 | |
| 559 | @Test |
| 560 | public void testIsHfpDevice_isHfpDevice() { |
| 561 | when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); |
| 562 | when(mHfpProfile.getConnectionStatus(mDevice)). |
| 563 | thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 564 | |
| 565 | assertThat(mCachedDevice.isHfpDevice()).isTrue(); |
| 566 | } |
| 567 | |
| 568 | @Test |
| 569 | public void testIsHfpDevice_isNotHfpDevice() { |
| 570 | when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); |
| 571 | when(mHfpProfile.getConnectionStatus(mDevice)). |
| 572 | thenReturn(BluetoothProfile.STATE_DISCONNECTING); |
| 573 | |
| 574 | assertThat(mCachedDevice.isHfpDevice()).isFalse(); |
| 575 | } |
Stanley Tng | c1d5c4f | 2018-09-30 22:43:06 -0700 | [diff] [blame] | 576 | |
| 577 | @Test |
| 578 | public void testIsHearingAidDevice_isHearingAidDevice() { |
| 579 | mCachedDevice.setHiSyncId(0x1234); |
| 580 | assertThat(mCachedDevice.isHearingAidDevice()).isTrue(); |
| 581 | } |
| 582 | |
| 583 | @Test |
| 584 | public void testIsHearingAidDevice_isNotHearingAidDevice() { |
| 585 | mCachedDevice.setHiSyncId(BluetoothHearingAid.HI_SYNC_ID_INVALID); |
| 586 | assertThat(mCachedDevice.isHearingAidDevice()).isFalse(); |
| 587 | } |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 588 | } |