• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 
17 package com.android.bluetooth.hfp;
18 
19 import static org.mockito.Mockito.*;
20 
21 import android.bluetooth.BluetoothDevice;
22 import android.bluetooth.BluetoothHeadset;
23 import android.bluetooth.BluetoothProfile;
24 import android.content.Intent;
25 
26 import org.junit.Assert;
27 
28 /**
29  * Helper functions for HFP related tests
30  */
31 public class HeadsetTestUtils {
32 
33     /**
34      * Verify the content of a {@link BluetoothHeadset#ACTION_AUDIO_STATE_CHANGED} intent
35      *
36      * @param device Bluetooth device
37      * @param toState value of {@link BluetoothProfile#EXTRA_STATE}
38      * @param fromState value of {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}
39      * @param intent a {@link BluetoothHeadset#ACTION_AUDIO_STATE_CHANGED} intent
40      */
verifyAudioStateBroadcast(BluetoothDevice device, int toState, int fromState, Intent intent)41     public static void verifyAudioStateBroadcast(BluetoothDevice device, int toState, int fromState,
42             Intent intent) {
43         Assert.assertNotNull(intent);
44         Assert.assertEquals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED, intent.getAction());
45         Assert.assertEquals(device, intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
46         Assert.assertEquals(toState, intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
47         Assert.assertEquals(fromState,
48                 intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1));
49     }
50 
51     /**
52      * Verify the content of a {@link BluetoothHeadset#ACTION_CONNECTION_STATE_CHANGED} intent
53      *
54      * @param device Bluetooth device
55      * @param toState value of {@link BluetoothProfile#EXTRA_STATE}
56      * @param fromState value of {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}
57      * @param intent a {@link BluetoothHeadset#ACTION_CONNECTION_STATE_CHANGED} intent
58      * @param checkFlag whether intent flag should be verified, normally this can only be done at
59      *                  the sender end
60      */
verifyConnectionStateBroadcast(BluetoothDevice device, int toState, int fromState, Intent intent, boolean checkFlag)61     public static void verifyConnectionStateBroadcast(BluetoothDevice device, int toState,
62             int fromState, Intent intent, boolean checkFlag) {
63         Assert.assertNotNull(intent);
64         Assert.assertEquals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED, intent.getAction());
65         if (checkFlag) {
66             Assert.assertEquals(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND, intent.getFlags());
67         }
68         Assert.assertEquals(device, intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
69         Assert.assertEquals(toState, intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
70         Assert.assertEquals(fromState,
71                 intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1));
72     }
73 
74     /**
75      * Verify the content of a {@link BluetoothHeadset#ACTION_CONNECTION_STATE_CHANGED} intent
76      * and its flag, normally used at sender end
77      *
78      * @param device Bluetooth device
79      * @param toState value of {@link BluetoothProfile#EXTRA_STATE}
80      * @param fromState value of {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}
81      * @param intent a {@link BluetoothHeadset#ACTION_CONNECTION_STATE_CHANGED} intent
82      */
verifyConnectionStateBroadcast(BluetoothDevice device, int toState, int fromState, Intent intent)83     public static void verifyConnectionStateBroadcast(BluetoothDevice device, int toState,
84             int fromState, Intent intent) {
85         verifyConnectionStateBroadcast(device, toState, fromState, intent, true);
86     }
87 
88     /**
89      * Verify the content of a {@link BluetoothHeadset#ACTION_ACTIVE_DEVICE_CHANGED} intent
90      * and its flag, normally used at sender end
91      *
92      * @param device intended active Bluetooth device
93      * @param intent a {@link BluetoothHeadset#ACTION_ACTIVE_DEVICE_CHANGED} intent
94      * @param checkFlag whether intent flag should be verified, normally this can only be done at
95      *                  the sender end
96      */
verifyActiveDeviceChangedBroadcast(BluetoothDevice device, Intent intent, boolean checkFlag)97     public static void verifyActiveDeviceChangedBroadcast(BluetoothDevice device, Intent intent,
98             boolean checkFlag) {
99         Assert.assertNotNull(intent);
100         Assert.assertEquals(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED, intent.getAction());
101         Assert.assertEquals(device, intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
102         if (checkFlag) {
103             Assert.assertEquals(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
104                     | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND, intent.getFlags());
105         }
106     }
107 
108     /**
109      * Helper function to check if {@link HeadsetPhoneState} is set to correct values indicated in
110      * {@param headsetCallState}
111      *
112      * @param headsetPhoneState a mocked {@link HeadsetPhoneState}
113      * @param headsetCallState intended headset call state
114      * @param timeoutMs timeout for this check in asynchronous test conditions
115      */
verifyPhoneStateChangeSetters(HeadsetPhoneState headsetPhoneState, HeadsetCallState headsetCallState, int timeoutMs)116     public static void verifyPhoneStateChangeSetters(HeadsetPhoneState headsetPhoneState,
117             HeadsetCallState headsetCallState, int timeoutMs) {
118         verify(headsetPhoneState, timeout(timeoutMs).times(1)).setNumActiveCall(
119                 headsetCallState.mNumActive);
120         verify(headsetPhoneState, timeout(timeoutMs).times(1)).setNumHeldCall(
121                 headsetCallState.mNumHeld);
122         verify(headsetPhoneState, timeout(timeoutMs).times(1)).setCallState(
123                 headsetCallState.mCallState);
124     }
125 }
126