blob: 03c10f3a86f83d4992ce6b8d64e834ddad4ae551 [file] [log] [blame]
Jay Aliomer8b2671b2019-10-24 13:18:06 -04001/*
2 * Copyright (C) 20019 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.server;
18
19import android.app.IUiModeManager;
20import android.content.BroadcastReceiver;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.pm.PackageManager;
24import android.content.res.Configuration;
25import android.content.res.Resources;
26import android.os.PowerManager;
27import android.os.RemoteException;
28import android.testing.AndroidTestingRunner;
29import android.testing.TestableLooper;
30import com.android.server.twilight.TwilightManager;
31import com.android.server.wm.WindowManagerInternal;
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36
37import java.util.HashSet;
38import java.util.Set;
39
40import static android.app.UiModeManager.MODE_NIGHT_AUTO;
41import static android.app.UiModeManager.MODE_NIGHT_NO;
42import static android.app.UiModeManager.MODE_NIGHT_YES;
43import static junit.framework.TestCase.assertFalse;
44import static junit.framework.TestCase.assertTrue;
45import static org.junit.Assert.assertEquals;
46import static org.mockito.ArgumentMatchers.any;
47import static org.mockito.ArgumentMatchers.anyString;
48import static org.mockito.BDDMockito.given;
49import static org.mockito.Mockito.verify;
50import static org.mockito.Mockito.when;
51
52@RunWith(AndroidTestingRunner.class)
53@TestableLooper.RunWithLooper
54public class UiModeManagerServiceTest extends UiServiceTestCase {
55 private UiModeManagerService mUiManagerService;
56 private IUiModeManager mService;
57 @Mock
58 private ContentResolver mContentResolver;
59 @Mock
60 private WindowManagerInternal mWindowManager;
61 @Mock
62 private Context mContext;
63 @Mock
64 private Resources mResources;
65 @Mock
66 TwilightManager mTwilightManager;
67 @Mock
68 PowerManager.WakeLock mWakeLock;
69 private Set<BroadcastReceiver> mScreenOffRecievers;
70
71 @Before
72 public void setUp() {
73 mUiManagerService = new UiModeManagerService(mContext, mWindowManager, mWakeLock,
74 mTwilightManager, true);
75 mScreenOffRecievers = new HashSet<>();
76 mService = mUiManagerService.getService();
77 when(mContext.checkCallingOrSelfPermission(anyString()))
78 .thenReturn(PackageManager.PERMISSION_GRANTED);
79 when(mContext.getResources()).thenReturn(mResources);
80 when(mContext.getContentResolver()).thenReturn(mContentResolver);
81 when(mContext.registerReceiver(any(), any())).then(inv -> {
82 mScreenOffRecievers.add(inv.getArgument(0));
83 return null;
84 });
85 }
86
87 @Test
88 public void setAutoMode_screenOffRegistered() throws RemoteException {
89 try {
90 mService.setNightMode(MODE_NIGHT_NO);
91 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
92 mService.setNightMode(MODE_NIGHT_AUTO);
93 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any());
94 }
95
96 @Test
97 public void setAutoMode_screenOffUnRegistered() throws RemoteException {
98 try {
99 mService.setNightMode(MODE_NIGHT_AUTO);
100 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
101 try {
102 mService.setNightMode(MODE_NIGHT_NO);
103 } catch (SecurityException e) { /*we should ignore this update config exception*/ }
104 given(mContext.registerReceiver(any(), any())).willThrow(SecurityException.class);
105 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
106 }
107
108 @Test
109 public void setAutoMode_clearCache() throws RemoteException {
110 try {
111 mService.setNightMode(MODE_NIGHT_AUTO);
112 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
113 try {
114 mService.setNightMode(MODE_NIGHT_NO);
115 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
116 verify(mWindowManager).clearSnapshotCache();
117 }
118
119 @Test
120 public void setNightModeActive_fromNightModeYesToNoWhenFalse() throws RemoteException {
121 try {
122 mService.setNightMode(MODE_NIGHT_YES);
123 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
124 try {
125 mService.setNightModeActivated(false);
126 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
127 assertEquals(MODE_NIGHT_NO, mService.getNightMode());
128 }
129
130 @Test
131 public void setNightModeActive_fromNightModeNoToYesWhenTrue() throws RemoteException {
132 try {
133 mService.setNightMode(MODE_NIGHT_NO);
134 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
135 try {
136 mService.setNightModeActivated(true);
137 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
138 assertEquals(MODE_NIGHT_YES, mService.getNightMode());
139 }
140
141 @Test
142 public void setNightModeActive_autoNightModeNoChanges() throws RemoteException {
143 try {
144 mService.setNightMode(MODE_NIGHT_AUTO);
145 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
146 try {
147 mService.setNightModeActivated(true);
148 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
149 assertEquals(MODE_NIGHT_AUTO, mService.getNightMode());
150 }
151
152 @Test
153 public void isNightModeActive_nightModeYes() throws RemoteException {
154 try {
155 mService.setNightMode(MODE_NIGHT_YES);
156 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
157 assertTrue(isNightModeActivated());
158 }
159
160 @Test
161 public void isNightModeActive_nightModeNo() throws RemoteException {
162 try {
163 mService.setNightMode(MODE_NIGHT_NO);
164 } catch (SecurityException e) { /* we should ignore this update config exception*/ }
165 assertFalse(isNightModeActivated());
166 }
167
168 private boolean isNightModeActivated() {
169 return (mUiManagerService.getConfiguration().uiMode
170 & Configuration.UI_MODE_NIGHT_YES) != 0;
171 }
172}