• 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         Assume.assumeTrue("Ignore test when SapService is not enabled",
71                 SapService.isEnabled());
72         MockitoAnnotations.initMocks(this);
73         TestUtils.setAdapterService(mAdapterService);
74         doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
75         TestUtils.startService(mServiceRule, SapService.class);
76         mService = SapService.getSapService();
77         assertThat(mService).isNotNull();
78         // Try getting the Bluetooth adapter
79         mAdapter = BluetoothAdapter.getDefaultAdapter();
80         assertThat(mAdapter).isNotNull();
81         mDevice = TestUtils.getTestDevice(mAdapter, 0);
82     }
83 
84     @After
tearDown()85     public void tearDown() throws Exception {
86         if (!SapService.isEnabled()) {
87             return;
88         }
89         TestUtils.stopService(mServiceRule, SapService.class);
90         mService = SapService.getSapService();
91         assertThat(mService).isNull();
92         TestUtils.clearAdapterService(mAdapterService);
93     }
94 
95     @Test
testGetSapService()96     public void testGetSapService() {
97         assertThat(mService).isEqualTo(SapService.getSapService());
98         assertThat(mService.getConnectedDevices()).isEmpty();
99     }
100 
101     /**
102      * Test stop SAP Service
103      */
104     @Test
testStopSapService()105     public void testStopSapService() throws Exception {
106         AtomicBoolean stopResult = new AtomicBoolean();
107         AtomicBoolean startResult = new AtomicBoolean();
108         CountDownLatch latch = new CountDownLatch(1);
109 
110         // SAP Service is already running: test stop(). Note: must be done on the main thread
111         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
112             public void run() {
113                 stopResult.set(mService.stop());
114                 startResult.set(mService.start());
115                 latch.countDown();
116             }
117         });
118 
119         assertThat(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
120         assertThat(stopResult.get()).isTrue();
121         assertThat(startResult.get()).isTrue();
122     }
123 
124     /**
125      * Test get connection policy for BluetoothDevice
126      */
127     @Test
testGetConnectionPolicy()128     public void testGetConnectionPolicy() {
129         when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
130         when(mDatabaseManager
131                 .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
132                 .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);
133         assertThat(mService.getConnectionPolicy(mDevice))
134                 .isEqualTo(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);
135 
136         when(mDatabaseManager
137                 .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
138                 .thenReturn(BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
139         assertThat(mService.getConnectionPolicy(mDevice))
140                 .isEqualTo(BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
141 
142         when(mDatabaseManager
143                 .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
144                 .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
145 
146         assertThat(mService.getConnectionPolicy(mDevice))
147                 .isEqualTo(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
148     }
149 
150     @Test
testGetRemoteDevice()151     public void testGetRemoteDevice() {
152         assertThat(mService.getRemoteDevice()).isNull();
153     }
154 
155     @Test
testGetRemoteDeviceName()156     public void testGetRemoteDeviceName() {
157         assertThat(SapService.getRemoteDeviceName()).isNull();
158     }
159 }
160 
161 
162