blob: 7f3c34e5eb1bcdce33446a0ab6110d4e39a31636 [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;
Jason Monk6a73e632017-03-17 11:08:30 -040020import static org.mockito.Mockito.mock;
Jason Monk9ef03b42017-06-13 12:49:55 -040021import static org.mockito.Mockito.verify;
Jason Monk6a73e632017-03-17 11:08:30 -040022import static org.mockito.Mockito.when;
23
24import android.bluetooth.BluetoothAdapter;
Jason Monk9ef03b42017-06-13 12:49:55 -040025import android.bluetooth.BluetoothDevice;
Jason Monk6a73e632017-03-17 11:08:30 -040026import android.bluetooth.BluetoothProfile;
Jason Monk9ef03b42017-06-13 12:49:55 -040027import android.os.Looper;
Jason Monk745d0a82017-04-17 11:34:22 -040028import android.testing.AndroidTestingRunner;
Jason Monk6a73e632017-03-17 11:08:30 -040029import android.testing.TestableLooper;
Jason Monk745d0a82017-04-17 11:34:22 -040030import android.testing.TestableLooper.RunWithLooper;
Brett Chabot84151d92019-02-27 15:37:59 -080031
32import androidx.test.filters.SmallTest;
Jason Monk6a73e632017-03-17 11:08:30 -040033
34import com.android.settingslib.bluetooth.BluetoothEventManager;
35import com.android.settingslib.bluetooth.CachedBluetoothDevice;
36import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
37import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
38import com.android.settingslib.bluetooth.LocalBluetoothManager;
Amin Shaikh45fb7a72018-04-18 14:14:38 -040039import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
Jason Monk6a73e632017-03-17 11:08:30 -040040import com.android.systemui.SysuiTestCase;
41
42import org.junit.Before;
43import org.junit.Test;
Jason Monk745d0a82017-04-17 11:34:22 -040044import org.junit.runner.RunWith;
Jason Monk6a73e632017-03-17 11:08:30 -040045
46import java.util.ArrayList;
47import java.util.List;
48
Jason Monk745d0a82017-04-17 11:34:22 -040049@RunWith(AndroidTestingRunner.class)
50@RunWithLooper
Jason Monk25a52b62017-05-23 10:42:59 -040051@SmallTest
Jason Monk6a73e632017-03-17 11:08:30 -040052public 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 Monk745d0a82017-04-17 11:34:22 -040064 mTestableLooper = TestableLooper.get(this);
Jason Monk6a73e632017-03-17 11:08:30 -040065 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 Shaikh45fb7a72018-04-18 14:14:38 -040073 when(mMockBluetoothManager.getProfileManager())
74 .thenReturn(mock(LocalBluetoothProfileManager.class));
Jason Monk6a73e632017-03-17 11:08:30 -040075
76 mBluetoothControllerImpl = new BluetoothControllerImpl(mContext,
Jason Monk8111bcc2018-12-21 13:38:56 -050077 mTestableLooper.getLooper(),
78 mMockBluetoothManager);
Jason Monk6a73e632017-03-17 11:08:30 -040079 }
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 Monk9ef03b42017-06-13 12:49:55 -040093
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 Monk0f5d4022017-08-15 14:29:49 -0400145
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 Shaikhc5483402018-04-26 13:51:54 -0400167
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 Monk6a73e632017-03-17 11:08:30 -0400201}