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         TestUtils.startService(mServiceRule, HidHostService.class);
66         mService = HidHostService.getHidHostService();
67         Assert.assertNotNull(mService);
68         // Try getting the Bluetooth adapter
69         mAdapter = BluetoothAdapter.getDefaultAdapter();
70         Assert.assertNotNull(mAdapter);
71 
72         // Get a device for testing
73         mTestDevice = TestUtils.getTestDevice(mAdapter, 0);
74     }
75 
76     @After
tearDown()77     public void tearDown() throws Exception {
78         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_hid_host)) {
79             return;
80         }
81         TestUtils.stopService(mServiceRule, HidHostService.class);
82         mService = HidHostService.getHidHostService();
83         Assert.assertNull(mService);
84         TestUtils.clearAdapterService(mAdapterService);
85     }
86 
87     @Test
testInitialize()88     public void testInitialize() {
89         Assert.assertNotNull(HidHostService.getHidHostService());
90     }
91 
92     /**
93      *  Test okToConnect method using various test cases
94      */
95     @Test
testOkToConnect()96     public void testOkToConnect() {
97         int badPriorityValue = 1024;
98         int badBondState = 42;
99         testOkToConnectCase(mTestDevice,
100                 BluetoothDevice.BOND_NONE, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, false);
101         testOkToConnectCase(mTestDevice,
102                 BluetoothDevice.BOND_NONE, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
103         testOkToConnectCase(mTestDevice,
104                 BluetoothDevice.BOND_NONE, BluetoothProfile.CONNECTION_POLICY_ALLOWED, false);
105         testOkToConnectCase(mTestDevice,
106                 BluetoothDevice.BOND_NONE, badPriorityValue, false);
107         testOkToConnectCase(mTestDevice,
108                 BluetoothDevice.BOND_BONDING, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, false);
109         testOkToConnectCase(mTestDevice,
110                 BluetoothDevice.BOND_BONDING, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
111         testOkToConnectCase(mTestDevice,
112                 BluetoothDevice.BOND_BONDING, BluetoothProfile.CONNECTION_POLICY_ALLOWED, false);
113         testOkToConnectCase(mTestDevice,
114                 BluetoothDevice.BOND_BONDING, badPriorityValue, false);
115         testOkToConnectCase(mTestDevice,
116                 BluetoothDevice.BOND_BONDED, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, true);
117         testOkToConnectCase(mTestDevice,
118                 BluetoothDevice.BOND_BONDED, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
119         testOkToConnectCase(mTestDevice,
120                 BluetoothDevice.BOND_BONDED, BluetoothProfile.CONNECTION_POLICY_ALLOWED, true);
121         testOkToConnectCase(mTestDevice,
122                 BluetoothDevice.BOND_BONDED, badPriorityValue, false);
123         testOkToConnectCase(mTestDevice,
124                 badBondState, BluetoothProfile.CONNECTION_POLICY_UNKNOWN, false);
125         testOkToConnectCase(mTestDevice,
126                 badBondState, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, false);
127         testOkToConnectCase(mTestDevice,
128                 badBondState, BluetoothProfile.CONNECTION_POLICY_ALLOWED, false);
129         testOkToConnectCase(mTestDevice,
130                 badBondState, badPriorityValue, false);
131         // Restore prirority to undefined for this test device
132         Assert.assertTrue(mService.setConnectionPolicy(
133                 mTestDevice, BluetoothProfile.CONNECTION_POLICY_UNKNOWN));
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(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
148         when(mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.HID_HOST))
149                 .thenReturn(priority);
150 
151         // Test when the AdapterService is in non-quiet mode.
152         doReturn(false).when(mAdapterService).isQuietModeEnabled();
153         Assert.assertEquals(expected, mService.okToConnect(device));
154 
155         // Test when the AdapterService is in quiet mode.
156         doReturn(true).when(mAdapterService).isQuietModeEnabled();
157         Assert.assertEquals(false, mService.okToConnect(device));
158     }
159 
160 }
161