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.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; 27 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; 34 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 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; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 public class BrightnessLevelPreferenceControllerTest { 49 50 @Mock 51 private PowerManager mPowerManager; 52 @Mock 53 private PreferenceScreen mScreen; 54 @Mock 55 private Preference mPreference; 56 57 private Context mContext; 58 59 private ContentResolver mContentResolver; 60 61 private BrightnessLevelPreferenceController mController; 62 63 @Before setUp()64 public void setUp() { 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, 73 mPowerManager); 74 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 75 mController = spy(new BrightnessLevelPreferenceController(mContext, null)); 76 doReturn(false).when(mController).isInVrMode(); 77 } 78 79 @Test isAvailable_shouldAlwaysReturnTrue()80 public void isAvailable_shouldAlwaysReturnTrue() { 81 assertThat(mController.isAvailable()).isTrue(); 82 } 83 84 @Test onStart_shouldRegisterObserver()85 public void onStart_shouldRegisterObserver() { 86 BrightnessLevelPreferenceController controller = 87 new BrightnessLevelPreferenceController(mContext, null); 88 ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver); 89 90 controller.onStart(); 91 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(); 98 } 99 100 @Test onStop_shouldUnregisterObserver()101 public void onStop_shouldUnregisterObserver() { 102 BrightnessLevelPreferenceController controller = 103 new BrightnessLevelPreferenceController(mContext, null); 104 ShadowContentResolver shadowContentResolver = Shadow.extract(mContext.getContentResolver()); 105 106 controller.displayPreference(mScreen); 107 controller.onStart(); 108 controller.onStop(); 109 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(); 116 } 117 118 @Test updateState_inVrMode_shouldSetSummaryToVrBrightness()119 public void updateState_inVrMode_shouldSetSummaryToVrBrightness() { 120 doReturn(true).when(mController).isInVrMode(); 121 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 85); 122 123 mController.updateState(mPreference); 124 125 verify(mPreference).setSummary("97%"); 126 } 127 128 @Test updateState_autoBrightness_shouldSetSummaryToAutoBrightness()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); 133 134 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 31); 135 136 mController.updateState(mPreference); 137 138 verify(mPreference).setSummary("78%"); 139 } 140 141 @Test updateState_manualBrightness_shouldSetSummaryToScreenBrightness()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); 146 147 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 45); 148 149 mController.updateState(mPreference); 150 151 verify(mPreference).setSummary("85%"); 152 } 153 154 @Test updateState_brightnessOutOfRange_shouldSetSummaryInRange()155 public void updateState_brightnessOutOfRange_shouldSetSummaryInRange() { 156 // VR mode 157 doReturn(true).when(mController).isInVrMode(); 158 159 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 105); 160 mController.updateState(mPreference); 161 verify(mPreference).setSummary("100%"); 162 163 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, -20); 164 mController.updateState(mPreference); 165 verify(mPreference).setSummary("0%"); 166 167 // Auto mode 168 doReturn(false).when(mController).isInVrMode(); 169 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE, 170 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 171 172 reset(mPreference); 173 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115); 174 mController.updateState(mPreference); 175 verify(mPreference).setSummary("100%"); 176 177 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10); 178 mController.updateState(mPreference); 179 verify(mPreference).setSummary("0%"); 180 181 // Manual mode 182 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE, 183 System.SCREEN_BRIGHTNESS_MODE_MANUAL); 184 185 reset(mPreference); 186 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115); 187 mController.updateState(mPreference); 188 verify(mPreference).setSummary("100%"); 189 190 System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10); 191 mController.updateState(mPreference); 192 verify(mPreference).setSummary("0%"); 193 } 194 } 195