• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 android.app.cts;
18 
19 import android.app.AlertDialog;
20 import android.app.Instrumentation;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.DialogInterface.OnCancelListener;
24 import android.content.DialogInterface.OnClickListener;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.os.Message;
28 import android.test.ActivityInstrumentationTestCase2;
29 import android.view.KeyEvent;
30 import android.view.View;
31 import android.widget.Button;
32 import dalvik.annotation.TestLevel;
33 import dalvik.annotation.TestTargetClass;
34 import dalvik.annotation.TestTargetNew;
35 import dalvik.annotation.TestTargets;
36 
37 /*
38  * Test AlertDialog
39  */
40 @TestTargetClass(AlertDialog.class)
41 public class AlertDialogTest extends ActivityInstrumentationTestCase2<DialogStubActivity> {
42     private static final String ALERTDIALOG_CUSTOM_TITLE = "Hello, World!";
43 
44     private Instrumentation mInstrumentation;
45     private DialogStubActivity mActivity;
46     private Button mPositiveButton;
47     private Button mNegativeButton;
48     private Button mNeutralButton;
49 
AlertDialogTest()50     public AlertDialogTest() {
51         super("com.android.cts.stub", DialogStubActivity.class);
52     }
53 
54     @Override
setUp()55     protected void setUp() throws Exception {
56         super.setUp();
57         mInstrumentation = getInstrumentation();
58         mActivity = getActivity();
59     }
60 
popDialog(int index)61     protected void popDialog(int index) {
62         while (index != 0) {
63             sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
64             index--;
65         }
66         sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
67     }
68 
69     @TestTargets({
70         @TestTargetNew(
71             level = TestLevel.COMPLETE,
72             notes = "",
73             method = "AlertDialog",
74             args = {Context.class}
75         ),
76         @TestTargetNew(
77             level = TestLevel.COMPLETE,
78             notes = "",
79             method = "getButton",
80             args = {int.class}
81         ),
82         @TestTargetNew(
83             level = TestLevel.COMPLETE,
84             notes = "",
85             method = "onCreate",
86             args = {Bundle.class}
87         ),
88         @TestTargetNew(
89             level = TestLevel.COMPLETE,
90             notes = "",
91             method = "setButton",
92             args = {int.class, CharSequence.class, OnClickListener.class}
93         ),
94         @TestTargetNew(
95             level = TestLevel.COMPLETE,
96             notes = "",
97             method = "setIcon",
98             args = {Drawable.class}
99         ),
100         @TestTargetNew(
101             level = TestLevel.COMPLETE,
102             notes = "",
103             method = "setIcon",
104             args = {int.class}
105         ),
106         @TestTargetNew(
107             level = TestLevel.COMPLETE,
108             notes = "",
109             method = "setMessage",
110             args = {CharSequence.class}
111         ),
112         @TestTargetNew(
113             level = TestLevel.COMPLETE,
114             notes = "",
115             method = "setTitle",
116             args = {CharSequence.class}
117         )
118     })
testAlertDialog()119     public void testAlertDialog() throws Throwable {
120         doTestAlertDialog(DialogStubActivity.TEST_ALERTDIALOG);
121     }
122 
doTestAlertDialog(int index)123     private void doTestAlertDialog(int index) throws Throwable {
124         popDialog(index);
125         assertTrue(mActivity.getDialog().isShowing());
126 
127         mPositiveButton = ((AlertDialog) (mActivity.getDialog())).getButton(
128                 DialogInterface.BUTTON_POSITIVE);
129         assertNotNull(mPositiveButton);
130         assertEquals(mActivity.getString(com.android.cts.stub.R.string.alert_dialog_positive),
131                 mPositiveButton.getText());
132         mNeutralButton = ((AlertDialog) (mActivity.getDialog())).getButton(
133                 DialogInterface.BUTTON_NEUTRAL);
134         assertNotNull(mNeutralButton);
135         assertEquals(mActivity.getString(com.android.cts.stub.R.string.alert_dialog_neutral),
136                 mNeutralButton.getText());
137         mNegativeButton = ((AlertDialog) (mActivity.getDialog())).getButton(
138                 DialogInterface.BUTTON_NEGATIVE);
139         assertNotNull(mNegativeButton);
140         assertEquals(mActivity.getString(com.android.cts.stub.R.string.alert_dialog_negative),
141                 mNegativeButton.getText());
142 
143         assertFalse(mActivity.isPositiveButtonClicked);
144         performClick(mPositiveButton);
145         assertTrue(mActivity.isPositiveButtonClicked);
146 
147         assertFalse(mActivity.isNegativeButtonClicked);
148         performClick(mNegativeButton);
149         assertTrue(mActivity.isNegativeButtonClicked);
150 
151         assertFalse(mActivity.isNeutralButtonClicked);
152         performClick(mNeutralButton);
153         assertTrue(mActivity.isNeutralButtonClicked);
154     }
155 
156     @TestTargets({
157         @TestTargetNew(
158             level = TestLevel.COMPLETE,
159             notes = "",
160             method = "AlertDialog",
161             args = {Context.class}
162         ),
163         @TestTargetNew(
164             level = TestLevel.COMPLETE,
165             notes = "",
166             method = "getButton",
167             args = {int.class}
168         ),
169         @TestTargetNew(
170             level = TestLevel.COMPLETE,
171             notes = "",
172             method = "getListView",
173             args = {}
174         ),
175         @TestTargetNew(
176             level = TestLevel.COMPLETE,
177             notes = "",
178             method = "setButton",
179             args = {CharSequence.class, OnClickListener.class}
180         ),
181         @TestTargetNew(
182             level = TestLevel.COMPLETE,
183             notes = "",
184             method = "setButton",
185             args = {int.class, CharSequence.class, Message.class}
186         ),
187         @TestTargetNew(
188             level = TestLevel.COMPLETE,
189             notes = "",
190             method = "setButton2",
191             args = {CharSequence.class, OnClickListener.class}
192         ),
193         @TestTargetNew(
194             level = TestLevel.COMPLETE,
195             notes = "Test setButton3",
196             method = "setButton3",
197             args = {CharSequence.class, OnClickListener.class}
198         ),
199         @TestTargetNew(
200             level = TestLevel.COMPLETE,
201             notes = "",
202             method = "setIcon",
203             args = {int.class}
204         ),
205         @TestTargetNew(
206             level = TestLevel.COMPLETE,
207             notes = "",
208             method = "setInverseBackgroundForced",
209             args = {boolean.class}
210         ),
211         @TestTargetNew(
212             level = TestLevel.COMPLETE,
213             notes = "",
214             method = "setMessage",
215             args = {CharSequence.class}
216         ),
217         @TestTargetNew(
218             level = TestLevel.COMPLETE,
219             notes = "",
220             method = "setTitle",
221             args = {CharSequence.class}
222         )
223     })
testAlertDialogDeprecatedAPI()224     public void testAlertDialogDeprecatedAPI() throws Throwable {
225         doTestAlertDialog(DialogStubActivity.TEST_ALERTDIALOG_DEPRECATED);
226     }
227 
228     @TestTargets({
229         @TestTargetNew(
230             level = TestLevel.COMPLETE,
231             notes = "",
232             method = "AlertDialog",
233             args = {Context.class}
234         ),
235         @TestTargetNew(
236             level = TestLevel.COMPLETE,
237             notes = "",
238             method = "getButton",
239             args = {int.class}
240         ),
241         @TestTargetNew(
242             level = TestLevel.COMPLETE,
243             notes = "",
244             method = "getListView",
245             args = {}
246         ),
247         @TestTargetNew(
248             level = TestLevel.COMPLETE,
249             notes = "",
250             method = "setButton",
251             args = {CharSequence.class, Message.class}
252         ),
253         @TestTargetNew(
254             level = TestLevel.COMPLETE,
255             notes = "",
256             method = "setButton",
257             args = {int.class, CharSequence.class, Message.class}
258         ),
259         @TestTargetNew(
260             level = TestLevel.COMPLETE,
261             notes = "",
262             method = "setButton2",
263             args = {CharSequence.class, Message.class}
264         ),
265         @TestTargetNew(
266             level = TestLevel.COMPLETE,
267             notes = "",
268             method = "setButton3",
269             args = {CharSequence.class, Message.class}
270         ),
271         @TestTargetNew(
272             level = TestLevel.COMPLETE,
273             notes = "",
274             method = "setIcon",
275             args = {int.class}
276         ),
277         @TestTargetNew(
278             level = TestLevel.COMPLETE,
279             notes = "",
280             method = "setInverseBackgroundForced",
281             args = {boolean.class}
282         ),
283         @TestTargetNew(
284             level = TestLevel.COMPLETE,
285             notes = "",
286             method = "setMessage",
287             args = {CharSequence.class}
288         ),
289         @TestTargetNew(
290             level = TestLevel.COMPLETE,
291             notes = "",
292             method = "setTitle",
293             args = {CharSequence.class}
294         )
295     })
testAlertDialogDeprecatedAPIWithMessage()296     public void testAlertDialogDeprecatedAPIWithMessage() throws Throwable {
297         popDialog(DialogStubActivity.TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE);
298         assertTrue(mActivity.getDialog().isShowing());
299 
300         mPositiveButton = ((AlertDialog) (mActivity.getDialog())).getButton(
301                 DialogInterface.BUTTON_POSITIVE);
302         assertNotNull(mPositiveButton);
303         assertEquals(mActivity.getString(com.android.cts.stub.R.string.alert_dialog_positive),
304                 mPositiveButton.getText());
305         mNegativeButton = ((AlertDialog) (mActivity.getDialog())).getButton(
306                 DialogInterface.BUTTON_NEGATIVE);
307         assertNotNull(mNegativeButton);
308         assertEquals(mActivity.getString(com.android.cts.stub.R.string.alert_dialog_negative),
309                 mNegativeButton.getText());
310         mNeutralButton = ((AlertDialog) (mActivity.getDialog())).getButton(
311                 DialogInterface.BUTTON_NEUTRAL);
312         assertNotNull(mNeutralButton);
313         assertEquals(mActivity.getString(com.android.cts.stub.R.string.alert_dialog_neutral),
314                 mNeutralButton.getText());
315 
316         DialogStubActivity.buttonIndex = 0;
317         performClick(mPositiveButton);
318         assertEquals(DialogInterface.BUTTON_POSITIVE, DialogStubActivity.buttonIndex);
319 
320         DialogStubActivity.buttonIndex = 0;
321         performClick(mNeutralButton);
322         assertEquals(DialogInterface.BUTTON_NEUTRAL, DialogStubActivity.buttonIndex);
323 
324         DialogStubActivity.buttonIndex = 0;
325         performClick(mNegativeButton);
326         assertEquals(DialogInterface.BUTTON_NEGATIVE, DialogStubActivity.buttonIndex);
327     }
328 
performClick(final Button button)329     private void performClick(final Button button) throws Throwable {
330         runTestOnUiThread(new Runnable() {
331             public void run() {
332                 button.performClick();
333             }
334         });
335         mInstrumentation.waitForIdleSync();
336     }
337 
338     @TestTargets({
339         @TestTargetNew(
340             level = TestLevel.COMPLETE,
341             notes = "",
342             method = "AlertDialog",
343             args = {Context.class, int.class}
344         ),
345         @TestTargetNew(
346             level = TestLevel.COMPLETE,
347             notes = "",
348             method = "AlertDialog",
349             args = {Context.class, boolean.class, OnCancelListener.class}
350          ),
351         @TestTargetNew(
352            level = TestLevel.COMPLETE,
353            notes = "",
354            method = "setCustomTitle",
355            args = {View.class}
356         ),
357         @TestTargetNew(
358             level = TestLevel.COMPLETE,
359             notes = "",
360             method = "setMessage",
361             args = {CharSequence.class}
362         ),
363         @TestTargetNew(
364             level = TestLevel.COMPLETE,
365             notes = "",
366             method = "setView",
367             args = {View.class}
368         )
369     })
testCustomAlertDialog()370     public void testCustomAlertDialog() {
371         popDialog(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG);
372         assertTrue(mActivity.getDialog().isShowing());
373     }
374 
375     @TestTargets({
376         @TestTargetNew(
377             level = TestLevel.COMPLETE,
378             notes = "",
379             method = "AlertDialog",
380             args = {Context.class, int.class}
381         ),
382         @TestTargetNew(
383             level = TestLevel.COMPLETE,
384             notes = "",
385             method = "AlertDialog",
386             args = {Context.class, boolean.class, OnCancelListener.class}
387          ),
388         @TestTargetNew(
389            level = TestLevel.COMPLETE,
390            notes = "",
391            method = "setCustomTitle",
392            args = {View.class}
393         ),
394         @TestTargetNew(
395             level = TestLevel.COMPLETE,
396             notes = "",
397             method = "setMessage",
398             args = {CharSequence.class}
399         ),
400         @TestTargetNew(
401             level = TestLevel.COMPLETE,
402             notes = "",
403             method = "setView",
404             args = {View.class, int.class, int.class, int.class, int.class}
405         )
406     })
testCustomAlertDialogView()407     public void testCustomAlertDialogView() {
408         popDialog(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG_VIEW);
409         assertTrue(mActivity.getDialog().isShowing());
410     }
411 
412 
413     @TestTargets({
414         @TestTargetNew(
415             level = TestLevel.COMPLETE,
416             notes = "",
417             method = "AlertDialog",
418             args = {Context.class}
419         ),
420         @TestTargetNew(
421             level = TestLevel.COMPLETE,
422             notes = "",
423             method = "onKeyDown",
424             args = {int.class, KeyEvent.class}
425         ),
426         @TestTargetNew(
427             level = TestLevel.COMPLETE,
428             notes = "",
429             method = "onKeyUp",
430             args = {int.class, KeyEvent.class}
431         ),
432         @TestTargetNew(
433             level = TestLevel.COMPLETE,
434             notes = "",
435             method = "onCreate",
436             args = {Bundle.class}
437         )
438     })
testCallback()439     public void testCallback() {
440         popDialog(DialogStubActivity.TEST_ALERTDIALOG_CALLBACK);
441         assertTrue(mActivity.onCreateCalled);
442 
443         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
444         assertTrue(mActivity.onKeyDownCalled);
445         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
446         assertTrue(mActivity.onKeyUpCalled);
447     }
448 
449     @TestTargetNew(
450         level = TestLevel.COMPLETE,
451         notes = "",
452         method = "AlertDialog",
453         args = {Context.class, int.class}
454     )
testAlertDialogTheme()455     public void testAlertDialogTheme() throws Exception {
456         popDialog(DialogStubActivity.TEST_ALERTDIALOG_THEME);
457         assertTrue(mActivity.getDialog().isShowing());
458     }
459 
460     @TestTargetNew(
461         level = TestLevel.COMPLETE,
462         notes = "",
463         method = "AlertDialog",
464         args = {Context.class, boolean.class, OnCancelListener.class}
465     )
testAlertDialogCancelable()466     public void testAlertDialogCancelable() throws Exception {
467         popDialog(DialogStubActivity.TEST_ALERTDIALOG_CANCELABLE);
468         assertTrue(mActivity.getDialog().isShowing());
469         assertFalse(mActivity.onCancelCalled);
470         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
471         mInstrumentation.waitForIdleSync();
472         assertTrue(mActivity.onCancelCalled);
473     }
474 
475     @TestTargetNew(
476         level = TestLevel.COMPLETE,
477         notes = "",
478         method = "AlertDialog",
479         args = {Context.class, boolean.class, OnCancelListener.class}
480     )
testAlertDialogNotCancelable()481     public void testAlertDialogNotCancelable() throws Exception {
482         popDialog(DialogStubActivity.TEST_ALERTDIALOG_NOT_CANCELABLE);
483         assertTrue(mActivity.getDialog().isShowing());
484         assertFalse(mActivity.onCancelCalled);
485         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
486         assertFalse(mActivity.onCancelCalled);
487     }
488 }
489