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.statusbar.notification.shelf.ui.viewmodel
18 
19 import android.os.PowerManager
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysUITestComponent
23 import com.android.systemui.SysUITestModule
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.TestMocksModule
26 import com.android.systemui.collectLastValue
27 import com.android.systemui.dagger.SysUISingleton
28 import com.android.systemui.keyguard.data.repository.FakeDeviceEntryFaceAuthRepository
29 import com.android.systemui.keyguard.data.repository.FakeKeyguardRepository
30 import com.android.systemui.power.data.repository.FakePowerRepository
31 import com.android.systemui.runTest
32 import com.android.systemui.statusbar.LockscreenShadeTransitionController
33 import com.android.systemui.statusbar.SysuiStatusBarStateController
34 import com.android.systemui.statusbar.notification.row.ui.viewmodel.ActivatableNotificationViewModelModule
35 import com.android.systemui.statusbar.phone.ScreenOffAnimationController
36 import com.android.systemui.util.mockito.eq
37 import com.android.systemui.util.mockito.mock
38 import com.android.systemui.util.mockito.whenever
39 import com.google.common.truth.Truth.assertThat
40 import dagger.BindsInstance
41 import dagger.Component
42 import org.junit.Test
43 import org.junit.runner.RunWith
44 import org.mockito.Mockito
45 import org.mockito.Mockito.verify
46 
47 @RunWith(AndroidJUnit4::class)
48 @SmallTest
49 class NotificationShelfViewModelTest : SysuiTestCase() {
50 
51     @Component(modules = [SysUITestModule::class, ActivatableNotificationViewModelModule::class])
52     @SysUISingleton
53     interface TestComponent : SysUITestComponent<NotificationShelfViewModel> {
54 
55         val deviceEntryFaceAuthRepository: FakeDeviceEntryFaceAuthRepository
56         val keyguardRepository: FakeKeyguardRepository
57         val powerRepository: FakePowerRepository
58 
59         @Component.Factory
60         interface Factory {
createnull61             fun create(
62                 @BindsInstance test: SysuiTestCase,
63                 mocks: TestMocksModule,
64             ): TestComponent
65         }
66     }
67 
68     private val keyguardTransitionController: LockscreenShadeTransitionController = mock()
69     private val screenOffAnimationController: ScreenOffAnimationController = mock {
70         whenever(allowWakeUpIfDozing()).thenReturn(true)
71     }
72     private val statusBarStateController: SysuiStatusBarStateController = mock()
73 
74     private val testComponent: TestComponent =
75         DaggerNotificationShelfViewModelTest_TestComponent.factory()
76             .create(
77                 test = this,
78                 mocks =
79                     TestMocksModule(
80                         lockscreenShadeTransitionController = keyguardTransitionController,
81                         screenOffAnimationController = screenOffAnimationController,
82                         statusBarStateController = statusBarStateController,
83                     )
84             )
85 
86     @Test
canModifyColorOfNotifications_whenKeyguardNotShowingnull87     fun canModifyColorOfNotifications_whenKeyguardNotShowing() =
88         testComponent.runTest {
89             val canModifyNotifColor by collectLastValue(underTest.canModifyColorOfNotifications)
90 
91             keyguardRepository.setKeyguardShowing(false)
92 
93             assertThat(canModifyNotifColor).isTrue()
94         }
95 
96     @Test
canModifyColorOfNotifications_whenKeyguardShowingAndNotBypassnull97     fun canModifyColorOfNotifications_whenKeyguardShowingAndNotBypass() =
98         testComponent.runTest {
99             val canModifyNotifColor by collectLastValue(underTest.canModifyColorOfNotifications)
100 
101             keyguardRepository.setKeyguardShowing(true)
102             deviceEntryFaceAuthRepository.isBypassEnabled.value = false
103 
104             assertThat(canModifyNotifColor).isTrue()
105         }
106 
107     @Test
cannotModifyColorOfNotifications_whenBypassnull108     fun cannotModifyColorOfNotifications_whenBypass() =
109         testComponent.runTest {
110             val canModifyNotifColor by collectLastValue(underTest.canModifyColorOfNotifications)
111 
112             keyguardRepository.setKeyguardShowing(true)
113             deviceEntryFaceAuthRepository.isBypassEnabled.value = true
114 
115             assertThat(canModifyNotifColor).isFalse()
116         }
117 
118     @Test
isClickable_whenKeyguardShowingnull119     fun isClickable_whenKeyguardShowing() =
120         testComponent.runTest {
121             val isClickable by collectLastValue(underTest.isClickable)
122 
123             keyguardRepository.setKeyguardShowing(true)
124 
125             assertThat(isClickable).isTrue()
126         }
127 
128     @Test
isNotClickable_whenKeyguardNotShowingnull129     fun isNotClickable_whenKeyguardNotShowing() =
130         testComponent.runTest {
131             val isClickable by collectLastValue(underTest.isClickable)
132 
133             keyguardRepository.setKeyguardShowing(false)
134 
135             assertThat(isClickable).isFalse()
136         }
137 
138     @Test
onClicked_goesToLockedShadenull139     fun onClicked_goesToLockedShade() =
140         with(testComponent) {
141             whenever(statusBarStateController.isDozing).thenReturn(true)
142 
143             underTest.onShelfClicked()
144 
145             assertThat(powerRepository.lastWakeReason).isNotNull()
146             assertThat(powerRepository.lastWakeReason).isEqualTo(PowerManager.WAKE_REASON_GESTURE)
147             verify(keyguardTransitionController).goToLockedShade(Mockito.isNull(), eq(true))
148         }
149 }
150