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 | |
| 19 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.test.AndroidTestCase; |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 21 | import android.test.suitebuilder.annotation.SmallTest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | public class PowerManagerTest extends AndroidTestCase { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | private PowerManager mPm; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | /** |
| 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 Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Confirm that the setup is good. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 38 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * @throws Exception |
| 40 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 41 | @SmallTest |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | public void testPreconditions() throws Exception { |
| 43 | assertNotNull(mPm); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Confirm that we can create functional wakelocks. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 48 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * @throws Exception |
| 50 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 51 | @SmallTest |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | 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 Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 64 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 65 | // 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] | 66 | // that confirms that things are really happening e.g. screen power, keyboard power. |
| 67 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Confirm that we can't create dysfunctional wakelocks. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 71 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | * @throws Exception |
| 73 | */ |
Brett Chabot | f76c56b | 2010-07-26 17:28:17 -0700 | [diff] [blame] | 74 | @SmallTest |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | public void testBadNewWakeLock() throws Exception { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 76 | final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | | 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 Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
| 88 | * 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] | 89 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * @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 Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | // 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 Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | // 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 Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | // TODO: Threaded test (needs handler) to make sure timed wakelocks work too |
| 120 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |