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.statusbar.chips.notification.demo.ui.viewmodel
18 
19 import android.content.packageManager
20 import android.graphics.drawable.BitmapDrawable
21 import android.platform.test.annotations.DisableFlags
22 import android.platform.test.annotations.EnableFlags
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.coroutines.collectLastValue
26 import com.android.systemui.kosmos.testScope
27 import com.android.systemui.statusbar.chips.notification.shared.StatusBarNotifChips
28 import com.android.systemui.statusbar.chips.ui.model.ColorsModel
29 import com.android.systemui.statusbar.chips.ui.model.OngoingActivityChipModel
30 import com.android.systemui.statusbar.commandline.CommandRegistry
31 import com.android.systemui.statusbar.commandline.commandRegistry
32 import com.android.systemui.testKosmos
33 import com.google.common.truth.Truth.assertThat
34 import java.io.PrintWriter
35 import java.io.StringWriter
36 import kotlin.test.Test
37 import kotlinx.coroutines.test.runTest
38 import org.junit.Before
39 import org.mockito.kotlin.any
40 import org.mockito.kotlin.whenever
41 
42 @SmallTest
43 class DemoNotifChipViewModelTest : SysuiTestCase() {
44     private val kosmos = testKosmos()
45     private val testScope = kosmos.testScope
46     private val commandRegistry = kosmos.commandRegistry
47     private val pw = PrintWriter(StringWriter())
48 
49     private val underTest = kosmos.demoNotifChipViewModel
50 
51     @Before
setUpnull52     fun setUp() {
53         underTest.start()
54         whenever(kosmos.packageManager.getApplicationIcon(any<String>()))
55             .thenReturn(BitmapDrawable())
56     }
57 
58     @Test
59     @DisableFlags(StatusBarNotifChips.FLAG_NAME)
chip_flagOff_hiddennull60     fun chip_flagOff_hidden() =
61         testScope.runTest {
62             val latest by collectLastValue(underTest.chip)
63 
64             addDemoNotifChip()
65 
66             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Hidden::class.java)
67         }
68 
69     @Test
70     @EnableFlags(StatusBarNotifChips.FLAG_NAME)
chip_noPackage_hiddennull71     fun chip_noPackage_hidden() =
72         testScope.runTest {
73             val latest by collectLastValue(underTest.chip)
74 
75             commandRegistry.onShellCommand(pw, arrayOf("demo-notif"))
76 
77             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Hidden::class.java)
78         }
79 
80     @Test
81     @EnableFlags(StatusBarNotifChips.FLAG_NAME)
chip_hasPackage_shownnull82     fun chip_hasPackage_shown() =
83         testScope.runTest {
84             val latest by collectLastValue(underTest.chip)
85 
86             commandRegistry.onShellCommand(pw, arrayOf("demo-notif", "-p", "com.android.systemui"))
87 
88             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
89         }
90 
91     @Test
92     @EnableFlags(StatusBarNotifChips.FLAG_NAME)
chip_hasText_shownWithTextnull93     fun chip_hasText_shownWithText() =
94         testScope.runTest {
95             val latest by collectLastValue(underTest.chip)
96 
97             commandRegistry.onShellCommand(
98                 pw,
99                 arrayOf("demo-notif", "-p", "com.android.systemui", "-t", "test"),
100             )
101 
102             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown.Text::class.java)
103         }
104 
105     @Test
106     @EnableFlags(StatusBarNotifChips.FLAG_NAME)
chip_supportsColornull107     fun chip_supportsColor() =
108         testScope.runTest {
109             val latest by collectLastValue(underTest.chip)
110 
111             commandRegistry.onShellCommand(
112                 pw,
113                 arrayOf("demo-notif", "-p", "com.android.systemui", "-c", "#434343"),
114             )
115 
116             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
117             assertThat((latest as OngoingActivityChipModel.Shown).colors)
118                 .isInstanceOf(ColorsModel.Custom::class.java)
119         }
120 
121     @Test
122     @EnableFlags(StatusBarNotifChips.FLAG_NAME)
chip_hasHideArg_hiddennull123     fun chip_hasHideArg_hidden() =
124         testScope.runTest {
125             val latest by collectLastValue(underTest.chip)
126 
127             // First, show a chip
128             addDemoNotifChip()
129             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Shown::class.java)
130 
131             // Then, hide the chip
132             commandRegistry.onShellCommand(pw, arrayOf("demo-notif", "--hide"))
133 
134             assertThat(latest).isInstanceOf(OngoingActivityChipModel.Hidden::class.java)
135         }
136 
addDemoNotifChipnull137     private fun addDemoNotifChip() {
138         addDemoNotifChip(commandRegistry, pw)
139     }
140 
141     companion object {
addDemoNotifChipnull142         fun addDemoNotifChip(commandRegistry: CommandRegistry, pw: PrintWriter) {
143             commandRegistry.onShellCommand(pw, arrayOf("demo-notif", "-p", "com.android.systemui"))
144         }
145     }
146 }
147