jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 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.server.power; |
| 18 | |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 19 | import static android.os.PowerManagerInternal.WAKEFULNESS_ASLEEP; |
| 20 | import static android.os.PowerManagerInternal.WAKEFULNESS_AWAKE; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 21 | import static android.os.PowerManagerInternal.WAKEFULNESS_DOZING; |
| 22 | import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 23 | |
| 24 | import static com.google.common.truth.Truth.assertThat; |
| 25 | |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.any; |
| 27 | import static org.mockito.ArgumentMatchers.anyInt; |
| 28 | import static org.mockito.ArgumentMatchers.anyString; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.argThat; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.eq; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 31 | import static org.mockito.ArgumentMatchers.isA; |
| 32 | import static org.mockito.ArgumentMatchers.isNull; |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 33 | import static org.mockito.Mockito.doAnswer; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 34 | import static org.mockito.Mockito.mock; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 35 | import static org.mockito.Mockito.spy; |
| 36 | import static org.mockito.Mockito.verify; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 37 | import static org.mockito.Mockito.when; |
| 38 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 39 | import android.app.ActivityManagerInternal; |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 40 | import android.attention.AttentionManagerInternal; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 41 | import android.content.Context; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 42 | import android.content.ContextWrapper; |
| 43 | import android.content.Intent; |
| 44 | import android.content.IntentFilter; |
| 45 | import android.content.res.Resources; |
| 46 | import android.hardware.SensorManager; |
| 47 | import android.hardware.display.AmbientDisplayConfiguration; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 48 | import android.hardware.display.DisplayManagerInternal; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 49 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 50 | import android.os.BatteryManager; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 51 | import android.os.BatteryManagerInternal; |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 52 | import android.os.Binder; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 53 | import android.os.Handler; |
| 54 | import android.os.IBinder; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 55 | import android.os.Looper; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 56 | import android.os.PowerManager; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 57 | import android.os.PowerSaveState; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 58 | import android.os.SystemClock; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 59 | import android.os.SystemProperties; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 60 | import android.os.UserHandle; |
| 61 | import android.provider.Settings; |
| 62 | |
| 63 | import androidx.test.InstrumentationRegistry; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 64 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 65 | import com.android.internal.app.IBatteryStats; |
| 66 | import com.android.server.LocalServices; |
| 67 | import com.android.server.SystemService; |
| 68 | import com.android.server.lights.LightsManager; |
| 69 | import com.android.server.policy.WindowManagerPolicy; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 70 | import com.android.server.power.PowerManagerService.BatteryReceiver; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 71 | import com.android.server.power.PowerManagerService.Injector; |
| 72 | import com.android.server.power.PowerManagerService.NativeWrapper; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 73 | import com.android.server.power.PowerManagerService.UserSwitchedReceiver; |
Kweku Adams | 7fb72a4 | 2019-01-08 16:08:49 -0800 | [diff] [blame] | 74 | import com.android.server.power.batterysaver.BatterySaverPolicy; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 75 | import com.android.server.power.batterysaver.BatterySavingStats; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 76 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 77 | import org.junit.After; |
| 78 | import org.junit.Before; |
| 79 | import org.junit.Test; |
| 80 | import org.mockito.ArgumentCaptor; |
| 81 | import org.mockito.ArgumentMatcher; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 82 | import org.mockito.Mock; |
| 83 | import org.mockito.MockitoAnnotations; |
| 84 | |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 85 | import java.util.HashMap; |
| 86 | import java.util.Map; |
| 87 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Tests for {@link com.android.server.power.PowerManagerService} |
| 90 | */ |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 91 | public class PowerManagerServiceTest { |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 92 | private static final float PRECISION = 0.001f; |
| 93 | private static final float BRIGHTNESS_FACTOR = 0.7f; |
| 94 | private static final boolean BATTERY_SAVER_ENABLED = true; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 95 | private static final String TEST_LAST_REBOOT_PROPERTY = "test.sys.boot.reason"; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 96 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 97 | @Mock private BatterySaverPolicy mBatterySaverPolicyMock; |
| 98 | @Mock private LightsManager mLightsManagerMock; |
| 99 | @Mock private DisplayManagerInternal mDisplayManagerInternalMock; |
| 100 | @Mock private BatteryManagerInternal mBatteryManagerInternalMock; |
| 101 | @Mock private ActivityManagerInternal mActivityManagerInternalMock; |
| 102 | @Mock private AttentionManagerInternal mAttentionManagerInternalMock; |
| 103 | @Mock private PowerManagerService.NativeWrapper mNativeWrapperMock; |
| 104 | @Mock private Notifier mNotifierMock; |
| 105 | @Mock private WirelessChargerDetector mWirelessChargerDetectorMock; |
| 106 | @Mock private AmbientDisplayConfiguration mAmbientDisplayConfigurationMock; |
| 107 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 108 | private PowerManagerService mService; |
| 109 | private PowerSaveState mPowerSaveState; |
| 110 | private DisplayPowerRequest mDisplayPowerRequest; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 111 | private ContextWrapper mContextSpy; |
| 112 | private BatteryReceiver mBatteryReceiver; |
| 113 | private UserSwitchedReceiver mUserSwitchedReceiver; |
| 114 | private Resources mResourcesSpy; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 115 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 116 | private class IntentFilterMatcher implements ArgumentMatcher<IntentFilter> { |
| 117 | private final IntentFilter mFilter; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 118 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 119 | IntentFilterMatcher(IntentFilter filter) { |
| 120 | mFilter = filter; |
| 121 | } |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 122 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 123 | @Override |
| 124 | public boolean matches(IntentFilter other) { |
| 125 | if (other.countActions() != mFilter.countActions()) { |
| 126 | return false; |
| 127 | } |
| 128 | for (int i = 0; i < mFilter.countActions(); i++) { |
| 129 | if (!mFilter.getAction(i).equals(other.getAction(i))) { |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Before |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 138 | public void setUp() throws Exception { |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 139 | MockitoAnnotations.initMocks(this); |
| 140 | |
| 141 | mPowerSaveState = new PowerSaveState.Builder() |
| 142 | .setBatterySaverEnabled(BATTERY_SAVER_ENABLED) |
| 143 | .setBrightnessFactor(BRIGHTNESS_FACTOR) |
| 144 | .build(); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 145 | when(mBatterySaverPolicyMock.getBatterySaverPolicy( |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 146 | eq(PowerManager.ServiceType.SCREEN_BRIGHTNESS))) |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 147 | .thenReturn(mPowerSaveState); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 148 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 149 | mDisplayPowerRequest = new DisplayPowerRequest(); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 150 | addLocalServiceMock(LightsManager.class, mLightsManagerMock); |
| 151 | addLocalServiceMock(DisplayManagerInternal.class, mDisplayManagerInternalMock); |
| 152 | addLocalServiceMock(BatteryManagerInternal.class, mBatteryManagerInternalMock); |
| 153 | addLocalServiceMock(ActivityManagerInternal.class, mActivityManagerInternalMock); |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 154 | addLocalServiceMock(AttentionManagerInternal.class, mAttentionManagerInternalMock); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 155 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 156 | mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext())); |
| 157 | mResourcesSpy = spy(mContextSpy.getResources()); |
| 158 | when(mContextSpy.getResources()).thenReturn(mResourcesSpy); |
| 159 | |
| 160 | mService = new PowerManagerService(mContextSpy, new Injector() { |
| 161 | @Override |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 162 | Notifier createNotifier(Looper looper, Context context, IBatteryStats batteryStats, |
| 163 | SuspendBlocker suspendBlocker, WindowManagerPolicy policy) { |
| 164 | return mNotifierMock; |
| 165 | } |
| 166 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 167 | @Override |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 168 | SuspendBlocker createSuspendBlocker(PowerManagerService service, String name) { |
| 169 | return mock(SuspendBlocker.class); |
| 170 | } |
| 171 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 172 | @Override |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 173 | BatterySaverPolicy createBatterySaverPolicy( |
| 174 | Object lock, Context context, BatterySavingStats batterySavingStats) { |
| 175 | return mBatterySaverPolicyMock; |
| 176 | } |
| 177 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 178 | @Override |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 179 | NativeWrapper createNativeWrapper() { |
| 180 | return mNativeWrapperMock; |
| 181 | } |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 182 | |
| 183 | @Override |
| 184 | WirelessChargerDetector createWirelessChargerDetector( |
| 185 | SensorManager sensorManager, SuspendBlocker suspendBlocker, Handler handler) { |
| 186 | return mWirelessChargerDetectorMock; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | AmbientDisplayConfiguration createAmbientDisplayConfiguration(Context context) { |
| 191 | return mAmbientDisplayConfigurationMock; |
| 192 | } |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 193 | }); |
| 194 | } |
| 195 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 196 | @After |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 197 | public void tearDown() throws Exception { |
| 198 | LocalServices.removeServiceForTest(LightsManager.class); |
| 199 | LocalServices.removeServiceForTest(DisplayManagerInternal.class); |
| 200 | LocalServices.removeServiceForTest(BatteryManagerInternal.class); |
| 201 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 202 | Settings.Global.putInt( |
| 203 | mContextSpy.getContentResolver(), Settings.Global.THEATER_MODE_ON, 0); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Creates a mock and registers it to {@link LocalServices}. |
| 208 | */ |
| 209 | private static <T> void addLocalServiceMock(Class<T> clazz, T mock) { |
| 210 | LocalServices.removeServiceForTest(clazz); |
| 211 | LocalServices.addService(clazz, mock); |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 214 | private void startSystem() throws Exception { |
| 215 | mService.systemReady(null); |
| 216 | |
| 217 | // Grab the BatteryReceiver |
| 218 | ArgumentCaptor<BatteryReceiver> batCaptor = ArgumentCaptor.forClass(BatteryReceiver.class); |
| 219 | IntentFilter batFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); |
| 220 | batFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 221 | verify(mContextSpy).registerReceiver(batCaptor.capture(), |
| 222 | argThat(new IntentFilterMatcher(batFilter)), isNull(), isA(Handler.class)); |
| 223 | mBatteryReceiver = batCaptor.getValue(); |
| 224 | |
| 225 | // Grab the UserSwitchedReceiver |
| 226 | ArgumentCaptor<UserSwitchedReceiver> userSwitchedCaptor = |
| 227 | ArgumentCaptor.forClass(UserSwitchedReceiver.class); |
| 228 | IntentFilter usFilter = new IntentFilter(Intent.ACTION_USER_SWITCHED); |
| 229 | verify(mContextSpy).registerReceiver(userSwitchedCaptor.capture(), |
| 230 | argThat(new IntentFilterMatcher(usFilter)), isNull(), isA(Handler.class)); |
| 231 | mUserSwitchedReceiver = userSwitchedCaptor.getValue(); |
| 232 | |
| 233 | mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); |
| 234 | } |
| 235 | |
| 236 | private void forceSleep() { |
| 237 | mService.getBinderServiceInstance().goToSleep(SystemClock.uptimeMillis(), |
| 238 | PowerManager.GO_TO_SLEEP_REASON_APPLICATION, PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE); |
| 239 | } |
| 240 | |
| 241 | private void forceDream() { |
| 242 | mService.getBinderServiceInstance().nap(SystemClock.uptimeMillis()); |
| 243 | } |
| 244 | |
| 245 | private void forceAwake() { |
| 246 | mService.getBinderServiceInstance().wakeUp(SystemClock.uptimeMillis(), |
| 247 | PowerManager.WAKE_REASON_UNKNOWN, "testing IPowerManager.wakeUp()", "pkg.name"); |
| 248 | } |
| 249 | |
| 250 | private void forceDozing() { |
| 251 | mService.getBinderServiceInstance().goToSleep(SystemClock.uptimeMillis(), |
| 252 | PowerManager.GO_TO_SLEEP_REASON_APPLICATION, 0); |
| 253 | } |
| 254 | |
| 255 | private void setPluggedIn(boolean isPluggedIn) { |
| 256 | // Set the callback to return the new state |
| 257 | when(mBatteryManagerInternalMock.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) |
| 258 | .thenReturn(isPluggedIn); |
| 259 | // Trigger PowerManager to reread the plug-in state |
| 260 | mBatteryReceiver.onReceive(mContextSpy, new Intent(Intent.ACTION_BATTERY_CHANGED)); |
| 261 | } |
| 262 | |
| 263 | @Test |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 264 | public void testUpdatePowerScreenPolicy_UpdateDisplayPowerRequest() { |
| 265 | mService.updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest); |
| 266 | assertThat(mDisplayPowerRequest.lowPowerMode).isEqualTo(BATTERY_SAVER_ENABLED); |
| 267 | assertThat(mDisplayPowerRequest.screenLowPowerBrightnessFactor) |
| 268 | .isWithin(PRECISION).of(BRIGHTNESS_FACTOR); |
| 269 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 270 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 271 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 272 | public void testGetLastShutdownReasonInternal() { |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 273 | SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal"); |
| 274 | int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY); |
| 275 | SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, ""); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 276 | assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); |
| 277 | } |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 278 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 279 | @Test |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 280 | public void testGetDesiredScreenPolicy_WithVR() throws Exception { |
| 281 | // Brighten up the screen |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 282 | mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, PowerManager.WAKE_REASON_UNKNOWN, 0); |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 283 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 284 | DisplayPowerRequest.POLICY_BRIGHT); |
| 285 | |
| 286 | // Move to VR |
| 287 | mService.setVrModeEnabled(true); |
| 288 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 289 | DisplayPowerRequest.POLICY_VR); |
| 290 | |
| 291 | // Then take a nap |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 292 | mService.setWakefulnessLocked(WAKEFULNESS_ASLEEP, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, |
| 293 | 0); |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 294 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 295 | DisplayPowerRequest.POLICY_OFF); |
| 296 | |
| 297 | // Wake up to VR |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 298 | mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, PowerManager.WAKE_REASON_UNKNOWN, 0); |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 299 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 300 | DisplayPowerRequest.POLICY_VR); |
| 301 | |
| 302 | // And back to normal |
| 303 | mService.setVrModeEnabled(false); |
| 304 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 305 | DisplayPowerRequest.POLICY_BRIGHT); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 306 | } |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 307 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 308 | @Test |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 309 | public void testWakefulnessAwake_InitialValue() throws Exception { |
| 310 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 311 | } |
| 312 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 313 | @Test |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 314 | public void testWakefulnessSleep_NoDozeSleepFlag() throws Exception { |
| 315 | // Start with AWAKE state |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 316 | startSystem(); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 317 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 318 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 319 | // Take a nap and verify. |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 320 | mService.getBinderServiceInstance().goToSleep(SystemClock.uptimeMillis(), |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 321 | PowerManager.GO_TO_SLEEP_REASON_APPLICATION, PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 322 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 323 | } |
Alex Kershaw | 2418ea9 | 2018-10-19 17:17:49 +0100 | [diff] [blame] | 324 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 325 | @Test |
| 326 | public void testWakefulnessAwake_AcquireCausesWakeup() throws Exception { |
| 327 | startSystem(); |
| 328 | forceSleep(); |
| 329 | |
| 330 | IBinder token = new Binder(); |
| 331 | String tag = "acq_causes_wakeup"; |
| 332 | String packageName = "pkg.name"; |
| 333 | |
| 334 | // First, ensure that a normal full wake lock does not cause a wakeup |
| 335 | int flags = PowerManager.FULL_WAKE_LOCK; |
| 336 | mService.getBinderServiceInstance().acquireWakeLock(token, flags, tag, packageName, |
| 337 | null /* workSource */, null /* historyTag */); |
| 338 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 339 | mService.getBinderServiceInstance().releaseWakeLock(token, 0 /* flags */); |
| 340 | |
| 341 | // Ensure that the flag does *NOT* work with a partial wake lock. |
| 342 | flags = PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; |
| 343 | mService.getBinderServiceInstance().acquireWakeLock(token, flags, tag, packageName, |
| 344 | null /* workSource */, null /* historyTag */); |
| 345 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 346 | mService.getBinderServiceInstance().releaseWakeLock(token, 0 /* flags */); |
| 347 | |
| 348 | // Verify that flag forces a wakeup when paired to a FULL_WAKE_LOCK |
| 349 | flags = PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; |
| 350 | mService.getBinderServiceInstance().acquireWakeLock(token, flags, tag, packageName, |
| 351 | null /* workSource */, null /* historyTag */); |
| 352 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 353 | mService.getBinderServiceInstance().releaseWakeLock(token, 0 /* flags */); |
| 354 | } |
| 355 | |
| 356 | @Test |
| 357 | public void testWakefulnessAwake_IPowerManagerWakeUp() throws Exception { |
| 358 | startSystem(); |
| 359 | forceSleep(); |
| 360 | mService.getBinderServiceInstance().wakeUp(SystemClock.uptimeMillis(), |
| 361 | PowerManager.WAKE_REASON_UNKNOWN, "testing IPowerManager.wakeUp()", "pkg.name"); |
| 362 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Tests a series of variants that control whether a device wakes-up when it is plugged in |
| 367 | * or docked. |
| 368 | */ |
| 369 | @Test |
| 370 | public void testWakefulnessAwake_ShouldWakeUpWhenPluggedIn() throws Exception { |
| 371 | boolean powerState; |
| 372 | startSystem(); |
| 373 | forceSleep(); |
| 374 | |
| 375 | // Test 1: |
| 376 | // Set config to prevent it wake up, test, verify, reset config value. |
| 377 | when(mResourcesSpy.getBoolean(com.android.internal.R.bool.config_unplugTurnsOnScreen)) |
| 378 | .thenReturn(false); |
| 379 | mService.readConfigurationLocked(); |
| 380 | setPluggedIn(true); |
| 381 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 382 | when(mResourcesSpy.getBoolean(com.android.internal.R.bool.config_unplugTurnsOnScreen)) |
| 383 | .thenReturn(true); |
| 384 | mService.readConfigurationLocked(); |
| 385 | |
| 386 | // Test 2: |
| 387 | // Turn the power off, sleep, then plug into a wireless charger. |
| 388 | // Verify that we do not wake up if the phone is being plugged into a wireless charger. |
| 389 | setPluggedIn(false); |
| 390 | forceSleep(); |
| 391 | when(mBatteryManagerInternalMock.getPlugType()) |
| 392 | .thenReturn(BatteryManager.BATTERY_PLUGGED_WIRELESS); |
| 393 | when(mWirelessChargerDetectorMock.update(true /* isPowered */, |
| 394 | BatteryManager.BATTERY_PLUGGED_WIRELESS)).thenReturn(false); |
| 395 | setPluggedIn(true); |
| 396 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 397 | |
| 398 | // Test 3: |
| 399 | // Do not wake up if the phone is being REMOVED from a wireless charger |
| 400 | when(mBatteryManagerInternalMock.getPlugType()).thenReturn(0); |
| 401 | setPluggedIn(false); |
| 402 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 403 | |
| 404 | // Test 4: |
| 405 | // Do not wake if we are dreaming. |
| 406 | forceAwake(); // Needs to be awake first before it can dream. |
| 407 | forceDream(); |
| 408 | setPluggedIn(true); |
| 409 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_DREAMING); |
| 410 | forceSleep(); |
| 411 | |
| 412 | // Test 5: |
| 413 | // Don't wake if the device is configured not to wake up in theater mode (and theater |
| 414 | // mode is enabled). |
| 415 | Settings.Global.putInt( |
| 416 | mContextSpy.getContentResolver(), Settings.Global.THEATER_MODE_ON, 1); |
| 417 | mUserSwitchedReceiver.onReceive(mContextSpy, new Intent(Intent.ACTION_USER_SWITCHED)); |
| 418 | when(mResourcesSpy.getBoolean( |
| 419 | com.android.internal.R.bool.config_allowTheaterModeWakeFromUnplug)) |
| 420 | .thenReturn(false); |
| 421 | setPluggedIn(false); |
| 422 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 423 | Settings.Global.putInt( |
| 424 | mContextSpy.getContentResolver(), Settings.Global.THEATER_MODE_ON, 0); |
| 425 | mUserSwitchedReceiver.onReceive(mContextSpy, new Intent(Intent.ACTION_USER_SWITCHED)); |
| 426 | |
| 427 | // Test 6: |
| 428 | // Don't wake up if we are Dozing away and always-on is enabled. |
| 429 | when(mAmbientDisplayConfigurationMock.alwaysOnEnabled(UserHandle.USER_CURRENT)) |
| 430 | .thenReturn(true); |
| 431 | mUserSwitchedReceiver.onReceive(mContextSpy, new Intent(Intent.ACTION_USER_SWITCHED)); |
| 432 | forceAwake(); |
| 433 | forceDozing(); |
| 434 | setPluggedIn(true); |
| 435 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_DOZING); |
| 436 | |
| 437 | // Test 7: |
| 438 | // Finally, take away all the factors above and ensure the device wakes up! |
| 439 | forceAwake(); |
| 440 | forceSleep(); |
| 441 | setPluggedIn(false); |
| 442 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 443 | } |
| 444 | |
| 445 | @Test |
| 446 | public void testWakefulnessDoze_goToSleep() throws Exception { |
| 447 | // Start with AWAKE state |
| 448 | startSystem(); |
| 449 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 450 | |
| 451 | // Take a nap and verify. |
| 452 | mService.getBinderServiceInstance().goToSleep(SystemClock.uptimeMillis(), |
| 453 | PowerManager.GO_TO_SLEEP_REASON_APPLICATION, 0); |
| 454 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_DOZING); |
| 455 | } |
| 456 | |
| 457 | @Test |
Alex Kershaw | 2418ea9 | 2018-10-19 17:17:49 +0100 | [diff] [blame] | 458 | public void testWasDeviceIdleFor_true() { |
| 459 | int interval = 1000; |
| 460 | mService.onUserActivity(); |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 461 | SystemClock.sleep(interval + 1 /* just a little more */); |
Alex Kershaw | 2418ea9 | 2018-10-19 17:17:49 +0100 | [diff] [blame] | 462 | assertThat(mService.wasDeviceIdleForInternal(interval)).isTrue(); |
| 463 | } |
| 464 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 465 | @Test |
Alex Kershaw | 2418ea9 | 2018-10-19 17:17:49 +0100 | [diff] [blame] | 466 | public void testWasDeviceIdleFor_false() { |
| 467 | int interval = 1000; |
| 468 | mService.onUserActivity(); |
| 469 | assertThat(mService.wasDeviceIdleForInternal(interval)).isFalse(); |
| 470 | } |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 471 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 472 | @Test |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 473 | public void testForceSuspend_putsDeviceToSleep() { |
| 474 | mService.systemReady(null); |
| 475 | mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); |
| 476 | |
| 477 | // Verify that we start awake |
| 478 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 479 | |
| 480 | // Grab the wakefulness value when PowerManager finally calls into the |
| 481 | // native component to actually perform the suspend. |
| 482 | when(mNativeWrapperMock.nativeForceSuspend()).then(inv -> { |
| 483 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 484 | return true; |
| 485 | }); |
| 486 | |
| 487 | boolean retval = mService.getBinderServiceInstance().forceSuspend(); |
| 488 | assertThat(retval).isTrue(); |
| 489 | |
| 490 | // Still asleep when the function returns. |
| 491 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
| 492 | } |
| 493 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 494 | @Test |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 495 | public void testForceSuspend_pakeLocksDisabled() { |
| 496 | final String tag = "TestWakelockTag_098213"; |
| 497 | final int flags = PowerManager.PARTIAL_WAKE_LOCK; |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 498 | final String pkg = mContextSpy.getOpPackageName(); |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 499 | |
| 500 | // Set up the Notification mock to keep track of the wakelocks that are currently |
| 501 | // active or disabled. We'll use this to verify that wakelocks are disabled when |
| 502 | // they should be. |
| 503 | final Map<String, Integer> wakelockMap = new HashMap<>(1); |
| 504 | doAnswer(inv -> { |
| 505 | wakelockMap.put((String) inv.getArguments()[1], (int) inv.getArguments()[0]); |
| 506 | return null; |
| 507 | }).when(mNotifierMock).onWakeLockAcquired(anyInt(), anyString(), anyString(), anyInt(), |
| 508 | anyInt(), any(), any()); |
| 509 | doAnswer(inv -> { |
| 510 | wakelockMap.remove((String) inv.getArguments()[1]); |
| 511 | return null; |
| 512 | }).when(mNotifierMock).onWakeLockReleased(anyInt(), anyString(), anyString(), anyInt(), |
| 513 | anyInt(), any(), any()); |
| 514 | |
| 515 | // |
| 516 | // TEST STARTS HERE |
| 517 | // |
| 518 | mService.systemReady(null); |
| 519 | mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); |
| 520 | |
| 521 | // Verify that we start awake |
| 522 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 523 | |
| 524 | // Create a wakelock |
| 525 | mService.getBinderServiceInstance().acquireWakeLock(new Binder(), flags, tag, pkg, |
| 526 | null /* workSource */, null /* historyTag */); |
| 527 | assertThat(wakelockMap.get(tag)).isEqualTo(flags); // Verify wakelock is active. |
| 528 | |
| 529 | // Confirm that the wakelocks have been disabled when the forceSuspend is in flight. |
| 530 | when(mNativeWrapperMock.nativeForceSuspend()).then(inv -> { |
| 531 | // Verify that the wakelock is disabled by the time we get to the native force |
| 532 | // suspend call. |
| 533 | assertThat(wakelockMap.containsKey(tag)).isFalse(); |
| 534 | return true; |
| 535 | }); |
| 536 | |
| 537 | assertThat(mService.getBinderServiceInstance().forceSuspend()).isTrue(); |
| 538 | assertThat(wakelockMap.get(tag)).isEqualTo(flags); |
| 539 | |
| 540 | } |
| 541 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 542 | @Test |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 543 | public void testForceSuspend_forceSuspendFailurePropogated() { |
| 544 | when(mNativeWrapperMock.nativeForceSuspend()).thenReturn(false); |
| 545 | assertThat(mService.getBinderServiceInstance().forceSuspend()).isFalse(); |
| 546 | } |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 547 | } |