ryanywlin | 8561d19 | 2018-03-28 17:19:39 +0800 | [diff] [blame] | 1 | package com.android.settingslib.bluetooth; |
| 2 | |
| 3 | import static com.google.common.truth.Truth.assertThat; |
| 4 | |
| 5 | import static org.mockito.ArgumentMatchers.any; |
| 6 | import static org.mockito.ArgumentMatchers.eq; |
| 7 | import static org.mockito.Mockito.doAnswer; |
| 8 | import static org.mockito.Mockito.spy; |
| 9 | import static org.mockito.Mockito.when; |
| 10 | |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 11 | import android.bluetooth.BluetoothDevice; |
ryanywlin | 8561d19 | 2018-03-28 17:19:39 +0800 | [diff] [blame] | 12 | import android.bluetooth.BluetoothHeadset; |
| 13 | import android.bluetooth.BluetoothProfile; |
| 14 | import android.content.Context; |
| 15 | |
| 16 | import org.junit.Before; |
| 17 | import org.junit.Test; |
| 18 | import org.junit.runner.RunWith; |
| 19 | import org.mockito.Mock; |
| 20 | import org.mockito.MockitoAnnotations; |
| 21 | import org.robolectric.RobolectricTestRunner; |
| 22 | import org.robolectric.RuntimeEnvironment; |
| 23 | |
| 24 | @RunWith(RobolectricTestRunner.class) |
| 25 | public 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; |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 35 | @Mock |
| 36 | private CachedBluetoothDevice mCachedBluetoothDevice; |
| 37 | @Mock |
| 38 | private BluetoothDevice mBluetoothDevice; |
ryanywlin | 8561d19 | 2018-03-28 17:19:39 +0800 | [diff] [blame] | 39 | 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)); |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 51 | when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); |
ryanywlin | 8561d19 | 2018-03-28 17:19:39 +0800 | [diff] [blame] | 52 | |
| 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 | } |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 65 | |
| 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 | } |
ryanywlin | 8561d19 | 2018-03-28 17:19:39 +0800 | [diff] [blame] | 78 | } |