Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 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 | |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.anyString; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.never; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | |
| 23 | import android.animation.Animator; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 24 | import android.os.Looper; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper; |
| 27 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 28 | import androidx.test.filters.SmallTest; |
| 29 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 30 | import com.android.systemui.SysuiTestCase; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 31 | import com.android.systemui.util.Assert; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.mockito.Mock; |
| 37 | import org.mockito.MockitoAnnotations; |
| 38 | |
| 39 | @SmallTest |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 40 | @TestableLooper.RunWithLooper |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 41 | @RunWith(AndroidTestingRunner.class) |
| 42 | public class KeepAwakeAnimationListenerTest extends SysuiTestCase { |
| 43 | @Mock WakeLock mWakeLock; |
| 44 | KeepAwakeAnimationListener mKeepAwakeAnimationListener; |
| 45 | |
| 46 | @Before |
| 47 | public void setup() { |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 48 | Assert.sMainLooper = TestableLooper.get(this).getLooper(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 49 | MockitoAnnotations.initMocks(this); |
| 50 | KeepAwakeAnimationListener.sWakeLock = mWakeLock; |
| 51 | mKeepAwakeAnimationListener = new KeepAwakeAnimationListener(getContext()); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void onAnimationStart_holdsWakeLock() { |
| 56 | mKeepAwakeAnimationListener.onAnimationStart((Animator) null); |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 57 | verify(mWakeLock).acquire(anyString()); |
| 58 | verify(mWakeLock, never()).release(anyString()); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 59 | |
| 60 | mKeepAwakeAnimationListener.onAnimationEnd((Animator) null); |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 61 | verify(mWakeLock).release(anyString()); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 62 | } |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 63 | |
| 64 | @Test(expected = IllegalStateException.class) |
| 65 | public void initThrows_onNonMainThread() { |
| 66 | Assert.sMainLooper = Looper.getMainLooper(); |
| 67 | new KeepAwakeAnimationListener(getContext()); |
| 68 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 69 | } |