1 package com.xtremelabs.robolectric.shadows; 2 3 import static junit.framework.Assert.assertFalse; 4 import static junit.framework.Assert.assertNotNull; 5 import static junit.framework.Assert.assertTrue; 6 7 import com.xtremelabs.robolectric.Robolectric; 8 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 13 import android.content.Context; 14 import android.os.PowerManager; 15 16 @RunWith(WithTestDefaultsRunner.class) 17 public class PowerManagerTest { 18 19 PowerManager powerManager; 20 ShadowPowerManager shadowPowerManager; 21 22 @Before before()23 public void before() { 24 powerManager = (PowerManager) Robolectric.application.getSystemService(Context.POWER_SERVICE); 25 shadowPowerManager = Robolectric.shadowOf(powerManager); 26 } 27 28 @Test testIsScreenOn()29 public void testIsScreenOn() { 30 assertTrue(powerManager.isScreenOn()); 31 shadowPowerManager.setIsScreenOn(false); 32 assertFalse(powerManager.isScreenOn()); 33 } 34 35 @Test shouldCreateWakeLock()36 public void shouldCreateWakeLock() throws Exception { 37 assertNotNull(powerManager.newWakeLock(0, "TAG")); 38 } 39 40 @Test shouldAcquireAndReleaseReferenceCountedLock()41 public void shouldAcquireAndReleaseReferenceCountedLock() throws Exception { 42 PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG"); 43 assertFalse(lock.isHeld()); 44 lock.acquire(); 45 assertTrue(lock.isHeld()); 46 lock.acquire(); 47 48 assertTrue(lock.isHeld()); 49 lock.release(); 50 51 assertTrue(lock.isHeld()); 52 lock.release(); 53 assertFalse(lock.isHeld()); 54 } 55 56 @Test shouldAcquireAndReleaseNonReferenceCountedLock()57 public void shouldAcquireAndReleaseNonReferenceCountedLock() throws Exception { 58 PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG"); 59 lock.setReferenceCounted(false); 60 61 assertFalse(lock.isHeld()); 62 lock.acquire(); 63 assertTrue(lock.isHeld()); 64 lock.acquire(); 65 assertTrue(lock.isHeld()); 66 67 lock.release(); 68 69 assertFalse(lock.isHeld()); 70 } 71 72 @Test(expected = RuntimeException.class) shouldThrowRuntimeExceptionIfLockisUnderlocked()73 public void shouldThrowRuntimeExceptionIfLockisUnderlocked() throws Exception { 74 PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG"); 75 lock.release(); 76 } 77 }