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