Skip to content

Commit

Permalink
Merge pull request #742 from SuperJugy/ShadowWakeLock
Browse files Browse the repository at this point in the history
Add "is "accessor to referenceCounted in ShadowWakeLock
  • Loading branch information
JakeWharton committed Oct 7, 2013
2 parents b7bce29 + 820be0c commit a10a02f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/robolectric/Robolectric.java
Expand Up @@ -791,6 +791,10 @@ public static ShadowPowerManager shadowOf(PowerManager instance) {
return (ShadowPowerManager) shadowOf_(instance);
}

public static ShadowPowerManager.ShadowWakeLock shadowOf(PowerManager.WakeLock instance) {
return (ShadowPowerManager.ShadowWakeLock) shadowOf_(instance);
}

public static ShadowPreference shadowOf(Preference instance) {
return (ShadowPreference) shadowOf_(instance);
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/robolectric/shadows/ShadowPowerManager.java
Expand Up @@ -45,13 +45,13 @@ public static void reset() {
*
* @return
*/
public PowerManager.WakeLock getLatestWakeLock() {
public static PowerManager.WakeLock getLatestWakeLock() {
return Robolectric.getShadowApplication().getLatestWakeLock();
}

@Implements(PowerManager.WakeLock.class)
public static class ShadowWakeLock {
private boolean refCounted = true;
private boolean refCounted = true;
private int refCount;
private boolean locked;

Expand Down Expand Up @@ -84,6 +84,15 @@ public synchronized boolean isHeld() {
return refCounted ? refCount > 0 : locked;
}

/**
* Non-Android accessor retrieves if the wake lock is reference counted or not
*
* @return
*/
public boolean isReferenceCounted() {
return refCounted;
}

@Implementation
public void setReferenceCounted(boolean value) {
refCounted = value;
Expand Down
21 changes: 18 additions & 3 deletions src/test/java/org/robolectric/shadows/PowerManagerTest.java
Expand Up @@ -2,14 +2,18 @@

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 junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.robolectric.Robolectric.shadowOf;

@RunWith(TestRunners.WithDefaults.class)
public class PowerManagerTest {
Expand All @@ -20,7 +24,7 @@ public class PowerManagerTest {
@Before
public void before() {
powerManager = (PowerManager) Robolectric.application.getSystemService(Context.POWER_SERVICE);
shadowPowerManager = Robolectric.shadowOf(powerManager);
shadowPowerManager = shadowOf(powerManager);
}

@Test
Expand Down Expand Up @@ -94,4 +98,15 @@ public void shouldLogLatestWakeLock() throws Exception {
ShadowPowerManager.reset();
assertThat(shadowPowerManager.getLatestWakeLock()).isNull();
}
}

@Test
public void shouldGetReferenceCounted() throws Exception {
PowerManager.WakeLock lock = powerManager.newWakeLock(0, "TAG");
ShadowPowerManager.ShadowWakeLock shadowLock = shadowOf(lock);
assertThat(shadowLock.isReferenceCounted()).isTrue();
lock.setReferenceCounted(false);
assertThat(shadowLock.isReferenceCounted()).isFalse();
lock.setReferenceCounted(true);
assertThat(shadowLock.isReferenceCounted()).isTrue();
}
}

0 comments on commit a10a02f

Please sign in to comment.