1 /*
2  * Copyright (C) 2018 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.mockito.ArgumentMatchers.anyString;
20 import static org.mockito.Mockito.never;
21 import static org.mockito.Mockito.verify;
22 
23 import android.animation.Animator;
24 import android.testing.AndroidTestingRunner;
25 import android.testing.TestableLooper;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.systemui.SysuiTestCase;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 
37 @SmallTest
38 @TestableLooper.RunWithLooper
39 @RunWith(AndroidTestingRunner.class)
40 public class KeepAwakeAnimationListenerTest extends SysuiTestCase {
41     @Mock WakeLock mWakeLock;
42     KeepAwakeAnimationListener mKeepAwakeAnimationListener;
43 
44     @Before
setup()45     public void setup() {
46         allowTestableLooperAsMainThread();
47         MockitoAnnotations.initMocks(this);
48         KeepAwakeAnimationListener.sWakeLock = mWakeLock;
49         mKeepAwakeAnimationListener = new KeepAwakeAnimationListener(getContext());
50     }
51 
52     @Test
onAnimationStart_holdsWakeLock()53     public void onAnimationStart_holdsWakeLock() {
54         mKeepAwakeAnimationListener.onAnimationStart((Animator) null);
55         verify(mWakeLock).acquire(anyString());
56         verify(mWakeLock, never()).release(anyString());
57 
58         mKeepAwakeAnimationListener.onAnimationEnd((Animator) null);
59         verify(mWakeLock).release(anyString());
60     }
61 
62     @Test(expected = IllegalStateException.class)
initThrows_onNonMainThread()63     public void initThrows_onNonMainThread() {
64         disallowTestableLooperAsMainThread();
65 
66         // we are creating the KeepAwakeAnimationListener from the TestableLooper, not the main
67         // looper, so we expect an IllegalStateException:
68         new KeepAwakeAnimationListener(getContext());
69     }
70 }
71