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