• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.settings.development.ClearAdbKeysPreferenceController.RO_ADB_SECURE_PROPERTY_KEY;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.never;
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.Fragment;
28 import android.content.Context;
29 import android.hardware.usb.IUsbManager;
30 import android.os.RemoteException;
31 import android.os.SystemProperties;
32 import android.support.v14.preference.SwitchPreference;
33 import android.support.v7.preference.PreferenceScreen;
34 
35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
36 import com.android.settings.testutils.shadow.ShadowUtils;
37 
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.annotation.Config;
46 import org.robolectric.annotation.Implementation;
47 import org.robolectric.annotation.Implements;
48 import org.robolectric.util.ReflectionHelpers;
49 
50 @RunWith(SettingsRobolectricTestRunner.class)
51 @Config(shadows = ShadowUtils.class)
52 public class ClearAdbKeysPreferenceControllerTest {
53 
54     @Mock
55     private PreferenceScreen mScreen;
56     @Mock
57     private SwitchPreference mPreference;
58     @Mock
59     private IUsbManager mUsbManager;
60     @Mock
61     private DevelopmentSettingsDashboardFragment mFragment;
62 
63     private ClearAdbKeysPreferenceController mController;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         final Context context = RuntimeEnvironment.application;
69         mController = spy(new ClearAdbKeysPreferenceController(context, mFragment));
70         ReflectionHelpers.setField(mController, "mUsbManager", mUsbManager);
71         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
72     }
73 
74     @After
tearDown()75     public void tearDown() {
76         ShadowClearAdbKeysWarningDialog.resetDialog();
77         ShadowUtils.reset();
78     }
79 
80     @Test
isAvailable_roAdbSecureEnabled_shouldBeTrue()81     public void isAvailable_roAdbSecureEnabled_shouldBeTrue() {
82         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
83 
84         assertThat(mController.isAvailable()).isTrue();
85     }
86 
87     @Test
isAvailable_roAdbSecureDisabled_shouldBeFalse()88     public void isAvailable_roAdbSecureDisabled_shouldBeFalse() {
89         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(false));
90 
91         assertThat(mController.isAvailable()).isFalse();
92     }
93 
94     @Test
displayPreference_isNotAdminUser_preferenceShouldBeDisabled()95     public void displayPreference_isNotAdminUser_preferenceShouldBeDisabled() {
96         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
97         doReturn(false).when(mController).isAdminUser();
98 
99         mController.displayPreference(mScreen);
100 
101         verify(mPreference).setEnabled(false);
102     }
103 
104     @Test
105     @Config(shadows = ShadowClearAdbKeysWarningDialog.class)
handlePreferenceTreeClick_clearAdbKeysPreference_shouldShowWarningDialog()106     public void handlePreferenceTreeClick_clearAdbKeysPreference_shouldShowWarningDialog() {
107         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
108         doReturn(true).when(mController).isAdminUser();
109         mController.displayPreference(mScreen);
110         final String preferenceKey = mController.getPreferenceKey();
111         when(mPreference.getKey()).thenReturn(preferenceKey);
112         final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
113 
114         assertThat(ShadowClearAdbKeysWarningDialog.sIsShowing).isTrue();
115         assertThat(isHandled).isTrue();
116     }
117 
118     @Test
handlePreferenceTreeClick_notClearAdbKeysPreference_shouldReturnFalse()119     public void handlePreferenceTreeClick_notClearAdbKeysPreference_shouldReturnFalse() {
120         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
121         doReturn(true).when(mController).isAdminUser();
122         mController.displayPreference(mScreen);
123         when(mPreference.getKey()).thenReturn("Some random key!!!");
124         final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
125 
126         assertThat(isHandled).isFalse();
127     }
128 
129     @Test
handlePreferenceTreeClick_monkeyUser_shouldReturnFalse()130     public void handlePreferenceTreeClick_monkeyUser_shouldReturnFalse() {
131         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
132         doReturn(true).when(mController).isAdminUser();
133         ShadowUtils.setIsUserAMonkey(true);
134         mController.displayPreference(mScreen);
135         final String preferenceKey = mController.getPreferenceKey();
136         when(mPreference.getKey()).thenReturn(preferenceKey);
137 
138         final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
139 
140         assertThat(isHandled).isFalse();
141     }
142 
143     @Test
onDeveloperOptionsSwitchEnabled_isAdminUser_shouldEnablePreference()144     public void onDeveloperOptionsSwitchEnabled_isAdminUser_shouldEnablePreference() {
145         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
146         doReturn(true).when(mController).isAdminUser();
147         mController.displayPreference(mScreen);
148         mController.onDeveloperOptionsSwitchEnabled();
149 
150         verify(mPreference).setEnabled(true);
151     }
152 
153     @Test
onDeveloperOptionsSwitchEnabled_isNotAdminUser_shouldNotEnablePreference()154     public void onDeveloperOptionsSwitchEnabled_isNotAdminUser_shouldNotEnablePreference() {
155         SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
156         doReturn(false).when(mController).isAdminUser();
157         mController.displayPreference(mScreen);
158         mController.onDeveloperOptionsSwitchEnabled();
159 
160         verify(mPreference, never()).setEnabled(true);
161     }
162 
163     @Test
onClearAdbKeysConfirmed_shouldClearKeys()164     public void onClearAdbKeysConfirmed_shouldClearKeys() throws RemoteException {
165         mController.onClearAdbKeysConfirmed();
166 
167         verify(mUsbManager).clearUsbDebuggingKeys();
168     }
169 
170     @Implements(ClearAdbKeysWarningDialog.class)
171     public static class ShadowClearAdbKeysWarningDialog {
172 
173         private static boolean sIsShowing;
174 
175         @Implementation
show(Fragment host)176         public static void show(Fragment host) {
177             sIsShowing = true;
178         }
179 
resetDialog()180         private static void resetDialog() {
181             sIsShowing = false;
182         }
183     }
184 }
185