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; |
| 23 | import android.test.AndroidTestCase; |
| 24 | import android.test.suitebuilder.annotation.SmallTest; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame^] | 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; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 32 | import org.mockito.Mock; |
| 33 | import org.mockito.MockitoAnnotations; |
| 34 | |
| 35 | import static com.google.common.truth.Truth.assertThat; |
| 36 | import static org.mockito.Matchers.anyBoolean; |
| 37 | import static org.mockito.Matchers.eq; |
| 38 | import static org.mockito.Mockito.when; |
| 39 | |
| 40 | /** |
| 41 | * Tests for {@link com.android.server.power.PowerManagerService} |
| 42 | */ |
| 43 | public class PowerManagerServiceTest extends AndroidTestCase { |
| 44 | private static final float PRECISION = 0.001f; |
| 45 | private static final float BRIGHTNESS_FACTOR = 0.7f; |
| 46 | private static final boolean BATTERY_SAVER_ENABLED = true; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame^] | 47 | private static final String LAST_REBOOT_REASON = "last_reboot_reason"; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 48 | |
| 49 | private @Mock BatterySaverPolicy mBatterySaverPolicy; |
| 50 | private PowerManagerService mService; |
| 51 | private PowerSaveState mPowerSaveState; |
| 52 | private DisplayPowerRequest mDisplayPowerRequest; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame^] | 53 | private File mTempReason; |
| 54 | |
| 55 | @Rule |
| 56 | public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 57 | |
| 58 | public void setUp() throws Exception { |
| 59 | super.setUp(); |
| 60 | MockitoAnnotations.initMocks(this); |
| 61 | |
| 62 | mPowerSaveState = new PowerSaveState.Builder() |
| 63 | .setBatterySaverEnabled(BATTERY_SAVER_ENABLED) |
| 64 | .setBrightnessFactor(BRIGHTNESS_FACTOR) |
| 65 | .build(); |
| 66 | when(mBatterySaverPolicy.getBatterySaverPolicy( |
| 67 | eq(BatterySaverPolicy.ServiceType.SCREEN_BRIGHTNESS), anyBoolean())) |
| 68 | .thenReturn(mPowerSaveState); |
| 69 | mDisplayPowerRequest = new DisplayPowerRequest(); |
| 70 | mService = new PowerManagerService(getContext(), mBatterySaverPolicy); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame^] | 71 | temporaryFolder.create(); |
| 72 | mTempReason = temporaryFolder.newFile(LAST_REBOOT_REASON); |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 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() { |
| 85 | try { |
| 86 | FileWriter writer = new FileWriter(mTempReason); |
| 87 | writer.append("thermal-shutdown\n"); |
| 88 | writer.close(); |
| 89 | } catch (IOException e) { |
| 90 | e.printStackTrace(); |
| 91 | } |
| 92 | int reason = mService.getLastShutdownReasonInternal(mTempReason); |
| 93 | assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); |
| 94 | } |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 95 | } |