OSDN Git Service

Add null check for vrManager
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / display / BrightnessLevelPreferenceControllerTest.java
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
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
80     public void isAvailable_shouldAlwaysReturnTrue() {
81         assertThat(mController.isAvailable()).isTrue();
82     }
83
84     @Test
85     public void isInVrMode_noVrManager_shouldAlwaysReturnFalse() {
86         doReturn(null).when(mController).safeGetVrManager();
87         assertThat(mController.isInVrMode()).isFalse();
88     }
89
90     @Test
91     public void onStart_shouldRegisterObserver() {
92         BrightnessLevelPreferenceController controller =
93                 new BrightnessLevelPreferenceController(mContext, null);
94         ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
95
96         controller.onStart();
97
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();
104     }
105
106     @Test
107     public void onStop_shouldUnregisterObserver() {
108         BrightnessLevelPreferenceController controller =
109                 new BrightnessLevelPreferenceController(mContext, null);
110         ShadowContentResolver shadowContentResolver = Shadow.extract(mContext.getContentResolver());
111
112         controller.displayPreference(mScreen);
113         controller.onStart();
114         controller.onStop();
115
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();
122     }
123
124     @Test
125     public void updateState_inVrMode_shouldSetSummaryToVrBrightness() {
126         doReturn(true).when(mController).isInVrMode();
127         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 85);
128
129         mController.updateState(mPreference);
130
131         verify(mPreference).setSummary("97%");
132     }
133
134     @Test
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);
139
140         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 31);
141
142         mController.updateState(mPreference);
143
144         verify(mPreference).setSummary("78%");
145     }
146
147     @Test
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);
152
153         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 45);
154
155         mController.updateState(mPreference);
156
157         verify(mPreference).setSummary("85%");
158     }
159
160     @Test
161     public void updateState_brightnessOutOfRange_shouldSetSummaryInRange() {
162         // VR mode
163         doReturn(true).when(mController).isInVrMode();
164
165         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 105);
166         mController.updateState(mPreference);
167         verify(mPreference).setSummary("100%");
168
169         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, -20);
170         mController.updateState(mPreference);
171         verify(mPreference).setSummary("0%");
172
173         // Auto mode
174         doReturn(false).when(mController).isInVrMode();
175         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
176                 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
177
178         reset(mPreference);
179         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
180         mController.updateState(mPreference);
181         verify(mPreference).setSummary("100%");
182
183         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
184         mController.updateState(mPreference);
185         verify(mPreference).setSummary("0%");
186
187         // Manual mode
188         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
189                 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
190
191         reset(mPreference);
192         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
193         mController.updateState(mPreference);
194         verify(mPreference).setSummary("100%");
195
196         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
197         mController.updateState(mPreference);
198         verify(mPreference).setSummary("0%");
199     }
200 }