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.shade.data.repository
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.shade.ShadeExpansionChangeEvent
24 import com.android.systemui.shade.ShadeExpansionStateManager
25 import com.android.systemui.shade.domain.model.ShadeModel
26 import com.android.systemui.util.mockito.any
27 import com.android.systemui.util.mockito.withArgCaptor
28 import com.google.common.truth.Truth.assertThat
29 import kotlinx.coroutines.ExperimentalCoroutinesApi
30 import kotlinx.coroutines.flow.launchIn
31 import kotlinx.coroutines.flow.onEach
32 import kotlinx.coroutines.test.StandardTestDispatcher
33 import kotlinx.coroutines.test.TestScope
34 import kotlinx.coroutines.test.runCurrent
35 import kotlinx.coroutines.test.runTest
36 import org.junit.Before
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.mockito.Mock
40 import org.mockito.Mockito.verify
41 import org.mockito.Mockito.`when`
42 import org.mockito.MockitoAnnotations
43 
44 @OptIn(ExperimentalCoroutinesApi::class)
45 @SmallTest
46 @RoboPilotTest
47 @RunWith(AndroidJUnit4::class)
48 class ShadeRepositoryImplTest : SysuiTestCase() {
49 
50     @Mock private lateinit var shadeExpansionStateManager: ShadeExpansionStateManager
51     private val testDispatcher = StandardTestDispatcher()
52     private val testScope = TestScope(testDispatcher)
53 
54     private lateinit var underTest: ShadeRepositoryImpl
55 
56     @Before
57     fun setUp() {
58         MockitoAnnotations.initMocks(this)
59 
60         underTest = ShadeRepositoryImpl(shadeExpansionStateManager)
61         `when`(shadeExpansionStateManager.addExpansionListener(any())).thenReturn(
62             ShadeExpansionChangeEvent(0f, false, false, 0f)
63         )
64     }
65 
66     @Test
67     fun shadeExpansionChangeEvent() =
68         testScope.runTest {
69             var latest: ShadeModel? = null
70             val job = underTest.shadeModel.onEach { latest = it }.launchIn(this)
71             runCurrent()
72             assertThat(latest?.expansionAmount).isEqualTo(0f)
73             assertThat(latest?.isExpanded).isEqualTo(false)
74             assertThat(latest?.isUserDragging).isEqualTo(false)
75 
76             val captor = withArgCaptor {
77                 verify(shadeExpansionStateManager).addExpansionListener(capture())
78             }
79 
80             captor.onPanelExpansionChanged(
81                 ShadeExpansionChangeEvent(
82                     fraction = 1f,
83                     expanded = true,
84                     tracking = false,
85                     dragDownPxAmount = 0f,
86                 )
87             )
88             runCurrent()
89             assertThat(latest?.expansionAmount).isEqualTo(1f)
90             assertThat(latest?.isExpanded).isEqualTo(true)
91             assertThat(latest?.isUserDragging).isEqualTo(false)
92 
93             captor.onPanelExpansionChanged(
94                 ShadeExpansionChangeEvent(
95                     fraction = .67f,
96                     expanded = false,
97                     tracking = true,
98                     dragDownPxAmount = 0f,
99                 )
100             )
101             runCurrent()
102             assertThat(latest?.expansionAmount).isEqualTo(.67f)
103             assertThat(latest?.isExpanded).isEqualTo(false)
104             assertThat(latest?.isUserDragging).isEqualTo(true)
105 
106             job.cancel()
107         }
108 
109     @Test
110     fun updateQsExpansion() =
111         testScope.runTest {
112             assertThat(underTest.qsExpansion.value).isEqualTo(0f)
113 
114             underTest.setQsExpansion(.5f)
115             assertThat(underTest.qsExpansion.value).isEqualTo(.5f)
116 
117             underTest.setQsExpansion(.82f)
118             assertThat(underTest.qsExpansion.value).isEqualTo(.82f)
119 
120             underTest.setQsExpansion(1f)
121             assertThat(underTest.qsExpansion.value).isEqualTo(1f)
122         }
123 
124     @Test
125     fun updateUdfpsTransitionToFullShadeProgress() =
126         testScope.runTest {
127             assertThat(underTest.udfpsTransitionToFullShadeProgress.value).isEqualTo(0f)
128 
129             underTest.setUdfpsTransitionToFullShadeProgress(.5f)
130             assertThat(underTest.udfpsTransitionToFullShadeProgress.value).isEqualTo(.5f)
131 
132             underTest.setUdfpsTransitionToFullShadeProgress(.82f)
133             assertThat(underTest.udfpsTransitionToFullShadeProgress.value).isEqualTo(.82f)
134 
135             underTest.setUdfpsTransitionToFullShadeProgress(1f)
136             assertThat(underTest.udfpsTransitionToFullShadeProgress.value).isEqualTo(1f)
137         }
138 }
139