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