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.SystemClock; 22 import android.os.PowerManager.WakeLock; 23 import android.test.AndroidTestCase; 24 25 public class PowerManagerTest extends AndroidTestCase { 26 private static final String TAG = "PowerManagerTest"; 27 public static final long TIME = 3000; 28 public static final int MORE_TIME = 300; 29 30 /** 31 * test points: 32 * 1 Get a wake lock at the level of the flags parameter 33 * 2 Force the device to go to sleep 34 * 3 User activity happened 35 */ testPowerManager()36 public void testPowerManager() throws InterruptedException { 37 PowerManager pm = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE); 38 39 WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG); 40 wl.acquire(TIME); 41 assertTrue(wl.isHeld()); 42 Thread.sleep(TIME + MORE_TIME); 43 assertFalse(wl.isHeld()); 44 45 try { 46 pm.goToSleep(SystemClock.uptimeMillis()); 47 fail("goToSleep should throw SecurityException"); 48 } catch (SecurityException e) { 49 // expected 50 } 51 52 try { 53 pm.wakeUp(SystemClock.uptimeMillis()); 54 fail("wakeUp should throw SecurityException"); 55 } catch (SecurityException e) { 56 // expected 57 } 58 59 try { 60 pm.nap(SystemClock.uptimeMillis()); 61 fail("nap should throw SecurityException"); 62 } catch (SecurityException e) { 63 // expected 64 } 65 66 try { 67 pm.reboot("Testing"); 68 fail("reboot should throw SecurityException"); 69 } catch (SecurityException e) { 70 // expected 71 } 72 73 // This method requires DEVICE_POWER but does not throw a SecurityException 74 // for historical reasons. So this call should be a no-op. 75 pm.userActivity(SystemClock.uptimeMillis(), false); 76 } 77 } 78