• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.settings.development;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.os.UserHandle;
31 
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.settingslib.RestrictedLockUtils;
35 import com.android.settingslib.RestrictedSwitchPreference;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class DefaultUsbConfigurationPreferenceControllerTest {
47 
48     private static final ComponentName TEST_COMPONENT_NAME = new ComponentName("test", "test");
49 
50     @Mock
51     private RestrictedSwitchPreference mPreference;
52     @Mock
53     private PreferenceScreen mPreferenceScreen;
54     @Mock
55     private DevicePolicyManager mDevicePolicyManager;
56 
57     private DefaultUsbConfigurationPreferenceController mController;
58 
59     @Before
setup()60     public void setup() throws Exception {
61         MockitoAnnotations.initMocks(this);
62         Context context = spy(RuntimeEnvironment.application);
63         mController = new DefaultUsbConfigurationPreferenceController(context);
64         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
65                 .thenReturn(mPreference);
66         mController.displayPreference(mPreferenceScreen);
67         when(context.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
68         doReturn(context).when(context).createPackageContextAsUser(
69                 any(String.class), anyInt(), any(UserHandle.class));
70     }
71 
72     @Test
updateState_usbDataSignalingEnabled_shouldNotDisablePreference()73     public void updateState_usbDataSignalingEnabled_shouldNotDisablePreference() {
74         when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser(
75                 UserHandle.myUserId())).thenReturn(true);
76         when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME);
77 
78         mController.updateState(mPreference);
79 
80         verify(mPreference).setDisabledByAdmin(null);
81     }
82 
83     @Test
updateState_usbDataSignalingDisabled_shouldDisablePreference()84     public void updateState_usbDataSignalingDisabled_shouldDisablePreference() {
85         when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser(
86                 UserHandle.myUserId())).thenReturn(false);
87         when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME);
88 
89         mController.updateState(mPreference);
90 
91         verify(mPreference).setDisabledByAdmin(eq(new RestrictedLockUtils.EnforcedAdmin(
92                 TEST_COMPONENT_NAME, null, UserHandle.SYSTEM)));
93     }
94 
95     @Test
onDeveloperOptionsSwitchEnabled_usbEnabled_shouldNotDisablePreference()96     public void onDeveloperOptionsSwitchEnabled_usbEnabled_shouldNotDisablePreference() {
97         when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser(
98                 UserHandle.myUserId())).thenReturn(true);
99         when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME);
100 
101         mController.onDeveloperOptionsSwitchEnabled();
102 
103         verify(mPreference).setDisabledByAdmin(null);
104     }
105 
106     @Test
onDeveloperOptionsSwitchEnabled_usbDisabled_shouldDisablePreference()107     public void onDeveloperOptionsSwitchEnabled_usbDisabled_shouldDisablePreference() {
108         when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser(
109                 UserHandle.myUserId())).thenReturn(false);
110         when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME);
111 
112         mController.onDeveloperOptionsSwitchEnabled();
113 
114         verify(mPreference).setDisabledByAdmin(eq(new RestrictedLockUtils.EnforcedAdmin(
115                 TEST_COMPONENT_NAME, null, UserHandle.SYSTEM)));
116     }
117 }
118