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.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.admin.DevicePolicyManager;
29 import android.content.Context;
30 import android.os.UserHandle;
31 import android.provider.Settings;
32 
33 import androidx.preference.PreferenceScreen;
34 
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 AdbPreferenceControllerTest {
47 
48     @Mock
49     private RestrictedSwitchPreference mPreference;
50     @Mock
51     private PreferenceScreen mPreferenceScreen;
52     @Mock
53     private DevelopmentSettingsDashboardFragment mFragment;
54     @Mock
55     private DevicePolicyManager mDevicePolicyManager;
56 
57     private Context mContext;
58     private AdbPreferenceController mController;
59 
60     @Before
setup()61     public void setup() throws Exception {
62         MockitoAnnotations.initMocks(this);
63         mContext = spy(RuntimeEnvironment.application);
64         mController = spy(new AdbPreferenceController(mContext, mFragment));
65         doReturn(true).when(mController).isAvailable();
66         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
67             .thenReturn(mPreference);
68         mController.displayPreference(mPreferenceScreen);
69         when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
70         doReturn(mContext).when(mContext).createPackageContextAsUser(
71                 any(String.class), anyInt(), any(UserHandle.class));
72     }
73 
74     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()75     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
76         mController.onDeveloperOptionsDisabled();
77         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
78                 Settings.Global.ADB_ENABLED, -1);
79 
80         assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_OFF);
81         verify(mPreference).setEnabled(false);
82         verify(mPreference).setChecked(false);
83     }
84 
85     @Test
onAdbDialogConfirmed_shouldEnableAdbSetting()86     public void onAdbDialogConfirmed_shouldEnableAdbSetting() {
87         mController.onAdbDialogConfirmed();
88         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
89                 Settings.Global.ADB_ENABLED, -1);
90 
91         assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_ON);
92     }
93 
94     @Test
onAdbDialogDismissed_preferenceShouldNotBeChecked()95     public void onAdbDialogDismissed_preferenceShouldNotBeChecked() {
96         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.ADB_ENABLED,
97                 AdbPreferenceController.ADB_SETTING_OFF);
98         mController.onAdbDialogDismissed();
99 
100         verify(mPreference).setChecked(false);
101     }
102 }
103