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