• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.content.ContentResolver
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.photopicker.data.model.Provider
23 import com.google.common.truth.Truth.assertThat
24 import kotlinx.coroutines.ExperimentalCoroutinesApi
25 import kotlinx.coroutines.flow.MutableStateFlow
26 import kotlinx.coroutines.test.runTest
27 import org.junit.Before
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.mockito.Mockito.times
31 
32 @SmallTest
33 @RunWith(AndroidJUnit4::class)
34 @OptIn(ExperimentalCoroutinesApi::class)
35 class CollectionInfoStateTest {
36     private lateinit var testContentProvider: TestMediaProvider
37     private lateinit var mediaProviderClient: MediaProviderClient
38     private lateinit var testContentResolverFlow: MutableStateFlow<ContentResolver>
39     private lateinit var availableProvidersFlow: MutableStateFlow<List<Provider>>
40 
41     @Before
setupnull42     fun setup() {
43         testContentProvider = TestMediaProvider()
44         mediaProviderClient = MediaProviderClient()
45         testContentResolverFlow = MutableStateFlow(ContentResolver.wrap(testContentProvider))
46         availableProvidersFlow = MutableStateFlow(listOf(testContentProvider.providers[0]))
47     }
48 
49     @Test
<lambda>null50     fun testUpdateCollectionInfo() = runTest {
51         val collectionInfoState =
52             CollectionInfoState(
53                 mediaProviderClient,
54                 testContentResolverFlow,
55                 availableProvidersFlow
56             )
57 
58         collectionInfoState.updateCollectionInfo(testContentProvider.collectionInfos)
59 
60         val expectedProvider = testContentProvider.providers[0]
61         val expectedCollectionInfo = testContentProvider.collectionInfos[0]
62         val cachedCollectionInfo = collectionInfoState.getCachedCollectionInfo(expectedProvider)
63         assertThat(cachedCollectionInfo?.authority).isEqualTo(expectedCollectionInfo.authority)
64         assertThat(cachedCollectionInfo?.accountName).isEqualTo(expectedCollectionInfo.accountName)
65         assertThat(cachedCollectionInfo?.collectionId)
66             .isEqualTo(expectedCollectionInfo.collectionId)
67     }
68 
69     @Test
<lambda>null70     fun testClearCollectionInfo() = runTest {
71         val collectionInfoState =
72             CollectionInfoState(
73                 mediaProviderClient,
74                 testContentResolverFlow,
75                 availableProvidersFlow
76             )
77 
78         collectionInfoState.updateCollectionInfo(testContentProvider.collectionInfos)
79 
80         val expectedProvider = testContentProvider.providers[0]
81         val expectedCollectionInfo = testContentProvider.collectionInfos[0]
82         val cachedCollectionInfo = collectionInfoState.getCachedCollectionInfo(expectedProvider)
83         assertThat(cachedCollectionInfo?.authority).isEqualTo(expectedCollectionInfo.authority)
84         assertThat(cachedCollectionInfo?.accountName).isEqualTo(expectedCollectionInfo.accountName)
85         assertThat(cachedCollectionInfo?.collectionId)
86             .isEqualTo(expectedCollectionInfo.collectionId)
87 
88         collectionInfoState.clear()
89         val clearedCollectionInfo = collectionInfoState.getCachedCollectionInfo(expectedProvider)
90         assertThat(clearedCollectionInfo).isNull()
91     }
92 
93     @Test
<lambda>null94     fun testGetCollectionInfo() = runTest {
95         val collectionInfoState =
96             CollectionInfoState(
97                 mediaProviderClient,
98                 testContentResolverFlow,
99                 availableProvidersFlow
100             )
101 
102         val expectedProvider = testContentProvider.providers[0]
103         val expectedCollectionInfo = testContentProvider.collectionInfos[0]
104 
105         collectionInfoState.clear()
106         val clearedCollectionInfo = collectionInfoState.getCachedCollectionInfo(expectedProvider)
107         assertThat(clearedCollectionInfo).isNull()
108 
109         val actualCollectionInfo = collectionInfoState.getCollectionInfo(expectedProvider)
110         assertThat(actualCollectionInfo.authority).isEqualTo(expectedCollectionInfo.authority)
111         assertThat(actualCollectionInfo.accountName).isEqualTo(expectedCollectionInfo.accountName)
112         assertThat(actualCollectionInfo.collectionId).isEqualTo(expectedCollectionInfo.collectionId)
113     }
114 }
115