• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.SystemProperties;
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 mode = SystemProperties
69                 .getBoolean(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP,
70                         false /* default */);
71         assertThat(mode).isFalse();
72     }
73 
74     @Test
onPreferenceChanged_settingDisabled_disableBlurs()75     public void onPreferenceChanged_settingDisabled_disableBlurs() {
76         mController.onPreferenceChange(mPreference, false /* new value */);
77 
78         final boolean mode = SystemProperties
79                 .getBoolean(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP,
80                         false /* default */);
81 
82         assertThat(mode).isTrue();
83     }
84 
85     @Test
updateState_settingEnabled_preferenceShouldNotBeChecked()86     public void updateState_settingEnabled_preferenceShouldNotBeChecked() {
87         SystemProperties.set(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP, "1");
88         mController.updateState(mPreference);
89 
90         verify(mPreference).setChecked(false);
91     }
92 
93     @Test
updateState_settingDisabled_preferenceShouldBeChecked()94     public void updateState_settingDisabled_preferenceShouldBeChecked() {
95         SystemProperties.set(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP, "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