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; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 20 | import static org.mockito.Mockito.mock; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 21 | import static org.mockito.Mockito.verify; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.when; |
| 23 | |
| 24 | import android.bluetooth.BluetoothAdapter; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 25 | import android.bluetooth.BluetoothDevice; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 26 | import android.bluetooth.BluetoothProfile; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 27 | import android.os.Looper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 28 | import android.testing.AndroidTestingRunner; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 29 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 30 | import android.testing.TestableLooper.RunWithLooper; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 31 | |
| 32 | import androidx.test.filters.SmallTest; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 33 | |
| 34 | import com.android.settingslib.bluetooth.BluetoothEventManager; |
| 35 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 36 | import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; |
| 37 | import com.android.settingslib.bluetooth.LocalBluetoothAdapter; |
| 38 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 39 | import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 40 | import com.android.systemui.SysuiTestCase; |
| 41 | |
| 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 44 | import org.junit.runner.RunWith; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 45 | |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.List; |
| 48 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 49 | @RunWith(AndroidTestingRunner.class) |
| 50 | @RunWithLooper |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 51 | @SmallTest |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 52 | public class BluetoothControllerImplTest extends SysuiTestCase { |
| 53 | |
| 54 | private LocalBluetoothManager mMockBluetoothManager; |
| 55 | private CachedBluetoothDeviceManager mMockDeviceManager; |
| 56 | private LocalBluetoothAdapter mMockAdapter; |
| 57 | private TestableLooper mTestableLooper; |
| 58 | private BluetoothControllerImpl mBluetoothControllerImpl; |
| 59 | |
| 60 | private List<CachedBluetoothDevice> mDevices; |
| 61 | |
| 62 | @Before |
| 63 | public void setup() throws Exception { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 64 | mTestableLooper = TestableLooper.get(this); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 65 | mMockBluetoothManager = mDependency.injectMockDependency(LocalBluetoothManager.class); |
| 66 | mDevices = new ArrayList<>(); |
| 67 | mMockDeviceManager = mock(CachedBluetoothDeviceManager.class); |
| 68 | when(mMockDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices); |
| 69 | when(mMockBluetoothManager.getCachedDeviceManager()).thenReturn(mMockDeviceManager); |
| 70 | mMockAdapter = mock(LocalBluetoothAdapter.class); |
| 71 | when(mMockBluetoothManager.getBluetoothAdapter()).thenReturn(mMockAdapter); |
| 72 | when(mMockBluetoothManager.getEventManager()).thenReturn(mock(BluetoothEventManager.class)); |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 73 | when(mMockBluetoothManager.getProfileManager()) |
| 74 | .thenReturn(mock(LocalBluetoothProfileManager.class)); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 75 | |
| 76 | mBluetoothControllerImpl = new BluetoothControllerImpl(mContext, |
Jason Monk | 8111bcc | 2018-12-21 13:38:56 -0500 | [diff] [blame] | 77 | mTestableLooper.getLooper(), |
| 78 | mMockBluetoothManager); |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testNoConnectionWithDevices() { |
| 83 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 84 | when(device.isConnected()).thenReturn(true); |
| 85 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 86 | mDevices.add(device); |
| 87 | when(mMockAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED); |
| 88 | |
| 89 | mBluetoothControllerImpl.onConnectionStateChanged(null, |
| 90 | BluetoothAdapter.STATE_DISCONNECTED); |
| 91 | assertTrue(mBluetoothControllerImpl.isBluetoothConnected()); |
| 92 | } |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 93 | |
| 94 | @Test |
| 95 | public void testDefaultConnectionState() { |
| 96 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 97 | assertEquals(BluetoothDevice.BOND_NONE, mBluetoothControllerImpl.getBondState(device)); |
| 98 | assertEquals(BluetoothProfile.STATE_DISCONNECTED, |
| 99 | mBluetoothControllerImpl.getMaxConnectionState(device)); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testAsyncBondState() throws Exception { |
| 104 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 105 | when(device.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); |
| 106 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 107 | mBluetoothControllerImpl.addCallback(callback); |
| 108 | |
| 109 | // Grab the main looper, we'll need it later. |
| 110 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 111 | |
| 112 | // Trigger the state getting. |
| 113 | assertEquals(BluetoothDevice.BOND_NONE, mBluetoothControllerImpl.getBondState(device)); |
| 114 | |
| 115 | mTestableLooper.processMessages(1); |
| 116 | mainLooper.processAllMessages(); |
| 117 | |
| 118 | assertEquals(BluetoothDevice.BOND_BONDED, mBluetoothControllerImpl.getBondState(device)); |
| 119 | verify(callback).onBluetoothDevicesChanged(); |
| 120 | mainLooper.destroy(); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testAsyncConnectionState() throws Exception { |
| 125 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 126 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 127 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 128 | mBluetoothControllerImpl.addCallback(callback); |
| 129 | |
| 130 | // Grab the main looper, we'll need it later. |
| 131 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 132 | |
| 133 | // Trigger the state getting. |
| 134 | assertEquals(BluetoothProfile.STATE_DISCONNECTED, |
| 135 | mBluetoothControllerImpl.getMaxConnectionState(device)); |
| 136 | |
| 137 | mTestableLooper.processMessages(1); |
| 138 | mainLooper.processAllMessages(); |
| 139 | |
| 140 | assertEquals(BluetoothProfile.STATE_CONNECTED, |
| 141 | mBluetoothControllerImpl.getMaxConnectionState(device)); |
| 142 | verify(callback).onBluetoothDevicesChanged(); |
| 143 | mainLooper.destroy(); |
| 144 | } |
Jason Monk | 0f5d402 | 2017-08-15 14:29:49 -0400 | [diff] [blame] | 145 | |
| 146 | @Test |
| 147 | public void testNullAsync_DoesNotCrash() throws Exception { |
| 148 | CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); |
| 149 | when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); |
| 150 | BluetoothController.Callback callback = mock(BluetoothController.Callback.class); |
| 151 | mBluetoothControllerImpl.addCallback(callback); |
| 152 | |
| 153 | // Grab the main looper, we'll need it later. |
| 154 | TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); |
| 155 | |
| 156 | try { |
| 157 | // Trigger the state getting. |
| 158 | assertEquals(BluetoothProfile.STATE_DISCONNECTED, |
| 159 | mBluetoothControllerImpl.getMaxConnectionState(null)); |
| 160 | |
| 161 | mTestableLooper.processMessages(1); |
| 162 | mainLooper.processAllMessages(); |
| 163 | } finally { |
| 164 | mainLooper.destroy(); |
| 165 | } |
| 166 | } |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 167 | |
| 168 | @Test |
| 169 | public void testOnServiceConnected_updatesConnectionState() { |
| 170 | when(mMockAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTING); |
| 171 | |
| 172 | mBluetoothControllerImpl.onServiceConnected(); |
| 173 | |
| 174 | assertTrue(mBluetoothControllerImpl.isBluetoothConnecting()); |
| 175 | assertFalse(mBluetoothControllerImpl.isBluetoothConnected()); |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void testOnBluetoothStateChange_updatesBluetoothState() { |
| 180 | mBluetoothControllerImpl.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF); |
| 181 | |
| 182 | assertEquals(BluetoothAdapter.STATE_OFF, mBluetoothControllerImpl.getBluetoothState()); |
| 183 | |
| 184 | mBluetoothControllerImpl.onBluetoothStateChanged(BluetoothAdapter.STATE_ON); |
| 185 | |
| 186 | assertEquals(BluetoothAdapter.STATE_ON, mBluetoothControllerImpl.getBluetoothState()); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void testOnBluetoothStateChange_updatesConnectionState() { |
| 191 | when(mMockAdapter.getConnectionState()).thenReturn( |
| 192 | BluetoothAdapter.STATE_CONNECTING, |
| 193 | BluetoothAdapter.STATE_DISCONNECTED); |
| 194 | |
| 195 | mBluetoothControllerImpl.onServiceConnected(); |
| 196 | mBluetoothControllerImpl.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF); |
| 197 | |
| 198 | assertFalse(mBluetoothControllerImpl.isBluetoothConnecting()); |
| 199 | assertFalse(mBluetoothControllerImpl.isBluetoothConnected()); |
| 200 | } |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 201 | } |