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.AllowAppsOnExternalPreferenceController
20         .SETTING_VALUE_OFF;
21 import static com.android.settings.development.AllowAppsOnExternalPreferenceController
22         .SETTING_VALUE_ON;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.provider.Settings;
31 
32 import androidx.preference.PreferenceScreen;
33 import androidx.preference.SwitchPreference;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 @RunWith(RobolectricTestRunner.class)
44 public class AllowAppsOnExternalPreferenceControllerTest {
45 
46     @Mock
47     private SwitchPreference mPreference;
48     @Mock
49     private PreferenceScreen mPreferenceScreen;
50 
51     private Context mContext;
52     private AllowAppsOnExternalPreferenceController mController;
53 
54     @Before
setup()55     public void setup() {
56         MockitoAnnotations.initMocks(this);
57         mContext = RuntimeEnvironment.application;
58         mController = new AllowAppsOnExternalPreferenceController(mContext);
59         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
60             .thenReturn(mPreference);
61         mController.displayPreference(mPreferenceScreen);
62     }
63 
64     @Test
onPreferenceChange_settingEnabled_allowAppsOnExternalShouldBeOn()65     public void onPreferenceChange_settingEnabled_allowAppsOnExternalShouldBeOn() {
66         mController.onPreferenceChange(mPreference, true /* new value */);
67 
68         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
69                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, -1 /* default */);
70 
71         assertThat(mode).isEqualTo(SETTING_VALUE_ON);
72     }
73 
74     @Test
onPreferenceChange_settingDisabled_allowAppsOnExternalShouldBeOff()75     public void onPreferenceChange_settingDisabled_allowAppsOnExternalShouldBeOff() {
76         mController.onPreferenceChange(mPreference, false /* new value */);
77 
78         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
79                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, -1 /* default */);
80 
81         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
82     }
83 
84     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()85     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
86         Settings.Global.putInt(mContext.getContentResolver(),
87                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, SETTING_VALUE_OFF);
88         mController.updateState(mPreference);
89 
90         verify(mPreference).setChecked(false);
91     }
92 
93     @Test
updateState_settingEnabled_preferenceShouldBeChecked()94     public void updateState_settingEnabled_preferenceShouldBeChecked() {
95         Settings.Global.putInt(mContext.getContentResolver(),
96                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, SETTING_VALUE_ON);
97         mController.updateState(mPreference);
98 
99         verify(mPreference).setChecked(true);
100     }
101 
102     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreference()103     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
104         mController.onDeveloperOptionsSwitchDisabled();
105 
106         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
107                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, -1 /* default */);
108 
109         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
110         verify(mPreference).setChecked(false);
111         verify(mPreference).setEnabled(false);
112     }
113 }
114