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 package com.android.bluetooth.hid;
17 
18 import static org.mockito.Mockito.*;
19 
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothDevice;
22 import android.bluetooth.BluetoothProfile;
23 import android.content.Context;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.filters.MediumTest;
27 import androidx.test.rule.ServiceTestRule;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.bluetooth.R;
31 import com.android.bluetooth.TestUtils;
32 import com.android.bluetooth.btservice.AdapterService;
33 import com.android.bluetooth.btservice.storage.DatabaseManager;
34 
35 import org.junit.After;
36 import org.junit.Assert;
37 import org.junit.Assume;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 
45 @MediumTest
46 @RunWith(AndroidJUnit4.class)
47 public class HidHostServiceTest {
48     private HidHostService mService = null;
49     private BluetoothAdapter mAdapter = null;
50     private BluetoothDevice mTestDevice;
51     private Context mTargetContext;
52 
53     @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
54 
55     @Mock private AdapterService mAdapterService;
56     @Mock private DatabaseManager mDatabaseManager;
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         mTargetContext = InstrumentationRegistry.getTargetContext();
61         Assume.assumeTrue("Ignore test when HidHostService is not enabled",
62                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_hid_host));
63         MockitoAnnotations.initMocks(this);
64         TestUtils.setAdapterService(mAdapterService);
65         when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
66         when(mAdapterService.isStartedProfile(anyString())).thenReturn(true);
67         TestUtils.startService(mServiceRule, HidHostService.class);
68         mService = HidHostService.getHidHostService();
69         Assert.assertNotNull(mService);
70         // Try getting the Bluetooth adapter
71         mAdapter = BluetoothAdapter.getDefaultAdapter();
72         Assert.assertNotNull(mAdapter);
73 
74         // Get a device for testing
75         mTestDevice = TestUtils.getTestDevice(mAdapter, 0);
76     }
77 
78     @After
tearDown()79     public void tearDown() throws Exception {
80         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_hid_host)) {
81             return;
82         }
83         when(mAdapterService.isStartedProfile(anyString())).thenReturn(false);
84         TestUtils.stopService(mServiceRule, HidHostService.class);
85         mService = HidHostService.getHidHostService();
86         Assert.assertNull(mService);
87         TestUtils.clearAdapterService(mAdapterService);
88     }
89 
90     @Test
testInitialize()91     public void testInitialize() {
92         Assert.assertNotNull(HidHostService.getHidHostService());
93     }
94 
95     /**
96      *  Test okToConnect method using various test cases
97      */
98     @Test
testOkToConnect()99     public void testOkToConnect() {
100         int badPriorityValue = 1024;
101         int badBondState = 42;
102         testOkToConnectCase(mTestDevice,
103                 BluetoothDevice.BOND_NONE, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, false);
104         testOkToConnectCase(mTestDevice,
105                 BluetoothDevice.BOND_NONE, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
106         testOkToConnectCase(mTestDevice,
107                 BluetoothDevice.BOND_NONE, BluetoothProfile.CONNECTION_POLICY_ALLOWED, false);
108         testOkToConnectCase(mTestDevice,
109                 BluetoothDevice.BOND_NONE, badPriorityValue, false);
110         testOkToConnectCase(mTestDevice,
111                 BluetoothDevice.BOND_BONDING, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, false);
112         testOkToConnectCase(mTestDevice,
113                 BluetoothDevice.BOND_BONDING, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
114         testOkToConnectCase(mTestDevice,
115                 BluetoothDevice.BOND_BONDING, BluetoothProfile.CONNECTION_POLICY_ALLOWED, false);
116         testOkToConnectCase(mTestDevice,
117                 BluetoothDevice.BOND_BONDING, badPriorityValue, false);
118         testOkToConnectCase(mTestDevice,
119                 BluetoothDevice.BOND_BONDED, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, true);
120         testOkToConnectCase(mTestDevice,
121                 BluetoothDevice.BOND_BONDED, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
122         testOkToConnectCase(mTestDevice,
123                 BluetoothDevice.BOND_BONDED, BluetoothProfile.CONNECTION_POLICY_ALLOWED, true);
124         testOkToConnectCase(mTestDevice,
125                 BluetoothDevice.BOND_BONDED, badPriorityValue, false);
126         testOkToConnectCase(mTestDevice,
127                 badBondState, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, false);
128         testOkToConnectCase(mTestDevice,
129                 badBondState, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
130         testOkToConnectCase(mTestDevice,
131                 badBondState, BluetoothProfile.CONNECTION_POLICY_ALLOWED, false);
132         testOkToConnectCase(mTestDevice,
133                 badBondState, badPriorityValue, false);
134     }
135 
136     /**
137      * Helper function to test okToConnect() method.
138      *
139      * @param device test device
140      * @param bondState bond state value, could be invalid
141      * @param priority value, could be invalid, could be invalid
142      * @param expected expected result from okToConnect()
143      */
testOkToConnectCase(BluetoothDevice device, int bondState, int priority, boolean expected)144     private void testOkToConnectCase(BluetoothDevice device, int bondState, int priority,
145             boolean expected) {
146         doReturn(bondState).when(mAdapterService).getBondState(device);
147         when(mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.HID_HOST))
148                 .thenReturn(priority);
149 
150         // Test when the AdapterService is in non-quiet mode.
151         doReturn(false).when(mAdapterService).isQuietModeEnabled();
152         Assert.assertEquals(expected, mService.okToConnect(device));
153 
154         // Test when the AdapterService is in quiet mode.
155         doReturn(true).when(mAdapterService).isQuietModeEnabled();
156         Assert.assertEquals(false, mService.okToConnect(device));
157     }
158 
159 }
160