1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19import android.content.Context;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22
23public class PowerManagerTest extends AndroidTestCase {
24
25    private PowerManager mPm;
26
27    /**
28     * Setup any common data for the upcoming tests.
29     */
30    @Override
31    public void setUp() throws Exception {
32        super.setUp();
33        mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
34    }
35
36    /**
37     * Confirm that the setup is good.
38     *
39     * @throws Exception
40     */
41    @SmallTest
42    public void testPreconditions() throws Exception {
43        assertNotNull(mPm);
44    }
45
46    /**
47     * Confirm that we can create functional wakelocks.
48     *
49     * @throws Exception
50     */
51    @SmallTest
52    public void testNewWakeLock() throws Exception {
53        PowerManager.WakeLock wl = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FULL_WAKE_LOCK");
54        doTestWakeLock(wl);
55
56        wl = mPm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "SCREEN_BRIGHT_WAKE_LOCK");
57        doTestWakeLock(wl);
58
59        wl = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SCREEN_DIM_WAKE_LOCK");
60        doTestWakeLock(wl);
61
62        wl = mPm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
63        doTestWakeLock(wl);
64
65        doTestSetBacklightBrightness();
66
67        // TODO: Some sort of functional test (maybe not in the unit test here?)
68        // that confirms that things are really happening e.g. screen power, keyboard power.
69}
70
71    /**
72     * Confirm that we can't create dysfunctional wakelocks.
73     *
74     * @throws Exception
75     */
76    @SmallTest
77    public void testBadNewWakeLock() throws Exception {
78
79        final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK
80                            | PowerManager.SCREEN_DIM_WAKE_LOCK;
81        // wrap in try because we want the error here
82        try {
83            PowerManager.WakeLock wl = mPm.newWakeLock(badFlags, "foo");
84        } catch (IllegalArgumentException e) {
85            return;
86        }
87        fail("Bad WakeLock flag was not caught.");
88    }
89
90    /**
91     * Apply a few tests to a wakelock to make sure it's healthy.
92     *
93     * @param wl The wakelock to be tested.
94     */
95    private void doTestWakeLock(PowerManager.WakeLock wl) {
96        // First try simple acquire/release
97        wl.acquire();
98        assertTrue(wl.isHeld());
99        wl.release();
100        assertFalse(wl.isHeld());
101
102        // Try ref-counted acquire/release
103        wl.setReferenceCounted(true);
104        wl.acquire();
105        assertTrue(wl.isHeld());
106        wl.acquire();
107        assertTrue(wl.isHeld());
108        wl.release();
109        assertTrue(wl.isHeld());
110        wl.release();
111        assertFalse(wl.isHeld());
112
113        // Try non-ref-counted
114        wl.setReferenceCounted(false);
115        wl.acquire();
116        assertTrue(wl.isHeld());
117        wl.acquire();
118        assertTrue(wl.isHeld());
119        wl.release();
120        assertFalse(wl.isHeld());
121
122        // TODO: Threaded test (needs handler) to make sure timed wakelocks work too
123    }
124
125
126    /**
127     * Test that calling {@link android.os.IHardwareService#setBacklights(int)} requires
128     * permissions.
129     * <p>Tests permission:
130     *   {@link android.Manifest.permission#DEVICE_POWER}
131     */
132    private void doTestSetBacklightBrightness() {
133        try {
134            mPm.setBacklightBrightness(0);
135            fail("setBacklights did not throw SecurityException as expected");
136        } catch (SecurityException e) {
137            // expected
138        }
139    }
140
141}
142