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 com.android.systemui.bouncer.data.repository.FakeKeyguardBouncerRepository
21 import com.android.systemui.common.ui.data.repository.FakeConfigurationRepository
22 import com.android.systemui.common.ui.domain.interactor.ConfigurationInteractorImpl
23 import com.android.systemui.flags.FakeFeatureFlags
24 import com.android.systemui.keyguard.data.repository.FakeKeyguardRepository
25 import com.android.systemui.keyguard.shared.model.KeyguardState
26 import com.android.systemui.keyguard.shared.model.TransitionStep
27 import com.android.systemui.power.domain.interactor.PowerInteractor
28 import com.android.systemui.power.domain.interactor.PowerInteractorFactory
29 import com.android.systemui.scene.domain.interactor.SceneInteractor
30 import com.android.systemui.shade.data.repository.FakeShadeRepository
31 import com.android.systemui.util.mockito.mock
32 import com.android.systemui.util.mockito.whenever
33 import kotlinx.coroutines.CoroutineScope
34 import kotlinx.coroutines.flow.MutableSharedFlow
35 import kotlinx.coroutines.flow.MutableStateFlow
36 import kotlinx.coroutines.test.TestScope
37 import org.mockito.kotlin.any
38 
39 /**
40  * Simply put, I got tired of adding a constructor argument and then having to tweak dozens of
41  * files. This should alleviate some of the burden by providing defaults for testing.
42  */
43 object KeyguardInteractorFactory {
44 
45     @JvmOverloads
46     @JvmStatic
createnull47     fun create(
48         featureFlags: FakeFeatureFlags = FakeFeatureFlags(),
49         repository: FakeKeyguardRepository = FakeKeyguardRepository(),
50         bouncerRepository: FakeKeyguardBouncerRepository = FakeKeyguardBouncerRepository(),
51         configurationRepository: FakeConfigurationRepository = FakeConfigurationRepository(),
52         shadeRepository: FakeShadeRepository = FakeShadeRepository(),
53         sceneInteractor: SceneInteractor = mock(),
54         fromGoneTransitionInteractor: FromGoneTransitionInteractor = mock(),
55         fromLockscreenTransitionInteractor: FromLockscreenTransitionInteractor = mock(),
56         fromOccludedTransitionInteractor: FromOccludedTransitionInteractor = mock(),
57         powerInteractor: PowerInteractor = PowerInteractorFactory.create().powerInteractor,
58         testScope: CoroutineScope = TestScope(),
59     ): WithDependencies {
60         // Mock these until they are replaced by kosmos
61         val currentKeyguardStateFlow = MutableSharedFlow<KeyguardState>()
62         val transitionStateFlow = MutableStateFlow(TransitionStep())
63         val keyguardTransitionInteractor =
64             mock<KeyguardTransitionInteractor>().also {
65                 whenever(it.currentKeyguardState).thenReturn(currentKeyguardStateFlow)
66                 whenever(it.transitionState).thenReturn(transitionStateFlow)
67                 whenever(it.isFinishedIn(any(), any())).thenReturn(MutableStateFlow(false))
68             }
69         return WithDependencies(
70             repository = repository,
71             featureFlags = featureFlags,
72             bouncerRepository = bouncerRepository,
73             configurationRepository = configurationRepository,
74             shadeRepository = shadeRepository,
75             powerInteractor = powerInteractor,
76             KeyguardInteractor(
77                 repository = repository,
78                 powerInteractor = powerInteractor,
79                 bouncerRepository = bouncerRepository,
80                 configurationInteractor = ConfigurationInteractorImpl(configurationRepository),
81                 shadeRepository = shadeRepository,
82                 keyguardTransitionInteractor = keyguardTransitionInteractor,
83                 sceneInteractorProvider = { sceneInteractor },
84                 fromGoneTransitionInteractor = { fromGoneTransitionInteractor },
85                 fromLockscreenTransitionInteractor = { fromLockscreenTransitionInteractor },
86                 fromOccludedTransitionInteractor = { fromOccludedTransitionInteractor },
87                 applicationScope = testScope,
88             ),
89         )
90     }
91 
92     data class WithDependencies(
93         val repository: FakeKeyguardRepository,
94         val featureFlags: FakeFeatureFlags,
95         val bouncerRepository: FakeKeyguardBouncerRepository,
96         val configurationRepository: FakeConfigurationRepository,
97         val shadeRepository: FakeShadeRepository,
98         val powerInteractor: PowerInteractor,
99         val keyguardInteractor: KeyguardInteractor,
100     )
101 }
102