blob: 8973d116e438942bb51351f0920d293780fb2900 [file] [log] [blame]
hughchen61c1c9f2019-01-17 14:34:51 +08001/*
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
17package com.android.settingslib.media;
18
19import static com.google.common.truth.Truth.assertThat;
20
hughchen31901c02020-04-22 19:31:28 +080021import static org.mockito.Mockito.mock;
hughchen61c1c9f2019-01-17 14:34:51 +080022import static org.mockito.Mockito.when;
23
hughchenc0c11632019-03-07 16:21:17 +080024import android.bluetooth.BluetoothDevice;
hughchen61c1c9f2019-01-17 14:34:51 +080025import android.bluetooth.BluetoothProfile;
26import android.content.Context;
27
28import com.android.settingslib.bluetooth.CachedBluetoothDevice;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35import org.robolectric.RobolectricTestRunner;
36import org.robolectric.RuntimeEnvironment;
37
38@RunWith(RobolectricTestRunner.class)
39public 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
hughchendf7b8382020-02-03 17:48:49 +080055 mBluetoothMediaDevice = new BluetoothMediaDevice(mContext, mDevice, null, null, null);
hughchen61c1c9f2019-01-17 14:34:51 +080056 }
57
58 @Test
hughchenc0c11632019-03-07 16:21:17 +080059 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 }
hughchen31901c02020-04-22 19:31:28 +080073
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 }
hughchen61c1c9f2019-01-17 14:34:51 +080099}