2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.display;
19 import static android.content.Context.POWER_SERVICE;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.reset;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.os.PowerManager;
31 import android.provider.Settings.System;
32 import android.support.v7.preference.Preference;
33 import android.support.v7.preference.PreferenceScreen;
35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.shadow.api.Shadow;
44 import org.robolectric.shadows.ShadowApplication;
45 import org.robolectric.shadows.ShadowContentResolver;
47 @RunWith(SettingsRobolectricTestRunner.class)
48 public class BrightnessLevelPreferenceControllerTest {
51 private PowerManager mPowerManager;
53 private PreferenceScreen mScreen;
55 private Preference mPreference;
57 private Context mContext;
59 private ContentResolver mContentResolver;
61 private BrightnessLevelPreferenceController mController;
65 MockitoAnnotations.initMocks(this);
66 mContext = RuntimeEnvironment.application;
67 mContentResolver = mContext.getContentResolver();
68 when(mPowerManager.getMinimumScreenBrightnessSetting()).thenReturn(0);
69 when(mPowerManager.getMaximumScreenBrightnessSetting()).thenReturn(100);
70 when(mPowerManager.getMinimumScreenBrightnessForVrSetting()).thenReturn(0);
71 when(mPowerManager.getMaximumScreenBrightnessForVrSetting()).thenReturn(100);
72 ShadowApplication.getInstance().setSystemService(POWER_SERVICE,
74 when(mScreen.findPreference(anyString())).thenReturn(mPreference);
75 mController = spy(new BrightnessLevelPreferenceController(mContext, null));
76 doReturn(false).when(mController).isInVrMode();
80 public void isAvailable_shouldAlwaysReturnTrue() {
81 assertThat(mController.isAvailable()).isTrue();
85 public void onStart_shouldRegisterObserver() {
86 BrightnessLevelPreferenceController controller =
87 new BrightnessLevelPreferenceController(mContext, null);
88 ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
92 assertThat(shadowContentResolver.getContentObservers(
93 System.getUriFor(System.SCREEN_BRIGHTNESS))).isNotEmpty();
94 assertThat(shadowContentResolver.getContentObservers(
95 System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
96 assertThat(shadowContentResolver.getContentObservers(
97 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isNotEmpty();
101 public void onStop_shouldUnregisterObserver() {
102 BrightnessLevelPreferenceController controller =
103 new BrightnessLevelPreferenceController(mContext, null);
104 ShadowContentResolver shadowContentResolver = Shadow.extract(mContext.getContentResolver());
106 controller.displayPreference(mScreen);
107 controller.onStart();
110 assertThat(shadowContentResolver.getContentObservers(
111 System.getUriFor(System.SCREEN_BRIGHTNESS))).isEmpty();
112 assertThat(shadowContentResolver.getContentObservers(
113 System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isEmpty();
114 assertThat(shadowContentResolver.getContentObservers(
115 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isEmpty();
119 public void updateState_inVrMode_shouldSetSummaryToVrBrightness() {
120 doReturn(true).when(mController).isInVrMode();
121 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 85);
123 mController.updateState(mPreference);
125 verify(mPreference).setSummary("97%");
129 public void updateState_autoBrightness_shouldSetSummaryToAutoBrightness() {
130 doReturn(false).when(mController).isInVrMode();
131 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
132 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
134 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 31);
136 mController.updateState(mPreference);
138 verify(mPreference).setSummary("78%");
142 public void updateState_manualBrightness_shouldSetSummaryToScreenBrightness() {
143 doReturn(false).when(mController).isInVrMode();
144 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
145 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
147 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 45);
149 mController.updateState(mPreference);
151 verify(mPreference).setSummary("85%");
155 public void updateState_brightnessOutOfRange_shouldSetSummaryInRange() {
157 doReturn(true).when(mController).isInVrMode();
159 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 105);
160 mController.updateState(mPreference);
161 verify(mPreference).setSummary("100%");
163 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, -20);
164 mController.updateState(mPreference);
165 verify(mPreference).setSummary("0%");
168 doReturn(false).when(mController).isInVrMode();
169 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
170 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
173 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
174 mController.updateState(mPreference);
175 verify(mPreference).setSummary("100%");
177 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
178 mController.updateState(mPreference);
179 verify(mPreference).setSummary("0%");
182 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
183 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
186 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
187 mController.updateState(mPreference);
188 verify(mPreference).setSummary("100%");
190 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
191 mController.updateState(mPreference);
192 verify(mPreference).setSummary("0%");