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.keyboard.data.repository
19 
20 import com.android.systemui.keyboard.data.model.Keyboard
21 import com.android.systemui.keyboard.shared.model.BacklightModel
22 import kotlinx.coroutines.channels.Channel
23 import kotlinx.coroutines.flow.Flow
24 import kotlinx.coroutines.flow.MutableStateFlow
25 import kotlinx.coroutines.flow.consumeAsFlow
26 import kotlinx.coroutines.flow.filterNotNull
27 
28 class FakeKeyboardRepository : KeyboardRepository {
29 
30     private val _isAnyKeyboardConnected = MutableStateFlow(false)
31     override val isAnyKeyboardConnected: Flow<Boolean> = _isAnyKeyboardConnected
32 
33     private val _backlightState: MutableStateFlow<BacklightModel?> = MutableStateFlow(null)
34     // filtering to make sure backlight doesn't have default initial value
35     override val backlight: Flow<BacklightModel> = _backlightState.filterNotNull()
36 
37     // implemented as channel because original implementation is modeling events: it doesn't hold
38     // state so it won't always emit once connected. And it's bad if some tests depend on that
39     // incorrect behaviour.
40     private val _newlyConnectedKeyboard: Channel<Keyboard> = Channel()
41     override val newlyConnectedKeyboard: Flow<Keyboard> = _newlyConnectedKeyboard.consumeAsFlow()
42 
43     private val _connectedKeyboards: MutableStateFlow<Set<Keyboard>> = MutableStateFlow(setOf())
44     override val connectedKeyboards: Flow<Set<Keyboard>> = _connectedKeyboards
45 
setBacklightnull46     fun setBacklight(state: BacklightModel) {
47         _backlightState.value = state
48     }
49 
setIsAnyKeyboardConnectednull50     fun setIsAnyKeyboardConnected(connected: Boolean) {
51         _isAnyKeyboardConnected.value = connected
52     }
53 
setConnectedKeyboardsnull54     fun setConnectedKeyboards(keyboards: Set<Keyboard>) {
55         _connectedKeyboards.value = keyboards
56         _isAnyKeyboardConnected.value = keyboards.isNotEmpty()
57     }
58 
setNewlyConnectedKeyboardnull59     fun setNewlyConnectedKeyboard(keyboard: Keyboard) {
60         _newlyConnectedKeyboard.trySend(keyboard)
61         _connectedKeyboards.value += keyboard
62         _isAnyKeyboardConnected.value = true
63     }
64 }
65