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.opp;
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 BluetoothOppServiceTest {
46     private BluetoothOppService 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 BluetoothOppService is not enabled",
58                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_opp));
59         MockitoAnnotations.initMocks(this);
60         TestUtils.setAdapterService(mAdapterService);
61         doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
62         TestUtils.startService(mServiceRule, BluetoothOppService.class);
63         mService = BluetoothOppService.getBluetoothOppService();
64         Assert.assertNotNull(mService);
65         // Try getting the Bluetooth adapter
66         mAdapter = BluetoothAdapter.getDefaultAdapter();
67         Assert.assertNotNull(mAdapter);
68     }
69 
70     @After
tearDown()71     public void tearDown() throws Exception {
72         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_opp)) {
73             return;
74         }
75         TestUtils.stopService(mServiceRule, BluetoothOppService.class);
76         TestUtils.clearAdapterService(mAdapterService);
77     }
78 
79     @Test
testInitialize()80     public void testInitialize() {
81         Assert.assertNotNull(BluetoothOppService.getBluetoothOppService());
82     }
83 }
84