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.volume.dialog.ringer.domain
18 
19 import android.media.AudioManager.RINGER_MODE_NORMAL
20 import android.media.AudioManager.RINGER_MODE_SILENT
21 import android.media.AudioManager.RINGER_MODE_VIBRATE
22 import android.media.AudioManager.STREAM_RING
23 import android.testing.TestableLooper
24 import androidx.test.ext.junit.runners.AndroidJUnit4
25 import androidx.test.filters.SmallTest
26 import com.android.settingslib.volume.shared.model.RingerMode
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.coroutines.collectLastValue
29 import com.android.systemui.kosmos.testScope
30 import com.android.systemui.plugins.fakeVolumeDialogController
31 import com.android.systemui.testKosmos
32 import com.google.common.truth.Truth.assertThat
33 import kotlinx.coroutines.ExperimentalCoroutinesApi
34 import kotlinx.coroutines.test.runCurrent
35 import kotlinx.coroutines.test.runTest
36 import org.junit.Before
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 
40 @OptIn(ExperimentalCoroutinesApi::class)
41 @SmallTest
42 @RunWith(AndroidJUnit4::class)
43 @TestableLooper.RunWithLooper
44 class VolumeDialogRingerInteractorTest : SysuiTestCase() {
45 
46     private val kosmos = testKosmos()
47     private val testScope = kosmos.testScope
48     private val controller = kosmos.fakeVolumeDialogController
49 
50     private lateinit var underTest: VolumeDialogRingerInteractor
51 
52     @Before
setUpnull53     fun setUp() {
54         underTest = kosmos.volumeDialogRingerInteractor
55         controller.setStreamVolume(STREAM_RING, 50)
56     }
57 
58     @Test
setRingerMode_normalnull59     fun setRingerMode_normal() =
60         testScope.runTest {
61             runCurrent()
62             val ringerModel by collectLastValue(underTest.ringerModel)
63 
64             underTest.setRingerMode(RingerMode(RINGER_MODE_NORMAL))
65             controller.getState()
66             runCurrent()
67 
68             assertThat(ringerModel).isNotNull()
69             assertThat(ringerModel?.currentRingerMode).isEqualTo(RingerMode(RINGER_MODE_NORMAL))
70         }
71 
72     @Test
setRingerMode_silentnull73     fun setRingerMode_silent() =
74         testScope.runTest {
75             runCurrent()
76             val ringerModel by collectLastValue(underTest.ringerModel)
77 
78             underTest.setRingerMode(RingerMode(RINGER_MODE_SILENT))
79             controller.getState()
80             runCurrent()
81 
82             assertThat(ringerModel).isNotNull()
83             assertThat(ringerModel?.currentRingerMode).isEqualTo(RingerMode(RINGER_MODE_SILENT))
84         }
85 
86     @Test
setRingerMode_vibratenull87     fun setRingerMode_vibrate() =
88         testScope.runTest {
89             runCurrent()
90             val ringerModel by collectLastValue(underTest.ringerModel)
91 
92             underTest.setRingerMode(RingerMode(RINGER_MODE_VIBRATE))
93             controller.getState()
94             runCurrent()
95 
96             assertThat(ringerModel).isNotNull()
97             assertThat(ringerModel?.currentRingerMode).isEqualTo(RingerMode(RINGER_MODE_VIBRATE))
98         }
99 }
100