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 junit.framework.TestCase.assertTrue;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.systemui.SysuiTestCase;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public class SettableWakeLockTest extends SysuiTestCase {
36 
37     private WakeLockFake mFake;
38     private SettableWakeLock mSettable;
39 
40     @Before
setup()41     public void setup() {
42         mFake = new WakeLockFake();
43         mSettable = new SettableWakeLock(mFake, "Fake");
44     }
45 
46     @Test
setAcquire_true_acquires()47     public void setAcquire_true_acquires() throws Exception {
48         mSettable.setAcquired(true);
49         assertTrue(mFake.isHeld());
50         assertEquals(mFake.isHeld(), mSettable.isAcquired());
51     }
52 
53     @Test
setAcquire_false_releases()54     public void setAcquire_false_releases() throws Exception {
55         mSettable.setAcquired(true);
56         mSettable.setAcquired(false);
57         assertFalse(mFake.isHeld());
58         assertEquals(mFake.isHeld(), mSettable.isAcquired());
59     }
60 
61     @Test
setAcquire_true_multipleTimes_isIdempotent()62     public void setAcquire_true_multipleTimes_isIdempotent() throws Exception {
63         mSettable.setAcquired(true);
64         mSettable.setAcquired(true);
65         mSettable.setAcquired(true);
66         mSettable.setAcquired(false);
67         assertFalse(mFake.isHeld());
68         assertEquals(mFake.isHeld(), mSettable.isAcquired());
69     }
70 
71     @Test
setAcquire_false_multipleTimes_idempotent()72     public void setAcquire_false_multipleTimes_idempotent() throws Exception {
73         mSettable.setAcquired(true);
74         mSettable.setAcquired(false);
75         mSettable.setAcquired(false);
76         assertFalse(mFake.isHeld());
77         assertEquals(mFake.isHeld(), mSettable.isAcquired());
78     }
79 
80     @Test
setAcquire_false_multipleTimes_idempotent_again()81     public void setAcquire_false_multipleTimes_idempotent_again() throws Exception {
82         mSettable.setAcquired(true);
83         mSettable.setAcquired(false);
84         mSettable.setAcquired(false);
85         mSettable.setAcquired(true);
86         assertTrue(mFake.isHeld());
87         assertEquals(mFake.isHeld(), mSettable.isAcquired());
88     }
89 
90 
91 }