1 /*
2  * Copyright (C) 2018 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.ShowFirstCrashDialogPreferenceController
20         .SETTING_VALUE_OFF;
21 import static com.android.settings.development.ShowFirstCrashDialogPreferenceController
22         .SETTING_VALUE_ON;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.provider.Settings;
30 
31 import androidx.preference.PreferenceScreen;
32 import androidx.preference.SwitchPreference;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class ShowFirstCrashDialogPreferenceControllerTest {
44 
45     @Mock
46     private PreferenceScreen mPreferenceScreen;
47 
48     private Context mContext;
49     private SwitchPreference mPreference;
50     private ShowFirstCrashDialogPreferenceController mController;
51 
52     @Before
setup()53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mPreference = new SwitchPreference(mContext);
57         mController = new ShowFirstCrashDialogPreferenceController(mContext);
58         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
59             .thenReturn(mPreference);
60         mController.displayPreference(mPreferenceScreen);
61     }
62 
63     @Test
onPreferenceChange_settingEnabled_showFirstCrashDialogShouldBeOn()64     public void onPreferenceChange_settingEnabled_showFirstCrashDialogShouldBeOn() {
65         mController.onPreferenceChange(mPreference, true /* new value */);
66 
67         final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
68                 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, -1 /* default */);
69 
70         assertThat(mode).isEqualTo(SETTING_VALUE_ON);
71     }
72 
73     @Test
onPreferenceChange_settingDisabled_showFirstCrashDialogShouldBeOff()74     public void onPreferenceChange_settingDisabled_showFirstCrashDialogShouldBeOff() {
75         mController.onPreferenceChange(mPreference, false /* new value */);
76 
77         final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
78                 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, -1 /* default */);
79 
80         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
81     }
82 
83     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()84     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
85         Settings.Secure.putInt(mContext.getContentResolver(),
86                 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, SETTING_VALUE_OFF);
87         mController.updateState(mPreference);
88 
89         assertThat(mPreference.isChecked()).isFalse();
90     }
91 
92     @Test
updateState_settingEnabled_preferenceShouldBeChecked()93     public void updateState_settingEnabled_preferenceShouldBeChecked() {
94         Settings.Secure.putInt(mContext.getContentResolver(),
95                 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, SETTING_VALUE_ON);
96         mController.updateState(mPreference);
97 
98         assertThat(mPreference.isChecked()).isTrue();
99     }
100 
101     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreference()102     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
103         mController.onDeveloperOptionsSwitchDisabled();
104 
105         final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
106                 Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, -1 /* default */);
107 
108         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
109         assertThat(mPreference.isChecked()).isFalse();
110         assertThat(mPreference.isEnabled()).isFalse();
111     }
112 
113     @Test
onShowFirstCrashDialogGlobalOff_shouldEnablePreference()114     public void onShowFirstCrashDialogGlobalOff_shouldEnablePreference() {
115         Settings.Global.putInt(mContext.getContentResolver(),
116                 Settings.Global.SHOW_FIRST_CRASH_DIALOG, SETTING_VALUE_OFF);
117 
118         mController.displayPreference(mPreferenceScreen);
119 
120         assertThat(mController.isAvailable()).isTrue();
121         assertThat(mPreference.isVisible()).isTrue();
122     }
123 
124     @Test
onShowFirstCrashDialogGlobalOn_shouldDisablePreference()125     public void onShowFirstCrashDialogGlobalOn_shouldDisablePreference() {
126         Settings.Global.putInt(mContext.getContentResolver(),
127                 Settings.Global.SHOW_FIRST_CRASH_DIALOG, SETTING_VALUE_ON);
128 
129         mController.displayPreference(mPreferenceScreen);
130 
131         assertThat(mController.isAvailable()).isFalse();
132         assertThat(mPreference.isVisible()).isFalse();
133     }
134 }
135