blob: 03b023b75f4a7b63b4b5afa9bc922c2ed6c7d57d [file] [log] [blame]
ryanywlin8561d192018-03-28 17:19:39 +08001package com.android.settingslib.bluetooth;
2
3import static com.google.common.truth.Truth.assertThat;
4
5import static org.mockito.ArgumentMatchers.any;
6import static org.mockito.ArgumentMatchers.eq;
7import static org.mockito.Mockito.doAnswer;
8import static org.mockito.Mockito.spy;
9import static org.mockito.Mockito.when;
10
hughchen23b947e2018-03-31 17:32:53 +080011import android.bluetooth.BluetoothDevice;
ryanywlin8561d192018-03-28 17:19:39 +080012import android.bluetooth.BluetoothHeadset;
13import android.bluetooth.BluetoothProfile;
14import android.content.Context;
15
16import org.junit.Before;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.mockito.Mock;
20import org.mockito.MockitoAnnotations;
21import org.robolectric.RobolectricTestRunner;
22import org.robolectric.RuntimeEnvironment;
23
24@RunWith(RobolectricTestRunner.class)
25public class HeadsetProfileTest {
26
27 @Mock
28 private LocalBluetoothAdapter mAdapter;
29 @Mock
30 private CachedBluetoothDeviceManager mDeviceManager;
31 @Mock
32 private LocalBluetoothProfileManager mProfileManager;
33 @Mock
34 private BluetoothHeadset mService;
hughchen23b947e2018-03-31 17:32:53 +080035 @Mock
36 private CachedBluetoothDevice mCachedBluetoothDevice;
37 @Mock
38 private BluetoothDevice mBluetoothDevice;
ryanywlin8561d192018-03-28 17:19:39 +080039 private BluetoothProfile.ServiceListener mServiceListener;
40 private HeadsetProfile mProfile;
41
42 @Before
43 public void setUp() {
44 MockitoAnnotations.initMocks(this);
45 Context context = spy(RuntimeEnvironment.application);
46
47 doAnswer((invocation) -> {
48 mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1];
49 return null;
50 }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET));
hughchen23b947e2018-03-31 17:32:53 +080051 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
ryanywlin8561d192018-03-28 17:19:39 +080052
53 mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager);
54 mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService);
55 }
56
57 @Test
58 public void bluetoothProfile_shouldReturnTheAudioStatusFromBlueToothHeadsetService() {
59 when(mService.isAudioOn()).thenReturn(true);
60 assertThat(mProfile.isAudioOn()).isTrue();
61
62 when(mService.isAudioOn()).thenReturn(false);
63 assertThat(mProfile.isAudioOn()).isFalse();
64 }
hughchen23b947e2018-03-31 17:32:53 +080065
66 @Test
67 public void testHeadsetProfile_shouldReturnAudioState() {
68 when(mService.getAudioState(mBluetoothDevice)).
69 thenReturn(BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
70 assertThat(mProfile.getAudioState(mBluetoothDevice)).
71 isEqualTo(BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
72
73 when(mService.getAudioState(mBluetoothDevice)).
74 thenReturn(BluetoothHeadset.STATE_AUDIO_CONNECTED);
75 assertThat(mProfile.getAudioState(mBluetoothDevice)).
76 isEqualTo(BluetoothHeadset.STATE_AUDIO_CONNECTED);
77 }
ryanywlin8561d192018-03-28 17:19:39 +080078}