1 /*
2  * Copyright (C) 2020 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.anyBoolean;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.provider.Settings;
27 
28 import androidx.preference.PreferenceScreen;
29 import androidx.preference.SwitchPreference;
30 
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnit;
37 import org.mockito.junit.MockitoRule;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public final class EnableBlursPreferenceControllerTest {
43 
44     @Mock
45     private SwitchPreference mPreference;
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48 
49     @Rule
50     public MockitoRule mockitoRule = MockitoJUnit.rule();
51 
52     private Context mContext;
53     private EnableBlursPreferenceController mController;
54 
55     @Before
setup()56     public void setup() {
57         mContext = RuntimeEnvironment.application;
58         mController = new EnableBlursPreferenceController(mContext, true);
59         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
60                 mPreference);
61         mController.displayPreference(mPreferenceScreen);
62     }
63 
64     @Test
onPreferenceChanged_settingEnabled_enableBlurs()65     public void onPreferenceChanged_settingEnabled_enableBlurs() {
66         mController.onPreferenceChange(mPreference, true /* new value */);
67 
68         final boolean blursDisabled = Settings.Global.getInt(mContext.getContentResolver(),
69                 Settings.Global.DISABLE_WINDOW_BLURS, 0) == 1;
70         assertThat(blursDisabled).isFalse();
71     }
72 
73     @Test
onPreferenceChanged_settingDisabled_disableBlurs()74     public void onPreferenceChanged_settingDisabled_disableBlurs() {
75         mController.onPreferenceChange(mPreference, false /* new value */);
76 
77         final boolean blursDisabled = Settings.Global.getInt(mContext.getContentResolver(),
78                 Settings.Global.DISABLE_WINDOW_BLURS, 0) == 1;
79 
80         assertThat(blursDisabled).isTrue();
81     }
82 
83     @Test
updateState_settingEnabled_preferenceShouldNotBeChecked()84     public void updateState_settingEnabled_preferenceShouldNotBeChecked() {
85         Settings.Global.putInt(mContext.getContentResolver(),
86                 Settings.Global.DISABLE_WINDOW_BLURS, 1);
87         mController.updateState(mPreference);
88 
89         verify(mPreference).setChecked(false);
90     }
91 
92     @Test
updateState_settingDisabled_preferenceShouldBeChecked()93     public void updateState_settingDisabled_preferenceShouldBeChecked() {
94         Settings.Global.putInt(mContext.getContentResolver(),
95                 Settings.Global.DISABLE_WINDOW_BLURS, 0);
96         mController.updateState(mPreference);
97 
98         verify(mPreference).setChecked(true);
99     }
100 
101     @Test
onDeveloperOptionsDisabled_shouldResetPreference()102     public void onDeveloperOptionsDisabled_shouldResetPreference() {
103         mController.onDeveloperOptionsDisabled();
104         // Can predict true or false, depends on device config.
105         verify(mPreference).setChecked(anyBoolean());
106     }
107 
108     @Test
isAvailable_whenSupported()109     public void isAvailable_whenSupported() {
110         assertThat(mController.isAvailable()).isTrue();
111 
112         mController = new EnableBlursPreferenceController(mContext, false);
113         assertThat(mController.isAvailable()).isFalse();
114     }
115 }
116