• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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.server.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.verify;
28 
29 import android.content.Context;
30 import android.content.ContextWrapper;
31 import android.content.Intent;
32 import android.os.HandlerThread;
33 import android.os.UserHandle;
34 import android.os.UserManager;
35 import android.provider.Settings;
36 
37 import androidx.test.platform.app.InstrumentationRegistry;
38 import androidx.test.runner.AndroidJUnit4;
39 
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 
47 @RunWith(AndroidJUnit4.class)
48 public class BluetoothManagerServiceTest {
49     static int sTimeout = 3000;
50     BluetoothManagerService mManagerService;
51     Context mContext;
52     @Mock
53     BluetoothServerProxy mBluetoothServerProxy;
54     @Mock
55     BluetoothManagerService.BluetoothHandler mHandler;
56     HandlerThread mHandlerThread;
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61         mContext = spy(new ContextWrapper(
62                 InstrumentationRegistry.getInstrumentation().getTargetContext()));
63         mHandlerThread = new HandlerThread("BluetoothManagerServiceTest");
64     }
65 
66     @After
tearDown()67     public void tearDown() {
68         mHandlerThread.quitSafely();
69     }
70 
createBluetoothManagerService()71     private void createBluetoothManagerService() {
72         doReturn(mock(Intent.class)).when(mContext).registerReceiverForAllUsers(any(), any(),
73                 eq(null), eq(null));
74         BluetoothServerProxy.setInstanceForTesting(mBluetoothServerProxy);
75         // Mock the handler to avoid handle message & to terminate the thread after
76         // test
77         doReturn(mHandlerThread).when(mBluetoothServerProxy).createHandlerThread(any());
78         doReturn(mHandler).when(mBluetoothServerProxy).newBluetoothHandler(any());
79 
80         // Mock these functions so security errors won't throw
81         doReturn("name").when(mBluetoothServerProxy).settingsSecureGetString(any(),
82                 eq(Settings.Secure.BLUETOOTH_NAME));
83         doReturn("00:11:22:33:44:55").when(mBluetoothServerProxy).settingsSecureGetString(any(),
84                 eq(Settings.Secure.BLUETOOTH_ADDRESS));
85         mManagerService = new BluetoothManagerService(mContext);
86     }
87 
88     @Test
onUserRestrictionsChanged_disallowBluetooth_onlySendDisableMessageOnSystemUser()89     public void onUserRestrictionsChanged_disallowBluetooth_onlySendDisableMessageOnSystemUser()
90             throws InterruptedException {
91         // Spy UserManager so we can mimic the case when restriction settings changed
92         UserManager userManager = mock(UserManager.class);
93         doReturn(userManager).when(mContext).getSystemService(UserManager.class);
94         doReturn(true).when(userManager).hasUserRestrictionForUser(
95                 eq(UserManager.DISALLOW_BLUETOOTH), any());
96         doReturn(false).when(userManager).hasUserRestrictionForUser(
97                 eq(UserManager.DISALLOW_BLUETOOTH_SHARING), any());
98         createBluetoothManagerService();
99 
100         // Check if disable message sent once for system user only
101         // Since Message object is recycled after processed, use proxy function to get what value
102 
103         // test run on user -1, should not turning Bluetooth off
104         mManagerService.onUserRestrictionsChanged(UserHandle.CURRENT);
105         verify(mBluetoothServerProxy, timeout(sTimeout).times(0)).handlerSendWhatMessage(mHandler,
106                 BluetoothManagerService.MESSAGE_DISABLE);
107 
108         // called from SYSTEM user, should try to toggle Bluetooth off
109         mManagerService.onUserRestrictionsChanged(UserHandle.SYSTEM);
110         verify(mBluetoothServerProxy, timeout(sTimeout)).handlerSendWhatMessage(mHandler,
111                 BluetoothManagerService.MESSAGE_DISABLE);
112     }
113 
114     @Test
testApmEnhancementEnabled()115     public void testApmEnhancementEnabled() {
116         createBluetoothManagerService();
117         mManagerService.setBluetoothModeChangeHelper(new BluetoothModeChangeHelper(mContext));
118 
119         // Change the apm enhancement enabled value to 0
120         Settings.Global.putInt(mContext.getContentResolver(),
121                 "apm_enhancement_enabled", 0);
122         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
123                 "apm_enhancement_enabled",  0)).isEqualTo(0);
124 
125         // Confirm that apm enhancement enabled value has been updated to 1
126         mManagerService.loadApmEnhancementStateFromResource();
127         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
128                 "apm_enhancement_enabled",  0)).isEqualTo(1);
129     }
130 }
131