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 
17 package com.android.photopicker.data
18 
19 import android.net.Uri
20 import android.os.CancellationSignal
21 import androidx.paging.PagingSource
22 import com.android.photopicker.data.model.Media
23 import com.android.photopicker.data.model.MediaPageKey
24 import com.android.photopicker.data.paging.FakeInMemoryMediaPagingSource
25 import com.android.photopicker.features.search.data.SearchDataService
26 import com.android.photopicker.features.search.model.SearchEnabledState
27 import com.android.photopicker.features.search.model.SearchSuggestion
28 import com.android.photopicker.features.search.model.SearchSuggestionType
29 import kotlinx.coroutines.flow.MutableStateFlow
30 import kotlinx.coroutines.flow.StateFlow
31 
32 /**
33  * A test implementation of [SearchDataService] that provides fake search suggestions and results.
34  */
35 class TestSearchDataServiceImpl() : SearchDataService {
36 
37     var mediaSetSize: Int = FakeInMemoryMediaPagingSource.DEFAULT_SIZE
38     var mediaList: List<Media>? = null
39 
40     override val isSearchEnabled: StateFlow<SearchEnabledState> =
41         MutableStateFlow(SearchEnabledState.ENABLED)
42 
getSearchSuggestionsnull43     override suspend fun getSearchSuggestions(
44         prefix: String,
45         limit: Int,
46         cancellationSignal: CancellationSignal?,
47     ): List<SearchSuggestion> {
48         return listOf(
49             SearchSuggestion("1", "authority", "France", SearchSuggestionType.LOCATION, null),
50             SearchSuggestion("2", "authority", "Favorites", SearchSuggestionType.ALBUM, null),
51             SearchSuggestion("3", "authority", "Emma", SearchSuggestionType.FACE, Uri.parse("xyz")),
52             SearchSuggestion(null, "authority", "paris", SearchSuggestionType.HISTORY, null),
53         )
54     }
55 
getSearchResultsnull56     override fun getSearchResults(
57         suggestion: SearchSuggestion,
58         cancellationSignal: CancellationSignal?,
59     ): PagingSource<MediaPageKey, Media> {
60         return mediaList?.let { FakeInMemoryMediaPagingSource(it) }
61             ?: FakeInMemoryMediaPagingSource(mediaSetSize)
62     }
63 
getSearchResultsnull64     override fun getSearchResults(
65         searchText: String,
66         cancellationSignal: CancellationSignal?,
67     ): PagingSource<MediaPageKey, Media> {
68         return mediaList?.let { FakeInMemoryMediaPagingSource(it) }
69             ?: FakeInMemoryMediaPagingSource(mediaSetSize)
70     }
71 }
72