• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.pbapclient;
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 PbapClientServiceTest {
47     private PbapClientService mService = null;
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 
55     @Mock private DatabaseManager mDatabaseManager;
56 
57     @Before
setUp()58     public void setUp() throws Exception {
59         mTargetContext = InstrumentationRegistry.getTargetContext();
60         Assume.assumeTrue("Ignore test when PbapClientService is not enabled",
61                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_pbapclient));
62         MockitoAnnotations.initMocks(this);
63         TestUtils.setAdapterService(mAdapterService);
64         doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
65         doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
66         TestUtils.startService(mServiceRule, PbapClientService.class);
67         mService = PbapClientService.getPbapClientService();
68         Assert.assertNotNull(mService);
69         // Try getting the Bluetooth adapter
70         mAdapter = BluetoothAdapter.getDefaultAdapter();
71         Assert.assertNotNull(mAdapter);
72     }
73 
74     @After
tearDown()75     public void tearDown() throws Exception {
76         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_pbapclient)) {
77             return;
78         }
79         TestUtils.stopService(mServiceRule, PbapClientService.class);
80         mService = PbapClientService.getPbapClientService();
81         Assert.assertNull(mService);
82         TestUtils.clearAdapterService(mAdapterService);
83     }
84 
85     @Test
testInitialize()86     public void testInitialize() {
87         Assert.assertNotNull(PbapClientService.getPbapClientService());
88     }
89 }
90