Hansong Zhang | 490d020 | 2018-04-10 16:06:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settingslib.bluetooth; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
| 23 | import android.bluetooth.BluetoothAdapter; |
| 24 | import android.bluetooth.BluetoothDevice; |
| 25 | import android.bluetooth.BluetoothUuid; |
| 26 | import android.content.Context; |
| 27 | import android.os.ParcelUuid; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.mockito.Mock; |
| 36 | import org.mockito.MockitoAnnotations; |
| 37 | import org.robolectric.RobolectricTestRunner; |
| 38 | import org.robolectric.RuntimeEnvironment; |
| 39 | import org.robolectric.annotation.Config; |
| 40 | |
| 41 | @RunWith(RobolectricTestRunner.class) |
| 42 | @Config(resourceDir = "../../res") |
| 43 | public 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 | } |