1 /*
2  *  Copyright (C) 2022 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.data.quickaffordance
19 
20 import com.android.systemui.RoboPilotTest
21 import com.android.systemui.animation.Expandable
22 import com.android.systemui.keyguard.data.quickaffordance.KeyguardQuickAffordanceConfig.OnTriggeredResult
23 import kotlinx.coroutines.flow.Flow
24 import kotlinx.coroutines.flow.MutableStateFlow
25 import kotlinx.coroutines.yield
26 
27 /** Fake implementation of a quick affordance data source. */
28 @RoboPilotTest
29 class FakeKeyguardQuickAffordanceConfig(
30     override val key: String,
31     private val pickerName: String = key,
32     override val pickerIconResourceId: Int = 0,
33 ) : KeyguardQuickAffordanceConfig {
34 
35     var onTriggeredResult: OnTriggeredResult = OnTriggeredResult.Handled
36 
37     private val _lockScreenState =
38         MutableStateFlow<KeyguardQuickAffordanceConfig.LockScreenState>(
39             KeyguardQuickAffordanceConfig.LockScreenState.Hidden
40         )
41     override val lockScreenState: Flow<KeyguardQuickAffordanceConfig.LockScreenState> =
42         _lockScreenState
43 
44     override fun pickerName(): String = pickerName
45 
46     override fun onTriggered(
47         expandable: Expandable?,
48     ): OnTriggeredResult {
49         return onTriggeredResult
50     }
51 
52     suspend fun setState(lockScreenState: KeyguardQuickAffordanceConfig.LockScreenState) {
53         _lockScreenState.value = lockScreenState
54         // Yield to allow the test's collection coroutine to "catch up" and collect this value
55         // before the test continues to the next line.
56         // TODO(b/239834928): once coroutines.test is updated, switch to the approach described in
57         // https://developer.android.com/kotlin/flow/test#continuous-collection and remove this.
58         yield()
59     }
60 }
61