1  /*
2   * Copyright (C) 2024 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  package com.android.avatatpicker.tests
17  
18  import android.content.Context
19  import android.content.Intent
20  import android.net.Uri
21  import androidx.test.platform.app.InstrumentationRegistry
22  import com.android.avatarpicker.domain.getTempPNG
23  import com.android.avatarpicker.ui.ResultHandler
24  import com.android.avatarpicker.ui.details.items.SelectableType
25  import com.android.avatarpicker.ui.details.items.UiState
26  import kotlinx.coroutines.flow.MutableStateFlow
27  import kotlinx.coroutines.flow.asStateFlow
28  
29  class FakeResultHandler : ResultHandler {
30      private val _uiState: MutableStateFlow<UiState> = MutableStateFlow(UiState.None())
31      override val uiState = _uiState.asStateFlow()
32  
onSelectnull33      override fun <T : SelectableType> onSelect(result: T) {
34          getSelected()?.isSelected = false
35          result.isSelected = true
36          _uiState.value = UiState.Success(result)
37      }
38  
getSelectednull39      override fun getSelected() = (uiState.value as? UiState.Success<*>)?.result
40  
41      override fun onLoading() {
42          _uiState.value = UiState.Loading()
43      }
44  
onErrornull45      override fun onError(exception: Exception) {
46          _uiState.value = UiState.Error(exception)
47      }
48  
unselectnull49      override fun unselect() {
50          _uiState.value = UiState.None()
51      }
52  
toResultIntentnull53      override fun toResultIntent(context: Context): Intent {
54          return Intent()
55      }
56  
getContentUrinull57      override fun getContentUri(): Uri {
58          return Uri.EMPTY
59      }
60  
getTempFilenull61      override fun getTempFile() =
62          InstrumentationRegistry.getInstrumentation().getContext().getTempPNG("tests.png")
63  
64  }