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 */ 16package com.android.settingslib.bluetooth; 17 18import static org.mockito.ArgumentMatchers.any; 19import static org.mockito.ArgumentMatchers.anyInt; 20import static org.mockito.Mockito.verify; 21 22import android.bluetooth.BluetoothHeadset; 23import android.bluetooth.BluetoothProfile; 24import android.content.Context; 25import android.content.Intent; 26 27import android.telephony.TelephonyManager; 28 29import org.junit.Before; 30import org.junit.Test; 31import org.junit.runner.RunWith; 32import org.mockito.Mock; 33import org.mockito.MockitoAnnotations; 34import org.robolectric.RobolectricTestRunner; 35import org.robolectric.RuntimeEnvironment; 36 37@RunWith(RobolectricTestRunner.class) 38public class BluetoothEventManagerTest { 39 40 @Mock 41 private LocalBluetoothAdapter mLocalAdapter; 42 @Mock 43 private CachedBluetoothDeviceManager mCachedDeviceManager; 44 @Mock 45 private BluetoothCallback mBluetoothCallback; 46 @Mock 47 private CachedBluetoothDevice mCachedBluetoothDevice; 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 } 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 102 verify(mCachedDeviceManager).onProfileConnectionStateChanged(mCachedBluetoothDevice, 103 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 104 } 105} 106