• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.robolectric.Shadows.shadowOf;
29 
30 import android.app.Activity;
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.hardware.display.BrightnessInfo;
35 import android.os.PowerManager;
36 import android.provider.Settings.System;
37 import android.view.Display;
38 
39 import androidx.preference.Preference;
40 import androidx.preference.PreferenceScreen;
41 
42 import com.android.settings.R;
43 import com.android.settings.core.SettingsBaseActivity;
44 import com.android.settingslib.transition.SettingsTransitionHelper;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.Robolectric;
52 import org.robolectric.RobolectricTestRunner;
53 import org.robolectric.RuntimeEnvironment;
54 import org.robolectric.shadow.api.Shadow;
55 import org.robolectric.shadows.ShadowActivity;
56 import org.robolectric.shadows.ShadowApplication;
57 import org.robolectric.shadows.ShadowContentResolver;
58 
59 /**
60  * Tests for {@link BrightnessLevelPreferenceController}.
61  */
62 @RunWith(RobolectricTestRunner.class)
63 public class BrightnessLevelPreferenceControllerTest {
64     @Mock
65     private PowerManager mPowerManager;
66     @Mock
67     private Display mDisplay;
68     @Mock
69     private PreferenceScreen mScreen;
70     @Mock
71     private Preference mPreference;
72 
73     private Context mContext;
74 
75     private ContentResolver mContentResolver;
76 
77     private BrightnessLevelPreferenceController mController;
78 
79     @Before
setUp()80     public void setUp() {
81         MockitoAnnotations.initMocks(this);
82         mContext = spy(RuntimeEnvironment.application);
83         mContentResolver = mContext.getContentResolver();
84         when(mPowerManager.getBrightnessConstraint(
85                 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM)).thenReturn(0.0f);
86         when(mPowerManager.getBrightnessConstraint(
87                 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM)).thenReturn(1.0f);
88         ShadowApplication.getInstance().setSystemService(POWER_SERVICE,
89                 mPowerManager);
90         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
91         doReturn(mDisplay).when(mContext).getDisplay();
92         mController = spy(new BrightnessLevelPreferenceController(mContext, /* lifecycle= */ null));
93     }
94 
95     @Test
isAvailable_shouldAlwaysReturnTrue()96     public void isAvailable_shouldAlwaysReturnTrue() {
97         assertThat(mController.isAvailable()).isTrue();
98     }
99 
100     @Test
onStart_shouldRegisterObserver()101     public void onStart_shouldRegisterObserver() {
102         BrightnessLevelPreferenceController controller =
103                 new BrightnessLevelPreferenceController(mContext, null);
104         ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
105 
106         controller.onStart();
107 
108         assertThat(shadowContentResolver.getContentObservers(
109                 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isNotEmpty();
110     }
111 
112     @Test
onStop_shouldUnregisterObserver()113     public void onStop_shouldUnregisterObserver() {
114         BrightnessLevelPreferenceController controller =
115                 new BrightnessLevelPreferenceController(mContext, null);
116         ShadowContentResolver shadowContentResolver = Shadow.extract(mContext.getContentResolver());
117 
118         controller.displayPreference(mScreen);
119         controller.onStart();
120         controller.onStop();
121 
122         assertThat(shadowContentResolver.getContentObservers(
123                 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isEmpty();
124     }
125 
126     @Test
onStart_shouldSetSummary()127     public void onStart_shouldSetSummary() {
128         BrightnessLevelPreferenceController controller =
129                 new BrightnessLevelPreferenceController(mContext, null);
130         controller.displayPreference(mScreen);
131 
132         controller.onStop();
133         when(mDisplay.getBrightnessInfo()).thenReturn(
134                 new BrightnessInfo(0.5f, 0.0f, 1.0f, BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF,
135                         0.5f, BrightnessInfo.BRIGHTNESS_MAX_REASON_NONE));
136         controller.onStart();
137 
138         verify(mPreference).setSummary("87%");
139     }
140 
141     @Test
updateState_autoBrightness_shouldSetSummaryToAutoBrightness()142     public void updateState_autoBrightness_shouldSetSummaryToAutoBrightness() {
143         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
144                 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
145 
146         when(mDisplay.getBrightnessInfo()).thenReturn(
147                 new BrightnessInfo(0.1f, 0.0f, 1.0f, BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF,
148                     0.5f, BrightnessInfo.BRIGHTNESS_MAX_REASON_NONE));
149 
150         mController.updateState(mPreference);
151 
152         verify(mPreference).setSummary("54%");
153     }
154 
155     @Test
updateState_manualBrightness_shouldSetSummaryToScreenBrightness()156     public void updateState_manualBrightness_shouldSetSummaryToScreenBrightness() {
157         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
158                 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
159 
160         when(mDisplay.getBrightnessInfo()).thenReturn(
161                 new BrightnessInfo(0.5f, 0.0f, 1.0f, BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF,
162                     0.5f, BrightnessInfo.BRIGHTNESS_MAX_REASON_NONE));
163 
164         mController.updateState(mPreference);
165 
166         verify(mPreference).setSummary("87%");
167     }
168 
169     @Test
handlePreferenceTreeClick_transitionTypeNone_shouldPassToNextActivity()170     public void handlePreferenceTreeClick_transitionTypeNone_shouldPassToNextActivity() {
171         final Activity activity = Robolectric.setupActivity(Activity.class);
172         final BrightnessLevelPreferenceController controller =
173                 new BrightnessLevelPreferenceController(activity, null);
174         final ShadowActivity shadowActivity = shadowOf(activity);
175 
176         String preferenceKey = mContext.getString(R.string.preference_key_brightness_level);
177         when(mPreference.getKey()).thenReturn(preferenceKey);
178 
179         controller.handlePreferenceTreeClick(mPreference);
180 
181         final Intent intent = shadowActivity.getNextStartedActivity();
182         assertThat(intent.getIntExtra(SettingsBaseActivity.EXTRA_PAGE_TRANSITION_TYPE, 0))
183                 .isEqualTo(SettingsTransitionHelper.TransitionType.TRANSITION_NONE);
184     }
185 }
186