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.mediarouter.data.repository
18 
19 import android.media.projection.StopReason
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.coroutines.collectLastValue
24 import com.android.systemui.kosmos.Kosmos
25 import com.android.systemui.kosmos.testScope
26 import com.android.systemui.statusbar.policy.CastDevice
27 import com.android.systemui.statusbar.policy.fakeCastController
28 import com.google.common.truth.Truth.assertThat
29 import kotlin.test.Test
30 import kotlinx.coroutines.ExperimentalCoroutinesApi
31 import kotlinx.coroutines.test.runCurrent
32 import kotlinx.coroutines.test.runTest
33 import org.junit.runner.RunWith
34 
35 @SmallTest
36 @OptIn(ExperimentalCoroutinesApi::class)
37 @RunWith(AndroidJUnit4::class)
38 class MediaRouterRepositoryTest : SysuiTestCase() {
39     val kosmos = Kosmos()
40     val testScope = kosmos.testScope
41     val castController = kosmos.fakeCastController
42 
43     val underTest = kosmos.realMediaRouterRepository
44 
45     @Test
castDevices_empty_isEmptynull46     fun castDevices_empty_isEmpty() =
47         testScope.runTest {
48             val latest by collectLastValue(underTest.castDevices)
49             // Required to let the listener attach before the devices get set
50             runCurrent()
51 
52             castController.castDevices = emptyList()
53 
54             assertThat(latest).isEmpty()
55         }
56 
57     @Test
castDevices_onlyIncludesMediaRouterOriginDevicesnull58     fun castDevices_onlyIncludesMediaRouterOriginDevices() =
59         testScope.runTest {
60             val latest by collectLastValue(underTest.castDevices)
61             // Required to let the listener attach before the devices get set
62             runCurrent()
63 
64             val projectionDevice =
65                 CastDevice(
66                     id = "idProjection",
67                     name = "name",
68                     description = "desc",
69                     state = CastDevice.CastState.Connected,
70                     origin = CastDevice.CastOrigin.MediaProjection,
71                 )
72             val routerDevice1 =
73                 CastDevice(
74                     id = "idRouter1",
75                     name = "name",
76                     description = "desc",
77                     state = CastDevice.CastState.Connected,
78                     origin = CastDevice.CastOrigin.MediaRouter,
79                 )
80 
81             val routerDevice2 =
82                 CastDevice(
83                     id = "idRouter2",
84                     name = "name",
85                     description = "desc",
86                     state = CastDevice.CastState.Connected,
87                     origin = CastDevice.CastOrigin.MediaRouter,
88                 )
89             castController.setCastDevices(listOf(projectionDevice, routerDevice1, routerDevice2))
90 
91             assertThat(latest).containsExactly(routerDevice1, routerDevice2).inOrder()
92         }
93 
94     @Test
stopCasting_notifiesCastControllernull95     fun stopCasting_notifiesCastController() {
96         val device =
97             CastDevice(
98                 id = "id",
99                 name = "name",
100                 description = "desc",
101                 state = CastDevice.CastState.Connected,
102                 origin = CastDevice.CastOrigin.MediaRouter,
103             )
104 
105         underTest.stopCasting(device, StopReason.STOP_UNKNOWN)
106 
107         assertThat(castController.lastStoppedDevice).isEqualTo(device)
108     }
109 }
110