package org.robolectric.shadows;

import android.content.Context;
import android.os.PowerManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.TestRunners;

import static junit.framework.Assert.*;
import static org.fest.assertions.api.Assertions.assertThat;

@RunWith(TestRunners.WithDefaults.class)
public class PowerManagerTest {

  PowerManager powerManager;
  ShadowPowerManager shadowPowerManager;

  @Before
  public void before() {
    powerManager = (PowerManager) Robolectric.application.getSystemService(Context.POWER_SERVICE);
    shadowPowerManager = Robolectric.shadowOf(powerManager);
  }

  @Test
  public void testIsScreenOn() {
    assertTrue(powerManager.isScreenOn());
    shadowPowerManager.setIsScreenOn(false);
    assertFalse(powerManager.isScreenOn());
  }

  @Test
  public void shouldCreateWakeLock() throws Exception {
    assertNotNull(powerManager.newWakeLock(0, "TAG"));
  }

  @Test
  public void shouldAcquireAndReleaseReferenceCountedLock() throws Exception {
    PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
    assertFalse(lock.isHeld());
    lock.acquire();
    assertTrue(lock.isHeld());
    lock.acquire();

    assertTrue(lock.isHeld());
    lock.release();

    assertTrue(lock.isHeld());
    lock.release();
    assertFalse(lock.isHeld());
  }

  @Test
  public void shouldAcquireAndReleaseNonReferenceCountedLock() throws Exception {
    PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
    lock.setReferenceCounted(false);

    assertFalse(lock.isHeld());
    lock.acquire();
    assertTrue(lock.isHeld());
    lock.acquire();
    assertTrue(lock.isHeld());

    lock.release();

    assertFalse(lock.isHeld());
  }

  @Test(expected = RuntimeException.class)
  public void shouldThrowRuntimeExceptionIfLockisUnderlocked() throws Exception {
    PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
    lock.release();
  }

  @Test
  public void shouldLogLatestWakeLock() throws Exception {
    ShadowPowerManager.reset();
    assertThat(shadowPowerManager.getLatestWakeLock()).isNull();

    PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
    lock.acquire();

    assertThat(shadowPowerManager.getLatestWakeLock()).isNotNull();
    assertThat(shadowPowerManager.getLatestWakeLock()).isSameAs(lock);
    assertThat(lock.isHeld()).isTrue();

    lock.release();

    assertThat(shadowPowerManager.getLatestWakeLock()).isNotNull();
    assertThat(shadowPowerManager.getLatestWakeLock()).isSameAs(lock);
    assertThat(lock.isHeld()).isFalse();

    ShadowPowerManager.reset();
    assertThat(shadowPowerManager.getLatestWakeLock()).isNull();
  }
}
@@ -91,16 +91,17 @@
 anager;%0A
+%0A
 import o
@@ -287,9 +287,120 @@
 ert.
-*
+assertFalse;%0Aimport static junit.framework.Assert.assertNotNull;%0Aimport static junit.framework.Assert.assertTrue
 ;%0Aim
@@ -457,16 +457,68 @@
 ertThat;
+%0Aimport static org.robolectric.Robolectric.shadowOf;
 %0A%0A@RunWi
@@ -790,16 +790,16 @@
 RVICE);%0A
+
     shad
@@ -815,28 +815,16 @@
 nager = 
-Robolectric.
 shadowOf
@@ -2893,9 +2893,468 @@
 ();%0A  %7D%0A
-%7D
+%0A  @Test%0A  public void shouldGetReferenceCounted() throws Exception %7B%0A    PowerManager.WakeLock lock = powerManager.newWakeLock(0, %22TAG%22);%0A    ShadowPowerManager.ShadowWakeLock shadowLock = shadowOf(lock);%0A    assertThat(shadowLock.isReferenceCounted()).isTrue();%0A    lock.setReferenceCounted(false);%0A    assertThat(shadowLock.isReferenceCounted()).isFalse();%0A    lock.setReferenceCounted(true);%0A    assertThat(shadowLock.isReferenceCounted()).isTrue();%0A  %7D%0A%7D%0A

Add test for ShadowWakeLock