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; |
| 21 | |
| 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 24 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | import static org.mockito.Mockito.mock; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
| 28 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 29 | import android.app.ActivityManagerInternal; |
| 30 | import android.content.Context; |
| 31 | import android.hardware.display.DisplayManagerInternal; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 32 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 33 | import android.os.BatteryManagerInternal; |
| 34 | import android.os.Looper; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 35 | import android.os.PowerManager; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 36 | import android.os.PowerSaveState; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 37 | import android.os.SystemClock; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 38 | import android.os.SystemProperties; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 39 | import android.test.AndroidTestCase; |
| 40 | import android.test.suitebuilder.annotation.SmallTest; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 41 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 42 | import com.android.internal.app.IBatteryStats; |
| 43 | import com.android.server.LocalServices; |
| 44 | import com.android.server.SystemService; |
| 45 | import com.android.server.lights.LightsManager; |
| 46 | import com.android.server.policy.WindowManagerPolicy; |
| 47 | import com.android.server.power.PowerManagerService.Injector; |
| 48 | import com.android.server.power.PowerManagerService.NativeWrapper; |
| 49 | import com.android.server.power.batterysaver.BatterySavingStats; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 50 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 51 | import org.junit.Rule; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 52 | import org.mockito.Mock; |
| 53 | import org.mockito.MockitoAnnotations; |
| 54 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Tests for {@link com.android.server.power.PowerManagerService} |
| 57 | */ |
| 58 | public class PowerManagerServiceTest extends AndroidTestCase { |
| 59 | private static final float PRECISION = 0.001f; |
| 60 | private static final float BRIGHTNESS_FACTOR = 0.7f; |
| 61 | private static final boolean BATTERY_SAVER_ENABLED = true; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 62 | private static final String TEST_LAST_REBOOT_PROPERTY = "test.sys.boot.reason"; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 63 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 64 | private @Mock BatterySaverPolicy mBatterySaverPolicyMock; |
| 65 | private @Mock LightsManager mLightsManagerMock; |
| 66 | private @Mock DisplayManagerInternal mDisplayManagerInternalMock; |
| 67 | private @Mock BatteryManagerInternal mBatteryManagerInternalMock; |
| 68 | private @Mock ActivityManagerInternal mActivityManagerInternalMock; |
| 69 | private @Mock PowerManagerService.NativeWrapper mNativeWrapperMock; |
| 70 | private @Mock Notifier mNotifierMock; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 71 | private PowerManagerService mService; |
| 72 | private PowerSaveState mPowerSaveState; |
| 73 | private DisplayPowerRequest mDisplayPowerRequest; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 74 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 75 | |
| 76 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 77 | @Rule |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 78 | public void setUp() throws Exception { |
| 79 | super.setUp(); |
| 80 | MockitoAnnotations.initMocks(this); |
| 81 | |
| 82 | mPowerSaveState = new PowerSaveState.Builder() |
| 83 | .setBatterySaverEnabled(BATTERY_SAVER_ENABLED) |
| 84 | .setBrightnessFactor(BRIGHTNESS_FACTOR) |
| 85 | .build(); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 86 | when(mBatterySaverPolicyMock.getBatterySaverPolicy( |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 87 | eq(PowerManager.ServiceType.SCREEN_BRIGHTNESS), anyBoolean())) |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 88 | .thenReturn(mPowerSaveState); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 89 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 90 | mDisplayPowerRequest = new DisplayPowerRequest(); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 91 | addLocalServiceMock(LightsManager.class, mLightsManagerMock); |
| 92 | addLocalServiceMock(DisplayManagerInternal.class, mDisplayManagerInternalMock); |
| 93 | addLocalServiceMock(BatteryManagerInternal.class, mBatteryManagerInternalMock); |
| 94 | addLocalServiceMock(ActivityManagerInternal.class, mActivityManagerInternalMock); |
| 95 | |
| 96 | mService = new PowerManagerService(getContext(), new Injector() { |
| 97 | Notifier createNotifier(Looper looper, Context context, IBatteryStats batteryStats, |
| 98 | SuspendBlocker suspendBlocker, WindowManagerPolicy policy) { |
| 99 | return mNotifierMock; |
| 100 | } |
| 101 | |
| 102 | SuspendBlocker createSuspendBlocker(PowerManagerService service, String name) { |
| 103 | return mock(SuspendBlocker.class); |
| 104 | } |
| 105 | |
| 106 | BatterySaverPolicy createBatterySaverPolicy( |
| 107 | Object lock, Context context, BatterySavingStats batterySavingStats) { |
| 108 | return mBatterySaverPolicyMock; |
| 109 | } |
| 110 | |
| 111 | NativeWrapper createNativeWrapper() { |
| 112 | return mNativeWrapperMock; |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void tearDown() throws Exception { |
| 119 | LocalServices.removeServiceForTest(LightsManager.class); |
| 120 | LocalServices.removeServiceForTest(DisplayManagerInternal.class); |
| 121 | LocalServices.removeServiceForTest(BatteryManagerInternal.class); |
| 122 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Creates a mock and registers it to {@link LocalServices}. |
| 127 | */ |
| 128 | private static <T> void addLocalServiceMock(Class<T> clazz, T mock) { |
| 129 | LocalServices.removeServiceForTest(clazz); |
| 130 | LocalServices.addService(clazz, mock); |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @SmallTest |
| 134 | public void testUpdatePowerScreenPolicy_UpdateDisplayPowerRequest() { |
| 135 | mService.updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest); |
| 136 | assertThat(mDisplayPowerRequest.lowPowerMode).isEqualTo(BATTERY_SAVER_ENABLED); |
| 137 | assertThat(mDisplayPowerRequest.screenLowPowerBrightnessFactor) |
| 138 | .isWithin(PRECISION).of(BRIGHTNESS_FACTOR); |
| 139 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 140 | |
| 141 | @SmallTest |
| 142 | public void testGetLastShutdownReasonInternal() { |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 143 | SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal"); |
| 144 | int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY); |
| 145 | SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, ""); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 146 | assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); |
| 147 | } |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 148 | |
| 149 | @SmallTest |
| 150 | public void testGetDesiredScreenPolicy_WithVR() throws Exception { |
| 151 | // Brighten up the screen |
| 152 | mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, 0); |
| 153 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 154 | DisplayPowerRequest.POLICY_BRIGHT); |
| 155 | |
| 156 | // Move to VR |
| 157 | mService.setVrModeEnabled(true); |
| 158 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 159 | DisplayPowerRequest.POLICY_VR); |
| 160 | |
| 161 | // Then take a nap |
| 162 | mService.setWakefulnessLocked(WAKEFULNESS_ASLEEP, 0); |
| 163 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 164 | DisplayPowerRequest.POLICY_OFF); |
| 165 | |
| 166 | // Wake up to VR |
| 167 | mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, 0); |
| 168 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 169 | DisplayPowerRequest.POLICY_VR); |
| 170 | |
| 171 | // And back to normal |
| 172 | mService.setVrModeEnabled(false); |
| 173 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 174 | DisplayPowerRequest.POLICY_BRIGHT); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 175 | } |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 176 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame^] | 177 | @SmallTest |
| 178 | public void testWakefulnessAwake_InitialValue() throws Exception { |
| 179 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 180 | } |
| 181 | |
| 182 | @SmallTest |
| 183 | public void testWakefulnessSleep_NoDozeSleepFlag() throws Exception { |
| 184 | // Start with AWAKE state |
| 185 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); |
| 186 | |
| 187 | mService.systemReady(null); |
| 188 | mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); |
| 189 | |
| 190 | // Take a nap with a flag. |
| 191 | mService.getBinderServiceInstance().goToSleep(SystemClock.uptimeMillis(), |
| 192 | PowerManager.GO_TO_SLEEP_REASON_APPLICATION, PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE); |
| 193 | |
| 194 | assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_ASLEEP); |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 195 | } |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 196 | } |