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.pan;
17 
18 import static org.mockito.Mockito.when;
19 
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothDevice;
22 import android.content.Context;
23 import android.os.UserManager;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.filters.MediumTest;
27 import androidx.test.rule.ServiceTestRule;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.bluetooth.R;
31 import com.android.bluetooth.TestUtils;
32 import com.android.bluetooth.btservice.AdapterService;
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 PanServiceTest {
47     private PanService 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     @Mock private UserManager mMockUserManager;
55 
56     @Before
setUp()57     public void setUp() throws Exception {
58         mTargetContext = InstrumentationRegistry.getTargetContext();
59         Assume.assumeTrue("Ignore test when PanService is not enabled",
60                 mTargetContext.getResources().getBoolean(R.bool.profile_supported_pan));
61         MockitoAnnotations.initMocks(this);
62         TestUtils.setAdapterService(mAdapterService);
63         TestUtils.startService(mServiceRule, PanService.class);
64         mService = PanService.getPanService();
65         Assert.assertNotNull(mService);
66         // Try getting the Bluetooth adapter
67         mAdapter = BluetoothAdapter.getDefaultAdapter();
68         Assert.assertNotNull(mAdapter);
69         mService.mUserManager = mMockUserManager;
70     }
71 
72     @After
tearDown()73     public void tearDown() throws Exception {
74         if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_pan)) {
75             return;
76         }
77         TestUtils.stopService(mServiceRule, PanService.class);
78         mService = PanService.getPanService();
79         Assert.assertNull(mService);
80         TestUtils.clearAdapterService(mAdapterService);
81     }
82 
83     @Test
testInitialize()84     public void testInitialize() {
85         Assert.assertNotNull(PanService.getPanService());
86     }
87 
88     @Test
testGuestUserConnect()89     public void testGuestUserConnect() {
90         BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
91         when(mMockUserManager.isGuestUser()).thenReturn(true);
92         Assert.assertFalse(mService.connect(device));
93     }
94 }
95