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        // TODO: Some sort of functional test (maybe not in the unit test here?)
66        // that confirms that things are really happening e.g. screen power, keyboard power.
67}
68
69    /**
70     * Confirm that we can't create dysfunctional wakelocks.
71     *
72     * @throws Exception
73     */
74    @SmallTest
75    public void testBadNewWakeLock() throws Exception {
76        final int badFlags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK
77                            | PowerManager.SCREEN_DIM_WAKE_LOCK;
78        // wrap in try because we want the error here
79        try {
80            PowerManager.WakeLock wl = mPm.newWakeLock(badFlags, "foo");
81        } catch (IllegalArgumentException e) {
82            return;
83        }
84        fail("Bad WakeLock flag was not caught.");
85    }
86
87    /**
88     * Apply a few tests to a wakelock to make sure it's healthy.
89     *
90     * @param wl The wakelock to be tested.
91     */
92    private void doTestWakeLock(PowerManager.WakeLock wl) {
93        // First try simple acquire/release
94        wl.acquire();
95        assertTrue(wl.isHeld());
96        wl.release();
97        assertFalse(wl.isHeld());
98
99        // Try ref-counted acquire/release
100        wl.setReferenceCounted(true);
101        wl.acquire();
102        assertTrue(wl.isHeld());
103        wl.acquire();
104        assertTrue(wl.isHeld());
105        wl.release();
106        assertTrue(wl.isHeld());
107        wl.release();
108        assertFalse(wl.isHeld());
109
110        // Try non-ref-counted
111        wl.setReferenceCounted(false);
112        wl.acquire();
113        assertTrue(wl.isHeld());
114        wl.acquire();
115        assertTrue(wl.isHeld());
116        wl.release();
117        assertFalse(wl.isHeld());
118
119        // TODO: Threaded test (needs handler) to make sure timed wakelocks work too
120    }
121}
122