PowerManagerTest.java revision 1a44d5dcabc18cd5ef111f732ccff91683a1a093
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 17package android.os; 18 19import android.content.Context; 20import android.os.PowerManager; 21import android.test.AndroidTestCase; 22import android.test.suitebuilder.annotation.MediumTest; 23 24public class PowerManagerTest extends AndroidTestCase { 25 26 private PowerManager mPm; 27 28 /** 29 * Setup any common data for the upcoming tests. 30 */ 31 @Override 32 public void setUp() throws Exception { 33 super.setUp(); 34 mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); 35 } 36 37 /** 38 * Confirm that the setup is good. 39 * 40 * @throws Exception 41 */ 42 @MediumTest 43 public void testPreconditions() throws Exception { 44 assertNotNull(mPm); 45 } 46 47 /** 48 * Confirm that we can create functional wakelocks. 49 * 50 * @throws Exception 51 */ 52 @MediumTest 53 public void testNewWakeLock() throws Exception { 54 PowerManager.WakeLock wl = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FULL_WAKE_LOCK"); 55 doTestWakeLock(wl); 56 57 wl = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SCREEN_BRIGHT_WAKE_LOCK"); 58 doTestWakeLock(wl); 59 60 wl = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK"); 61 doTestWakeLock(wl); 62 63 wl = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK"); 64 doTestWakeLock(wl); 65 66 doTestSetBacklightBrightness(); 67 68 // TODO: Some sort of functional test (maybe not in the unit test here?) 69 // that confirms that things are really happening e.g. screen power, keyboard power. 70} 71 72 /** 73 * Confirm that we can't create dysfunctional wakelocks. 74 * 75 * @throws Exception 76 */ 77 @MediumTest 78 public void testBadNewWakeLock() throws Exception { 79 80 final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK 81 | PowerManager.SCREEN_DIM_WAKE_LOCK; 82 // wrap in try because we want the error here 83 try { 84 PowerManager.WakeLock wl = mPm.newWakeLock(badFlags, "foo"); 85 } catch (IllegalArgumentException e) { 86 return; 87 } 88 fail("Bad WakeLock flag was not caught."); 89 } 90 91 /** 92 * Apply a few tests to a wakelock to make sure it's healthy. 93 * 94 * @param wl The wakelock to be tested. 95 */ 96 private void doTestWakeLock(PowerManager.WakeLock wl) { 97 // First try simple acquire/release 98 wl.acquire(); 99 assertTrue(wl.isHeld()); 100 wl.release(); 101 assertFalse(wl.isHeld()); 102 103 // Try ref-counted acquire/release 104 wl.setReferenceCounted(true); 105 wl.acquire(); 106 assertTrue(wl.isHeld()); 107 wl.acquire(); 108 assertTrue(wl.isHeld()); 109 wl.release(); 110 assertTrue(wl.isHeld()); 111 wl.release(); 112 assertFalse(wl.isHeld()); 113 114 // Try non-ref-counted 115 wl.setReferenceCounted(false); 116 wl.acquire(); 117 assertTrue(wl.isHeld()); 118 wl.acquire(); 119 assertTrue(wl.isHeld()); 120 wl.release(); 121 assertFalse(wl.isHeld()); 122 123 // TODO: Threaded test (needs handler) to make sure timed wakelocks work too 124 } 125 126 127 /** 128 * Test that calling {@link android.os.IHardwareService#setBacklights(int)} requires 129 * permissions. 130 * <p>Tests permission: 131 * {@link android.Manifest.permission#DEVICE_POWER} 132 */ 133 private void doTestSetBacklightBrightness() { 134 try { 135 mPm.setBacklightBrightness(0); 136 fail("setBacklights did not throw SecurityException as expected"); 137 } catch (SecurityException e) { 138 // expected 139 } 140 } 141 142} 143