blob: 766ad978f475f674168d66f4d28dd807c156e570 [file] [log] [blame]
Jason Monk6a73e632017-03-17 11:08:30 -04001/*
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
15package com.android.systemui.statusbar.policy;
16
Jason Monk9ef03b42017-06-13 12:49:55 -040017import static org.junit.Assert.assertEquals;
Amin Shaikhc5483402018-04-26 13:51:54 -040018import static org.junit.Assert.assertFalse;
Brett Chabot84151d92019-02-27 15:37:59 -080019import static org.junit.Assert.assertTrue;
Fabian Kozynski7f27b8e2019-05-07 16:18:15 -040020import static org.mockito.ArgumentMatchers.anyBoolean;
21import static org.mockito.Mockito.atLeastOnce;
Jason Monk6a73e632017-03-17 11:08:30 -040022import static org.mockito.Mockito.mock;
Fabian Kozynski7f27b8e2019-05-07 16:18:15 -040023import static org.mockito.Mockito.reset;
Jason Monk9ef03b42017-06-13 12:49:55 -040024import static org.mockito.Mockito.verify;
Jason Monk6a73e632017-03-17 11:08:30 -040025import static org.mockito.Mockito.when;
26
27import android.bluetooth.BluetoothAdapter;
Jason Monk9ef03b42017-06-13 12:49:55 -040028import android.bluetooth.BluetoothDevice;
Jason Monk6a73e632017-03-17 11:08:30 -040029import android.bluetooth.BluetoothProfile;
Jason Monk9ef03b42017-06-13 12:49:55 -040030import android.os.Looper;
Jason Monk745d0a82017-04-17 11:34:22 -040031import android.testing.AndroidTestingRunner;
Jason Monk6a73e632017-03-17 11:08:30 -040032import android.testing.TestableLooper;
Jason Monk745d0a82017-04-17 11:34:22 -040033import android.testing.TestableLooper.RunWithLooper;
Brett Chabot84151d92019-02-27 15:37:59 -080034
35import androidx.test.filters.SmallTest;
Jason Monk6a73e632017-03-17 11:08:30 -040036
37import com.android.settingslib.bluetooth.BluetoothEventManager;
38import com.android.settingslib.bluetooth.CachedBluetoothDevice;
39import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
40import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
41import com.android.settingslib.bluetooth.LocalBluetoothManager;
Amin Shaikh45fb7a72018-04-18 14:14:38 -040042import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
Jason Monk6a73e632017-03-17 11:08:30 -040043import com.android.systemui.SysuiTestCase;
44
45import org.junit.Before;
46import org.junit.Test;
Jason Monk745d0a82017-04-17 11:34:22 -040047import org.junit.runner.RunWith;
Jason Monk6a73e632017-03-17 11:08:30 -040048
49import java.util.ArrayList;
50import java.util.List;
51
Jason Monk745d0a82017-04-17 11:34:22 -040052@RunWith(AndroidTestingRunner.class)
53@RunWithLooper
Jason Monk25a52b62017-05-23 10:42:59 -040054@SmallTest
Jason Monk6a73e632017-03-17 11:08:30 -040055public 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 Monk745d0a82017-04-17 11:34:22 -040067 mTestableLooper = TestableLooper.get(this);
Jason Monk6a73e632017-03-17 11:08:30 -040068 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 Shaikh45fb7a72018-04-18 14:14:38 -040076 when(mMockBluetoothManager.getProfileManager())
77 .thenReturn(mock(LocalBluetoothProfileManager.class));
Jason Monk6a73e632017-03-17 11:08:30 -040078
79 mBluetoothControllerImpl = new BluetoothControllerImpl(mContext,
Jason Monk8111bcc2018-12-21 13:38:56 -050080 mTestableLooper.getLooper(),
81 mMockBluetoothManager);
Jason Monk6a73e632017-03-17 11:08:30 -040082 }
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 Monk9ef03b42017-06-13 12:49:55 -040096
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 Monk0f5d4022017-08-15 14:29:49 -0400148
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 Shaikhc5483402018-04-26 13:51:54 -0400170
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 Kozynski7f27b8e2019-05-07 16:18:15 -0400204
205 @Test
Fabian Kozynskia8b20b32019-05-10 10:01:54 -0400206 public void testOnACLConnectionStateChange_updatesBluetoothStateOnConnection()
207 throws Exception {
Fabian Kozynski7f27b8e2019-05-07 16:18:15 -0400208 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 Kozynskia8b20b32019-05-10 10:01:54 -0400219
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 Kozynski7f27b8e2019-05-07 16:18:15 -0400229 assertTrue(mBluetoothControllerImpl.isBluetoothConnected());
230 verify(callback, atLeastOnce()).onBluetoothStateChange(anyBoolean());
231 }
Jason Monk6a73e632017-03-17 11:08:30 -0400232}