1 /*
<lambda>null2  * 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.customization.model.grid.ui.viewmodel
19 
20 import androidx.test.filters.SmallTest
21 import androidx.test.platform.app.InstrumentationRegistry
22 import com.android.customization.model.grid.data.repository.FakeGridRepository
23 import com.android.customization.picker.grid.domain.interactor.GridInteractor
24 import com.android.customization.picker.grid.domain.interactor.GridSnapshotRestorer
25 import com.android.customization.picker.grid.ui.viewmodel.GridIconViewModel
26 import com.android.customization.picker.grid.ui.viewmodel.GridScreenViewModel
27 import com.android.wallpaper.picker.option.ui.viewmodel.OptionItemViewModel
28 import com.android.wallpaper.testing.FakeSnapshotStore
29 import com.android.wallpaper.testing.collectLastValue
30 import com.google.common.truth.Truth.assertThat
31 import kotlinx.coroutines.ExperimentalCoroutinesApi
32 import kotlinx.coroutines.runBlocking
33 import kotlinx.coroutines.test.TestScope
34 import kotlinx.coroutines.test.runTest
35 import org.junit.Before
36 import org.junit.Ignore
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.junit.runners.JUnit4
40 
41 @OptIn(ExperimentalCoroutinesApi::class)
42 @SmallTest
43 @RunWith(JUnit4::class)
44 class GridScreenViewModelTest {
45 
46     private lateinit var underTest: GridScreenViewModel
47     private lateinit var testScope: TestScope
48     private lateinit var interactor: GridInteractor
49     private lateinit var store: FakeSnapshotStore
50 
51     @Before
52     fun setUp() {
53         testScope = TestScope()
54         store = FakeSnapshotStore()
55         interactor =
56             GridInteractor(
57                 applicationScope = testScope.backgroundScope,
58                 repository =
59                     FakeGridRepository(
60                         scope = testScope.backgroundScope,
61                         initialOptionCount = 4,
62                     ),
63                 snapshotRestorer = {
64                     GridSnapshotRestorer(
65                             interactor = interactor,
66                         )
67                         .apply { runBlocking { setUpSnapshotRestorer(store) } }
68                 }
69             )
70 
71         underTest =
72             GridScreenViewModel(
73                 context = InstrumentationRegistry.getInstrumentation().targetContext,
74                 interactor = interactor,
75             )
76     }
77 
78     @Test
79     @Ignore("b/270371382")
80     fun clickOnItem_itGetsSelected() =
81         testScope.runTest {
82             val optionItemsValueProvider = collectLastValue(underTest.optionItems)
83             var optionItemsValue = checkNotNull(optionItemsValueProvider.invoke())
84             assertThat(optionItemsValue).hasSize(4)
85             assertThat(getSelectedIndex(optionItemsValue)).isEqualTo(0)
86             assertThat(getOnClick(optionItemsValue[0])).isNull()
87 
88             val item1OnClickedValue = getOnClick(optionItemsValue[1])
89             assertThat(item1OnClickedValue).isNotNull()
90             item1OnClickedValue?.invoke()
91 
92             optionItemsValue = checkNotNull(optionItemsValueProvider.invoke())
93             assertThat(optionItemsValue).hasSize(4)
94             assertThat(getSelectedIndex(optionItemsValue)).isEqualTo(1)
95             assertThat(getOnClick(optionItemsValue[0])).isNotNull()
96             assertThat(getOnClick(optionItemsValue[1])).isNull()
97         }
98 
99     private fun TestScope.getSelectedIndex(
100         optionItems: List<OptionItemViewModel<GridIconViewModel>>
101     ): Int {
102         return optionItems.indexOfFirst { optionItem ->
103             collectLastValue(optionItem.isSelected).invoke() == true
104         }
105     }
106 
107     private fun TestScope.getOnClick(
108         optionItem: OptionItemViewModel<GridIconViewModel>
109     ): (() -> Unit)? {
110         return collectLastValue(optionItem.onClicked).invoke()
111     }
112 }
113