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 19 import android.content.Context; 20 import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; 21 import android.os.PowerManager; 22 import android.os.PowerSaveState; 23 import android.test.AndroidTestCase; 24 import android.test.suitebuilder.annotation.SmallTest; 25 import android.text.TextUtils; 26 import java.io.File; 27 import java.io.FileWriter; 28 import java.io.IOException; 29 import java.io.OutputStreamWriter; 30 import org.junit.Rule; 31 import org.junit.rules.TemporaryFolder; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 35 import static android.os.PowerManagerInternal.WAKEFULNESS_ASLEEP; 36 import static android.os.PowerManagerInternal.WAKEFULNESS_AWAKE; 37 import static android.os.PowerManagerInternal.WAKEFULNESS_DOZING; 38 import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING; 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Matchers.anyBoolean; 41 import static org.mockito.Matchers.eq; 42 import static org.mockito.Mockito.when; 43 44 /** 45 * Tests for {@link com.android.server.power.PowerManagerService} 46 */ 47 public class PowerManagerServiceTest extends AndroidTestCase { 48 private static final float PRECISION = 0.001f; 49 private static final float BRIGHTNESS_FACTOR = 0.7f; 50 private static final boolean BATTERY_SAVER_ENABLED = true; 51 private static final String LAST_REBOOT_REASON = "last_reboot_reason"; 52 53 private @Mock BatterySaverPolicy mBatterySaverPolicy; 54 private PowerManagerService mService; 55 private PowerSaveState mPowerSaveState; 56 private DisplayPowerRequest mDisplayPowerRequest; 57 private File mTempReason; 58 59 @Rule 60 public TemporaryFolder temporaryFolder = new TemporaryFolder(); 61 62 public void setUp() throws Exception { 63 super.setUp(); 64 MockitoAnnotations.initMocks(this); 65 66 mPowerSaveState = new PowerSaveState.Builder() 67 .setBatterySaverEnabled(BATTERY_SAVER_ENABLED) 68 .setBrightnessFactor(BRIGHTNESS_FACTOR) 69 .build(); 70 when(mBatterySaverPolicy.getBatterySaverPolicy( 71 eq(BatterySaverPolicy.ServiceType.SCREEN_BRIGHTNESS), anyBoolean())) 72 .thenReturn(mPowerSaveState); 73 mDisplayPowerRequest = new DisplayPowerRequest(); 74 mService = new PowerManagerService(getContext(), mBatterySaverPolicy); 75 temporaryFolder.create(); 76 mTempReason = temporaryFolder.newFile(LAST_REBOOT_REASON); 77 } 78 79 @SmallTest 80 public void testUpdatePowerScreenPolicy_UpdateDisplayPowerRequest() { 81 mService.updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest); 82 assertThat(mDisplayPowerRequest.lowPowerMode).isEqualTo(BATTERY_SAVER_ENABLED); 83 assertThat(mDisplayPowerRequest.screenLowPowerBrightnessFactor) 84 .isWithin(PRECISION).of(BRIGHTNESS_FACTOR); 85 } 86 87 @SmallTest 88 public void testGetLastShutdownReasonInternal() { 89 try { 90 FileWriter writer = new FileWriter(mTempReason); 91 writer.append("thermal-shutdown\n"); 92 writer.close(); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 int reason = mService.getLastShutdownReasonInternal(mTempReason); 97 assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); 98 } 99 100 @SmallTest 101 public void testGetDesiredScreenPolicy_WithVR() throws Exception { 102 // Brighten up the screen 103 mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, 0); 104 assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( 105 DisplayPowerRequest.POLICY_BRIGHT); 106 107 // Move to VR 108 mService.setVrModeEnabled(true); 109 assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( 110 DisplayPowerRequest.POLICY_VR); 111 112 // Then take a nap 113 mService.setWakefulnessLocked(WAKEFULNESS_ASLEEP, 0); 114 assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( 115 DisplayPowerRequest.POLICY_OFF); 116 117 // Wake up to VR 118 mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, 0); 119 assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( 120 DisplayPowerRequest.POLICY_VR); 121 122 // And back to normal 123 mService.setVrModeEnabled(false); 124 assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( 125 DisplayPowerRequest.POLICY_BRIGHT); 126 127 } 128 } 129