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 package com.android.intentresolver.emptystate
18 
19 import com.android.intentresolver.ResolverListAdapter
20 import com.google.common.truth.Truth.assertThat
21 import org.junit.Test
22 import org.mockito.kotlin.mock
23 
24 class CompositeEmptyStateProviderTest {
25     val listAdapter = mock<ResolverListAdapter>()
26 
27     val emptyState1 = object : EmptyState {}
28     val emptyState2 = object : EmptyState {}
29 
30     val positiveEmptyStateProvider1 =
31         object : EmptyStateProvider {
getEmptyStatenull32             override fun getEmptyState(listAdapter: ResolverListAdapter) = emptyState1
33         }
34     val positiveEmptyStateProvider2 =
35         object : EmptyStateProvider {
36             override fun getEmptyState(listAdapter: ResolverListAdapter) = emptyState2
37         }
38     val nullEmptyStateProvider =
39         object : EmptyStateProvider {
getEmptyStatenull40             override fun getEmptyState(listAdapter: ResolverListAdapter) = null
41         }
42 
43     @Test
44     fun testComposedProvider_returnsFirstEmptyStateInOrder() {
45         val provider =
46             CompositeEmptyStateProvider(
47                 nullEmptyStateProvider,
48                 positiveEmptyStateProvider1,
49                 positiveEmptyStateProvider2
50             )
51         assertThat(provider.getEmptyState(listAdapter)).isSameInstanceAs(emptyState1)
52     }
53 
54     @Test
testComposedProvider_allProvidersReturnNull_composedResultIsNullnull55     fun testComposedProvider_allProvidersReturnNull_composedResultIsNull() {
56         val provider = CompositeEmptyStateProvider(nullEmptyStateProvider)
57         assertThat(provider.getEmptyState(listAdapter)).isNull()
58     }
59 
60     @Test
testComposedProvider_noEmptyStateIfNoDelegateProvidersnull61     fun testComposedProvider_noEmptyStateIfNoDelegateProviders() {
62         val provider = CompositeEmptyStateProvider()
63         assertThat(provider.getEmptyState(listAdapter)).isNull()
64     }
65 }
66