1 /*
2  * Copyright 2017 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.bluetooth.hfpclient;
18 
19 import static org.mockito.Mockito.anyInt;
20 import static org.mockito.Mockito.anyString;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.eq;
23 import static org.mockito.Mockito.timeout;
24 import static org.mockito.Mockito.verify;
25 
26 import android.bluetooth.BluetoothAdapter;
27 import android.bluetooth.BluetoothDevice;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.os.BatteryManager;
31 
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.filters.MediumTest;
34 import androidx.test.rule.ServiceTestRule;
35 import androidx.test.runner.AndroidJUnit4;
36 
37 import com.android.bluetooth.R;
38 import com.android.bluetooth.TestUtils;
39 import com.android.bluetooth.btservice.AdapterService;
40 import com.android.bluetooth.btservice.storage.DatabaseManager;
41 
42 import org.junit.After;
43 import org.junit.Assert;
44 import org.junit.Assume;
45 import org.junit.Before;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 
52 @MediumTest
53 @RunWith(AndroidJUnit4.class)
54 public class HeadsetClientServiceTest {
55     private HeadsetClientService mService = null;
56     private BluetoothAdapter mAdapter = null;
57     private Context mTargetContext;
58 
59     private static final int STANDARD_WAIT_MILLIS = 1000;
60 
61     @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
62 
63     @Mock private AdapterService mAdapterService;
64     @Mock private HeadsetClientStateMachine mStateMachine;
65 
66     @Mock private DatabaseManager mDatabaseManager;
67 
68     @Before
setUp()69     public void setUp() throws Exception {
70         mTargetContext = InstrumentationRegistry.getTargetContext();
71         Assume.assumeTrue("Ignore test when HeadsetClientService is not enabled",
72                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_hfpclient));
73         MockitoAnnotations.initMocks(this);
74         TestUtils.setAdapterService(mAdapterService);
75         doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
76         doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
77         TestUtils.startService(mServiceRule, HeadsetClientService.class);
78         // At this point the service should have started so check NOT null
79         mService = HeadsetClientService.getHeadsetClientService();
80         Assert.assertNotNull(mService);
81         // Try getting the Bluetooth adapter
82         mAdapter = BluetoothAdapter.getDefaultAdapter();
83         Assert.assertNotNull(mAdapter);
84     }
85 
86     @After
tearDown()87     public void tearDown() throws Exception {
88         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_hfpclient)) {
89             return;
90         }
91         TestUtils.stopService(mServiceRule, HeadsetClientService.class);
92         mService = HeadsetClientService.getHeadsetClientService();
93         Assert.assertNull(mService);
94         TestUtils.clearAdapterService(mAdapterService);
95     }
96 
97     @Test
testInitialize()98     public void testInitialize() {
99         Assert.assertNotNull(HeadsetClientService.getHeadsetClientService());
100     }
101 
102     @Test
testSendBIEVtoStateMachineWhenBatteryChanged()103     public void testSendBIEVtoStateMachineWhenBatteryChanged() {
104         // Put mock state machine
105         BluetoothDevice device =
106                 BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:01:02:03:04:05");
107         mService.getStateMachineMap().put(device, mStateMachine);
108 
109         // Send battery changed intent
110         Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
111         intent.putExtra(BatteryManager.EXTRA_LEVEL, 50);
112         mService.sendBroadcast(intent);
113 
114         // Expect send BIEV to state machine
115         verify(mStateMachine, timeout(STANDARD_WAIT_MILLIS).times(1))
116                 .sendMessage(
117                     eq(HeadsetClientStateMachine.SEND_BIEV),
118                     eq(2),
119                     anyInt());
120     }
121 
122     @Test
testUpdateBatteryLevel()123     public void testUpdateBatteryLevel() {
124         // Put mock state machine
125         BluetoothDevice device =
126                 BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:01:02:03:04:05");
127         mService.getStateMachineMap().put(device, mStateMachine);
128 
129         mService.updateBatteryLevel();
130 
131         // Expect send BIEV to state machine
132         verify(mStateMachine, timeout(STANDARD_WAIT_MILLIS).times(1))
133                 .sendMessage(
134                     eq(HeadsetClientStateMachine.SEND_BIEV),
135                     eq(2),
136                     anyInt());
137     }
138 }
139