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 | |
| 24 | import static org.mockito.Matchers.anyBoolean; |
| 25 | import static org.mockito.Matchers.eq; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 28 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 29 | import android.os.PowerManager; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 30 | import android.os.PowerSaveState; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 31 | import android.os.SystemProperties; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 32 | import android.test.AndroidTestCase; |
| 33 | import android.test.suitebuilder.annotation.SmallTest; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame^] | 34 | |
| 35 | import com.android.server.power.batterysaver.BatterySaverController; |
| 36 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 37 | import org.junit.Rule; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 38 | import org.mockito.Mock; |
| 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Tests for {@link com.android.server.power.PowerManagerService} |
| 43 | */ |
| 44 | public class PowerManagerServiceTest extends AndroidTestCase { |
| 45 | private static final float PRECISION = 0.001f; |
| 46 | private static final float BRIGHTNESS_FACTOR = 0.7f; |
| 47 | private static final boolean BATTERY_SAVER_ENABLED = true; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 48 | private static final String TEST_LAST_REBOOT_PROPERTY = "test.sys.boot.reason"; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 49 | |
| 50 | private @Mock BatterySaverPolicy mBatterySaverPolicy; |
| 51 | private PowerManagerService mService; |
| 52 | private PowerSaveState mPowerSaveState; |
| 53 | private DisplayPowerRequest mDisplayPowerRequest; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 54 | |
| 55 | @Rule |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 56 | public void setUp() throws Exception { |
| 57 | super.setUp(); |
| 58 | MockitoAnnotations.initMocks(this); |
| 59 | |
| 60 | mPowerSaveState = new PowerSaveState.Builder() |
| 61 | .setBatterySaverEnabled(BATTERY_SAVER_ENABLED) |
| 62 | .setBrightnessFactor(BRIGHTNESS_FACTOR) |
| 63 | .build(); |
| 64 | when(mBatterySaverPolicy.getBatterySaverPolicy( |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 65 | eq(PowerManager.ServiceType.SCREEN_BRIGHTNESS), anyBoolean())) |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 66 | .thenReturn(mPowerSaveState); |
| 67 | mDisplayPowerRequest = new DisplayPowerRequest(); |
| 68 | mService = new PowerManagerService(getContext(), mBatterySaverPolicy); |
| 69 | } |
| 70 | |
| 71 | @SmallTest |
| 72 | public void testUpdatePowerScreenPolicy_UpdateDisplayPowerRequest() { |
| 73 | mService.updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest); |
| 74 | assertThat(mDisplayPowerRequest.lowPowerMode).isEqualTo(BATTERY_SAVER_ENABLED); |
| 75 | assertThat(mDisplayPowerRequest.screenLowPowerBrightnessFactor) |
| 76 | .isWithin(PRECISION).of(BRIGHTNESS_FACTOR); |
| 77 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 78 | |
| 79 | @SmallTest |
| 80 | public void testGetLastShutdownReasonInternal() { |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 81 | SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal"); |
| 82 | int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY); |
| 83 | SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, ""); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 84 | assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); |
| 85 | } |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 86 | |
| 87 | @SmallTest |
| 88 | public void testGetDesiredScreenPolicy_WithVR() throws Exception { |
| 89 | // Brighten up the screen |
| 90 | mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, 0); |
| 91 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 92 | DisplayPowerRequest.POLICY_BRIGHT); |
| 93 | |
| 94 | // Move to VR |
| 95 | mService.setVrModeEnabled(true); |
| 96 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 97 | DisplayPowerRequest.POLICY_VR); |
| 98 | |
| 99 | // Then take a nap |
| 100 | mService.setWakefulnessLocked(WAKEFULNESS_ASLEEP, 0); |
| 101 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 102 | DisplayPowerRequest.POLICY_OFF); |
| 103 | |
| 104 | // Wake up to VR |
| 105 | mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, 0); |
| 106 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 107 | DisplayPowerRequest.POLICY_VR); |
| 108 | |
| 109 | // And back to normal |
| 110 | mService.setVrModeEnabled(false); |
| 111 | assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( |
| 112 | DisplayPowerRequest.POLICY_BRIGHT); |
| 113 | |
| 114 | } |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 115 | } |