hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.settingslib.media; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.when; |
| 23 | |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 24 | import android.bluetooth.BluetoothDevice; |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 25 | import android.bluetooth.BluetoothProfile; |
| 26 | import android.content.Context; |
| 27 | |
| 28 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | import org.mockito.Mock; |
| 34 | import org.mockito.MockitoAnnotations; |
| 35 | import org.robolectric.RobolectricTestRunner; |
| 36 | import org.robolectric.RuntimeEnvironment; |
| 37 | |
| 38 | @RunWith(RobolectricTestRunner.class) |
| 39 | public class BluetoothMediaDeviceTest { |
| 40 | |
| 41 | @Mock |
| 42 | private CachedBluetoothDevice mDevice; |
| 43 | |
| 44 | private Context mContext; |
| 45 | private BluetoothMediaDevice mBluetoothMediaDevice; |
| 46 | |
| 47 | @Before |
| 48 | public void setUp() { |
| 49 | MockitoAnnotations.initMocks(this); |
| 50 | mContext = RuntimeEnvironment.application; |
| 51 | |
| 52 | when(mDevice.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(true); |
| 53 | when(mDevice.isActiveDevice(BluetoothProfile.HEARING_AID)).thenReturn(true); |
| 54 | |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 55 | mBluetoothMediaDevice = new BluetoothMediaDevice(mContext, mDevice, null, null, null); |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Test |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 59 | public void isCachedBluetoothDeviceConnected_deviceConnected_returnTrue() { |
| 60 | when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); |
| 61 | when(mDevice.isConnected()).thenReturn(true); |
| 62 | |
| 63 | assertThat(mBluetoothMediaDevice.isConnected()).isTrue(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void isCachedBluetoothDeviceConnected_deviceNotConnected_returnFalse() { |
| 68 | when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); |
| 69 | when(mDevice.isConnected()).thenReturn(false); |
| 70 | |
| 71 | assertThat(mBluetoothMediaDevice.isConnected()).isFalse(); |
| 72 | } |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 73 | |
| 74 | @Test |
| 75 | public void isFastPairDevice_isUntetheredHeadset_returnTrue() { |
| 76 | final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); |
| 77 | when(mDevice.getDevice()).thenReturn(bluetoothDevice); |
| 78 | |
| 79 | final String value = "True"; |
| 80 | final byte[] bytes = value.getBytes(); |
| 81 | when(bluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) |
| 82 | .thenReturn(bytes); |
| 83 | |
| 84 | assertThat(mBluetoothMediaDevice.isFastPairDevice()).isTrue(); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void isFastPairDevice_isNotUntetheredHeadset_returnFalse() { |
| 89 | final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); |
| 90 | when(mDevice.getDevice()).thenReturn(bluetoothDevice); |
| 91 | |
| 92 | final String value = "asjdaioshfaio"; |
| 93 | final byte[] bytes = value.getBytes(); |
| 94 | when(bluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) |
| 95 | .thenReturn(bytes); |
| 96 | |
| 97 | assertThat(mBluetoothMediaDevice.isFastPairDevice()).isFalse(); |
| 98 | } |
hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 99 | } |