Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.media |
| 18 | |
| 19 | import android.graphics.Rect |
| 20 | import android.testing.AndroidTestingRunner |
| 21 | import android.testing.TestableLooper |
| 22 | import android.view.ViewGroup |
| 23 | import androidx.test.filters.SmallTest |
| 24 | import com.android.systemui.SysuiTestCase |
| 25 | import com.android.systemui.controls.controller.ControlsControllerImplTest.Companion.eq |
| 26 | import com.android.systemui.keyguard.WakefulnessLifecycle |
| 27 | import com.android.systemui.statusbar.NotificationLockscreenUserManager |
| 28 | import com.android.systemui.statusbar.StatusBarState |
| 29 | import com.android.systemui.statusbar.SysuiStatusBarStateController |
| 30 | import com.android.systemui.statusbar.phone.KeyguardBypassController |
| 31 | import com.android.systemui.statusbar.policy.KeyguardStateController |
| 32 | import com.android.systemui.util.animation.UniqueObjectHostView |
| 33 | import org.junit.Assert.assertNotNull |
| 34 | import org.junit.Before |
| 35 | import org.junit.Rule |
| 36 | import org.junit.Test |
| 37 | import org.junit.runner.RunWith |
| 38 | import org.mockito.ArgumentCaptor |
| 39 | import org.mockito.ArgumentMatchers |
| 40 | import org.mockito.Captor |
| 41 | import org.mockito.Mock |
| 42 | import org.mockito.Mockito.`when` |
| 43 | import org.mockito.Mockito.any |
| 44 | import org.mockito.Mockito.anyBoolean |
| 45 | import org.mockito.Mockito.anyLong |
| 46 | import org.mockito.Mockito.clearInvocations |
| 47 | import org.mockito.Mockito.times |
| 48 | import org.mockito.Mockito.verify |
| 49 | import org.mockito.junit.MockitoJUnit |
| 50 | |
| 51 | @SmallTest |
| 52 | @RunWith(AndroidTestingRunner::class) |
| 53 | @TestableLooper.RunWithLooper |
| 54 | class MediaHierarchyManagerTest : SysuiTestCase() { |
| 55 | |
| 56 | @Mock |
| 57 | private lateinit var lockHost: MediaHost |
| 58 | @Mock |
| 59 | private lateinit var qsHost: MediaHost |
| 60 | @Mock |
| 61 | private lateinit var qqsHost: MediaHost |
| 62 | @Mock |
| 63 | private lateinit var bypassController: KeyguardBypassController |
| 64 | @Mock |
| 65 | private lateinit var mediaFrame: ViewGroup |
| 66 | @Mock |
| 67 | private lateinit var keyguardStateController: KeyguardStateController |
| 68 | @Mock |
| 69 | private lateinit var statusBarStateController: SysuiStatusBarStateController |
| 70 | @Mock |
| 71 | private lateinit var notificationLockscreenUserManager: NotificationLockscreenUserManager |
| 72 | @Mock |
| 73 | private lateinit var mediaViewManager: MediaViewManager |
| 74 | @Mock |
| 75 | private lateinit var wakefulnessLifecycle: WakefulnessLifecycle |
| 76 | @Captor |
| 77 | private lateinit var wakefullnessObserver: ArgumentCaptor<(WakefulnessLifecycle.Observer)> |
| 78 | @JvmField |
| 79 | @Rule |
| 80 | val mockito = MockitoJUnit.rule() |
| 81 | private lateinit var mediaHiearchyManager: MediaHierarchyManager |
| 82 | |
| 83 | @Before |
| 84 | fun setup() { |
| 85 | `when`(mediaViewManager.mediaFrame).thenReturn(mediaFrame) |
| 86 | mediaHiearchyManager = MediaHierarchyManager( |
| 87 | context, |
| 88 | statusBarStateController, |
| 89 | keyguardStateController, |
| 90 | bypassController, |
| 91 | mediaViewManager, |
| 92 | notificationLockscreenUserManager, |
| 93 | wakefulnessLifecycle) |
| 94 | verify(wakefulnessLifecycle).addObserver(wakefullnessObserver.capture()) |
| 95 | setupHost(lockHost, MediaHierarchyManager.LOCATION_LOCKSCREEN) |
| 96 | setupHost(qsHost, MediaHierarchyManager.LOCATION_QS) |
| 97 | setupHost(qqsHost, MediaHierarchyManager.LOCATION_QQS) |
| 98 | `when`(statusBarStateController.state).thenReturn(StatusBarState.SHADE) |
| 99 | // We'll use the viewmanager to verify a few calls below, let's reset this. |
| 100 | clearInvocations(mediaViewManager) |
| 101 | |
| 102 | } |
| 103 | |
| 104 | private fun setupHost(host: MediaHost, location: Int) { |
| 105 | `when`(host.location).thenReturn(location) |
| 106 | `when`(host.currentBounds).thenReturn(Rect()) |
| 107 | `when`(host.hostView).thenReturn(UniqueObjectHostView(context)) |
| 108 | mediaHiearchyManager.register(host) |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | fun testHostViewSetOnRegister() { |
| 113 | val host = mediaHiearchyManager.register(lockHost) |
| 114 | verify(lockHost).hostView = eq(host) |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | fun testBlockedWhenScreenTurningOff() { |
| 119 | // Let's set it onto QS: |
| 120 | mediaHiearchyManager.qsExpansion = 1.0f |
| 121 | verify(mediaViewManager).onDesiredLocationChanged(ArgumentMatchers.anyInt(), |
| 122 | any(MediaHostState::class.java), anyBoolean(), anyLong(), anyLong()) |
| 123 | val observer = wakefullnessObserver.value |
| 124 | assertNotNull("lifecycle observer wasn't registered", observer) |
| 125 | observer.onStartedGoingToSleep() |
| 126 | clearInvocations(mediaViewManager) |
| 127 | mediaHiearchyManager.qsExpansion = 0.0f |
| 128 | verify(mediaViewManager, times(0)).onDesiredLocationChanged(ArgumentMatchers.anyInt(), |
| 129 | any(MediaHostState::class.java), anyBoolean(), anyLong(), anyLong()) |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | fun testAllowedWhenNotTurningOff() { |
| 134 | // Let's set it onto QS: |
| 135 | mediaHiearchyManager.qsExpansion = 1.0f |
| 136 | verify(mediaViewManager).onDesiredLocationChanged(ArgumentMatchers.anyInt(), |
| 137 | any(MediaHostState::class.java), anyBoolean(), anyLong(), anyLong()) |
| 138 | val observer = wakefullnessObserver.value |
| 139 | assertNotNull("lifecycle observer wasn't registered", observer) |
| 140 | clearInvocations(mediaViewManager) |
| 141 | mediaHiearchyManager.qsExpansion = 0.0f |
| 142 | verify(mediaViewManager).onDesiredLocationChanged(ArgumentMatchers.anyInt(), |
| 143 | any(MediaHostState::class.java), anyBoolean(), anyLong(), anyLong()) |
| 144 | } |
| 145 | } |