blob: b60d5bf907666996d298a18646c7f5bf61248ed0 [file] [log] [blame]
jackqdyulei92681e82017-02-28 11:26:28 -08001/*
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
17package com.android.server.power;
18
Salvador Martineza6f7b252017-04-10 10:46:15 -070019import android.content.Context;
jackqdyulei92681e82017-02-28 11:26:28 -080020import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
Salvador Martineza6f7b252017-04-10 10:46:15 -070021import android.os.PowerManager;
jackqdyulei92681e82017-02-28 11:26:28 -080022import android.os.PowerSaveState;
Mark Salyzyne65c0c62017-08-15 07:53:47 -070023import android.os.SystemProperties;
jackqdyulei92681e82017-02-28 11:26:28 -080024import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
Salvador Martineza6f7b252017-04-10 10:46:15 -070026import android.text.TextUtils;
27import java.io.File;
28import java.io.FileWriter;
29import java.io.IOException;
30import java.io.OutputStreamWriter;
31import org.junit.Rule;
32import org.junit.rules.TemporaryFolder;
jackqdyulei92681e82017-02-28 11:26:28 -080033import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35
Santos Cordon21e9f2b2017-09-13 11:59:39 -070036import static android.os.PowerManagerInternal.WAKEFULNESS_ASLEEP;
37import static android.os.PowerManagerInternal.WAKEFULNESS_AWAKE;
38import static android.os.PowerManagerInternal.WAKEFULNESS_DOZING;
39import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING;
jackqdyulei92681e82017-02-28 11:26:28 -080040import static com.google.common.truth.Truth.assertThat;
41import static org.mockito.Matchers.anyBoolean;
42import static org.mockito.Matchers.eq;
43import static org.mockito.Mockito.when;
44
45/**
46 * Tests for {@link com.android.server.power.PowerManagerService}
47 */
48public 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 Salyzyne65c0c62017-08-15 07:53:47 -070052 private static final String TEST_LAST_REBOOT_PROPERTY = "test.sys.boot.reason";
jackqdyulei92681e82017-02-28 11:26:28 -080053
54 private @Mock BatterySaverPolicy mBatterySaverPolicy;
55 private PowerManagerService mService;
56 private PowerSaveState mPowerSaveState;
57 private DisplayPowerRequest mDisplayPowerRequest;
Salvador Martineza6f7b252017-04-10 10:46:15 -070058
59 @Rule
jackqdyulei92681e82017-02-28 11:26:28 -080060 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 Onuki2eccd022017-11-01 13:44:23 -070069 eq(PowerManager.ServiceType.SCREEN_BRIGHTNESS), anyBoolean()))
jackqdyulei92681e82017-02-28 11:26:28 -080070 .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 Martineza6f7b252017-04-10 10:46:15 -070082
83 @SmallTest
84 public void testGetLastShutdownReasonInternal() {
Mark Salyzyne65c0c62017-08-15 07:53:47 -070085 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 Martineza6f7b252017-04-10 10:46:15 -070088 assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN);
89 }
Santos Cordon21e9f2b2017-09-13 11:59:39 -070090
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 }
jackqdyulei92681e82017-02-28 11:26:28 -0800119}