1 /* 2 * Copyright (C) 2009 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 17 package android.os.cts; 18 19 import android.content.Context; 20 import android.os.PowerManager; 21 import android.os.PowerManager.WakeLock; 22 import android.os.SystemClock; 23 import android.test.AndroidTestCase; 24 25 public class PowerManager_WakeLockTest extends AndroidTestCase { 26 private static final String TAG = "PowerManager_WakeLockTest"; 27 28 /** 29 * Test points: 30 * 1 Makes sure the device is on at the level you asked when you created the wake lock 31 * 2 Release your claim to the CPU or screen being on 32 * 3 Sets whether this WakeLock is ref counted 33 */ 34 public void testPowerManagerWakeLock() throws InterruptedException { 35 PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); 36 WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG); 37 assertNotNull(wl.toString()); 38 39 wl.acquire(); 40 assertTrue(wl.isHeld()); 41 wl.release(); 42 assertFalse(wl.isHeld()); 43 44 // Try ref-counted acquire/release 45 wl.setReferenceCounted(true); 46 wl.acquire(); 47 assertTrue(wl.isHeld()); 48 wl.acquire(); 49 assertTrue(wl.isHeld()); 50 wl.release(); 51 assertTrue(wl.isHeld()); 52 wl.release(); 53 assertFalse(wl.isHeld()); 54 55 // Try non-ref-counted 56 wl.setReferenceCounted(false); 57 wl.acquire(); 58 assertTrue(wl.isHeld()); 59 wl.acquire(); 60 assertTrue(wl.isHeld()); 61 wl.release(); 62 assertFalse(wl.isHeld()); 63 64 // test acquire(long) 65 wl.acquire(PowerManagerTest.TIME); 66 assertTrue(wl.isHeld()); 67 Thread.sleep(PowerManagerTest.TIME + PowerManagerTest.MORE_TIME); 68 assertFalse(wl.isHeld()); 69 } 70 71 public void testWakeLockTimeout() throws Exception { 72 final PowerManager pm = getContext().getSystemService(PowerManager.class); 73 74 final WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); 75 lock.acquire(2000); 76 SystemClock.sleep(4000); 77 78 lock.release(); 79 } 80 } 81