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.data.repository 18 19 import androidx.test.filters.SmallTest 20 import com.android.customization.model.grid.FakeShapeGridManager 21 import com.android.wallpaper.picker.di.modules.BackgroundDispatcher 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.CoroutineDispatcher 28 import kotlinx.coroutines.CoroutineScope 29 import kotlinx.coroutines.Dispatchers 30 import kotlinx.coroutines.ExperimentalCoroutinesApi 31 import kotlinx.coroutines.test.TestScope 32 import kotlinx.coroutines.test.resetMain 33 import kotlinx.coroutines.test.runTest 34 import org.junit.After 35 import org.junit.Before 36 import org.junit.Rule 37 import org.junit.Test 38 import org.junit.runner.RunWith 39 import org.robolectric.RobolectricTestRunner 40 41 @HiltAndroidTest 42 @OptIn(ExperimentalCoroutinesApi::class) 43 @SmallTest 44 @RunWith(RobolectricTestRunner::class) 45 class ShapeGridRepositoryTest { 46 47 @get:Rule var hiltRule = HiltAndroidRule(this) 48 @Inject lateinit var gridOptionsManager: FakeShapeGridManager 49 @Inject lateinit var testScope: TestScope 50 @BackgroundDispatcher @Inject lateinit var bgScope: CoroutineScope 51 @BackgroundDispatcher @Inject lateinit var bgDispatcher: CoroutineDispatcher 52 53 private lateinit var underTest: ShapeGridRepository 54 55 @Before setUpnull56 fun setUp() { 57 hiltRule.inject() 58 underTest = 59 ShapeGridRepository( 60 manager = gridOptionsManager, 61 bgScope = bgScope, 62 bgDispatcher = bgDispatcher, 63 ) 64 } 65 66 @After tearDownnull67 fun tearDown() { 68 Dispatchers.resetMain() 69 } 70 71 @Test shapeOptions_defaultnull72 fun shapeOptions_default() = 73 testScope.runTest { 74 val gridOptions = collectLastValue(underTest.shapeOptions) 75 76 assertThat(gridOptions()).isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST) 77 } 78 79 @Test shapeOptions_shouldUpdateAfterApplyShapeGridOptionnull80 fun shapeOptions_shouldUpdateAfterApplyShapeGridOption() = 81 testScope.runTest { 82 val shapeOptions = collectLastValue(underTest.shapeOptions) 83 84 underTest.applySelectedOption("circle", "practical") 85 86 assertThat(shapeOptions()) 87 .isEqualTo( 88 FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST.map { 89 it.copy(isCurrent = (it.key == "circle")) 90 } 91 ) 92 } 93 94 @Test selectedShapeOption_defaultnull95 fun selectedShapeOption_default() = 96 testScope.runTest { 97 val selectedGridOption = collectLastValue(underTest.selectedShapeOption) 98 99 assertThat(selectedGridOption()) 100 .isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST[0]) 101 } 102 103 @Test selectedShapeOption_shouldUpdateAfterApplyShapeGridOptionnull104 fun selectedShapeOption_shouldUpdateAfterApplyShapeGridOption() = 105 testScope.runTest { 106 val selectedShapeOption = collectLastValue(underTest.selectedShapeOption) 107 108 underTest.applySelectedOption("circle", "practical") 109 110 assertThat(selectedShapeOption()) 111 .isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST[4].copy(isCurrent = true)) 112 } 113 114 @Test gridOptions_defaultnull115 fun gridOptions_default() = 116 testScope.runTest { 117 val gridOptions = collectLastValue(underTest.gridOptions) 118 119 assertThat(gridOptions()).isEqualTo(FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST) 120 } 121 122 @Test gridOptions_shouldUpdateAfterApplyShapeGridOptionnull123 fun gridOptions_shouldUpdateAfterApplyShapeGridOption() = 124 testScope.runTest { 125 val gridOptions = collectLastValue(underTest.gridOptions) 126 127 underTest.applySelectedOption("circle", "practical") 128 129 assertThat(gridOptions()) 130 .isEqualTo( 131 FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST.map { 132 it.copy(isCurrent = (it.key == "practical")) 133 } 134 ) 135 } 136 137 @Test selectedGridOption_defaultnull138 fun selectedGridOption_default() = 139 testScope.runTest { 140 val selectedGridOption = collectLastValue(underTest.selectedGridOption) 141 142 assertThat(selectedGridOption()) 143 .isEqualTo(FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST[0]) 144 } 145 146 @Test selectedGridOption_shouldUpdateAfterApplyShapeGridOptionnull147 fun selectedGridOption_shouldUpdateAfterApplyShapeGridOption() = 148 testScope.runTest { 149 val selectedGridOption = collectLastValue(underTest.selectedGridOption) 150 151 underTest.applySelectedOption("circle", "practical") 152 153 assertThat(selectedGridOption()) 154 .isEqualTo(FakeShapeGridManager.DEFAULT_GRID_OPTION_LIST[1].copy(isCurrent = true)) 155 } 156 } 157