The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Neal Nguyen | 1a44d5d | 2010-01-13 10:42:43 -0800 | [diff] [blame] | 17 | package android.os; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.reset; |
| 20 | import static org.mockito.Mockito.timeout; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 24 | import android.support.test.uiautomator.UiDevice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.test.AndroidTestCase; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 26 | |
| 27 | import androidx.test.InstrumentationRegistry; |
| 28 | import androidx.test.filters.SmallTest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 30 | import org.junit.After; |
| 31 | import org.junit.Test; |
| 32 | import org.mockito.Mock; |
| 33 | import org.mockito.MockitoAnnotations; |
| 34 | |
| 35 | import java.util.concurrent.Executor; |
| 36 | import java.util.concurrent.Executors; |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | public class PowerManagerTest extends AndroidTestCase { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | private PowerManager mPm; |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 41 | private UiDevice mUiDevice; |
| 42 | private Executor mExec = Executors.newSingleThreadExecutor(); |
| 43 | @Mock |
| 44 | private PowerManager.ThermalStatusCallback mCallback; |
| 45 | private static final long CALLBACK_TIMEOUT_MILLI_SEC = 5000; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Setup any common data for the upcoming tests. |
| 49 | */ |
| 50 | @Override |
| 51 | public void setUp() throws Exception { |
| 52 | super.setUp(); |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 53 | MockitoAnnotations.initMocks(this); |
| 54 | mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 56 | mUiDevice.executeShellCommand("cmd thermalservice override-status 0"); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Reset data for the upcoming tests. |
| 61 | */ |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | mUiDevice.executeShellCommand("cmd thermalservice reset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Confirm that the setup is good. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 69 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | * @throws Exception |
| 71 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 72 | @SmallTest |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | public void testPreconditions() throws Exception { |
| 74 | assertNotNull(mPm); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Confirm that we can create functional wakelocks. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 79 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | * @throws Exception |
| 81 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 82 | @SmallTest |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | public void testNewWakeLock() throws Exception { |
| 84 | PowerManager.WakeLock wl = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FULL_WAKE_LOCK"); |
| 85 | doTestWakeLock(wl); |
| 86 | |
| 87 | wl = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SCREEN_BRIGHT_WAKE_LOCK"); |
| 88 | doTestWakeLock(wl); |
| 89 | |
| 90 | wl = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK"); |
| 91 | doTestWakeLock(wl); |
| 92 | |
| 93 | wl = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK"); |
| 94 | doTestWakeLock(wl); |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 95 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 96 | // TODO: Some sort of functional test (maybe not in the unit test here?) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | // that confirms that things are really happening e.g. screen power, keyboard power. |
Artem Iglikov | c474bfd0 | 2018-09-25 11:56:09 -0700 | [diff] [blame] | 98 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Confirm that we can't create dysfunctional wakelocks. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 102 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | * @throws Exception |
| 104 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 105 | @SmallTest |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | public void testBadNewWakeLock() throws Exception { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 107 | final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | | PowerManager.SCREEN_DIM_WAKE_LOCK; |
| 109 | // wrap in try because we want the error here |
| 110 | try { |
| 111 | PowerManager.WakeLock wl = mPm.newWakeLock(badFlags, "foo"); |
| 112 | } catch (IllegalArgumentException e) { |
| 113 | return; |
| 114 | } |
| 115 | fail("Bad WakeLock flag was not caught."); |
| 116 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | /** |
Artem Iglikov | c474bfd0 | 2018-09-25 11:56:09 -0700 | [diff] [blame] | 119 | * Ensure that we can have work sources with work chains when uid is not set directly on work |
| 120 | * source, and that this doesn't crash system server. |
| 121 | * |
| 122 | * @throws Exception |
| 123 | */ |
| 124 | @SmallTest |
| 125 | public void testWakeLockWithWorkChains() throws Exception { |
| 126 | PowerManager.WakeLock wakeLock = mPm.newWakeLock( |
| 127 | PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, |
| 128 | "TEST_LOCK"); |
| 129 | WorkSource workSource = new WorkSource(); |
| 130 | WorkSource.WorkChain workChain = workSource.createWorkChain(); |
| 131 | workChain.addNode(1000, "test"); |
| 132 | wakeLock.setWorkSource(workSource); |
| 133 | |
| 134 | doTestWakeLock(wakeLock); |
| 135 | } |
| 136 | |
| 137 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | * Apply a few tests to a wakelock to make sure it's healthy. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 139 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | * @param wl The wakelock to be tested. |
| 141 | */ |
| 142 | private void doTestWakeLock(PowerManager.WakeLock wl) { |
| 143 | // First try simple acquire/release |
| 144 | wl.acquire(); |
| 145 | assertTrue(wl.isHeld()); |
| 146 | wl.release(); |
| 147 | assertFalse(wl.isHeld()); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | // Try ref-counted acquire/release |
| 150 | wl.setReferenceCounted(true); |
| 151 | wl.acquire(); |
| 152 | assertTrue(wl.isHeld()); |
| 153 | wl.acquire(); |
| 154 | assertTrue(wl.isHeld()); |
| 155 | wl.release(); |
| 156 | assertTrue(wl.isHeld()); |
| 157 | wl.release(); |
| 158 | assertFalse(wl.isHeld()); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | // Try non-ref-counted |
| 161 | wl.setReferenceCounted(false); |
| 162 | wl.acquire(); |
| 163 | assertTrue(wl.isHeld()); |
| 164 | wl.acquire(); |
| 165 | assertTrue(wl.isHeld()); |
| 166 | wl.release(); |
| 167 | assertFalse(wl.isHeld()); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | // TODO: Threaded test (needs handler) to make sure timed wakelocks work too |
| 170 | } |
Wei Wang | 2d644ab | 2018-12-10 22:48:52 -0800 | [diff] [blame] | 171 | |
| 172 | @Test |
| 173 | public void testGetThermalStatus() throws Exception { |
| 174 | int status = 0; |
| 175 | assertEquals(status, mPm.getCurrentThermalStatus()); |
| 176 | status = 3; |
| 177 | mUiDevice.executeShellCommand("cmd thermalservice override-status " |
| 178 | + Integer.toString(status)); |
| 179 | assertEquals(status, mPm.getCurrentThermalStatus()); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testThermalStatusCallback() throws Exception { |
| 184 | mPm.registerThermalStatusCallback(mCallback, mExec); |
| 185 | verify(mCallback, timeout(CALLBACK_TIMEOUT_MILLI_SEC) |
| 186 | .times(1)).onStatusChange(0); |
| 187 | reset(mCallback); |
| 188 | int status = 3; |
| 189 | mUiDevice.executeShellCommand("cmd thermalservice override-status " |
| 190 | + Integer.toString(status)); |
| 191 | verify(mCallback, timeout(CALLBACK_TIMEOUT_MILLI_SEC) |
| 192 | .times(1)).onStatusChange(status); |
| 193 | reset(mCallback); |
| 194 | mPm.unregisterThermalStatusCallback(mCallback); |
| 195 | status = 2; |
| 196 | mUiDevice.executeShellCommand("cmd thermalservice override-status " |
| 197 | + Integer.toString(status)); |
| 198 | verify(mCallback, timeout(CALLBACK_TIMEOUT_MILLI_SEC) |
| 199 | .times(0)).onStatusChange(status); |
| 200 | |
| 201 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |