Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.systemui.util.wakelock; |
| 18 | |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 22 | import android.os.PowerManager; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 23 | import android.support.test.filters.SmallTest; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 26 | import com.android.systemui.SysuiTestCase; |
| 27 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | |
| 33 | @SmallTest |
| 34 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 35 | public class WakeLockTest extends SysuiTestCase { |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 36 | |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 37 | private static final String WHY = "test"; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 38 | WakeLock mWakeLock; |
| 39 | PowerManager.WakeLock mInner; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() { |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 43 | mInner = WakeLock.createPartialInner(mContext, WakeLockTest.class.getName()); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 44 | mWakeLock = WakeLock.wrap(mInner); |
| 45 | } |
| 46 | |
| 47 | @After |
| 48 | public void tearDown() { |
| 49 | mInner.setReferenceCounted(false); |
| 50 | mInner.release(); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void createPartialInner_notHeldYet() { |
| 55 | assertFalse(mInner.isHeld()); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void wakeLock_acquire() { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 60 | mWakeLock.acquire(WHY); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 61 | assertTrue(mInner.isHeld()); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void wakeLock_release() { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 66 | mWakeLock.acquire(WHY); |
| 67 | mWakeLock.release(WHY); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 68 | assertFalse(mInner.isHeld()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void wakeLock_refCounted() { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 73 | mWakeLock.acquire(WHY); |
| 74 | mWakeLock.acquire(WHY); |
| 75 | mWakeLock.release(WHY); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 76 | assertTrue(mInner.isHeld()); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void wakeLock_wrap() { |
| 81 | boolean[] ran = new boolean[1]; |
| 82 | |
| 83 | Runnable wrapped = mWakeLock.wrap(() -> { |
| 84 | ran[0] = true; |
| 85 | }); |
| 86 | |
| 87 | assertTrue(mInner.isHeld()); |
| 88 | assertFalse(ran[0]); |
| 89 | |
| 90 | wrapped.run(); |
| 91 | |
| 92 | assertTrue(ran[0]); |
| 93 | assertFalse(mInner.isHeld()); |
| 94 | } |
| 95 | } |