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.customization.picker.grid.domain.interactor
18 
19 import androidx.test.filters.SmallTest
20 import com.android.customization.model.grid.FakeShapeGridManager
21 import com.android.customization.picker.grid.data.repository.ShapeGridRepository
22 import com.android.wallpaper.testing.collectLastValue
23 import com.google.common.truth.Truth.assertThat
24 import dagger.hilt.android.testing.HiltAndroidRule
25 import dagger.hilt.android.testing.HiltAndroidTest
26 import javax.inject.Inject
27 import kotlinx.coroutines.Dispatchers
28 import kotlinx.coroutines.ExperimentalCoroutinesApi
29 import kotlinx.coroutines.test.TestScope
30 import kotlinx.coroutines.test.resetMain
31 import kotlinx.coroutines.test.runTest
32 import org.junit.After
33 import org.junit.Before
34 import org.junit.Rule
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.robolectric.RobolectricTestRunner
38 
39 @HiltAndroidTest
40 @OptIn(ExperimentalCoroutinesApi::class)
41 @SmallTest
42 @RunWith(RobolectricTestRunner::class)
43 class ShapeGridInteractorTest {
44 
45     @get:Rule var hiltRule = HiltAndroidRule(this)
46     @Inject lateinit var gridOptionsManager: FakeShapeGridManager
47     @Inject lateinit var repository: ShapeGridRepository
48     @Inject lateinit var testScope: TestScope
49 
50     private lateinit var underTest: ShapeGridInteractor
51 
52     @Before
setUpnull53     fun setUp() {
54         hiltRule.inject()
55         underTest = ShapeGridInteractor(repository)
56     }
57 
58     @After
tearDownnull59     fun tearDown() {
60         Dispatchers.resetMain()
61     }
62 
63     @Test
shapeOptions_defaultnull64     fun shapeOptions_default() =
65         testScope.runTest {
66             val shapeOptions = collectLastValue(underTest.shapeOptions)
67 
68             assertThat(shapeOptions()).isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST)
69         }
70 
71     @Test
shapeOptions_shouldUpdateAfterApplyGridOptionnull72     fun shapeOptions_shouldUpdateAfterApplyGridOption() =
73         testScope.runTest {
74             val shapeOptions = collectLastValue(underTest.shapeOptions)
75 
76             underTest.applySelectedOption("circle", "practical")
77 
78             assertThat(shapeOptions())
79                 .isEqualTo(
80                     FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST.map {
81                         it.copy(isCurrent = (it.key == "circle"))
82                     }
83                 )
84         }
85 
86     @Test
selectedShapeOption_defaultnull87     fun selectedShapeOption_default() =
88         testScope.runTest {
89             val selectedShapeOption = collectLastValue(underTest.selectedShapeOption)
90 
91             assertThat(selectedShapeOption())
92                 .isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST[0])
93         }
94 
95     @Test
selectedShapeOption_shouldUpdateAfterApplyGridOptionnull96     fun selectedShapeOption_shouldUpdateAfterApplyGridOption() =
97         testScope.runTest {
98             val selectedShapeOption = collectLastValue(underTest.selectedShapeOption)
99 
100             underTest.applySelectedOption("circle", "practical")
101 
102             assertThat(selectedShapeOption())
103                 .isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST[4].copy(isCurrent = true))
104         }
105 
106     @Test
gridOptions_defaultnull107     fun gridOptions_default() =
108         testScope.runTest {
109             val gridOptions = collectLastValue(underTest.gridOptions)
110 
111             assertThat(gridOptions()).isEqualTo(FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST)
112         }
113 
114     @Test
gridOptions_shouldUpdateAfterApplyGridOptionnull115     fun gridOptions_shouldUpdateAfterApplyGridOption() =
116         testScope.runTest {
117             val gridOptions = collectLastValue(underTest.gridOptions)
118 
119             underTest.applySelectedOption("arch", "practical")
120 
121             assertThat(gridOptions())
122                 .isEqualTo(
123                     FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST.map {
124                         it.copy(isCurrent = (it.key == "practical"))
125                     }
126                 )
127         }
128 
129     @Test
selectedGridOption_defaultnull130     fun selectedGridOption_default() =
131         testScope.runTest {
132             val selectedGridOption = collectLastValue(underTest.selectedGridOption)
133 
134             assertThat(selectedGridOption())
135                 .isEqualTo(FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST[0])
136         }
137 
138     @Test
selectedGridOption_shouldUpdateAfterApplyGridOptionnull139     fun selectedGridOption_shouldUpdateAfterApplyGridOption() =
140         testScope.runTest {
141             val selectedGridOption = collectLastValue(underTest.selectedGridOption)
142 
143             underTest.applySelectedOption("arch", "practical")
144 
145             assertThat(selectedGridOption())
146                 .isEqualTo(FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST[1].copy(isCurrent = true))
147         }
148 }
149