blob: 16a292b06234af5707aebc6fee8ef3a09adb25cf [file] [log] [blame]
Jack He6258aae2017-06-29 17:01:23 -07001/*
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 */
16package com.android.settingslib.bluetooth;
17
18import static com.google.common.truth.Truth.assertThat;
ryanywline26aecd2018-05-15 14:20:50 +080019
Jack Hec219bc92017-07-24 14:55:59 -070020import static org.mockito.Matchers.any;
21import static org.mockito.Matchers.anyString;
Jack He6258aae2017-06-29 17:01:23 -070022import static org.mockito.Mockito.doAnswer;
Jack Hec219bc92017-07-24 14:55:59 -070023import static org.mockito.Mockito.never;
Jack He6258aae2017-06-29 17:01:23 -070024import static org.mockito.Mockito.spy;
Jack Hec219bc92017-07-24 14:55:59 -070025import static org.mockito.Mockito.verify;
Jack He6258aae2017-06-29 17:01:23 -070026import static org.mockito.Mockito.when;
timhypengf0509322018-03-29 14:23:21 +080027import static org.robolectric.Shadows.shadowOf;
Jack He6258aae2017-06-29 17:01:23 -070028
29import android.bluetooth.BluetoothAdapter;
30import android.bluetooth.BluetoothDevice;
Stanley Tngc1d5c4f2018-09-30 22:43:06 -070031import android.bluetooth.BluetoothHearingAid;
Jack He6258aae2017-06-29 17:01:23 -070032import android.bluetooth.BluetoothProfile;
33import android.content.Context;
timhypengf0509322018-03-29 14:23:21 +080034import android.media.AudioManager;
35
36import com.android.settingslib.SettingsLibRobolectricTestRunner;
Jack He6258aae2017-06-29 17:01:23 -070037
Jack He6258aae2017-06-29 17:01:23 -070038import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
Jack He6258aae2017-06-29 17:01:23 -070043import org.robolectric.RuntimeEnvironment;
timhypengf0509322018-03-29 14:23:21 +080044import org.robolectric.shadows.ShadowAudioManager;
Jack He6258aae2017-06-29 17:01:23 -070045
timhypengf0509322018-03-29 14:23:21 +080046@RunWith(SettingsLibRobolectricTestRunner.class)
Jack He6258aae2017-06-29 17:01:23 -070047public class CachedBluetoothDeviceTest {
Jack Hec219bc92017-07-24 14:55:59 -070048 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 He6258aae2017-06-29 17:01:23 -070052 @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 Zhang1c1bc252017-10-30 16:38:16 -070061 private PanProfile mPanProfile;
Jack He6258aae2017-06-29 17:01:23 -070062 @Mock
Hansong Zhangd7b35912018-03-16 09:15:48 -070063 private HearingAidProfile mHearingAidProfile;
64 @Mock
Jack He6258aae2017-06-29 17:01:23 -070065 private BluetoothDevice mDevice;
66 private CachedBluetoothDevice mCachedDevice;
timhypengf0509322018-03-29 14:23:21 +080067 private ShadowAudioManager mShadowAudioManager;
Jack He6258aae2017-06-29 17:01:23 -070068 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;
timhypengf0509322018-03-29 14:23:21 +080075 mShadowAudioManager = shadowOf(mContext.getSystemService(AudioManager.class));
Jack Hec219bc92017-07-24 14:55:59 -070076 when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS);
Jack He6258aae2017-06-29 17:01:23 -070077 when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
78 when(mHfpProfile.isProfileReady()).thenReturn(true);
79 when(mA2dpProfile.isProfileReady()).thenReturn(true);
Hansong Zhang1c1bc252017-10-30 16:38:16 -070080 when(mPanProfile.isProfileReady()).thenReturn(true);
Hansong Zhangd7b35912018-03-16 09:15:48 -070081 when(mHearingAidProfile.isProfileReady()).thenReturn(true);
Jack He6258aae2017-06-29 17:01:23 -070082 mCachedDevice = spy(
83 new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice));
84 doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel();
85 }
86
Jack He6258aae2017-06-29 17:01:23 -070087 @Test
88 public void testGetConnectionSummary_testSingleProfileConnectDisconnect() {
89 // Test without battery level
Hansong Zhang1c1bc252017-10-30 16:38:16 -070090 // Set PAN profile to be connected and test connection state summary
91 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED);
timhypengf0509322018-03-29 14:23:21 +080092 assertThat(mCachedDevice.getConnectionSummary()).isNull();
Jack He6258aae2017-06-29 17:01:23 -070093
Hansong Zhang1c1bc252017-10-30 16:38:16 -070094 // Set PAN profile to be disconnected and test connection state summary
95 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED);
Jack He6258aae2017-06-29 17:01:23 -070096 assertThat(mCachedDevice.getConnectionSummary()).isNull();
97
98 // Test with battery level
99 mBatteryLevel = 10;
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700100 // Set PAN profile to be connected and test connection state summary
101 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED);
timhypengf0509322018-03-29 14:23:21 +0800102 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
Jack He6258aae2017-06-29 17:01:23 -0700103
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700104 // Set PAN profile to be disconnected and test connection state summary
105 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED);
Jack He6258aae2017-06-29 17:01:23 -0700106 assertThat(mCachedDevice.getConnectionSummary()).isNull();
107
108 // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level
109 mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN;
110
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700111 // Set PAN profile to be connected and test connection state summary
112 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED);
timhypengf0509322018-03-29 14:23:21 +0800113 assertThat(mCachedDevice.getConnectionSummary()).isNull();
Jack He6258aae2017-06-29 17:01:23 -0700114
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700115 // Set PAN profile to be disconnected and test connection state summary
116 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED);
Jack He6258aae2017-06-29 17:01:23 -0700117 assertThat(mCachedDevice.getConnectionSummary()).isNull();
118 }
119
120 @Test
121 public void testGetConnectionSummary_testMultipleProfileConnectDisconnect() {
122 mBatteryLevel = 10;
123
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700124 // Set HFP, A2DP and PAN profile to be connected and test connection state summary
Jack He6258aae2017-06-29 17:01:23 -0700125 mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
126 mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700127 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED);
timhypengf0509322018-03-29 14:23:21 +0800128 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
Jack He6258aae2017-06-29 17:01:23 -0700129
130 // Disconnect HFP only and test connection state summary
131 mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED);
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800132 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800133 "10% battery");
Jack He6258aae2017-06-29 17:01:23 -0700134
135 // Disconnect A2DP only and test connection state summary
136 mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
137 mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED);
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800138 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800139 "10% battery");
Jack He6258aae2017-06-29 17:01:23 -0700140
141 // Disconnect both HFP and A2DP and test connection state summary
142 mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED);
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800143 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800144 "10% battery");
Jack He6258aae2017-06-29 17:01:23 -0700145
146 // Disconnect all profiles and test connection state summary
Hansong Zhang1c1bc252017-10-30 16:38:16 -0700147 mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED);
Jack He6258aae2017-06-29 17:01:23 -0700148 assertThat(mCachedDevice.getConnectionSummary()).isNull();
149 }
Jack Hec219bc92017-07-24 14:55:59 -0700150
151 @Test
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800152 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);
timhypengf0509322018-03-29 14:23:21 +0800156 assertThat(mCachedDevice.getConnectionSummary()).isNull();
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800157
158 // Set device as Active for A2DP and test connection state summary
Pavlin Radoslavovc285d552018-02-06 16:14:00 -0800159 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
timhypengf0509322018-03-29 14:23:21 +0800160 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800161
162 // Test with battery level
163 mBatteryLevel = 10;
ryanywline26aecd2018-05-15 14:20:50 +0800164 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800165 "Active, 10% battery");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800166
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 Radoslavovc285d552018-02-06 16:14:00 -0800175 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
timhypengf0509322018-03-29 14:23:21 +0800176 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800177
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);
timhypengf0509322018-03-29 14:23:21 +0800188 assertThat(mCachedDevice.getConnectionSummary()).isNull();
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800189
190 // Set device as Active for HFP and test connection state summary
timhypengf0509322018-03-29 14:23:21 +0800191 mCachedDevice.onAudioModeChanged();
192 mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
Pavlin Radoslavovc285d552018-02-06 16:14:00 -0800193 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET);
timhypengf0509322018-03-29 14:23:21 +0800194 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800195
196 // Test with battery level
197 mBatteryLevel = 10;
ryanywline26aecd2018-05-15 14:20:50 +0800198 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800199 "Active, 10% battery");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800200
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 Radoslavovc285d552018-02-06 16:14:00 -0800209 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET);
timhypengf0509322018-03-29 14:23:21 +0800210 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800211
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 Zhangd7b35912018-03-16 09:15:48 -0700218 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);
timhypengf0509322018-03-29 14:23:21 +0800222 assertThat(mCachedDevice.getConnectionSummary()).isNull();
Hansong Zhangd7b35912018-03-16 09:15:48 -0700223
224 // Set device as Active for Hearing Aid and test connection state summary
225 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
timhypengf0509322018-03-29 14:23:21 +0800226 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Hansong Zhangd7b35912018-03-16 09:15:48 -0700227
228 // Set Hearing Aid profile to be disconnected and test connection state summary
229 mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEARING_AID);
timhypengf0509322018-03-29 14:23:21 +0800230 mCachedDevice.
231 onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_DISCONNECTED);
Hansong Zhangd7b35912018-03-16 09:15:48 -0700232 assertThat(mCachedDevice.getConnectionSummary()).isNull();
233 }
234
235 @Test
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800236 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);
timhypengf0509322018-03-29 14:23:21 +0800241 assertThat(mCachedDevice.getConnectionSummary()).isNull();
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800242
243 // Set device as Active for A2DP and HFP and test connection state summary
Pavlin Radoslavovc285d552018-02-06 16:14:00 -0800244 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
245 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET);
timhypengf0509322018-03-29 14:23:21 +0800246 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800247
248 // Test with battery level
249 mBatteryLevel = 10;
250 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800251 "Active, 10% battery");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800252
253 // Disconnect A2DP only and test connection state summary
Pavlin Radoslavovc285d552018-02-06 16:14:00 -0800254 mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.A2DP);
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800255 mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED);
256 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800257 "10% battery");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800258
259 // Disconnect HFP only and test connection state summary
Pavlin Radoslavovc285d552018-02-06 16:14:00 -0800260 mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEADSET);
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800261 mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED);
262 mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
Pavlin Radoslavovc285d552018-02-06 16:14:00 -0800263 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800264 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(
timhypengf0509322018-03-29 14:23:21 +0800265 "Active, 10% battery");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800266
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 Radoslavovc285d552018-02-06 16:14:00 -0800272 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
273 mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET);
timhypengf0509322018-03-29 14:23:21 +0800274 assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
Pavlin Radoslavove6e080f2018-02-06 12:21:34 -0800275
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
ryanywline26aecd2018-05-15 14:20:50 +0800283 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 Hec219bc92017-07-24 14:55:59 -0700477 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 Zhang6a416322018-03-19 18:20:38 -0700520
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 }
hughchen23b947e2018-03-31 17:32:53 +0800540
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 Tngc1d5c4f2018-09-30 22:43:06 -0700576
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 He6258aae2017-06-29 17:01:23 -0700588}