1package com.xtremelabs.robolectric.shadows;
2
3import static junit.framework.Assert.assertFalse;
4import static junit.framework.Assert.assertNotNull;
5import static junit.framework.Assert.assertTrue;
6
7import com.xtremelabs.robolectric.Robolectric;
8import com.xtremelabs.robolectric.WithTestDefaultsRunner;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import android.content.Context;
14import android.os.PowerManager;
15
16@RunWith(WithTestDefaultsRunner.class)
17public class PowerManagerTest {
18
19    PowerManager powerManager;
20    ShadowPowerManager shadowPowerManager;
21
22    @Before
23    public void before() {
24        powerManager = (PowerManager) Robolectric.application.getSystemService(Context.POWER_SERVICE);
25        shadowPowerManager = Robolectric.shadowOf(powerManager);
26    }
27
28    @Test
29    public void testIsScreenOn() {
30        assertTrue(powerManager.isScreenOn());
31        shadowPowerManager.setIsScreenOn(false);
32        assertFalse(powerManager.isScreenOn());
33    }
34
35    @Test
36    public void shouldCreateWakeLock() throws Exception {
37        assertNotNull(powerManager.newWakeLock(0, "TAG"));
38    }
39
40    @Test
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
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)
73    public void shouldThrowRuntimeExceptionIfLockisUnderlocked() throws Exception {
74        PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
75        lock.release();
76    }
77}