FileDocCategorySizeDatePackage
PowerManagerTest.javaAPI DocAndroid 5.1 API4275Thu Mar 12 22:22:12 GMT 2015android.os

PowerManagerTest

public class PowerManagerTest extends android.test.AndroidTestCase

Fields Summary
private PowerManager
mPm
Constructors Summary
Methods Summary
private voiddoTestSetBacklightBrightness()
Test that calling {@link android.os.IHardwareService#setBacklights(int)} requires permissions.

Tests permission: {@link android.Manifest.permission#DEVICE_POWER}

        try {
            mPm.setBacklightBrightness(0);
            fail("setBacklights did not throw SecurityException as expected");
        } catch (SecurityException e) {
            // expected
        }
    
private voiddoTestWakeLock(PowerManager.WakeLock wl)
Apply a few tests to a wakelock to make sure it's healthy.

param
wl The wakelock to be tested.

        // First try simple acquire/release
        wl.acquire();
        assertTrue(wl.isHeld());
        wl.release();
        assertFalse(wl.isHeld());
        
        // Try ref-counted acquire/release
        wl.setReferenceCounted(true);
        wl.acquire();
        assertTrue(wl.isHeld());
        wl.acquire();
        assertTrue(wl.isHeld());
        wl.release();
        assertTrue(wl.isHeld());
        wl.release();
        assertFalse(wl.isHeld());
        
        // Try non-ref-counted
        wl.setReferenceCounted(false);
        wl.acquire();
        assertTrue(wl.isHeld());
        wl.acquire();
        assertTrue(wl.isHeld());
        wl.release();
        assertFalse(wl.isHeld());
        
        // TODO: Threaded test (needs handler) to make sure timed wakelocks work too
    
public voidsetUp()
Setup any common data for the upcoming tests.

        super.setUp();
        mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
    
public voidtestBadNewWakeLock()
Confirm that we can't create dysfunctional wakelocks.

throws
Exception

        
        final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK 
                            | PowerManager.SCREEN_DIM_WAKE_LOCK;
        // wrap in try because we want the error here
        try {
            PowerManager.WakeLock wl = mPm.newWakeLock(badFlags, "foo");
        } catch (IllegalArgumentException e) {
            return;
        }
        fail("Bad WakeLock flag was not caught.");
    
public voidtestNewWakeLock()
Confirm that we can create functional wakelocks.

throws
Exception

        PowerManager.WakeLock wl = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FULL_WAKE_LOCK");
        doTestWakeLock(wl);

        wl = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SCREEN_BRIGHT_WAKE_LOCK");
        doTestWakeLock(wl);

        wl = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK");
        doTestWakeLock(wl);

        wl = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
        doTestWakeLock(wl);
        
        doTestSetBacklightBrightness();

        // TODO: Some sort of functional test (maybe not in the unit test here?) 
        // that confirms that things are really happening e.g. screen power, keyboard power.
public voidtestPreconditions()
Confirm that the setup is good.

throws
Exception

        assertNotNull(mPm);