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 android.bluetooth.BluetoothAdapter;
20 import android.content.Context;
21 
22 import androidx.test.InstrumentationRegistry;
23 import androidx.test.filters.MediumTest;
24 import androidx.test.rule.ServiceTestRule;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.bluetooth.R;
28 import com.android.bluetooth.TestUtils;
29 import com.android.bluetooth.btservice.AdapterService;
30 
31 import org.junit.After;
32 import org.junit.Assert;
33 import org.junit.Assume;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 
41 @MediumTest
42 @RunWith(AndroidJUnit4.class)
43 public class HeadsetClientServiceTest {
44     private HeadsetClientService mService = null;
45     private BluetoothAdapter mAdapter = null;
46     private Context mTargetContext;
47 
48     @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
49 
50     @Mock private AdapterService mAdapterService;
51 
52     @Before
setUp()53     public void setUp() throws Exception {
54         mTargetContext = InstrumentationRegistry.getTargetContext();
55         Assume.assumeTrue("Ignore test when HeadsetClientService is not enabled",
56                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_hfpclient));
57         MockitoAnnotations.initMocks(this);
58         TestUtils.setAdapterService(mAdapterService);
59         TestUtils.startService(mServiceRule, HeadsetClientService.class);
60         // At this point the service should have started so check NOT null
61         mService = HeadsetClientService.getHeadsetClientService();
62         Assert.assertNotNull(mService);
63         // Try getting the Bluetooth adapter
64         mAdapter = BluetoothAdapter.getDefaultAdapter();
65         Assert.assertNotNull(mAdapter);
66     }
67 
68     @After
tearDown()69     public void tearDown() throws Exception {
70         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_hfpclient)) {
71             return;
72         }
73         TestUtils.stopService(mServiceRule, HeadsetClientService.class);
74         mService = HeadsetClientService.getHeadsetClientService();
75         Assert.assertNull(mService);
76         TestUtils.clearAdapterService(mAdapterService);
77     }
78 
79     @Test
testInitialize()80     public void testInitialize() {
81         Assert.assertNotNull(HeadsetClientService.getHeadsetClientService());
82     }
83 }
84