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