1 /*
2  * Copyright (C) 2021 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.qs.external
18 
19 import android.graphics.drawable.Icon
20 import android.testing.AndroidTestingRunner
21 import android.testing.TestableLooper
22 import android.view.ViewGroup
23 import android.widget.ImageView
24 import android.widget.TextView
25 import androidx.test.filters.SmallTest
26 import com.android.systemui.R
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.plugins.qs.QSTileView
29 import com.google.common.truth.Truth.assertThat
30 import org.junit.After
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 @SmallTest
36 @RunWith(AndroidTestingRunner::class)
37 @TestableLooper.RunWithLooper
38 class TileRequestDialogTest : SysuiTestCase() {
39 
40     companion object {
41         private const val APP_NAME = "App name"
42         private const val LABEL = "Label"
43     }
44 
45     private lateinit var dialog: TileRequestDialog
46 
47     @Before
48     fun setUp() {
49         // Create in looper so we can make sure that the tile is fully updated
50         TestableLooper.get(this).runWithLooper {
51             dialog = TileRequestDialog(mContext)
52         }
53     }
54 
55     @After
56     fun teardown() {
57         if (this::dialog.isInitialized) {
58             dialog.dismiss()
59         }
60     }
61 
62     @Test
63     fun setTileData_hasCorrectViews() {
64         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
65         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
66 
67         dialog.setTileData(tileData)
68         dialog.show()
69 
70         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
71 
72         assertThat(content.childCount).isEqualTo(2)
73         assertThat(content.getChildAt(0)).isInstanceOf(TextView::class.java)
74         assertThat(content.getChildAt(1)).isInstanceOf(QSTileView::class.java)
75     }
76 
77     @Test
78     fun setTileData_hasCorrectAppName() {
79         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
80         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
81 
82         dialog.setTileData(tileData)
83         dialog.show()
84 
85         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
86         val text = content.getChildAt(0) as TextView
87         assertThat(text.text.toString()).contains(APP_NAME)
88     }
89 
90     @Test
91     fun setTileData_hasCorrectLabel() {
92         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
93         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
94 
95         dialog.setTileData(tileData)
96         dialog.show()
97 
98         TestableLooper.get(this).processAllMessages()
99 
100         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
101         val tile = content.getChildAt(1) as QSTileView
102         assertThat((tile.label as TextView).text.toString()).isEqualTo(LABEL)
103     }
104 
105     @Test
106     fun setTileData_hasIcon() {
107         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
108         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
109 
110         dialog.setTileData(tileData)
111         dialog.show()
112 
113         TestableLooper.get(this).processAllMessages()
114 
115         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
116         val tile = content.getChildAt(1) as QSTileView
117         assertThat((tile.icon.iconView as ImageView).drawable).isNotNull()
118     }
119 
120     @Test
121     fun setTileData_nullIcon_hasIcon() {
122         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, null)
123 
124         dialog.setTileData(tileData)
125         dialog.show()
126 
127         TestableLooper.get(this).processAllMessages()
128 
129         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
130         val tile = content.getChildAt(1) as QSTileView
131         assertThat((tile.icon.iconView as ImageView).drawable).isNotNull()
132     }
133 
134     @Test
135     fun setTileData_hasNoStateDescription() {
136         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
137         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
138 
139         dialog.setTileData(tileData)
140         dialog.show()
141 
142         TestableLooper.get(this).processAllMessages()
143 
144         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
145         val tile = content.getChildAt(1) as QSTileView
146 
147         assertThat(tile.stateDescription).isEqualTo("")
148     }
149 
150     @Test
151     fun setTileData_tileNotClickable() {
152         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
153         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
154 
155         dialog.setTileData(tileData)
156         dialog.show()
157 
158         TestableLooper.get(this).processAllMessages()
159 
160         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
161         val tile = content.getChildAt(1) as QSTileView
162 
163         assertThat(tile.isClickable).isFalse()
164         assertThat(tile.isLongClickable).isFalse()
165     }
166 
167     @Test
168     fun setTileData_tileHasCorrectContentDescription() {
169         val icon = Icon.createWithResource(mContext, R.drawable.cloud)
170         val tileData = TileRequestDialog.TileData(APP_NAME, LABEL, icon)
171 
172         dialog.setTileData(tileData)
173         dialog.show()
174 
175         TestableLooper.get(this).processAllMessages()
176 
177         val content = dialog.requireViewById<ViewGroup>(TileRequestDialog.CONTENT_ID)
178         val tile = content.getChildAt(1) as QSTileView
179 
180         assertThat(tile.contentDescription).isEqualTo(LABEL)
181     }
182 }
183