• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.bluetooth.btservice;
2 
3 import static org.mockito.Mockito.*;
4 
5 import android.bluetooth.BluetoothAdapter;
6 import android.bluetooth.BluetoothAssignedNumbers;
7 import android.bluetooth.BluetoothDevice;
8 import android.bluetooth.BluetoothHeadset;
9 import android.bluetooth.BluetoothProfile;
10 import android.content.Intent;
11 import android.os.HandlerThread;
12 import android.os.Message;
13 import android.os.TestLooperManager;
14 import android.support.test.InstrumentationRegistry;
15 import android.support.test.filters.MediumTest;
16 import android.support.test.runner.AndroidJUnit4;
17 
18 import com.android.bluetooth.Utils;
19 import com.android.bluetooth.hfp.HeadsetHalConstants;
20 
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 
30 import java.util.ArrayList;
31 
32 @MediumTest
33 @RunWith(AndroidJUnit4.class)
34 public class RemoteDevicesTest {
35     private static final String TEST_BT_ADDR_1 = "00:11:22:33:44:55";
36 
37     private ArgumentCaptor<Intent> mIntentArgument = ArgumentCaptor.forClass(Intent.class);
38     private ArgumentCaptor<String> mStringArgument = ArgumentCaptor.forClass(String.class);
39     private BluetoothDevice mDevice1;
40     private RemoteDevices mRemoteDevices;
41     private HandlerThread mHandlerThread;
42     private TestLooperManager mTestLooperManager;
43 
44     @Mock private AdapterService mAdapterService;
45 
46     @Before
setUp()47     public void setUp() {
48         MockitoAnnotations.initMocks(this);
49         mDevice1 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(TEST_BT_ADDR_1);
50         mHandlerThread = new HandlerThread("RemoteDevicesTestHandlerThread");
51         mHandlerThread.start();
52         mTestLooperManager = InstrumentationRegistry.getInstrumentation()
53                 .acquireLooperManager(mHandlerThread.getLooper());
54         mRemoteDevices = new RemoteDevices(mAdapterService, mHandlerThread.getLooper());
55     }
56 
57     @After
tearDown()58     public void tearDown() {
59         mTestLooperManager.release();
60         mHandlerThread.quit();
61     }
62 
63     @Test
testSendUuidIntent()64     public void testSendUuidIntent() {
65         // Verify that a handler message is sent by the method call
66         mRemoteDevices.updateUuids(mDevice1);
67         Message msg = mTestLooperManager.next();
68         Assert.assertNotNull(msg);
69 
70         // Verify that executing that message results in a broadcast intent
71         mTestLooperManager.execute(msg);
72         verify(mAdapterService).sendBroadcast(any(), anyString());
73         verifyNoMoreInteractions(mAdapterService);
74     }
75 
76     @Test
testUpdateBatteryLevel_normalSequence()77     public void testUpdateBatteryLevel_normalSequence() {
78         int batteryLevel = 10;
79 
80         // Verify that device property is null initially
81         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
82 
83         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
84         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
85         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
86         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
87         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
88 
89         // Verify that user can get battery level after the update
90         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
91         Assert.assertEquals(mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel(),
92                 batteryLevel);
93 
94         // Verify that update same battery level for the same device does not trigger intent
95         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
96         verify(mAdapterService).sendBroadcast(any(), anyString());
97 
98         // Verify that updating battery level to different value triggers the intent again
99         batteryLevel = 15;
100         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
101         verify(mAdapterService, times(2)).sendBroadcast(mIntentArgument.capture(),
102                 mStringArgument.capture());
103         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
104         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
105 
106         // Verify that user can get battery level after the update
107         Assert.assertEquals(mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel(),
108                 batteryLevel);
109 
110         verifyNoMoreInteractions(mAdapterService);
111     }
112 
113     @Test
testUpdateBatteryLevel_errorNegativeValue()114     public void testUpdateBatteryLevel_errorNegativeValue() {
115         int batteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN;
116 
117         // Verify that device property is null initially
118         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
119 
120         // Verify that updating with invalid battery level does not trigger the intent
121         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
122         verify(mAdapterService, never()).sendBroadcast(any(), anyString());
123 
124         // Verify that device property stays null after invalid update
125         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
126 
127         verifyNoMoreInteractions(mAdapterService);
128     }
129 
130     @Test
testUpdateBatteryLevel_errorTooLargeValue()131     public void testUpdateBatteryLevel_errorTooLargeValue() {
132         int batteryLevel = 101;
133 
134         // Verify that device property is null initially
135         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
136 
137         // Verify that updating invalid battery level does not trigger the intent
138         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
139         verify(mAdapterService, never()).sendBroadcast(any(), anyString());
140 
141         // Verify that device property stays null after invalid update
142         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
143 
144         verifyNoMoreInteractions(mAdapterService);
145     }
146 
147     @Test
testResetBatteryLevel_testResetBeforeUpdate()148     public void testResetBatteryLevel_testResetBeforeUpdate() {
149         // Verify that device property is null initially
150         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
151 
152         // Verify that resetting battery level keeps device property null
153         mRemoteDevices.resetBatteryLevel(mDevice1);
154         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
155 
156         verifyNoMoreInteractions(mAdapterService);
157     }
158 
159     @Test
testResetBatteryLevel_testResetAfterUpdate()160     public void testResetBatteryLevel_testResetAfterUpdate() {
161         int batteryLevel = 10;
162 
163         // Verify that device property is null initially
164         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
165 
166         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
167         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
168         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
169         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
170         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
171 
172         // Verify that user can get battery level after the update
173         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
174         Assert.assertEquals(mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel(),
175                 batteryLevel);
176 
177         // Verify that resetting battery level changes it back to BluetoothDevice
178         // .BATTERY_LEVEL_UNKNOWN
179         mRemoteDevices.resetBatteryLevel(mDevice1);
180         // Verify BATTERY_LEVEL_CHANGED intent is sent after first reset
181         verify(mAdapterService, times(2)).sendBroadcast(mIntentArgument.capture(),
182                 mStringArgument.capture());
183         verifyBatteryLevelChangedIntent(mDevice1, BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
184                 mIntentArgument);
185         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
186         // Verify value is reset in properties
187         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
188         Assert.assertEquals(mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel(),
189                 BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
190 
191         // Verify no intent is sent after second reset
192         mRemoteDevices.resetBatteryLevel(mDevice1);
193         verify(mAdapterService, times(2)).sendBroadcast(any(), anyString());
194 
195         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent again
196         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
197         verify(mAdapterService, times(3)).sendBroadcast(mIntentArgument.capture(),
198                 mStringArgument.capture());
199         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
200         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
201 
202         verifyNoMoreInteractions(mAdapterService);
203     }
204 
205     @Test
testResetBatteryLevelOnHeadsetStateChange()206     public void testResetBatteryLevelOnHeadsetStateChange() {
207         int batteryLevel = 10;
208 
209         // Verify that device property is null initially
210         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
211 
212         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
213         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
214         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
215         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
216         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
217 
218         // Verify that user can get battery level after the update
219         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
220         Assert.assertEquals(mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel(),
221                 batteryLevel);
222 
223         // Verify that resetting battery level changes it back to BluetoothDevice
224         // .BATTERY_LEVEL_UNKNOWN
225         mRemoteDevices.onHeadsetConnectionStateChanged(
226                 getHeadsetConnectionStateChangedIntent(mDevice1,
227                         BluetoothProfile.STATE_DISCONNECTING, BluetoothProfile.STATE_DISCONNECTED));
228         // Verify BATTERY_LEVEL_CHANGED intent is sent after first reset
229         verify(mAdapterService, times(2)).sendBroadcast(mIntentArgument.capture(),
230                 mStringArgument.capture());
231         verifyBatteryLevelChangedIntent(mDevice1, BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
232                 mIntentArgument);
233         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
234         // Verify value is reset in properties
235         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
236         Assert.assertEquals(mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel(),
237                 BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
238 
239         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent again
240         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
241         verify(mAdapterService, times(3)).sendBroadcast(mIntentArgument.capture(),
242                 mStringArgument.capture());
243         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
244         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
245 
246         verifyNoMoreInteractions(mAdapterService);
247     }
248 
249     @Test
testResetBatteryLevel_testAclStateChangeCallback()250     public void testResetBatteryLevel_testAclStateChangeCallback() {
251         int batteryLevel = 10;
252 
253         // Verify that device property is null initially
254         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
255 
256         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
257         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
258         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
259         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
260         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
261 
262         // Verify that user can get battery level after the update
263         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
264         Assert.assertEquals(batteryLevel,
265                 mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel());
266 
267         // Verify that when device is completely disconnected, RemoteDevices reset battery level to
268         // BluetoothDevice.BATTERY_LEVEL_UNKNOWN
269         when(mAdapterService.getState()).thenReturn(BluetoothAdapter.STATE_ON);
270         mRemoteDevices.aclStateChangeCallback(0, Utils.getByteAddress(mDevice1),
271                 AbstractionLayer.BT_ACL_STATE_DISCONNECTED);
272         verify(mAdapterService).getState();
273         verify(mAdapterService).getConnectionState(mDevice1);
274         // Verify ACTION_ACL_DISCONNECTED and BATTERY_LEVEL_CHANGED intent are sent
275         verify(mAdapterService, times(3)).sendBroadcast(mIntentArgument.capture(),
276                 mStringArgument.capture());
277         verifyBatteryLevelChangedIntent(mDevice1, BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
278                 mIntentArgument.getAllValues().get(mIntentArgument.getAllValues().size() - 2));
279         Assert.assertEquals(AdapterService.BLUETOOTH_PERM,
280                 mStringArgument.getAllValues().get(mStringArgument.getAllValues().size() - 2));
281         Assert.assertEquals(BluetoothDevice.ACTION_ACL_DISCONNECTED,
282                 mIntentArgument.getValue().getAction());
283         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
284         // Verify value is reset in properties
285         Assert.assertNotNull(mRemoteDevices.getDeviceProperties(mDevice1));
286         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
287                 mRemoteDevices.getDeviceProperties(mDevice1).getBatteryLevel());
288 
289         // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent again
290         mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
291         verify(mAdapterService, times(4)).sendBroadcast(mIntentArgument.capture(),
292                 mStringArgument.capture());
293         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
294         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
295 
296         verifyNoMoreInteractions(mAdapterService);
297     }
298 
299     @Test
testHfIndicatorParser_testCorrectValue()300     public void testHfIndicatorParser_testCorrectValue() {
301         int batteryLevel = 10;
302 
303         // Verify that device property is null initially
304         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
305 
306         // Verify that ACTION_HF_INDICATORS_VALUE_CHANGED intent updates battery level
307         mRemoteDevices.onHfIndicatorValueChanged(getHfIndicatorIntent(mDevice1, batteryLevel,
308                 HeadsetHalConstants.HF_INDICATOR_BATTERY_LEVEL_STATUS));
309         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
310         verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
311         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
312     }
313 
314     @Test
testHfIndicatorParser_testWrongIndicatorId()315     public void testHfIndicatorParser_testWrongIndicatorId() {
316         int batteryLevel = 10;
317 
318         // Verify that device property is null initially
319         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
320 
321         // Verify that ACTION_HF_INDICATORS_VALUE_CHANGED intent updates battery level
322         mRemoteDevices.onHfIndicatorValueChanged(getHfIndicatorIntent(mDevice1, batteryLevel, 3));
323         verify(mAdapterService, never()).sendBroadcast(any(), anyString());
324         // Verify that device property is still null after invalid update
325         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
326     }
327 
328     @Test
testOnVendorSpecificHeadsetEvent_testCorrectPlantronicsXEvent()329     public void testOnVendorSpecificHeadsetEvent_testCorrectPlantronicsXEvent() {
330         // Verify that device property is null initially
331         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
332 
333         // Verify that correct ACTION_VENDOR_SPECIFIC_HEADSET_EVENT updates battery level
334         mRemoteDevices.onVendorSpecificHeadsetEvent(getVendorSpecificHeadsetEventIntent(
335                 BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT,
336                 BluetoothAssignedNumbers.PLANTRONICS, BluetoothHeadset.AT_CMD_TYPE_SET,
337                 getXEventArray(3, 8), mDevice1));
338         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
339         verifyBatteryLevelChangedIntent(mDevice1, 37, mIntentArgument);
340         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
341     }
342 
343     @Test
testOnVendorSpecificHeadsetEvent_testCorrectAppleBatteryVsc()344     public void testOnVendorSpecificHeadsetEvent_testCorrectAppleBatteryVsc() {
345         // Verify that device property is null initially
346         Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
347 
348         // Verify that correct ACTION_VENDOR_SPECIFIC_HEADSET_EVENT updates battery level
349         mRemoteDevices.onVendorSpecificHeadsetEvent(getVendorSpecificHeadsetEventIntent(
350                 BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV,
351                 BluetoothAssignedNumbers.APPLE, BluetoothHeadset.AT_CMD_TYPE_SET, new Object[]{
352                         3,
353                         BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL,
354                         5,
355                         2,
356                         1,
357                         3,
358                         10
359                 }, mDevice1));
360         verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture());
361         verifyBatteryLevelChangedIntent(mDevice1, 60, mIntentArgument);
362         Assert.assertEquals(AdapterService.BLUETOOTH_PERM, mStringArgument.getValue());
363     }
364 
365     @Test
testGetBatteryLevelFromXEventVsc()366     public void testGetBatteryLevelFromXEventVsc() {
367         Assert.assertEquals(37, RemoteDevices.getBatteryLevelFromXEventVsc(getXEventArray(3, 8)));
368         Assert.assertEquals(100, RemoteDevices.getBatteryLevelFromXEventVsc(getXEventArray(1, 1)));
369         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
370                 RemoteDevices.getBatteryLevelFromXEventVsc(getXEventArray(3, 1)));
371         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
372                 RemoteDevices.getBatteryLevelFromXEventVsc(getXEventArray(-1, 1)));
373         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
374                 RemoteDevices.getBatteryLevelFromXEventVsc(getXEventArray(-1, -1)));
375     }
376 
377     @Test
testGetBatteryLevelFromAppleBatteryVsc()378     public void testGetBatteryLevelFromAppleBatteryVsc() {
379         Assert.assertEquals(10, RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
380                 1, BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL, 0
381         }));
382         Assert.assertEquals(100, RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
383                 1, BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL, 9
384         }));
385         Assert.assertEquals(60, RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
386                 3,
387                 BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL,
388                 5,
389                 2,
390                 1,
391                 3,
392                 10
393         }));
394         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
395                 RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
396                         3,
397                         BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL,
398                         5,
399                         2,
400                         1,
401                         3
402                 }));
403         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
404                 RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
405                         1,
406                         BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL,
407                         10
408                 }));
409         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
410                 RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
411                         1,
412                         BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL,
413                         -1
414                 }));
415         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
416                 RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{
417                         1,
418                         BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL,
419                         "5"
420                 }));
421         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
422                 RemoteDevices.getBatteryLevelFromAppleBatteryVsc(new Object[]{1, 35, 37}));
423         Assert.assertEquals(BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
424                 RemoteDevices.getBatteryLevelFromAppleBatteryVsc(
425                         new Object[]{1, "WRONG", "WRONG"}));
426     }
427 
verifyBatteryLevelChangedIntent(BluetoothDevice device, int batteryLevel, ArgumentCaptor<Intent> intentArgument)428     private static void verifyBatteryLevelChangedIntent(BluetoothDevice device, int batteryLevel,
429             ArgumentCaptor<Intent> intentArgument) {
430         verifyBatteryLevelChangedIntent(device, batteryLevel, intentArgument.getValue());
431     }
432 
verifyBatteryLevelChangedIntent(BluetoothDevice device, int batteryLevel, Intent intent)433     private static void verifyBatteryLevelChangedIntent(BluetoothDevice device, int batteryLevel,
434             Intent intent) {
435         Assert.assertEquals(BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED, intent.getAction());
436         Assert.assertEquals(device, intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
437         Assert.assertEquals(batteryLevel,
438                 intent.getIntExtra(BluetoothDevice.EXTRA_BATTERY_LEVEL, -15));
439         Assert.assertEquals(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, intent.getFlags());
440     }
441 
getHeadsetConnectionStateChangedIntent(BluetoothDevice device, int oldState, int newState)442     private static Intent getHeadsetConnectionStateChangedIntent(BluetoothDevice device,
443             int oldState, int newState) {
444         Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
445         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
446         intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, oldState);
447         intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
448         return intent;
449     }
450 
getHfIndicatorIntent(BluetoothDevice device, int batteryLevel, int indicatorId)451     private static Intent getHfIndicatorIntent(BluetoothDevice device, int batteryLevel,
452             int indicatorId) {
453         Intent intent = new Intent(BluetoothHeadset.ACTION_HF_INDICATORS_VALUE_CHANGED);
454         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
455         intent.putExtra(BluetoothHeadset.EXTRA_HF_INDICATORS_IND_ID, indicatorId);
456         intent.putExtra(BluetoothHeadset.EXTRA_HF_INDICATORS_IND_VALUE, batteryLevel);
457         return intent;
458     }
459 
getVendorSpecificHeadsetEventIntent(String command, int companyId, int commandType, Object[] arguments, BluetoothDevice device)460     private static Intent getVendorSpecificHeadsetEventIntent(String command, int companyId,
461             int commandType, Object[] arguments, BluetoothDevice device) {
462         Intent intent = new Intent(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
463         intent.putExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD, command);
464         intent.putExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE, commandType);
465         // assert: all elements of args are Serializable
466         intent.putExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS, arguments);
467         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
468         intent.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY + "."
469                 + Integer.toString(companyId));
470         return intent;
471     }
472 
getXEventArray(int batteryLevel, int numLevels)473     private static Object[] getXEventArray(int batteryLevel, int numLevels) {
474         ArrayList<Object> list = new ArrayList<>();
475         list.add(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT_BATTERY_LEVEL);
476         list.add(batteryLevel);
477         list.add(numLevels);
478         list.add(0);
479         list.add(0);
480         return list.toArray();
481     }
482 }
483