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