• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.keyguard.ui.viewmodel
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.RoboPilotTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.bouncer.domain.interactor.PrimaryBouncerInteractor
24 import com.android.systemui.coroutines.collectValues
25 import com.android.systemui.keyguard.data.repository.FakeKeyguardTransitionRepository
26 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractorFactory
27 import com.android.systemui.keyguard.shared.model.KeyguardState
28 import com.android.systemui.keyguard.shared.model.ScrimAlpha
29 import com.android.systemui.keyguard.shared.model.TransitionState
30 import com.android.systemui.keyguard.shared.model.TransitionStep
31 import com.android.systemui.statusbar.SysuiStatusBarStateController
32 import com.android.systemui.util.mockito.whenever
33 import com.google.common.collect.Range
34 import com.google.common.truth.Truth.assertThat
35 import kotlinx.coroutines.test.TestScope
36 import kotlinx.coroutines.test.UnconfinedTestDispatcher
37 import kotlinx.coroutines.test.runTest
38 import org.junit.Before
39 import org.junit.Test
40 import org.junit.runner.RunWith
41 import org.mockito.Mock
42 import org.mockito.MockitoAnnotations
43 
44 @SmallTest
45 @RoboPilotTest
46 @RunWith(AndroidJUnit4::class)
47 class PrimaryBouncerToGoneTransitionViewModelTest : SysuiTestCase() {
48     private lateinit var underTest: PrimaryBouncerToGoneTransitionViewModel
49     private lateinit var repository: FakeKeyguardTransitionRepository
50     @Mock private lateinit var statusBarStateController: SysuiStatusBarStateController
51     @Mock private lateinit var primaryBouncerInteractor: PrimaryBouncerInteractor
52 
53     @Before
setUpnull54     fun setUp() {
55         MockitoAnnotations.initMocks(this)
56         repository = FakeKeyguardTransitionRepository()
57         val interactor =
58             KeyguardTransitionInteractorFactory.create(
59                     scope = TestScope().backgroundScope,
60                     repository = repository,
61                 )
62                 .keyguardTransitionInteractor
63         underTest =
64             PrimaryBouncerToGoneTransitionViewModel(
65                 interactor,
66                 statusBarStateController,
67                 primaryBouncerInteractor
68             )
69 
70         whenever(primaryBouncerInteractor.willRunDismissFromKeyguard()).thenReturn(false)
71         whenever(statusBarStateController.leaveOpenOnKeyguardHide()).thenReturn(false)
72     }
73 
74     @Test
bouncerAlphanull75     fun bouncerAlpha() =
76         runTest(UnconfinedTestDispatcher()) {
77             val values by collectValues(underTest.bouncerAlpha)
78 
79             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
80             repository.sendTransitionStep(step(0.3f))
81             repository.sendTransitionStep(step(0.6f))
82 
83             assertThat(values.size).isEqualTo(3)
84             values.forEach { assertThat(it).isIn(Range.closed(0f, 1f)) }
85         }
86 
87     @Test
bouncerAlpha_runDimissFromKeyguardnull88     fun bouncerAlpha_runDimissFromKeyguard() =
89         runTest(UnconfinedTestDispatcher()) {
90             val values by collectValues(underTest.bouncerAlpha)
91 
92             whenever(primaryBouncerInteractor.willRunDismissFromKeyguard()).thenReturn(true)
93 
94             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
95             repository.sendTransitionStep(step(0.3f))
96             repository.sendTransitionStep(step(0.6f))
97 
98             assertThat(values.size).isEqualTo(3)
99             values.forEach { assertThat(it).isEqualTo(0f) }
100         }
101 
102     @Test
lockscreenAlphanull103     fun lockscreenAlpha() =
104         runTest(UnconfinedTestDispatcher()) {
105             val values by collectValues(underTest.lockscreenAlpha)
106 
107             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
108             repository.sendTransitionStep(step(1f))
109 
110             assertThat(values.size).isEqualTo(2)
111             values.forEach { assertThat(it).isEqualTo(0f) }
112         }
113 
114     @Test
lockscreenAlpha_runDimissFromKeyguardnull115     fun lockscreenAlpha_runDimissFromKeyguard() =
116         runTest(UnconfinedTestDispatcher()) {
117             val values by collectValues(underTest.lockscreenAlpha)
118 
119             whenever(primaryBouncerInteractor.willRunDismissFromKeyguard()).thenReturn(true)
120 
121             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
122             repository.sendTransitionStep(step(1f))
123 
124             assertThat(values.size).isEqualTo(2)
125             values.forEach { assertThat(it).isEqualTo(1f) }
126         }
127 
128     @Test
lockscreenAlpha_leaveShadeOpennull129     fun lockscreenAlpha_leaveShadeOpen() =
130         runTest(UnconfinedTestDispatcher()) {
131             val values by collectValues(underTest.lockscreenAlpha)
132 
133             whenever(statusBarStateController.leaveOpenOnKeyguardHide()).thenReturn(true)
134 
135             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
136             repository.sendTransitionStep(step(1f))
137 
138             assertThat(values.size).isEqualTo(2)
139             values.forEach { assertThat(it).isEqualTo(1f) }
140         }
141 
142     @Test
scrimAlpha_runDimissFromKeyguardnull143     fun scrimAlpha_runDimissFromKeyguard() =
144         runTest(UnconfinedTestDispatcher()) {
145             val values by collectValues(underTest.scrimAlpha)
146 
147             whenever(primaryBouncerInteractor.willRunDismissFromKeyguard()).thenReturn(true)
148 
149             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
150             repository.sendTransitionStep(step(0.3f))
151             repository.sendTransitionStep(step(0.6f))
152             repository.sendTransitionStep(step(1f))
153 
154             assertThat(values.size).isEqualTo(4)
155             values.forEach { assertThat(it).isEqualTo(ScrimAlpha()) }
156         }
157 
158     @Test
scrimBehindAlpha_leaveShadeOpennull159     fun scrimBehindAlpha_leaveShadeOpen() =
160         runTest(UnconfinedTestDispatcher()) {
161             val values by collectValues(underTest.scrimAlpha)
162 
163             whenever(statusBarStateController.leaveOpenOnKeyguardHide()).thenReturn(true)
164 
165             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
166             repository.sendTransitionStep(step(0.3f))
167             repository.sendTransitionStep(step(0.6f))
168             repository.sendTransitionStep(step(1f))
169 
170             assertThat(values.size).isEqualTo(4)
171             values.forEach {
172                 assertThat(it).isEqualTo(ScrimAlpha(notificationsAlpha = 1f, behindAlpha = 1f))
173             }
174         }
175 
176     @Test
scrimBehindAlpha_doNotLeaveShadeOpennull177     fun scrimBehindAlpha_doNotLeaveShadeOpen() =
178         runTest(UnconfinedTestDispatcher()) {
179             val values by collectValues(underTest.scrimAlpha)
180 
181             whenever(statusBarStateController.leaveOpenOnKeyguardHide()).thenReturn(false)
182 
183             repository.sendTransitionStep(step(0f, TransitionState.STARTED))
184             repository.sendTransitionStep(step(0.3f))
185             repository.sendTransitionStep(step(0.6f))
186             repository.sendTransitionStep(step(1f))
187 
188             assertThat(values.size).isEqualTo(4)
189             values.forEach { assertThat(it.notificationsAlpha).isEqualTo(0f) }
190             values.forEach { assertThat(it.frontAlpha).isEqualTo(0f) }
191             values.forEach { assertThat(it.behindAlpha).isIn(Range.closed(0f, 1f)) }
192             assertThat(values[3].behindAlpha).isEqualTo(0f)
193         }
194 
stepnull195     private fun step(
196         value: Float,
197         state: TransitionState = TransitionState.RUNNING
198     ): TransitionStep {
199         return TransitionStep(
200             from = KeyguardState.PRIMARY_BOUNCER,
201             to = KeyguardState.GONE,
202             value = value,
203             transitionState = state,
204             ownerName = "PrimaryBouncerToGoneTransitionViewModelTest"
205         )
206     }
207 }
208