1 /*
2  * Copyright (C) 2022 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 
18 package com.android.systemui.util
19 
20 import android.content.DialogInterface
21 import android.content.DialogInterface.BUTTON_NEGATIVE
22 import android.content.DialogInterface.BUTTON_NEUTRAL
23 import android.content.DialogInterface.BUTTON_POSITIVE
24 import android.testing.AndroidTestingRunner
25 import android.testing.TestableLooper
26 import androidx.test.filters.SmallTest
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.util.mockito.any
29 import com.android.systemui.util.mockito.mock
30 import com.google.common.truth.Truth.assertThat
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.Mockito.anyInt
34 import org.mockito.Mockito.inOrder
35 import org.mockito.Mockito.never
36 import org.mockito.Mockito.verify
37 
38 @SmallTest
39 @RunWith(AndroidTestingRunner::class)
40 @TestableLooper.RunWithLooper
41 class TestableAlertDialogTest : SysuiTestCase() {
42 
43     @Test
dialogNotShowingWhenCreatednull44     fun dialogNotShowingWhenCreated() {
45         val dialog = TestableAlertDialog(context)
46 
47         assertThat(dialog.isShowing).isFalse()
48     }
49 
50     @Test
dialogShownDoesntCrashnull51     fun dialogShownDoesntCrash() {
52         val dialog = TestableAlertDialog(context)
53 
54         dialog.show()
55     }
56 
57     @Test
dialogShowingnull58     fun dialogShowing() {
59         val dialog = TestableAlertDialog(context)
60 
61         dialog.show()
62 
63         assertThat(dialog.isShowing).isTrue()
64     }
65 
66     @Test
showListenerCallednull67     fun showListenerCalled() {
68         val dialog = TestableAlertDialog(context)
69         val listener: DialogInterface.OnShowListener = mock()
70         dialog.setOnShowListener(listener)
71 
72         dialog.show()
73 
74         verify(listener).onShow(dialog)
75     }
76 
77     @Test
showListenerRemovednull78     fun showListenerRemoved() {
79         val dialog = TestableAlertDialog(context)
80         val listener: DialogInterface.OnShowListener = mock()
81         dialog.setOnShowListener(listener)
82         dialog.setOnShowListener(null)
83 
84         dialog.show()
85 
86         verify(listener, never()).onShow(any())
87     }
88 
89     @Test
dialogHiddenNotShowingnull90     fun dialogHiddenNotShowing() {
91         val dialog = TestableAlertDialog(context)
92 
93         dialog.show()
94         dialog.hide()
95 
96         assertThat(dialog.isShowing).isFalse()
97     }
98 
99     @Test
dialogDismissNotShowingnull100     fun dialogDismissNotShowing() {
101         val dialog = TestableAlertDialog(context)
102 
103         dialog.show()
104         dialog.dismiss()
105 
106         assertThat(dialog.isShowing).isFalse()
107     }
108 
109     @Test
dismissListenerCalled_ifShowingnull110     fun dismissListenerCalled_ifShowing() {
111         val dialog = TestableAlertDialog(context)
112         val listener: DialogInterface.OnDismissListener = mock()
113         dialog.setOnDismissListener(listener)
114 
115         dialog.show()
116         dialog.dismiss()
117 
118         verify(listener).onDismiss(dialog)
119     }
120 
121     @Test
dismissListenerNotCalled_ifNotShowingnull122     fun dismissListenerNotCalled_ifNotShowing() {
123         val dialog = TestableAlertDialog(context)
124         val listener: DialogInterface.OnDismissListener = mock()
125         dialog.setOnDismissListener(listener)
126 
127         dialog.dismiss()
128 
129         verify(listener, never()).onDismiss(any())
130     }
131 
132     @Test
dismissListenerRemovednull133     fun dismissListenerRemoved() {
134         val dialog = TestableAlertDialog(context)
135         val listener: DialogInterface.OnDismissListener = mock()
136         dialog.setOnDismissListener(listener)
137         dialog.setOnDismissListener(null)
138 
139         dialog.show()
140         dialog.dismiss()
141 
142         verify(listener, never()).onDismiss(any())
143     }
144 
145     @Test
cancelListenerCalled_showingnull146     fun cancelListenerCalled_showing() {
147         val dialog = TestableAlertDialog(context)
148         val listener: DialogInterface.OnCancelListener = mock()
149         dialog.setOnCancelListener(listener)
150 
151         dialog.show()
152         dialog.cancel()
153 
154         verify(listener).onCancel(dialog)
155     }
156 
157     @Test
cancelListenerCalled_notShowingnull158     fun cancelListenerCalled_notShowing() {
159         val dialog = TestableAlertDialog(context)
160         val listener: DialogInterface.OnCancelListener = mock()
161         dialog.setOnCancelListener(listener)
162 
163         dialog.cancel()
164 
165         verify(listener).onCancel(dialog)
166     }
167 
168     @Test
dismissCalledOnCancel_showingnull169     fun dismissCalledOnCancel_showing() {
170         val dialog = TestableAlertDialog(context)
171         val listener: DialogInterface.OnDismissListener = mock()
172         dialog.setOnDismissListener(listener)
173 
174         dialog.show()
175         dialog.cancel()
176 
177         verify(listener).onDismiss(dialog)
178     }
179 
180     @Test
dialogCancelNotShowingnull181     fun dialogCancelNotShowing() {
182         val dialog = TestableAlertDialog(context)
183 
184         dialog.show()
185         dialog.cancel()
186 
187         assertThat(dialog.isShowing).isFalse()
188     }
189 
190     @Test
cancelListenerRemovednull191     fun cancelListenerRemoved() {
192         val dialog = TestableAlertDialog(context)
193         val listener: DialogInterface.OnCancelListener = mock()
194         dialog.setOnCancelListener(listener)
195         dialog.setOnCancelListener(null)
196 
197         dialog.show()
198         dialog.cancel()
199 
200         verify(listener, never()).onCancel(any())
201     }
202 
203     @Test
positiveButtonClicknull204     fun positiveButtonClick() {
205         val dialog = TestableAlertDialog(context)
206         val listener: DialogInterface.OnClickListener = mock()
207         dialog.setButton(BUTTON_POSITIVE, "", listener)
208 
209         dialog.show()
210         dialog.clickButton(BUTTON_POSITIVE)
211 
212         verify(listener).onClick(dialog, BUTTON_POSITIVE)
213     }
214 
215     @Test
positiveButtonListener_noCalledWhenClickOtherButtonsnull216     fun positiveButtonListener_noCalledWhenClickOtherButtons() {
217         val dialog = TestableAlertDialog(context)
218         val listener: DialogInterface.OnClickListener = mock()
219         dialog.setButton(BUTTON_POSITIVE, "", listener)
220 
221         dialog.show()
222         dialog.clickButton(BUTTON_NEUTRAL)
223         dialog.clickButton(BUTTON_NEGATIVE)
224 
225         verify(listener, never()).onClick(any(), anyInt())
226     }
227 
228     @Test
negativeButtonClicknull229     fun negativeButtonClick() {
230         val dialog = TestableAlertDialog(context)
231         val listener: DialogInterface.OnClickListener = mock()
232         dialog.setButton(BUTTON_NEGATIVE, "", listener)
233 
234         dialog.show()
235         dialog.clickButton(BUTTON_NEGATIVE)
236 
237         verify(listener).onClick(dialog, DialogInterface.BUTTON_NEGATIVE)
238     }
239 
240     @Test
negativeButtonListener_noCalledWhenClickOtherButtonsnull241     fun negativeButtonListener_noCalledWhenClickOtherButtons() {
242         val dialog = TestableAlertDialog(context)
243         val listener: DialogInterface.OnClickListener = mock()
244         dialog.setButton(BUTTON_NEGATIVE, "", listener)
245 
246         dialog.show()
247         dialog.clickButton(BUTTON_NEUTRAL)
248         dialog.clickButton(BUTTON_POSITIVE)
249 
250         verify(listener, never()).onClick(any(), anyInt())
251     }
252 
253     @Test
neutralButtonClicknull254     fun neutralButtonClick() {
255         val dialog = TestableAlertDialog(context)
256         val listener: DialogInterface.OnClickListener = mock()
257         dialog.setButton(BUTTON_NEUTRAL, "", listener)
258 
259         dialog.show()
260         dialog.clickButton(BUTTON_NEUTRAL)
261 
262         verify(listener).onClick(dialog, BUTTON_NEUTRAL)
263     }
264 
265     @Test
neutralButtonListener_noCalledWhenClickOtherButtonsnull266     fun neutralButtonListener_noCalledWhenClickOtherButtons() {
267         val dialog = TestableAlertDialog(context)
268         val listener: DialogInterface.OnClickListener = mock()
269         dialog.setButton(BUTTON_NEUTRAL, "", listener)
270 
271         dialog.show()
272         dialog.clickButton(BUTTON_POSITIVE)
273         dialog.clickButton(BUTTON_NEGATIVE)
274 
275         verify(listener, never()).onClick(any(), anyInt())
276     }
277 
278     @Test
sameClickListenerCalledCorrectlynull279     fun sameClickListenerCalledCorrectly() {
280         val dialog = TestableAlertDialog(context)
281         val listener: DialogInterface.OnClickListener = mock()
282         dialog.setButton(BUTTON_POSITIVE, "", listener)
283         dialog.setButton(BUTTON_NEUTRAL, "", listener)
284         dialog.setButton(BUTTON_NEGATIVE, "", listener)
285 
286         dialog.show()
287         dialog.clickButton(BUTTON_POSITIVE)
288         dialog.clickButton(BUTTON_NEGATIVE)
289         dialog.clickButton(BUTTON_NEUTRAL)
290 
291         val inOrder = inOrder(listener)
292         inOrder.verify(listener).onClick(dialog, BUTTON_POSITIVE)
293         inOrder.verify(listener).onClick(dialog, BUTTON_NEGATIVE)
294         inOrder.verify(listener).onClick(dialog, BUTTON_NEUTRAL)
295     }
296 
297     @Test(expected = IllegalArgumentException::class)
clickBadButtonnull298     fun clickBadButton() {
299         val dialog = TestableAlertDialog(context)
300 
301         dialog.clickButton(10000)
302     }
303 
304     @Test
clickButtonDismisses_positivenull305     fun clickButtonDismisses_positive() {
306         val dialog = TestableAlertDialog(context)
307 
308         dialog.show()
309         dialog.clickButton(BUTTON_POSITIVE)
310 
311         assertThat(dialog.isShowing).isFalse()
312     }
313 
314     @Test
clickButtonDismisses_negativenull315     fun clickButtonDismisses_negative() {
316         val dialog = TestableAlertDialog(context)
317 
318         dialog.show()
319         dialog.clickButton(BUTTON_NEGATIVE)
320 
321         assertThat(dialog.isShowing).isFalse()
322     }
323 
324     @Test
clickButtonDismisses_neutralnull325     fun clickButtonDismisses_neutral() {
326         val dialog = TestableAlertDialog(context)
327 
328         dialog.show()
329         dialog.clickButton(BUTTON_NEUTRAL)
330 
331         assertThat(dialog.isShowing).isFalse()
332     }
333 }
334