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