blob: 14b1ce1662c7c5d655eceeb84d08e6b3a40b6257 [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
36import static com.google.common.truth.Truth.assertThat;
37import static org.mockito.Matchers.anyBoolean;
38import static org.mockito.Matchers.eq;
39import static org.mockito.Mockito.when;
40
41/**
42 * Tests for {@link com.android.server.power.PowerManagerService}
43 */
44public 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 Salyzyne65c0c62017-08-15 07:53:47 -070048 private static final String TEST_LAST_REBOOT_PROPERTY = "test.sys.boot.reason";
jackqdyulei92681e82017-02-28 11:26:28 -080049
50 private @Mock BatterySaverPolicy mBatterySaverPolicy;
51 private PowerManagerService mService;
52 private PowerSaveState mPowerSaveState;
53 private DisplayPowerRequest mDisplayPowerRequest;
Salvador Martineza6f7b252017-04-10 10:46:15 -070054
55 @Rule
jackqdyulei92681e82017-02-28 11:26:28 -080056 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(
65 eq(BatterySaverPolicy.ServiceType.SCREEN_BRIGHTNESS), anyBoolean()))
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 Martineza6f7b252017-04-10 10:46:15 -070078
79 @SmallTest
80 public void testGetLastShutdownReasonInternal() {
Mark Salyzyne65c0c62017-08-15 07:53:47 -070081 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 Martineza6f7b252017-04-10 10:46:15 -070084 assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN);
85 }
jackqdyulei92681e82017-02-28 11:26:28 -080086}