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.common.preview.domain.interactor
18 
19 import android.content.Context
20 import android.content.pm.ActivityInfo
21 import androidx.test.core.app.ActivityScenario
22 import com.android.wallpaper.model.WallpaperModelsPair
23 import com.android.wallpaper.module.InjectorProvider
24 import com.android.wallpaper.picker.common.preview.data.repository.BasePreviewRepository
25 import com.android.wallpaper.picker.customization.data.repository.WallpaperRepository
26 import com.android.wallpaper.picker.preview.PreviewTestActivity
27 import com.android.wallpaper.testing.FakeWallpaperClient
28 import com.android.wallpaper.testing.TestInjector
29 import com.android.wallpaper.testing.TestWallpaperPreferences
30 import com.android.wallpaper.testing.WallpaperModelUtils
31 import com.android.wallpaper.testing.collectLastValue
32 import com.google.common.truth.Truth.assertThat
33 import dagger.hilt.EntryPoint
34 import dagger.hilt.InstallIn
35 import dagger.hilt.android.EntryPointAccessors
36 import dagger.hilt.android.components.ActivityComponent
37 import dagger.hilt.android.qualifiers.ApplicationContext
38 import dagger.hilt.android.testing.HiltAndroidRule
39 import dagger.hilt.android.testing.HiltAndroidTest
40 import javax.inject.Inject
41 import kotlinx.coroutines.Dispatchers
42 import kotlinx.coroutines.ExperimentalCoroutinesApi
43 import kotlinx.coroutines.test.TestDispatcher
44 import kotlinx.coroutines.test.TestScope
45 import kotlinx.coroutines.test.runTest
46 import kotlinx.coroutines.test.setMain
47 import org.junit.Before
48 import org.junit.Rule
49 import org.junit.Test
50 import org.junit.runner.RunWith
51 import org.robolectric.RobolectricTestRunner
52 import org.robolectric.Shadows.shadowOf
53 
54 @HiltAndroidTest
55 @OptIn(ExperimentalCoroutinesApi::class)
56 @RunWith(RobolectricTestRunner::class)
57 class BasePreviewInteractorTest {
58     @get:Rule var hiltRule = HiltAndroidRule(this)
59 
60     private lateinit var scenario: ActivityScenario<PreviewTestActivity>
61     private lateinit var basePreviewRepository: BasePreviewRepository
62     private lateinit var wallpaperRepository: WallpaperRepository
63     private lateinit var interactor: BasePreviewInteractor
64 
65     @Inject @ApplicationContext lateinit var appContext: Context
66     @Inject lateinit var testDispatcher: TestDispatcher
67     @Inject lateinit var testScope: TestScope
68     @Inject lateinit var testInjector: TestInjector
69     @Inject lateinit var wallpaperPreferences: TestWallpaperPreferences
70     @Inject lateinit var wallpaperClient: FakeWallpaperClient
71 
72     @Before
setUpnull73     fun setUp() {
74         hiltRule.inject()
75 
76         InjectorProvider.setInjector(testInjector)
77         Dispatchers.setMain(testDispatcher)
78 
79         val activityInfo =
80             ActivityInfo().apply {
81                 name = PreviewTestActivity::class.java.name
82                 packageName = appContext.packageName
83             }
84         shadowOf(appContext.packageManager).addOrUpdateActivity(activityInfo)
85         scenario = ActivityScenario.launch(PreviewTestActivity::class.java)
86         scenario.onActivity {
87             val activityScopeEntryPoint =
88                 EntryPointAccessors.fromActivity(it, ActivityScopeEntryPoint::class.java)
89             basePreviewRepository = activityScopeEntryPoint.basePreviewRepository()
90             wallpaperRepository = activityScopeEntryPoint.wallpaperRepository()
91             interactor = activityScopeEntryPoint.basePreviewInteractor()
92         }
93     }
94 
95     @EntryPoint
96     @InstallIn(ActivityComponent::class)
97     interface ActivityScopeEntryPoint {
basePreviewRepositorynull98         fun basePreviewRepository(): BasePreviewRepository
99 
100         fun wallpaperRepository(): WallpaperRepository
101 
102         fun basePreviewInteractor(): BasePreviewInteractor
103     }
104 
105     @Test
106     fun wallpapers_withHomeAndLockScreenAndPreviewWallpapers_shouldEmitPreview() {
107         testScope.runTest {
108             val homeStaticWallpaperModel =
109                 WallpaperModelUtils.getStaticWallpaperModel(
110                     wallpaperId = "homeWallpaperId",
111                     collectionId = "homeCollection",
112                 )
113             val lockStaticWallpaperModel =
114                 WallpaperModelUtils.getStaticWallpaperModel(
115                     wallpaperId = "lockWallpaperId",
116                     collectionId = "lockCollection",
117                 )
118             val previewStaticWallpaperModel =
119                 WallpaperModelUtils.getStaticWallpaperModel(
120                     wallpaperId = "previewWallpaperId",
121                     collectionId = "previewCollection",
122                 )
123 
124             // Current wallpaper models need to be set up before the view model is run.
125             wallpaperClient.setCurrentWallpaperModels(
126                 homeStaticWallpaperModel,
127                 lockStaticWallpaperModel
128             )
129             basePreviewRepository.setWallpaperModel(previewStaticWallpaperModel)
130 
131             val actual = collectLastValue(interactor.wallpapers)()
132             assertThat(actual).isNotNull()
133             assertThat(actual).isEqualTo(WallpaperModelsPair(previewStaticWallpaperModel, null))
134         }
135     }
136 
137     @Test
wallpapers_withHomeAndLockScreenAndNoPreviewWallpapers_shouldEmitCurrentHomeAndLocknull138     fun wallpapers_withHomeAndLockScreenAndNoPreviewWallpapers_shouldEmitCurrentHomeAndLock() {
139         testScope.runTest {
140             val homeStaticWallpaperModel =
141                 WallpaperModelUtils.getStaticWallpaperModel(
142                     wallpaperId = "homeWallpaperId",
143                     collectionId = "homeCollection",
144                 )
145             val lockStaticWallpaperModel =
146                 WallpaperModelUtils.getStaticWallpaperModel(
147                     wallpaperId = "lockWallpaperId",
148                     collectionId = "lockCollection",
149                 )
150 
151             // Current wallpaper models need to be set up before the view model is run.
152             wallpaperClient.setCurrentWallpaperModels(
153                 homeStaticWallpaperModel,
154                 lockStaticWallpaperModel
155             )
156 
157             val actual = collectLastValue(interactor.wallpapers)()
158             assertThat(actual).isNotNull()
159             assertThat(actual)
160                 .isEqualTo(WallpaperModelsPair(homeStaticWallpaperModel, lockStaticWallpaperModel))
161         }
162     }
163 }
164