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.systemui.scene.shared.model
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.coroutines.collectLastValue
23 import com.android.systemui.kosmos.testScope
24 import com.android.systemui.scene.initialSceneKey
25 import com.android.systemui.testKosmos
26 import com.google.common.truth.Truth.assertThat
27 import kotlinx.coroutines.test.runTest
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @RunWith(AndroidJUnit4::class)
33 class SceneDataSourceDelegatorTest : SysuiTestCase() {
34 
35     private val kosmos = testKosmos()
36     private val testScope = kosmos.testScope
37     private val initialSceneKey = kosmos.initialSceneKey
38     private val fakeSceneDataSource = kosmos.fakeSceneDataSource
39 
40     // TODO(b/356596436): Add tests for showing, hiding, and replacing overlays after we've defined
41     //  them.
42     private val underTest = kosmos.sceneDataSourceDelegator
43 
44     @Test
currentScene_withoutDelegate_startsWithInitialScenenull45     fun currentScene_withoutDelegate_startsWithInitialScene() =
46         testScope.runTest {
47             val currentScene by collectLastValue(underTest.currentScene)
48             underTest.setDelegate(null)
49 
50             assertThat(currentScene).isEqualTo(initialSceneKey)
51         }
52 
53     @Test
currentScene_withoutDelegate_doesNothingnull54     fun currentScene_withoutDelegate_doesNothing() =
55         testScope.runTest {
56             val currentScene by collectLastValue(underTest.currentScene)
57             underTest.setDelegate(null)
58             assertThat(currentScene).isNotEqualTo(Scenes.Bouncer)
59 
60             underTest.changeScene(toScene = Scenes.Bouncer)
61 
62             assertThat(currentScene).isEqualTo(initialSceneKey)
63         }
64 
65     @Test
currentScene_withDelegate_startsWithInitialScenenull66     fun currentScene_withDelegate_startsWithInitialScene() =
67         testScope.runTest {
68             val currentScene by collectLastValue(underTest.currentScene)
69             assertThat(currentScene).isEqualTo(initialSceneKey)
70         }
71 
72     @Test
currentScene_withDelegate_changesScenesnull73     fun currentScene_withDelegate_changesScenes() =
74         testScope.runTest {
75             val currentScene by collectLastValue(underTest.currentScene)
76             assertThat(currentScene).isNotEqualTo(Scenes.Bouncer)
77 
78             underTest.changeScene(toScene = Scenes.Bouncer)
79 
80             assertThat(currentScene).isEqualTo(Scenes.Bouncer)
81         }
82 
83     @Test
currentScene_reflectsDelegatenull84     fun currentScene_reflectsDelegate() =
85         testScope.runTest {
86             val currentScene by collectLastValue(underTest.currentScene)
87 
88             fakeSceneDataSource.changeScene(toScene = Scenes.Bouncer)
89 
90             assertThat(currentScene).isEqualTo(Scenes.Bouncer)
91         }
92 }
93