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.wallpaper.picker.category.repository 18 19 import android.content.Context 20 import com.android.wallpaper.model.Category 21 import com.android.wallpaper.model.ImageCategory 22 import com.android.wallpaper.model.ThirdPartyLiveWallpaperCategory 23 import com.android.wallpaper.model.WallpaperInfo 24 import com.android.wallpaper.module.InjectorProvider 25 import com.android.wallpaper.picker.category.data.repository.DefaultWallpaperCategoryRepository 26 import com.android.wallpaper.testing.FakeDefaultCategoryFactory 27 import com.android.wallpaper.testing.FakeDefaultWallpaperCategoryClient 28 import com.android.wallpaper.testing.TestInjector 29 import com.android.wallpaper.testing.TestStaticWallpaperInfo 30 import com.android.wallpaper.testing.TestWallpaperCategory 31 import com.google.common.truth.Truth.assertThat 32 import dagger.hilt.android.qualifiers.ApplicationContext 33 import dagger.hilt.android.testing.HiltAndroidRule 34 import dagger.hilt.android.testing.HiltAndroidTest 35 import javax.inject.Inject 36 import kotlinx.coroutines.ExperimentalCoroutinesApi 37 import kotlinx.coroutines.launch 38 import kotlinx.coroutines.test.TestScope 39 import kotlinx.coroutines.test.advanceUntilIdle 40 import kotlinx.coroutines.test.runTest 41 import org.junit.Before 42 import org.junit.Rule 43 import org.junit.Test 44 import org.junit.runner.RunWith 45 import org.robolectric.RobolectricTestRunner 46 47 @HiltAndroidTest 48 @OptIn(ExperimentalCoroutinesApi::class) 49 @RunWith(RobolectricTestRunner::class) 50 class DefaultWallpaperCategoryRepositoryTest { 51 52 @get:Rule var hiltRule = HiltAndroidRule(this) 53 @Inject @ApplicationContext lateinit var context: Context 54 @Inject lateinit var defaultCategoryFactory: FakeDefaultCategoryFactory 55 @Inject lateinit var defaultWallpaperCategoryClient: FakeDefaultWallpaperCategoryClient 56 @Inject lateinit var testScope: TestScope 57 @Inject lateinit var testInjector: TestInjector 58 59 lateinit var repository: DefaultWallpaperCategoryRepository 60 61 @Before setUpnull62 fun setUp() { 63 hiltRule.inject() 64 InjectorProvider.setInjector(testInjector) 65 } 66 67 @Test fetchAllCategories should update categories and set isAllCategoriesFetched to truenull68 fun `fetchAllCategories should update categories and set isAllCategoriesFetched to true`() = 69 runTest { 70 val category1: Category = 71 ImageCategory( 72 "My photos" /* title */, 73 "image_wallpapers" /* collection */, 74 0, /* priority */ 75 ) 76 77 val wallpapers = ArrayList<WallpaperInfo>() 78 val wallpaperInfo: WallpaperInfo = TestStaticWallpaperInfo(0) 79 wallpapers.add(wallpaperInfo) 80 val category2: Category = 81 TestWallpaperCategory( 82 "Test category", 83 "init_collection", 84 wallpapers, 85 1, /* priority */ 86 ) 87 88 val thirdPartyLiveWallpaperCategory: Category = 89 ThirdPartyLiveWallpaperCategory( 90 "Third_Party_Title", 91 "Third_Party_CollectionId", 92 wallpapers, 93 1, 94 emptySet(), 95 ) 96 97 val mCategories = ArrayList<Category>() 98 mCategories.add(category1) 99 mCategories.add(category2) 100 101 defaultWallpaperCategoryClient.setSystemCategories(mCategories) 102 defaultWallpaperCategoryClient.setThirdPartyLiveWallpaperCategories( 103 listOf(thirdPartyLiveWallpaperCategory) 104 ) 105 106 repository = 107 DefaultWallpaperCategoryRepository( 108 context, 109 defaultWallpaperCategoryClient, 110 defaultCategoryFactory, 111 testScope, 112 ) 113 testScope.advanceUntilIdle() 114 assertThat(repository.isDefaultCategoriesFetched.value).isTrue() 115 assertThat(repository.systemCategories).isNotNull() 116 assertThat(repository.thirdPartyLiveWallpaperCategory).isNotNull() 117 } 118 119 @Test <lambda>null120 fun initialStateShouldBeEmpty() = runTest { 121 repository = 122 DefaultWallpaperCategoryRepository( 123 context, 124 defaultWallpaperCategoryClient, 125 defaultCategoryFactory, 126 testScope, 127 ) 128 assertThat(repository.systemCategories.value).isEmpty() 129 assertThat(repository.isDefaultCategoriesFetched.value).isFalse() 130 } 131 132 @Test <lambda>null133 fun refreshThirdPartyLiveWallpaperCategoriesShouldUpdateStateCorrectly() = runTest { 134 repository = 135 DefaultWallpaperCategoryRepository( 136 context, 137 defaultWallpaperCategoryClient, 138 defaultCategoryFactory, 139 testScope, 140 ) 141 142 val job = launch { repository.refreshThirdPartyLiveWallpaperCategories() } 143 assertThat(repository.isDefaultCategoriesFetched.value).isFalse() 144 testScope.advanceUntilIdle() 145 job.join() 146 assertThat(repository.isDefaultCategoriesFetched.value).isTrue() 147 } 148 149 @Test <lambda>null150 fun refreshThirdPartyAppCategoriesShouldUpdateStateCorrectly() = runTest { 151 repository = 152 DefaultWallpaperCategoryRepository( 153 context, 154 defaultWallpaperCategoryClient, 155 defaultCategoryFactory, 156 testScope, 157 ) 158 159 val job = launch { repository.refreshThirdPartyAppCategories() } 160 assertThat(repository.isDefaultCategoriesFetched.value).isFalse() 161 testScope.advanceUntilIdle() 162 job.join() 163 assertThat(repository.isDefaultCategoriesFetched.value).isTrue() 164 } 165 } 166