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
17package com.android.settings.display;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.anyString;
21import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.reset;
23import static org.mockito.Mockito.spy;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.when;
26
27import android.content.ContentResolver;
28import android.content.Context;
29import android.provider.Settings.System;
30import android.support.v7.preference.Preference;
31import android.support.v7.preference.PreferenceScreen;
32
33import com.android.settings.testutils.SettingsRobolectricTestRunner;
34import com.android.settings.TestConfig;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.MockitoAnnotations;
41import org.robolectric.RuntimeEnvironment;
42import org.robolectric.annotation.Config;
43import org.robolectric.internal.ShadowExtractor;
44import org.robolectric.shadows.ShadowContentResolver;
45
46@RunWith(SettingsRobolectricTestRunner.class)
47@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
48public class BrightnessLevelPreferenceControllerTest {
49    @Mock
50    private Context mContext;
51    @Mock
52    private ContentResolver mContentResolver;
53    @Mock
54    private PowerManagerWrapper mPowerManager;
55    @Mock
56    private PreferenceScreen mScreen;
57    @Mock
58    private Preference mPreference;
59
60    private BrightnessLevelPreferenceController mController;
61
62    @Before
63    public void setUp() {
64        MockitoAnnotations.initMocks(this);
65        when(mContext.getContentResolver()).thenReturn(mContentResolver);
66        when(mPowerManager.getMinimumScreenBrightnessSetting()).thenReturn(0);
67        when(mPowerManager.getMaximumScreenBrightnessSetting()).thenReturn(100);
68        when(mPowerManager.getMinimumScreenBrightnessForVrSetting()).thenReturn(0);
69        when(mPowerManager.getMaximumScreenBrightnessForVrSetting()).thenReturn(100);
70        when(mScreen.findPreference(anyString())).thenReturn(mPreference);
71        mController = spy(new BrightnessLevelPreferenceController(mContext, null, mPowerManager));
72        doReturn(false).when(mController).isInVrMode();
73
74    }
75
76    @Test
77    public void isAvailable_shouldAlwaysReturnTrue() {
78        assertThat(mController.isAvailable()).isTrue();
79    }
80
81    @Test
82    public void onStart_shouldRegisterObserver() {
83        Context context = RuntimeEnvironment.application;
84        BrightnessLevelPreferenceController controller =
85            new BrightnessLevelPreferenceController(context, null, mPowerManager);
86        ShadowContentResolver shadowContentResolver =
87            (ShadowContentResolver) ShadowExtractor.extract(context.getContentResolver());
88
89        controller.onStart();
90
91        assertThat(shadowContentResolver.getContentObservers(
92            System.getUriFor(System.SCREEN_BRIGHTNESS_MODE))).isNotEmpty();
93        assertThat(shadowContentResolver.getContentObservers(
94            System.getUriFor(System.SCREEN_BRIGHTNESS))).isNotEmpty();
95        assertThat(shadowContentResolver.getContentObservers(
96            System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
97        assertThat(shadowContentResolver.getContentObservers(
98            System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isNotEmpty();
99    }
100
101    @Test
102    public void onStop_shouldUnregisterObserver() {
103        Context context = RuntimeEnvironment.application;
104        BrightnessLevelPreferenceController controller =
105            new BrightnessLevelPreferenceController(context, null, mPowerManager);
106        ShadowContentResolver shadowContentResolver =
107            (ShadowContentResolver) ShadowExtractor.extract(context.getContentResolver());
108
109        controller.displayPreference(mScreen);
110        controller.onStart();
111        controller.onStop();
112
113        assertThat(shadowContentResolver.getContentObservers(
114            System.getUriFor(System.SCREEN_BRIGHTNESS_MODE))).isEmpty();
115        assertThat(shadowContentResolver.getContentObservers(
116            System.getUriFor(System.SCREEN_BRIGHTNESS))).isEmpty();
117        assertThat(shadowContentResolver.getContentObservers(
118            System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isEmpty();
119        assertThat(shadowContentResolver.getContentObservers(
120            System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isEmpty();
121    }
122
123    @Test
124    public void updateState_inVrMode_shouldSetSummaryToVrBrightness() {
125        doReturn(true).when(mController).isInVrMode();
126        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 85);
127
128        mController.updateState(mPreference);
129
130        verify(mPreference).setSummary("85%");
131    }
132
133    @Test
134    public void updateState_autoBrightness_shouldSetSummaryToAutoBrightness() {
135        doReturn(false).when(mController).isInVrMode();
136        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
137            System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
138
139        System.putFloat(mContentResolver, System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f);
140
141        mController.updateState(mPreference);
142
143        verify(mPreference).setSummary("50%");
144    }
145
146    @Test
147    public void updateState_manualBrightness_shouldSetSummaryToScreenBrightness() {
148        doReturn(false).when(mController).isInVrMode();
149        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
150            System.SCREEN_BRIGHTNESS_MODE_MANUAL);
151
152        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 45);
153
154        mController.updateState(mPreference);
155
156        verify(mPreference).setSummary("45%");
157    }
158
159    @Test
160    public void updateState_brightnessOutOfRange_shouldSetSummaryInRange() {
161        // VR mode
162        doReturn(true).when(mController).isInVrMode();
163
164        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 105);
165        mController.updateState(mPreference);
166        verify(mPreference).setSummary("100%");
167
168        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, -20);
169        mController.updateState(mPreference);
170        verify(mPreference).setSummary("0%");
171
172        // Auto mode
173        doReturn(false).when(mController).isInVrMode();
174        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
175                System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
176
177        reset(mPreference);
178        System.putFloat(mContentResolver, System.SCREEN_AUTO_BRIGHTNESS_ADJ, 1.5f);
179        mController.updateState(mPreference);
180        verify(mPreference).setSummary("100%");
181
182        System.putFloat(mContentResolver, System.SCREEN_AUTO_BRIGHTNESS_ADJ, -1.5f);
183        mController.updateState(mPreference);
184        verify(mPreference).setSummary("0%");
185
186        // Manual mode
187        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
188                System.SCREEN_BRIGHTNESS_MODE_MANUAL);
189
190        reset(mPreference);
191        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 115);
192        mController.updateState(mPreference);
193        verify(mPreference).setSummary("100%");
194
195        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, -10);
196        mController.updateState(mPreference);
197        verify(mPreference).setSummary("0%");
198    }
199}
200