• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package android.app.cts;
17 
18 import android.app.UiModeManager;
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.content.res.Configuration;
22 import android.test.AndroidTestCase;
23 import android.util.Log;
24 
25 import com.android.compatibility.common.util.BatteryUtils;
26 import com.android.compatibility.common.util.SettingsUtils;
27 
28 public class UiModeManagerTest extends AndroidTestCase {
29     private static final String TAG = "UiModeManagerTest";
30 
31     private UiModeManager mUiModeManager;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         mUiModeManager = (UiModeManager) getContext().getSystemService(Context.UI_MODE_SERVICE);
37         assertNotNull(mUiModeManager);
38     }
39 
testUiMode()40     public void testUiMode() throws Exception {
41         if (isAutomotive()) {
42             Log.i(TAG, "testUiMode automotive");
43             doTestUiModeForAutomotive();
44         } else if (isTelevision()) {
45             assertEquals(Configuration.UI_MODE_TYPE_TELEVISION,
46                     mUiModeManager.getCurrentModeType());
47             doTestLockedUiMode();
48         } else if (isWatch()) {
49             assertEquals(Configuration.UI_MODE_TYPE_WATCH,
50                     mUiModeManager.getCurrentModeType());
51             doTestLockedUiMode();
52         } else {
53             Log.i(TAG, "testUiMode generic");
54             doTestUiModeGeneric();
55         }
56     }
57 
testNightMode()58     public void testNightMode() throws Exception {
59         if (isAutomotive()) {
60             assertTrue(mUiModeManager.isNightModeLocked());
61             doTestLockedNightMode();
62         } else {
63             if (mUiModeManager.isNightModeLocked()) {
64                 doTestLockedNightMode();
65             } else {
66                 doTestUnlockedNightMode();
67             }
68         }
69     }
70 
testNightModeInCarModeIsTransient()71     public void testNightModeInCarModeIsTransient() {
72         if (mUiModeManager.isNightModeLocked()) {
73             return;
74         }
75 
76         assertNightModeChange(UiModeManager.MODE_NIGHT_NO);
77 
78         mUiModeManager.enableCarMode(0);
79         assertEquals(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
80 
81         assertNightModeChange(UiModeManager.MODE_NIGHT_YES);
82 
83         mUiModeManager.disableCarMode(0);
84         assertNotSame(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
85         assertEquals(UiModeManager.MODE_NIGHT_NO, mUiModeManager.getNightMode());
86     }
87 
testNightModeToggleInCarModeDoesNotChangeSetting()88     public void testNightModeToggleInCarModeDoesNotChangeSetting() {
89         if (mUiModeManager.isNightModeLocked()) {
90             return;
91         }
92 
93         assertNightModeChange(UiModeManager.MODE_NIGHT_NO);
94         assertStoredNightModeSetting(UiModeManager.MODE_NIGHT_NO);
95 
96         mUiModeManager.enableCarMode(0);
97         assertStoredNightModeSetting(UiModeManager.MODE_NIGHT_NO);
98 
99         assertNightModeChange(UiModeManager.MODE_NIGHT_YES);
100         assertStoredNightModeSetting(UiModeManager.MODE_NIGHT_NO);
101 
102         mUiModeManager.disableCarMode(0);
103         assertStoredNightModeSetting(UiModeManager.MODE_NIGHT_NO);
104     }
105 
testNightModeInCarModeOnPowerSaveIsTransient()106     public void testNightModeInCarModeOnPowerSaveIsTransient() throws Throwable {
107         if (mUiModeManager.isNightModeLocked() || !BatteryUtils.isBatterySaverSupported()) {
108             return;
109         }
110 
111         BatteryUtils.runDumpsysBatteryUnplug();
112 
113         // Turn off battery saver, disable night mode
114         BatteryUtils.enableBatterySaver(false);
115         mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
116         assertEquals(UiModeManager.MODE_NIGHT_NO, mUiModeManager.getNightMode());
117         assertVisibleNightModeInConfiguration(Configuration.UI_MODE_NIGHT_NO);
118 
119         // Then enable battery saver to check night mode is made visible
120         BatteryUtils.enableBatterySaver(true);
121         assertEquals(UiModeManager.MODE_NIGHT_NO, mUiModeManager.getNightMode());
122         assertVisibleNightModeInConfiguration(Configuration.UI_MODE_NIGHT_YES);
123 
124         // Then disable it, enable car mode, and check night mode is not visible
125         BatteryUtils.enableBatterySaver(false);
126         mUiModeManager.enableCarMode(0);
127         assertEquals(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
128         assertVisibleNightModeInConfiguration(Configuration.UI_MODE_NIGHT_NO);
129 
130         // Enable battery saver, check that night mode is still not visible, overridden by car mode
131         BatteryUtils.enableBatterySaver(true);
132         assertEquals(UiModeManager.MODE_NIGHT_NO, mUiModeManager.getNightMode());
133         assertVisibleNightModeInConfiguration(Configuration.UI_MODE_NIGHT_NO);
134 
135         // Disable car mode
136         mUiModeManager.disableCarMode(0);
137 
138         // Toggle night mode to force propagation of uiMode update, since disabling car mode
139         // is deferred to a broadcast.
140         mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
141         mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
142 
143         // Check battery saver mode now shows night mode
144         assertNotSame(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
145         assertVisibleNightModeInConfiguration(Configuration.UI_MODE_NIGHT_YES);
146 
147         // Disable battery saver and check night mode back to not visible
148         BatteryUtils.enableBatterySaver(false);
149         assertEquals(UiModeManager.MODE_NIGHT_NO, mUiModeManager.getNightMode());
150         assertVisibleNightModeInConfiguration(Configuration.UI_MODE_NIGHT_NO);
151 
152         BatteryUtils.runDumpsysBatteryReset();
153     }
154 
isAutomotive()155     private boolean isAutomotive() {
156         return getContext().getPackageManager().hasSystemFeature(
157                 PackageManager.FEATURE_AUTOMOTIVE);
158     }
159 
isTelevision()160     private boolean isTelevision() {
161         PackageManager pm = getContext().getPackageManager();
162         return pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
163                 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
164     }
165 
isWatch()166     private boolean isWatch() {
167         return getContext().getPackageManager().hasSystemFeature(
168                 PackageManager.FEATURE_WATCH);
169     }
170 
doTestUiModeForAutomotive()171     private void doTestUiModeForAutomotive() throws Exception {
172         assertEquals(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
173         assertTrue(mUiModeManager.isUiModeLocked());
174         doTestLockedUiMode();
175     }
176 
doTestUiModeGeneric()177     private void doTestUiModeGeneric() throws Exception {
178         if (mUiModeManager.isUiModeLocked()) {
179             doTestLockedUiMode();
180         } else {
181             doTestUnlockedUiMode();
182         }
183     }
184 
doTestLockedUiMode()185     private void doTestLockedUiMode() throws Exception {
186         int originalMode = mUiModeManager.getCurrentModeType();
187         mUiModeManager.enableCarMode(0);
188         assertEquals(originalMode, mUiModeManager.getCurrentModeType());
189         mUiModeManager.disableCarMode(0);
190         assertEquals(originalMode, mUiModeManager.getCurrentModeType());
191     }
192 
doTestUnlockedUiMode()193     private void doTestUnlockedUiMode() throws Exception {
194         mUiModeManager.enableCarMode(0);
195         assertEquals(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
196         mUiModeManager.disableCarMode(0);
197         assertNotSame(Configuration.UI_MODE_TYPE_CAR, mUiModeManager.getCurrentModeType());
198     }
199 
doTestLockedNightMode()200     private void doTestLockedNightMode() throws Exception {
201         int currentMode = mUiModeManager.getNightMode();
202         if (currentMode == UiModeManager.MODE_NIGHT_YES) {
203             mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
204             assertEquals(currentMode, mUiModeManager.getNightMode());
205         } else {
206             mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
207             assertEquals(currentMode, mUiModeManager.getNightMode());
208         }
209     }
210 
doTestUnlockedNightMode()211     private void doTestUnlockedNightMode() throws Exception {
212         // day night mode should be settable regardless of car mode.
213         mUiModeManager.enableCarMode(0);
214         doTestAllNightModes();
215         mUiModeManager.disableCarMode(0);
216         doTestAllNightModes();
217     }
218 
doTestAllNightModes()219     private void doTestAllNightModes() {
220         assertNightModeChange(UiModeManager.MODE_NIGHT_AUTO);
221         assertNightModeChange(UiModeManager.MODE_NIGHT_YES);
222         assertNightModeChange(UiModeManager.MODE_NIGHT_NO);
223     }
224 
assertNightModeChange(int mode)225     private void assertNightModeChange(int mode) {
226         mUiModeManager.setNightMode(mode);
227         assertEquals(mode, mUiModeManager.getNightMode());
228     }
229 
assertVisibleNightModeInConfiguration(int mode)230     private void assertVisibleNightModeInConfiguration(int mode) {
231         int uiMode = getContext().getResources().getConfiguration().uiMode;
232         int flags = uiMode & Configuration.UI_MODE_NIGHT_MASK;
233         assertEquals(mode, flags);
234     }
235 
assertStoredNightModeSetting(int mode)236     private void assertStoredNightModeSetting(int mode) {
237         // Settings.Secure.UI_NIGHT_MODE
238         String storedMode = SettingsUtils.getSecureSetting("ui_night_mode");
239         int storedModeInt = Integer.parseInt(storedMode);
240         assertEquals(mode, storedModeInt);
241     }
242 }
243