Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.statusbar.policy; |
| 16 | |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 18 | import static org.junit.Assert.assertFalse; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 19 | import static org.junit.Assert.assertTrue; |
Fabian Kozynski | 7f27b8e | 2019-05-07 16:18:15 -0400 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 21 | import static org.mockito.Mockito.atLeastOnce; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
Fabian Kozynski | 7f27b8e | 2019-05-07 16:18:15 -0400 | [diff] [blame] | 23 | import static org.mockito.Mockito.reset; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 24 | import static org.mockito.Mockito.verify; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.bluetooth.BluetoothAdapter; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 28 | import android.bluetooth.BluetoothDevice; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 29 | import android.bluetooth.BluetoothProfile; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 30 | import android.os.Looper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 31 | import android.testing.AndroidTestingRunner; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 32 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 33 | import android.testing.TestableLooper.RunWithLooper; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 34 | |
| 35 | import androidx.test.filters.SmallTest; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 36 | |
| 37 | import com.android.settingslib.bluetooth.BluetoothEventManager; |
| 38 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 39 | import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; |
| 40 | import com.android.settingslib.bluetooth.LocalBluetoothAdapter; |
| 41 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 42 | import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 43 | import com.android.systemui.SysuiTestCase; |
| 44 | |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 47 | import org.junit.runner.RunWith; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 48 | |
| 49 | import java.util.ArrayList; |
| 50 | import java.util.List; |
| 51 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 52 | @RunWith(AndroidTestingRunner.class) |
| 53 | @RunWithLooper |
Jason Monk | 25a52b65 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 54 | @SmallTest |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 55 | public class BluetoothControllerImplTest extends SysuiTestCase { |
| 56 | |
| 57 | private LocalBluetoothManager mMockBluetoothManager; |
| 58 | private CachedBluetoothDeviceManager mMockDeviceManager; |
| 59 | private LocalBluetoothAdapter mMockAdapter; |
| 60 | private TestableLooper mTestableLooper; |
| 61 | private BluetoothControllerImpl mBluetoothControllerImpl; |
| 62 | |
| 63 | private List<CachedBluetoothDevice> mDevices; |
| 64 | |
| 65 | @Before |
| 66 | public void setup() throws Exception { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 67 | mTestableLooper = TestableLooper.get(this); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 68 | mMockBluetoothManager = mDependency.injectMockDependency(LocalBluetoothManager.class); |
| 69 | mDevices = new ArrayList<>(); |
| 70 | mMockDeviceManager = mock(CachedBluetoothDeviceManager.class); |
| 71 | when(mMockDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices); |
| 72 | when(mMockBluetoothManager.getCachedDeviceManager()).thenReturn(mMockDeviceManager); |
| 73 | mMockAdapter = mock(LocalBluetoothAdapter.class); |
| 74 | when(mMockBluetoothManager.getBluetoothAdapter()).thenReturn(mMockAdapter); |
| 75 | when(mMockBluetoothManager.getEventManager()).thenReturn(mock(BluetoothEventManager.class)); |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 76 | when(mMockBluetoothManager.getProfileManager()) |
| 77 | .thenReturn(mock(LocalBluetoothProfileManager.class)); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 78 | |
| 79 | mBluetoothControllerImpl = new BluetoothControllerImpl(mContext, |
Jason Monk | 8111bcc | 2018-12-21 13:38:56 -0500 | [diff] [blame] | 80 | mTestableLooper.getLooper(), |
| 81 | mMockBluetoothManager); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testNoConnectionWithDevices() { |
| 86 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 87 | when(device.isConnected()).thenReturn(true); |
| 88 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 89 | mDevices.add(device); |
| 90 | when(mMockAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED); |
| 91 | |
| 92 | mBluetoothControllerImpl.onConnectionStateChanged(null, |
| 93 | BluetoothAdapter.STATE_DISCONNECTED); |
| 94 | assertTrue(mBluetoothControllerImpl.isBluetoothConnected()); |
| 95 | } |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 96 | |
| 97 | @Test |
| 98 | public void testDefaultConnectionState() { |
| 99 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 100 | assertEquals(BluetoothDevice.BOND_NONE, mBluetoothControllerImpl.getBondState(device)); |
| 101 | assertEquals(BluetoothProfile.STATE_DISCONNECTED, |
| 102 | mBluetoothControllerImpl.getMaxConnectionState(device)); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testAsyncBondState() throws Exception { |
| 107 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 108 | when(device.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); |
| 109 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 110 | mBluetoothControllerImpl.addCallback(callback); |
| 111 | |
| 112 | // Grab the main looper, we'll need it later. |
| 113 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 114 | |
| 115 | // Trigger the state getting. |
| 116 | assertEquals(BluetoothDevice.BOND_NONE, mBluetoothControllerImpl.getBondState(device)); |
| 117 | |
| 118 | mTestableLooper.processMessages(1); |
| 119 | mainLooper.processAllMessages(); |
| 120 | |
| 121 | assertEquals(BluetoothDevice.BOND_BONDED, mBluetoothControllerImpl.getBondState(device)); |
| 122 | verify(callback).onBluetoothDevicesChanged(); |
| 123 | mainLooper.destroy(); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testAsyncConnectionState() throws Exception { |
| 128 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 129 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 130 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 131 | mBluetoothControllerImpl.addCallback(callback); |
| 132 | |
| 133 | // Grab the main looper, we'll need it later. |
| 134 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 135 | |
| 136 | // Trigger the state getting. |
| 137 | assertEquals(BluetoothProfile.STATE_DISCONNECTED, |
| 138 | mBluetoothControllerImpl.getMaxConnectionState(device)); |
| 139 | |
| 140 | mTestableLooper.processMessages(1); |
| 141 | mainLooper.processAllMessages(); |
| 142 | |
| 143 | assertEquals(BluetoothProfile.STATE_CONNECTED, |
| 144 | mBluetoothControllerImpl.getMaxConnectionState(device)); |
| 145 | verify(callback).onBluetoothDevicesChanged(); |
| 146 | mainLooper.destroy(); |
| 147 | } |
Jason Monk | 0f5d402 | 2017-08-15 14:29:49 -0400 | [diff] [blame] | 148 | |
| 149 | @Test |
| 150 | public void testNullAsync_DoesNotCrash() throws Exception { |
| 151 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 152 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 153 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 154 | mBluetoothControllerImpl.addCallback(callback); |
| 155 | |
| 156 | // Grab the main looper, we'll need it later. |
| 157 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 158 | |
| 159 | try { |
| 160 | // Trigger the state getting. |
| 161 | assertEquals(BluetoothProfile.STATE_DISCONNECTED, |
| 162 | mBluetoothControllerImpl.getMaxConnectionState(null)); |
| 163 | |
| 164 | mTestableLooper.processMessages(1); |
| 165 | mainLooper.processAllMessages(); |
| 166 | } finally { |
| 167 | mainLooper.destroy(); |
| 168 | } |
| 169 | } |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 170 | |
| 171 | @Test |
| 172 | public void testOnServiceConnected_updatesConnectionState() { |
| 173 | when(mMockAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTING); |
| 174 | |
| 175 | mBluetoothControllerImpl.onServiceConnected(); |
| 176 | |
| 177 | assertTrue(mBluetoothControllerImpl.isBluetoothConnecting()); |
| 178 | assertFalse(mBluetoothControllerImpl.isBluetoothConnected()); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testOnBluetoothStateChange_updatesBluetoothState() { |
| 183 | mBluetoothControllerImpl.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF); |
| 184 | |
| 185 | assertEquals(BluetoothAdapter.STATE_OFF, mBluetoothControllerImpl.getBluetoothState()); |
| 186 | |
| 187 | mBluetoothControllerImpl.onBluetoothStateChanged(BluetoothAdapter.STATE_ON); |
| 188 | |
| 189 | assertEquals(BluetoothAdapter.STATE_ON, mBluetoothControllerImpl.getBluetoothState()); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testOnBluetoothStateChange_updatesConnectionState() { |
| 194 | when(mMockAdapter.getConnectionState()).thenReturn( |
| 195 | BluetoothAdapter.STATE_CONNECTING, |
| 196 | BluetoothAdapter.STATE_DISCONNECTED); |
| 197 | |
| 198 | mBluetoothControllerImpl.onServiceConnected(); |
| 199 | mBluetoothControllerImpl.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF); |
| 200 | |
| 201 | assertFalse(mBluetoothControllerImpl.isBluetoothConnecting()); |
| 202 | assertFalse(mBluetoothControllerImpl.isBluetoothConnected()); |
| 203 | } |
Fabian Kozynski | 7f27b8e | 2019-05-07 16:18:15 -0400 | [diff] [blame] | 204 | |
| 205 | @Test |
Fabian Kozynski | a8b20b3 | 2019-05-10 10:01:54 -0400 | [diff] [blame] | 206 | public void testOnACLConnectionStateChange_updatesBluetoothStateOnConnection() |
| 207 | throws Exception { |
Fabian Kozynski | 7f27b8e | 2019-05-07 16:18:15 -0400 | [diff] [blame] | 208 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 209 | mBluetoothControllerImpl.addCallback(callback); |
| 210 | |
| 211 | assertFalse(mBluetoothControllerImpl.isBluetoothConnected()); |
| 212 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 213 | mDevices.add(device); |
| 214 | when(device.isConnected()).thenReturn(true); |
| 215 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 216 | reset(callback); |
| 217 | mBluetoothControllerImpl.onAclConnectionStateChanged(device, |
| 218 | BluetoothProfile.STATE_CONNECTED); |
Fabian Kozynski | a8b20b3 | 2019-05-10 10:01:54 -0400 | [diff] [blame] | 219 | |
| 220 | // Grab the main looper, we'll need it later. |
| 221 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 222 | |
| 223 | try { |
| 224 | mTestableLooper.processAllMessages(); |
| 225 | mainLooper.processAllMessages(); |
| 226 | } finally { |
| 227 | mainLooper.destroy(); |
| 228 | } |
Fabian Kozynski | 7f27b8e | 2019-05-07 16:18:15 -0400 | [diff] [blame] | 229 | assertTrue(mBluetoothControllerImpl.isBluetoothConnected()); |
| 230 | verify(callback, atLeastOnce()).onBluetoothStateChange(anyBoolean()); |
| 231 | } |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 232 | } |