1 /* 2 * Copyright (C) 2016 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 17 package com.android.server.telecom.tests; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.bluetooth.BluetoothHeadset; 21 import android.bluetooth.BluetoothProfile; 22 import android.content.BroadcastReceiver; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.Parcel; 26 import android.test.suitebuilder.annotation.SmallTest; 27 28 import com.android.server.telecom.BluetoothAdapterProxy; 29 import com.android.server.telecom.BluetoothHeadsetProxy; 30 import com.android.server.telecom.TelecomSystem; 31 import com.android.server.telecom.bluetooth.BluetoothDeviceManager; 32 import com.android.server.telecom.bluetooth.BluetoothRouteManager; 33 import com.android.server.telecom.bluetooth.BluetoothStateReceiver; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.junit.runners.JUnit4; 39 import org.mockito.ArgumentCaptor; 40 import org.mockito.Mock; 41 42 import static org.junit.Assert.assertEquals; 43 import static org.junit.Assert.assertNull; 44 import static org.junit.Assert.assertTrue; 45 import static org.mockito.Matchers.eq; 46 import static org.mockito.Mockito.verify; 47 48 @RunWith(JUnit4.class) 49 public class BluetoothDeviceManagerTest extends TelecomTestCase { 50 @Mock BluetoothRouteManager mRouteManager; 51 @Mock BluetoothHeadsetProxy mHeadsetProxy; 52 @Mock BluetoothAdapterProxy mAdapterProxy; 53 54 BluetoothDeviceManager mBluetoothDeviceManager; 55 BluetoothProfile.ServiceListener serviceListenerUnderTest; 56 BroadcastReceiver receiverUnderTest; 57 58 private BluetoothDevice device1; 59 private BluetoothDevice device2; 60 private BluetoothDevice device3; 61 62 @Override 63 @Before 64 public void setUp() throws Exception { 65 super.setUp(); 66 device1 = makeBluetoothDevice("00:00:00:00:00:01"); 67 device2 = makeBluetoothDevice("00:00:00:00:00:02"); 68 device3 = makeBluetoothDevice("00:00:00:00:00:03"); 69 70 mContext = mComponentContextFixture.getTestDouble().getApplicationContext(); 71 mBluetoothDeviceManager = new BluetoothDeviceManager(mContext, mAdapterProxy, 72 new TelecomSystem.SyncRoot() { }); 73 mBluetoothDeviceManager.setBluetoothRouteManager(mRouteManager); 74 75 ArgumentCaptor<BluetoothProfile.ServiceListener> serviceCaptor = 76 ArgumentCaptor.forClass(BluetoothProfile.ServiceListener.class); 77 verify(mAdapterProxy).getProfileProxy(eq(mContext), 78 serviceCaptor.capture(), eq(BluetoothProfile.HEADSET)); 79 serviceListenerUnderTest = serviceCaptor.getValue(); 80 81 receiverUnderTest = new BluetoothStateReceiver(mBluetoothDeviceManager, 82 null /* route mgr not needed here */); 83 84 mBluetoothDeviceManager.setHeadsetServiceForTesting(mHeadsetProxy); 85 } 86 87 @SmallTest 88 @Test 89 public void testSingleDeviceConnectAndDisconnect() { 90 receiverUnderTest.onReceive(mContext, 91 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device1)); 92 assertEquals(1, mBluetoothDeviceManager.getNumConnectedDevices()); 93 assertEquals(device1.getAddress(), 94 mBluetoothDeviceManager.getMostRecentlyConnectedDevice(null)); 95 receiverUnderTest.onReceive(mContext, 96 buildConnectionActionIntent(BluetoothHeadset.STATE_DISCONNECTED, device1)); 97 assertEquals(0, mBluetoothDeviceManager.getNumConnectedDevices()); 98 assertNull(mBluetoothDeviceManager.getMostRecentlyConnectedDevice(null)); 99 } 100 101 @SmallTest 102 @Test 103 public void testMultiDeviceConnectAndDisconnect() { 104 receiverUnderTest.onReceive(mContext, 105 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device1)); 106 receiverUnderTest.onReceive(mContext, 107 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2)); 108 receiverUnderTest.onReceive(mContext, 109 buildConnectionActionIntent(BluetoothHeadset.STATE_DISCONNECTED, device1)); 110 receiverUnderTest.onReceive(mContext, 111 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device3)); 112 receiverUnderTest.onReceive(mContext, 113 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2)); 114 assertEquals(2, mBluetoothDeviceManager.getNumConnectedDevices()); 115 assertEquals(device3.getAddress(), 116 mBluetoothDeviceManager.getMostRecentlyConnectedDevice(null)); 117 receiverUnderTest.onReceive(mContext, 118 buildConnectionActionIntent(BluetoothHeadset.STATE_DISCONNECTED, device3)); 119 assertEquals(1, mBluetoothDeviceManager.getNumConnectedDevices()); 120 assertEquals(device2.getAddress(), 121 mBluetoothDeviceManager.getMostRecentlyConnectedDevice(null)); 122 } 123 124 @SmallTest 125 @Test 126 public void testExclusionaryGetRecentDevices() { 127 receiverUnderTest.onReceive(mContext, 128 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device1)); 129 receiverUnderTest.onReceive(mContext, 130 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2)); 131 receiverUnderTest.onReceive(mContext, 132 buildConnectionActionIntent(BluetoothHeadset.STATE_DISCONNECTED, device1)); 133 receiverUnderTest.onReceive(mContext, 134 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device3)); 135 receiverUnderTest.onReceive(mContext, 136 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2)); 137 assertEquals(2, mBluetoothDeviceManager.getNumConnectedDevices()); 138 assertEquals(device2.getAddress(), 139 mBluetoothDeviceManager.getMostRecentlyConnectedDevice(device3.getAddress())); 140 } 141 142 @SmallTest 143 @Test 144 public void testHeadsetServiceDisconnect() { 145 receiverUnderTest.onReceive(mContext, 146 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device1)); 147 receiverUnderTest.onReceive(mContext, 148 buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device2)); 149 serviceListenerUnderTest.onServiceDisconnected(0); 150 151 verify(mRouteManager).onDeviceLost(device1.getAddress()); 152 verify(mRouteManager).onDeviceLost(device2.getAddress()); 153 assertNull(mBluetoothDeviceManager.getHeadsetService()); 154 assertEquals(0, mBluetoothDeviceManager.getNumConnectedDevices()); 155 } 156 157 private Intent buildConnectionActionIntent(int state, BluetoothDevice device) { 158 Intent i = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); 159 i.putExtra(BluetoothHeadset.EXTRA_STATE, state); 160 i.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 161 return i; 162 } 163 164 private BluetoothDevice makeBluetoothDevice(String address) { 165 Parcel p1 = Parcel.obtain(); 166 p1.writeString(address); 167 p1.setDataPosition(0); 168 BluetoothDevice device = BluetoothDevice.CREATOR.createFromParcel(p1); 169 p1.recycle(); 170 return device; 171 } 172 } 173