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.display;
18 
19 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
20 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
21 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
22 
23 import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
24 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
25 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import android.content.ContentResolver;
30 import android.content.Context;
31 import android.platform.test.annotations.DisableFlags;
32 import android.platform.test.annotations.EnableFlags;
33 import android.platform.test.flag.junit.SetFlagsRule;
34 import android.provider.Settings;
35 
36 import com.android.settings.R;
37 import com.android.settings.accessibility.Flags;
38 import com.android.settings.testutils.shadow.SettingsShadowResources;
39 
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.annotation.Config;
48 
49 @RunWith(RobolectricTestRunner.class)
50 @Config(shadows = {SettingsShadowResources.class})
51 public class AutoBrightnessPreferenceControllerTest {
52 
53     @Rule
54     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
55 
56     private static final String PREFERENCE_KEY = "auto_brightness";
57 
58     private Context mContext;
59     private AutoBrightnessPreferenceController mController;
60     private ContentResolver mContentResolver;
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65 
66         mContext = RuntimeEnvironment.application;
67         mContentResolver = mContext.getContentResolver();
68         mController = new AutoBrightnessPreferenceController(mContext, PREFERENCE_KEY);
69     }
70 
71     @Test
onPreferenceChange_TurnOnAuto_ReturnAuto()72     public void onPreferenceChange_TurnOnAuto_ReturnAuto() {
73         mController.onPreferenceChange(null, true);
74 
75         final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
76                 SCREEN_BRIGHTNESS_MODE_MANUAL);
77         assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
78     }
79 
80     @Test
onPreferenceChange_TurnOffAuto_ReturnManual()81     public void onPreferenceChange_TurnOffAuto_ReturnManual() {
82         mController.onPreferenceChange(null, false);
83 
84         final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
85                 SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
86         assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_MANUAL);
87     }
88 
89     @Test
setChecked_updatesCorrectly()90     public void setChecked_updatesCorrectly() {
91         mController.setChecked(true);
92 
93         assertThat(mController.isChecked()).isTrue();
94 
95         mController.setChecked(false);
96 
97         assertThat(mController.isChecked()).isFalse();
98     }
99 
100     @Test
isChecked_no()101     public void isChecked_no() {
102         Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
103                 SCREEN_BRIGHTNESS_MODE_MANUAL);
104 
105         assertThat(mController.isChecked()).isFalse();
106     }
107 
108     @Test
isChecked_yes()109     public void isChecked_yes() {
110         Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
111                 SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
112 
113         assertThat(mController.isChecked()).isTrue();
114     }
115 
116     @Test
getSummary_settingOn_shouldReturnOnSummary()117     public void getSummary_settingOn_shouldReturnOnSummary() {
118         mController.setChecked(true);
119 
120         assertThat(mController.getSummary())
121                 .isEqualTo(mContext.getText(R.string.auto_brightness_summary_on));
122     }
123 
124     @Test
getSummary_settingOff_shouldReturnOffSummary()125     public void getSummary_settingOff_shouldReturnOffSummary() {
126         mController.setChecked(false);
127 
128         assertThat(mController.getSummary())
129                 .isEqualTo(mContext.getText(R.string.auto_brightness_summary_off));
130     }
131 
132     @Test
getAvailabilityStatusNotInSUW_configTrueSet_shouldReturnAvailableUnsearchable()133     public void getAvailabilityStatusNotInSUW_configTrueSet_shouldReturnAvailableUnsearchable() {
134         SettingsShadowResources.overrideResource(
135                 com.android.internal.R.bool.config_automatic_brightness_available, true);
136 
137         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
138     }
139 
140     @Test
141     @EnableFlags(Flags.FLAG_ADD_BRIGHTNESS_SETTINGS_IN_SUW)
getAvailabilityStatusInSUW_configTrueAndFlagOn_shouldReturnAvailableUnsearchable()142     public void getAvailabilityStatusInSUW_configTrueAndFlagOn_shouldReturnAvailableUnsearchable() {
143         SettingsShadowResources.overrideResource(
144                 com.android.internal.R.bool.config_automatic_brightness_available, true);
145         mController.setInSetupWizard(true);
146 
147         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
148     }
149 
150     @Test
151     @DisableFlags(Flags.FLAG_ADD_BRIGHTNESS_SETTINGS_IN_SUW)
152     public void
getAvailabilityStatusInSUW_configTrueAndFlagOff_shouldReturnConditionallyUnavailable()153             getAvailabilityStatusInSUW_configTrueAndFlagOff_shouldReturnConditionallyUnavailable() {
154         SettingsShadowResources.overrideResource(
155                 com.android.internal.R.bool.config_automatic_brightness_available, true);
156         mController.setInSetupWizard(true);
157 
158         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
159     }
160 
161     @Test
getAvailabilityStatus_configFalseSet_shouldReturnUnsupportedOnDevice()162     public void getAvailabilityStatus_configFalseSet_shouldReturnUnsupportedOnDevice() {
163         SettingsShadowResources.overrideResource(
164                 com.android.internal.R.bool.config_automatic_brightness_available, false);
165 
166         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
167     }
168 }
169