• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.UNSUPPORTED_ON_DEVICE;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.provider.Settings;
31 
32 import com.android.settings.R;
33 import com.android.settings.testutils.shadow.SettingsShadowResources;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 
43 /**
44  * Tests for {@link AutoBrightnessPreferenceController}.
45  */
46 // LINT.IfChange
47 @RunWith(RobolectricTestRunner.class)
48 @Config(shadows = {SettingsShadowResources.class})
49 public class AutoBrightnessPreferenceControllerTest {
50 
51     private static final String PREFERENCE_KEY = "auto_brightness";
52 
53     private Context mContext;
54     private AutoBrightnessPreferenceController mController;
55     private ContentResolver mContentResolver;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60 
61         mContext = RuntimeEnvironment.application;
62         mContentResolver = mContext.getContentResolver();
63         mController = new AutoBrightnessPreferenceController(mContext, PREFERENCE_KEY);
64     }
65 
66     @Test
onPreferenceChange_TurnOnAuto_ReturnAuto()67     public void onPreferenceChange_TurnOnAuto_ReturnAuto() {
68         mController.onPreferenceChange(null, true);
69 
70         final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
71                 SCREEN_BRIGHTNESS_MODE_MANUAL);
72         assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
73     }
74 
75     @Test
onPreferenceChange_TurnOffAuto_ReturnManual()76     public void onPreferenceChange_TurnOffAuto_ReturnManual() {
77         mController.onPreferenceChange(null, false);
78 
79         final int mode = Settings.System.getInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
80                 SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
81         assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_MANUAL);
82     }
83 
84     @Test
setChecked_updatesCorrectly()85     public void setChecked_updatesCorrectly() {
86         mController.setChecked(true);
87 
88         assertThat(mController.isChecked()).isTrue();
89 
90         mController.setChecked(false);
91 
92         assertThat(mController.isChecked()).isFalse();
93     }
94 
95     @Test
isChecked_no()96     public void isChecked_no() {
97         Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
98                 SCREEN_BRIGHTNESS_MODE_MANUAL);
99 
100         assertThat(mController.isChecked()).isFalse();
101     }
102 
103     @Test
isChecked_yes()104     public void isChecked_yes() {
105         Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS_MODE,
106                 SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
107 
108         assertThat(mController.isChecked()).isTrue();
109     }
110 
111     @Test
getSummary_settingOn_shouldReturnOnSummary()112     public void getSummary_settingOn_shouldReturnOnSummary() {
113         mController.setChecked(true);
114 
115         assertThat(mController.getSummary())
116                 .isEqualTo(mContext.getText(R.string.auto_brightness_summary_on));
117     }
118 
119     @Test
getSummary_settingOff_shouldReturnOffSummary()120     public void getSummary_settingOff_shouldReturnOffSummary() {
121         mController.setChecked(false);
122 
123         assertThat(mController.getSummary())
124                 .isEqualTo(mContext.getText(R.string.auto_brightness_summary_off));
125     }
126 
127     @Test
getAvailabilityStatus_configTrueSet_shouldReturnAvailableUnsearchable()128     public void getAvailabilityStatus_configTrueSet_shouldReturnAvailableUnsearchable() {
129         SettingsShadowResources.overrideResource(
130                 com.android.internal.R.bool.config_automatic_brightness_available, true);
131 
132         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
133     }
134 
135     @Test
getAvailabilityStatus_configFalseSet_shouldReturnUnsupportedOnDevice()136     public void getAvailabilityStatus_configFalseSet_shouldReturnUnsupportedOnDevice() {
137         SettingsShadowResources.overrideResource(
138                 com.android.internal.R.bool.config_automatic_brightness_available, false);
139 
140         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
141     }
142 }
143 // LINT.ThenChange(AutoBrightnessScreenTest.kt)
144