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