blob: 0d250b825d97cbe8fd29c5de8fd199587229675c [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
19import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.test.AndroidTestCase;
Brett Chabotf76c56b2010-07-26 17:28:17 -070021import android.test.suitebuilder.annotation.SmallTest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23public class PowerManagerTest extends AndroidTestCase {
Michael Wrightd8460232018-01-16 18:04:59 +000024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025 private PowerManager mPm;
Michael Wrightd8460232018-01-16 18:04:59 +000026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 /**
28 * Setup any common data for the upcoming tests.
29 */
30 @Override
31 public void setUp() throws Exception {
32 super.setUp();
33 mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
34 }
Michael Wrightd8460232018-01-16 18:04:59 +000035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 /**
37 * Confirm that the setup is good.
Michael Wrightd8460232018-01-16 18:04:59 +000038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * @throws Exception
40 */
Brett Chabotf76c56b2010-07-26 17:28:17 -070041 @SmallTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 public void testPreconditions() throws Exception {
43 assertNotNull(mPm);
44 }
45
46 /**
47 * Confirm that we can create functional wakelocks.
Michael Wrightd8460232018-01-16 18:04:59 +000048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * @throws Exception
50 */
Brett Chabotf76c56b2010-07-26 17:28:17 -070051 @SmallTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 public void testNewWakeLock() throws Exception {
53 PowerManager.WakeLock wl = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FULL_WAKE_LOCK");
54 doTestWakeLock(wl);
55
56 wl = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SCREEN_BRIGHT_WAKE_LOCK");
57 doTestWakeLock(wl);
58
59 wl = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK");
60 doTestWakeLock(wl);
61
62 wl = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
63 doTestWakeLock(wl);
Mike Lockwood237a2992009-09-15 14:42:16 -040064
Michael Wrightd8460232018-01-16 18:04:59 +000065 // TODO: Some sort of functional test (maybe not in the unit test here?)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 // that confirms that things are really happening e.g. screen power, keyboard power.
67}
Michael Wrightd8460232018-01-16 18:04:59 +000068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * Confirm that we can't create dysfunctional wakelocks.
Michael Wrightd8460232018-01-16 18:04:59 +000071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * @throws Exception
73 */
Brett Chabotf76c56b2010-07-26 17:28:17 -070074 @SmallTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 public void testBadNewWakeLock() throws Exception {
Michael Wrightd8460232018-01-16 18:04:59 +000076 final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 | PowerManager.SCREEN_DIM_WAKE_LOCK;
78 // wrap in try because we want the error here
79 try {
80 PowerManager.WakeLock wl = mPm.newWakeLock(badFlags, "foo");
81 } catch (IllegalArgumentException e) {
82 return;
83 }
84 fail("Bad WakeLock flag was not caught.");
85 }
Michael Wrightd8460232018-01-16 18:04:59 +000086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /**
88 * Apply a few tests to a wakelock to make sure it's healthy.
Michael Wrightd8460232018-01-16 18:04:59 +000089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * @param wl The wakelock to be tested.
91 */
92 private void doTestWakeLock(PowerManager.WakeLock wl) {
93 // First try simple acquire/release
94 wl.acquire();
95 assertTrue(wl.isHeld());
96 wl.release();
97 assertFalse(wl.isHeld());
Michael Wrightd8460232018-01-16 18:04:59 +000098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 // Try ref-counted acquire/release
100 wl.setReferenceCounted(true);
101 wl.acquire();
102 assertTrue(wl.isHeld());
103 wl.acquire();
104 assertTrue(wl.isHeld());
105 wl.release();
106 assertTrue(wl.isHeld());
107 wl.release();
108 assertFalse(wl.isHeld());
Michael Wrightd8460232018-01-16 18:04:59 +0000109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 // Try non-ref-counted
111 wl.setReferenceCounted(false);
112 wl.acquire();
113 assertTrue(wl.isHeld());
114 wl.acquire();
115 assertTrue(wl.isHeld());
116 wl.release();
117 assertFalse(wl.isHeld());
Michael Wrightd8460232018-01-16 18:04:59 +0000118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 // TODO: Threaded test (needs handler) to make sure timed wakelocks work too
120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121}