blob: 1080690225676b36bc3e74b51c4bf0b276cc871c [file] [log] [blame]
timhypenge745e4a2018-04-08 13:55:08 +08001/*
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
ryanywlin44de3a02018-05-02 15:15:37 +080018import static org.mockito.ArgumentMatchers.any;
19import static org.mockito.ArgumentMatchers.anyInt;
timhypenge745e4a2018-04-08 13:55:08 +080020import static org.mockito.Mockito.verify;
21
22import android.bluetooth.BluetoothHeadset;
ryanywlin44de3a02018-05-02 15:15:37 +080023import android.bluetooth.BluetoothProfile;
timhypenge745e4a2018-04-08 13:55:08 +080024import android.content.Context;
25import android.content.Intent;
26
27import android.telephony.TelephonyManager;
ryanywlin44de3a02018-05-02 15:15:37 +080028
timhypenge745e4a2018-04-08 13:55:08 +080029import 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;
ryanywlin44de3a02018-05-02 15:15:37 +080046 @Mock
47 private CachedBluetoothDevice mCachedBluetoothDevice;
timhypenge745e4a2018-04-08 13:55:08 +080048
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 }
ryanywlin44de3a02018-05-02 15:15:37 +080087
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);
Stanley Tng61598422018-05-13 15:08:33 -0700101
102 verify(mCachedDeviceManager).onProfileConnectionStateChanged(mCachedBluetoothDevice,
103 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
ryanywlin44de3a02018-05-02 15:15:37 +0800104 }
timhypenge745e4a2018-04-08 13:55:08 +0800105}