timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | package com.android.settingslib.bluetooth; |
| 17 | |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame^] | 18 | import static org.mockito.ArgumentMatchers.any; |
| 19 | import static org.mockito.ArgumentMatchers.anyInt; |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 20 | import static org.mockito.Mockito.verify; |
| 21 | |
| 22 | import android.bluetooth.BluetoothHeadset; |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame^] | 23 | import android.bluetooth.BluetoothProfile; |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | |
| 27 | import android.telephony.TelephonyManager; |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame^] | 28 | |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.mockito.Mock; |
| 33 | import org.mockito.MockitoAnnotations; |
| 34 | import org.robolectric.RobolectricTestRunner; |
| 35 | import org.robolectric.RuntimeEnvironment; |
| 36 | |
| 37 | @RunWith(RobolectricTestRunner.class) |
| 38 | public class BluetoothEventManagerTest { |
| 39 | |
| 40 | @Mock |
| 41 | private LocalBluetoothAdapter mLocalAdapter; |
| 42 | @Mock |
| 43 | private CachedBluetoothDeviceManager mCachedDeviceManager; |
| 44 | @Mock |
| 45 | private BluetoothCallback mBluetoothCallback; |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame^] | 46 | @Mock |
| 47 | private CachedBluetoothDevice mCachedBluetoothDevice; |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 48 | |
| 49 | private Context mContext; |
| 50 | private Intent mIntent; |
| 51 | private BluetoothEventManager mBluetoothEventManager; |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | MockitoAnnotations.initMocks(this); |
| 56 | mContext = RuntimeEnvironment.application; |
| 57 | |
| 58 | mBluetoothEventManager = new BluetoothEventManager(mLocalAdapter, |
| 59 | mCachedDeviceManager, mContext); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Intent ACTION_AUDIO_STATE_CHANGED should dispatch to callback. |
| 64 | */ |
| 65 | @Test |
| 66 | public void intentWithExtraState_audioStateChangedShouldDispatchToRegisterCallback() { |
| 67 | mBluetoothEventManager.registerCallback(mBluetoothCallback); |
| 68 | mIntent = new Intent(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
| 69 | |
| 70 | mContext.sendBroadcast(mIntent); |
| 71 | |
| 72 | verify(mBluetoothCallback).onAudioModeChanged(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Intent ACTION_PHONE_STATE_CHANGED should dispatch to callback. |
| 77 | */ |
| 78 | @Test |
| 79 | public void intentWithExtraState_phoneStateChangedShouldDispatchToRegisterCallback() { |
| 80 | mBluetoothEventManager.registerCallback(mBluetoothCallback); |
| 81 | mIntent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| 82 | |
| 83 | mContext.sendBroadcast(mIntent); |
| 84 | |
| 85 | verify(mBluetoothCallback).onAudioModeChanged(); |
| 86 | } |
ryanywlin | 44de3a0 | 2018-05-02 15:15:37 +0800 | [diff] [blame^] | 87 | |
| 88 | /** |
| 89 | * dispatchProfileConnectionStateChanged should dispatch to onProfileConnectionStateChanged |
| 90 | * callback. |
| 91 | */ |
| 92 | @Test |
| 93 | public void dispatchProfileConnectionStateChanged_registerCallback_shouldDispatchCallback() { |
| 94 | mBluetoothEventManager.registerCallback(mBluetoothCallback); |
| 95 | |
| 96 | mBluetoothEventManager.dispatchProfileConnectionStateChanged(mCachedBluetoothDevice, |
| 97 | BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); |
| 98 | |
| 99 | verify(mBluetoothCallback).onProfileConnectionStateChanged(mCachedBluetoothDevice, |
| 100 | BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); |
| 101 | } |
timhypeng | e745e4a | 2018-04-08 13:55:08 +0800 | [diff] [blame] | 102 | } |