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.pbap;
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 import com.android.bluetooth.btservice.storage.DatabaseManager;
33 
34 import org.junit.After;
35 import org.junit.Assert;
36 import org.junit.Assume;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 
44 @MediumTest
45 @RunWith(AndroidJUnit4.class)
46 public class BluetoothPbapServiceTest {
47     private BluetoothPbapService mService;
48     private BluetoothAdapter mAdapter = null;
49     private Context mTargetContext;
50 
51     @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
52 
53     @Mock private AdapterService mAdapterService;
54     @Mock private DatabaseManager mDatabaseManager;
55 
56     @Before
setUp()57     public void setUp() throws Exception {
58         mTargetContext = InstrumentationRegistry.getTargetContext();
59         Assume.assumeTrue("Ignore test when BluetoothPbapService is not enabled",
60                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_pbap));
61         MockitoAnnotations.initMocks(this);
62         TestUtils.setAdapterService(mAdapterService);
63         doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
64         doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
65         TestUtils.startService(mServiceRule, BluetoothPbapService.class);
66         mService = BluetoothPbapService.getBluetoothPbapService();
67         Assert.assertNotNull(mService);
68         // Try getting the Bluetooth adapter
69         mAdapter = BluetoothAdapter.getDefaultAdapter();
70         Assert.assertNotNull(mAdapter);
71     }
72 
73     @After
tearDown()74     public void tearDown() throws Exception {
75         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_pbap)) {
76             return;
77         }
78         TestUtils.stopService(mServiceRule, BluetoothPbapService.class);
79         mService = BluetoothPbapService.getBluetoothPbapService();
80         Assert.assertNull(mService);
81         TestUtils.clearAdapterService(mAdapterService);
82     }
83 
84     @Test
testInitialize()85     public void testInitialize() {
86         Assert.assertNotNull(BluetoothPbapService.getBluetoothPbapService());
87     }
88 }
89