blob: 1b587dd92db032bdda6a1042b37264fa3ad61777 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Nguyen1a44d5d2010-01-13 10:42:43 -080017package android.os;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Wei Wang2d644ab2018-12-10 22:48:52 -080019import static org.mockito.Mockito.reset;
20import static org.mockito.Mockito.timeout;
21import static org.mockito.Mockito.verify;
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Wei Wang2d644ab2018-12-10 22:48:52 -080024import android.support.test.uiautomator.UiDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.test.AndroidTestCase;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090026
27import androidx.test.InstrumentationRegistry;
28import androidx.test.filters.SmallTest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Wei Wang2d644ab2018-12-10 22:48:52 -080030import org.junit.After;
31import org.junit.Test;
32import org.mockito.Mock;
33import org.mockito.MockitoAnnotations;
34
35import java.util.concurrent.Executor;
36import java.util.concurrent.Executors;
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038public class PowerManagerTest extends AndroidTestCase {
Michael Wrightd8460232018-01-16 18:04:59 +000039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 private PowerManager mPm;
Wei Wang2d644ab2018-12-10 22:48:52 -080041 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 Wrightd8460232018-01-16 18:04:59 +000046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 /**
48 * Setup any common data for the upcoming tests.
49 */
50 @Override
51 public void setUp() throws Exception {
52 super.setUp();
Wei Wang2d644ab2018-12-10 22:48:52 -080053 MockitoAnnotations.initMocks(this);
54 mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Wei Wang2d644ab2018-12-10 22:48:52 -080056 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 Project9066cfe2009-03-03 19:31:44 -080065 }
Michael Wrightd8460232018-01-16 18:04:59 +000066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 /**
68 * Confirm that the setup is good.
Michael Wrightd8460232018-01-16 18:04:59 +000069 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * @throws Exception
71 */
Brett Chabotf76c56b2010-07-26 17:28:17 -070072 @SmallTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 public void testPreconditions() throws Exception {
74 assertNotNull(mPm);
75 }
76
77 /**
78 * Confirm that we can create functional wakelocks.
Michael Wrightd8460232018-01-16 18:04:59 +000079 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 * @throws Exception
81 */
Brett Chabotf76c56b2010-07-26 17:28:17 -070082 @SmallTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 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 Lockwood237a2992009-09-15 14:42:16 -040095
Michael Wrightd8460232018-01-16 18:04:59 +000096 // TODO: Some sort of functional test (maybe not in the unit test here?)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 // that confirms that things are really happening e.g. screen power, keyboard power.
Artem Iglikovc474bfd02018-09-25 11:56:09 -070098 }
Michael Wrightd8460232018-01-16 18:04:59 +000099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
101 * Confirm that we can't create dysfunctional wakelocks.
Michael Wrightd8460232018-01-16 18:04:59 +0000102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * @throws Exception
104 */
Brett Chabotf76c56b2010-07-26 17:28:17 -0700105 @SmallTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public void testBadNewWakeLock() throws Exception {
Michael Wrightd8460232018-01-16 18:04:59 +0000107 final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 | 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 Wrightd8460232018-01-16 18:04:59 +0000117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Artem Iglikovc474bfd02018-09-25 11:56:09 -0700119 * 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 Project9066cfe2009-03-03 19:31:44 -0800138 * Apply a few tests to a wakelock to make sure it's healthy.
Michael Wrightd8460232018-01-16 18:04:59 +0000139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * @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 Wrightd8460232018-01-16 18:04:59 +0000148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 // 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 Wrightd8460232018-01-16 18:04:59 +0000159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 // 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 Wrightd8460232018-01-16 18:04:59 +0000168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 // TODO: Threaded test (needs handler) to make sure timed wakelocks work too
170 }
Wei Wang2d644ab2018-12-10 22:48:52 -0800171
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 Project9066cfe2009-03-03 19:31:44 -0800202}