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.anyString; 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.when; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.bluetooth.BluetoothDevice; 24 import android.content.Context; 25 import android.os.UserManager; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.filters.MediumTest; 29 import androidx.test.rule.ServiceTestRule; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import com.android.bluetooth.R; 33 import com.android.bluetooth.TestUtils; 34 import com.android.bluetooth.btservice.AdapterService; 35 import com.android.bluetooth.btservice.storage.DatabaseManager; 36 37 import org.junit.After; 38 import org.junit.Assert; 39 import org.junit.Assume; 40 import org.junit.Before; 41 import org.junit.Rule; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 47 @MediumTest 48 @RunWith(AndroidJUnit4.class) 49 public class PanServiceTest { 50 private PanService mService = null; 51 private BluetoothAdapter mAdapter = null; 52 private Context mTargetContext; 53 54 @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); 55 56 @Mock private AdapterService mAdapterService; 57 @Mock private DatabaseManager mDatabaseManager; 58 @Mock private UserManager mMockUserManager; 59 60 @Before setUp()61 public void setUp() throws Exception { 62 mTargetContext = InstrumentationRegistry.getTargetContext(); 63 Assume.assumeTrue("Ignore test when PanService is not enabled", 64 mTargetContext.getResources().getBoolean(R.bool.profile_supported_pan)); 65 MockitoAnnotations.initMocks(this); 66 TestUtils.setAdapterService(mAdapterService); 67 doReturn(mDatabaseManager).when(mAdapterService).getDatabase(); 68 doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); 69 TestUtils.startService(mServiceRule, PanService.class); 70 mService = PanService.getPanService(); 71 Assert.assertNotNull(mService); 72 // Try getting the Bluetooth adapter 73 mAdapter = BluetoothAdapter.getDefaultAdapter(); 74 Assert.assertNotNull(mAdapter); 75 mService.mUserManager = mMockUserManager; 76 } 77 78 @After tearDown()79 public void tearDown() throws Exception { 80 if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_pan)) { 81 return; 82 } 83 TestUtils.stopService(mServiceRule, PanService.class); 84 mService = PanService.getPanService(); 85 Assert.assertNull(mService); 86 TestUtils.clearAdapterService(mAdapterService); 87 } 88 89 @Test testInitialize()90 public void testInitialize() { 91 Assert.assertNotNull(PanService.getPanService()); 92 } 93 94 @Test testGuestUserConnect()95 public void testGuestUserConnect() { 96 BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0); 97 when(mMockUserManager.isGuestUser()).thenReturn(true); 98 Assert.assertFalse(mService.connect(device)); 99 } 100 } 101