• 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 
17 package com.android.bluetooth.sap;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.anyString;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.when;
24 
25 import android.bluetooth.BluetoothAdapter;
26 import android.bluetooth.BluetoothDevice;
27 import android.bluetooth.BluetoothProfile;
28 import android.content.Context;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.filters.MediumTest;
32 import androidx.test.rule.ServiceTestRule;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import com.android.bluetooth.TestUtils;
36 import com.android.bluetooth.btservice.AdapterService;
37 import com.android.bluetooth.btservice.storage.DatabaseManager;
38 
39 import org.junit.After;
40 import org.junit.Assume;
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 
48 import java.util.concurrent.CountDownLatch;
49 import java.util.concurrent.TimeUnit;
50 import java.util.concurrent.atomic.AtomicBoolean;
51 
52 @MediumTest
53 @RunWith(AndroidJUnit4.class)
54 public class SapServiceTest {
55     private static final int TIMEOUT_MS = 5_000;
56 
57     private SapService mService = null;
58     private BluetoothAdapter mAdapter = null;
59     private Context mTargetContext;
60 
61     @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
62 
63     @Mock private AdapterService mAdapterService;
64     @Mock private DatabaseManager mDatabaseManager;
65     private BluetoothDevice mDevice;
66 
67     @Before
setUp()68     public void setUp() throws Exception {
69         mTargetContext = InstrumentationRegistry.getTargetContext();
70         MockitoAnnotations.initMocks(this);
71         TestUtils.setAdapterService(mAdapterService);
72         doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
73         TestUtils.startService(mServiceRule, SapService.class);
74         mService = SapService.getSapService();
75         assertThat(mService).isNotNull();
76         // Try getting the Bluetooth adapter
77         mAdapter = BluetoothAdapter.getDefaultAdapter();
78         assertThat(mAdapter).isNotNull();
79         mDevice = TestUtils.getTestDevice(mAdapter, 0);
80     }
81 
82     @After
tearDown()83     public void tearDown() throws Exception {
84         TestUtils.stopService(mServiceRule, SapService.class);
85         mService = SapService.getSapService();
86         assertThat(mService).isNull();
87         TestUtils.clearAdapterService(mAdapterService);
88     }
89 
90     @Test
testGetSapService()91     public void testGetSapService() {
92         assertThat(mService).isEqualTo(SapService.getSapService());
93         assertThat(mService.getConnectedDevices()).isEmpty();
94     }
95 
96     /**
97      * Test stop SAP Service
98      */
99     @Test
testStopSapService()100     public void testStopSapService() throws Exception {
101         AtomicBoolean stopResult = new AtomicBoolean();
102         AtomicBoolean startResult = new AtomicBoolean();
103         CountDownLatch latch = new CountDownLatch(1);
104 
105         // SAP Service is already running: test stop(). Note: must be done on the main thread
106         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
107             public void run() {
108                 stopResult.set(mService.stop());
109                 startResult.set(mService.start());
110                 latch.countDown();
111             }
112         });
113 
114         assertThat(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
115         assertThat(stopResult.get()).isTrue();
116         assertThat(startResult.get()).isTrue();
117     }
118 
119     /**
120      * Test get connection policy for BluetoothDevice
121      */
122     @Test
testGetConnectionPolicy()123     public void testGetConnectionPolicy() {
124         when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
125         when(mDatabaseManager
126                 .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
127                 .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);
128         assertThat(mService.getConnectionPolicy(mDevice))
129                 .isEqualTo(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);
130 
131         when(mDatabaseManager
132                 .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
133                 .thenReturn(BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
134         assertThat(mService.getConnectionPolicy(mDevice))
135                 .isEqualTo(BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
136 
137         when(mDatabaseManager
138                 .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
139                 .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
140 
141         assertThat(mService.getConnectionPolicy(mDevice))
142                 .isEqualTo(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
143     }
144 
145     @Test
testGetRemoteDevice()146     public void testGetRemoteDevice() {
147         assertThat(mService.getRemoteDevice()).isNull();
148     }
149 
150     @Test
testGetRemoteDeviceName()151     public void testGetRemoteDeviceName() {
152         assertThat(SapService.getRemoteDeviceName()).isNull();
153     }
154 }
155 
156 
157