blob: 88c7a556d51e7f7cb177b8e7dbb2d090ec10b0f1 [file] [log] [blame]
Hansong Zhang490d0202018-04-10 16:06:19 -07001/*
2 * Copyright (C) 2018 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.settingslib.bluetooth;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.mockito.Mockito.when;
22
23import android.bluetooth.BluetoothAdapter;
24import android.bluetooth.BluetoothDevice;
25import android.bluetooth.BluetoothUuid;
26import android.content.Context;
27import android.os.ParcelUuid;
28
29import java.util.ArrayList;
30import java.util.List;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.RobolectricTestRunner;
38import org.robolectric.RuntimeEnvironment;
39import org.robolectric.annotation.Config;
40
41@RunWith(RobolectricTestRunner.class)
42@Config(resourceDir = "../../res")
43public class LocalBluetoothProfileManagerTest {
44 @Mock private CachedBluetoothDeviceManager mDeviceManager;
45 @Mock private BluetoothEventManager mEventManager;
46 @Mock private LocalBluetoothAdapter mAdapter;
47 @Mock private BluetoothDevice mDevice;
48 private Context mContext;
49 private LocalBluetoothProfileManager mProfileManager;
50
51 @Before
52 public void setUp() {
53 MockitoAnnotations.initMocks(this);
54 mContext = RuntimeEnvironment.application;
55 when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
56 }
57
58 /**
59 * Verify HID and HID Device profiles are not null without running updateUuids()
60 */
61 @Test
62 public void constructor_initiateHidAndHidDeviceProfile() {
63 mProfileManager =
64 new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, mEventManager);
65
66 assertThat(mProfileManager.getHidProfile()).isNotNull();
67 assertThat(mProfileManager.getHidDeviceProfile()).isNotNull();
68 }
69
70 /**
71 * Verify updateLocalProfiles() for a local A2DP source adds A2dpProfile
72 */
73 @Test
74 public void updateLocalProfiles_addA2dpToLocalProfiles() {
75 mProfileManager =
76 new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, mEventManager);
77 when(mAdapter.getUuids()).thenReturn(new ParcelUuid[] {BluetoothUuid.AudioSource});
78 assertThat(mProfileManager.getA2dpProfile()).isNull();
79 assertThat(mProfileManager.getHeadsetProfile()).isNull();
80
81 ParcelUuid[] uuids = mAdapter.getUuids();
82 mProfileManager.updateLocalProfiles(uuids);
83
84 assertThat(mProfileManager.getA2dpProfile()).isNotNull();
85 assertThat(mProfileManager.getHeadsetProfile()).isNull();
86 }
87
88 /**
89 * Verify updateProfiles() for a remote HID device updates profiles and removedProfiles
90 */
91 @Test
92 public void updateProfiles_addHidProfileForRemoteDevice() {
93 mProfileManager =
94 new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, mEventManager);
95 ParcelUuid[] uuids = new ParcelUuid[]{BluetoothUuid.Hid};
96 ParcelUuid[] localUuids = new ParcelUuid[]{};
97 List<LocalBluetoothProfile> profiles = new ArrayList<>();
98 List<LocalBluetoothProfile> removedProfiles = new ArrayList<>();
99
100 mProfileManager.updateProfiles(uuids, localUuids, profiles, removedProfiles, false,
101 mDevice);
102
103 assertThat(mProfileManager.getHidProfile()).isNotNull();
104 assertThat(profiles.contains(mProfileManager.getHidProfile())).isTrue();
105 assertThat(removedProfiles.contains(mProfileManager.getHidProfile())).isFalse();
106 }
107}