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 
18 package com.android.systemui.keyguard.domain.interactor
19 
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.common.ui.data.repository.FakeConfigurationRepository
24 import com.android.systemui.coroutines.collectLastValue
25 import com.android.systemui.doze.util.BurnInHelperWrapper
26 import com.android.systemui.keyguard.data.repository.FakeKeyguardRepository
27 import com.android.systemui.util.mockito.whenever
28 import com.google.common.truth.Truth.assertThat
29 import kotlinx.coroutines.ExperimentalCoroutinesApi
30 import kotlinx.coroutines.test.TestScope
31 import kotlinx.coroutines.test.runTest
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.ArgumentMatchers.anyBoolean
36 import org.mockito.ArgumentMatchers.anyInt
37 import org.mockito.Mock
38 import org.mockito.MockitoAnnotations
39 
40 @ExperimentalCoroutinesApi
41 @SmallTest
42 @RunWith(AndroidJUnit4::class)
43 class BurnInInteractorTest : SysuiTestCase() {
44     private val burnInOffset = 7
45     private var burnInProgress = 0f
46 
47     @Mock private lateinit var burnInHelperWrapper: BurnInHelperWrapper
48 
49     private lateinit var configurationRepository: FakeConfigurationRepository
50     private lateinit var keyguardInteractor: KeyguardInteractor
51     private lateinit var fakeKeyguardRepository: FakeKeyguardRepository
52     private lateinit var testScope: TestScope
53     private lateinit var underTest: BurnInInteractor
54 
55     @Before
56     fun setUp() {
57         MockitoAnnotations.initMocks(this)
58         configurationRepository = FakeConfigurationRepository()
59         KeyguardInteractorFactory.create().let {
60             keyguardInteractor = it.keyguardInteractor
61             fakeKeyguardRepository = it.repository
62         }
63         whenever(burnInHelperWrapper.burnInOffset(anyInt(), anyBoolean())).thenReturn(burnInOffset)
64         setBurnInProgress(.65f)
65 
66         testScope = TestScope()
67         underTest =
68             BurnInInteractor(
69                 context,
70                 burnInHelperWrapper,
71                 testScope.backgroundScope,
72                 configurationRepository,
73                 keyguardInteractor,
74             )
75     }
76 
77     @Test
78     fun udfpsBurnInOffset_updatesOnResolutionScaleChange() =
79         testScope.runTest {
80             val udfpsBurnInOffsetX by collectLastValue(underTest.udfpsBurnInXOffset)
81             val udfpsBurnInOffsetY by collectLastValue(underTest.udfpsBurnInYOffset)
82             assertThat(udfpsBurnInOffsetX).isEqualTo(burnInOffset)
83             assertThat(udfpsBurnInOffsetY).isEqualTo(burnInOffset)
84 
85             configurationRepository.setScaleForResolution(3f)
86             assertThat(udfpsBurnInOffsetX).isEqualTo(burnInOffset * 3)
87             assertThat(udfpsBurnInOffsetY).isEqualTo(burnInOffset * 3)
88 
89             configurationRepository.setScaleForResolution(.5f)
90             assertThat(udfpsBurnInOffsetX).isEqualTo(burnInOffset / 2)
91             assertThat(udfpsBurnInOffsetY).isEqualTo(burnInOffset / 2)
92         }
93 
94     @Test
95     fun udfpsBurnInProgress_updatesOnDozeTimeTick() =
96         testScope.runTest {
97             val udfpsBurnInProgress by collectLastValue(underTest.udfpsBurnInProgress)
98             assertThat(udfpsBurnInProgress).isEqualTo(burnInProgress)
99 
100             setBurnInProgress(.88f)
101             fakeKeyguardRepository.dozeTimeTick(10)
102             assertThat(udfpsBurnInProgress).isEqualTo(burnInProgress)
103 
104             setBurnInProgress(.92f)
105             fakeKeyguardRepository.dozeTimeTick(20)
106             assertThat(udfpsBurnInProgress).isEqualTo(burnInProgress)
107 
108             setBurnInProgress(.32f)
109             fakeKeyguardRepository.dozeTimeTick(30)
110             assertThat(udfpsBurnInProgress).isEqualTo(burnInProgress)
111         }
112 
113     private fun setBurnInProgress(progress: Float) {
114         burnInProgress = progress
115         whenever(burnInHelperWrapper.burnInProgressOffset()).thenReturn(burnInProgress)
116     }
117 }
118