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 isInVrMode_noVrManager_shouldAlwaysReturnFalse() {
86 doReturn(null).when(mController).safeGetVrManager();
87 assertThat(mController.isInVrMode()).isFalse();
91 public void onStart_shouldRegisterObserver() {
92 BrightnessLevelPreferenceController controller =
93 new BrightnessLevelPreferenceController(mContext, null);
94 ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
98 assertThat(shadowContentResolver.getContentObservers(
99 System.getUriFor(System.SCREEN_BRIGHTNESS))).isNotEmpty();
100 assertThat(shadowContentResolver.getContentObservers(
101 System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
102 assertThat(shadowContentResolver.getContentObservers(
103 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isNotEmpty();
107 public void onStop_shouldUnregisterObserver() {
108 BrightnessLevelPreferenceController controller =
109 new BrightnessLevelPreferenceController(mContext, null);
110 ShadowContentResolver shadowContentResolver = Shadow.extract(mContext.getContentResolver());
112 controller.displayPreference(mScreen);
113 controller.onStart();
116 assertThat(shadowContentResolver.getContentObservers(
117 System.getUriFor(System.SCREEN_BRIGHTNESS))).isEmpty();
118 assertThat(shadowContentResolver.getContentObservers(
119 System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isEmpty();
120 assertThat(shadowContentResolver.getContentObservers(
121 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isEmpty();
125 public void updateState_inVrMode_shouldSetSummaryToVrBrightness() {
126 doReturn(true).when(mController).isInVrMode();
127 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 85);
129 mController.updateState(mPreference);
131 verify(mPreference).setSummary("97%");
135 public void updateState_autoBrightness_shouldSetSummaryToAutoBrightness() {
136 doReturn(false).when(mController).isInVrMode();
137 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
138 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
140 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 31);
142 mController.updateState(mPreference);
144 verify(mPreference).setSummary("78%");
148 public void updateState_manualBrightness_shouldSetSummaryToScreenBrightness() {
149 doReturn(false).when(mController).isInVrMode();
150 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
151 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
153 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 45);
155 mController.updateState(mPreference);
157 verify(mPreference).setSummary("85%");
161 public void updateState_brightnessOutOfRange_shouldSetSummaryInRange() {
163 doReturn(true).when(mController).isInVrMode();
165 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 105);
166 mController.updateState(mPreference);
167 verify(mPreference).setSummary("100%");
169 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, -20);
170 mController.updateState(mPreference);
171 verify(mPreference).setSummary("0%");
174 doReturn(false).when(mController).isInVrMode();
175 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
176 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
179 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
180 mController.updateState(mPreference);
181 verify(mPreference).setSummary("100%");
183 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
184 mController.updateState(mPreference);
185 verify(mPreference).setSummary("0%");
188 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
189 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
192 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
193 mController.updateState(mPreference);
194 verify(mPreference).setSummary("100%");
196 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
197 mController.updateState(mPreference);
198 verify(mPreference).setSummary("0%");