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.panel.component.captioning.ui.viewmodel
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.internal.logging.uiEventLogger
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.accessibility.data.repository.captioningRepository
24 import com.android.systemui.accessibility.domain.interactor.captioningInteractor
25 import com.android.systemui.coroutines.collectLastValue
26 import com.android.systemui.kosmos.applicationCoroutineScope
27 import com.android.systemui.kosmos.testScope
28 import com.android.systemui.testKosmos
29 import com.google.common.truth.Truth.assertThat
30 import kotlinx.coroutines.ExperimentalCoroutinesApi
31 import kotlinx.coroutines.test.runCurrent
32 import kotlinx.coroutines.test.runTest
33 import org.junit.Before
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 
37 @OptIn(ExperimentalCoroutinesApi::class)
38 @SmallTest
39 @RunWith(AndroidJUnit4::class)
40 class CaptioningViewModelTest : SysuiTestCase() {
41 
42     private val kosmos = testKosmos()
43 
44     private lateinit var underTest: CaptioningViewModel
45 
46     @Before
setupnull47     fun setup() {
48         underTest =
49             with(kosmos) {
50                 CaptioningViewModel(
51                     context,
52                     captioningInteractor,
53                     applicationCoroutineScope,
54                     uiEventLogger,
55                 )
56             }
57     }
58 
59     @Test
captioningDisabled_buttonViewModel_notCheckednull60     fun captioningDisabled_buttonViewModel_notChecked() {
61         with(kosmos) {
62             testScope.runTest {
63                 captioningRepository.setIsSystemAudioCaptioningEnabled(false)
64 
65                 val buttonViewModel by collectLastValue(underTest.buttonViewModel)
66                 runCurrent()
67 
68                 assertThat(buttonViewModel!!.isActive).isFalse()
69             }
70         }
71     }
72 
73     @Test
captioningDisabled_buttonViewModel_checkednull74     fun captioningDisabled_buttonViewModel_checked() {
75         with(kosmos) {
76             testScope.runTest {
77                 captioningRepository.setIsSystemAudioCaptioningEnabled(true)
78 
79                 val buttonViewModel by collectLastValue(underTest.buttonViewModel)
80                 runCurrent()
81 
82                 assertThat(buttonViewModel!!.isActive).isTrue()
83             }
84         }
85     }
86 }
87