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.app.stubs.DialogStubActivity;
22 import android.content.DialogInterface;
23 import android.cts.util.PollingCheck;
24 import android.test.ActivityInstrumentationTestCase2;
25 import android.test.suitebuilder.annotation.SmallTest;
26 import android.view.KeyEvent;
27 import android.widget.Button;
28 
29 import android.app.stubs.R;
30 /*
31  * Test AlertDialog
32  */
33 @SmallTest
34 public class AlertDialogTest extends ActivityInstrumentationTestCase2<DialogStubActivity> {
35     private Instrumentation mInstrumentation;
36     private DialogStubActivity mActivity;
37     private Button mPositiveButton;
38     private Button mNegativeButton;
39     private Button mNeutralButton;
40 
AlertDialogTest()41     public AlertDialogTest() {
42         super("android.app.stubs", DialogStubActivity.class);
43     }
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         super.setUp();
48         mInstrumentation = getInstrumentation();
49     }
50 
startDialogActivity(int dialogNumber)51     protected void startDialogActivity(int dialogNumber) {
52         mActivity = DialogStubActivity.startDialogActivity(this, dialogNumber);
53 
54         PollingCheck.waitFor(() -> mActivity.getDialog().isShowing());
55     }
56 
testAlertDialog()57     public void testAlertDialog() throws Throwable {
58         doTestAlertDialog(DialogStubActivity.TEST_ALERTDIALOG);
59     }
60 
doTestAlertDialog(int index)61     private void doTestAlertDialog(int index) throws Throwable {
62         startDialogActivity(index);
63         assertTrue(mActivity.getDialog().isShowing());
64 
65         mPositiveButton = ((AlertDialog) (mActivity.getDialog())).getButton(
66                 DialogInterface.BUTTON_POSITIVE);
67         assertNotNull(mPositiveButton);
68         assertEquals(mActivity.getString(R.string.alert_dialog_positive),
69                 mPositiveButton.getText());
70         mNeutralButton = ((AlertDialog) (mActivity.getDialog())).getButton(
71                 DialogInterface.BUTTON_NEUTRAL);
72         assertNotNull(mNeutralButton);
73         assertEquals(mActivity.getString(R.string.alert_dialog_neutral),
74                 mNeutralButton.getText());
75         mNegativeButton = ((AlertDialog) (mActivity.getDialog())).getButton(
76                 DialogInterface.BUTTON_NEGATIVE);
77         assertNotNull(mNegativeButton);
78         assertEquals(mActivity.getString(R.string.alert_dialog_negative),
79                 mNegativeButton.getText());
80 
81         assertFalse(mActivity.isPositiveButtonClicked);
82         performClick(mPositiveButton);
83         assertTrue(mActivity.isPositiveButtonClicked);
84 
85         assertFalse(mActivity.isNegativeButtonClicked);
86         performClick(mNegativeButton);
87         assertTrue(mActivity.isNegativeButtonClicked);
88 
89         assertFalse(mActivity.isNeutralButtonClicked);
90         performClick(mNeutralButton);
91         assertTrue(mActivity.isNeutralButtonClicked);
92     }
93 
testAlertDialogDeprecatedAPI()94     public void testAlertDialogDeprecatedAPI() throws Throwable {
95         doTestAlertDialog(DialogStubActivity.TEST_ALERTDIALOG_DEPRECATED);
96     }
97 
testAlertDialogAPIWithMessage(final boolean useDeprecatedAPIs)98     private void testAlertDialogAPIWithMessage(final boolean useDeprecatedAPIs) throws Throwable {
99         startDialogActivity(useDeprecatedAPIs
100                 ? DialogStubActivity.TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE
101                 : DialogStubActivity.TEST_ALERTDIALOG_WITH_MESSAGE);
102 
103         assertTrue(mActivity.getDialog().isShowing());
104 
105         mPositiveButton = ((AlertDialog) (mActivity.getDialog())).getButton(
106                 DialogInterface.BUTTON_POSITIVE);
107         assertNotNull(mPositiveButton);
108         assertEquals(mActivity.getString(R.string.alert_dialog_positive),
109                 mPositiveButton.getText());
110         mNegativeButton = ((AlertDialog) (mActivity.getDialog())).getButton(
111                 DialogInterface.BUTTON_NEGATIVE);
112         assertNotNull(mNegativeButton);
113         assertEquals(mActivity.getString(R.string.alert_dialog_negative),
114                 mNegativeButton.getText());
115         mNeutralButton = ((AlertDialog) (mActivity.getDialog())).getButton(
116                 DialogInterface.BUTTON_NEUTRAL);
117         assertNotNull(mNeutralButton);
118         assertEquals(mActivity.getString(R.string.alert_dialog_neutral),
119                 mNeutralButton.getText());
120 
121         DialogStubActivity.buttonIndex = 0;
122         performClick(mPositiveButton);
123         assertEquals(DialogInterface.BUTTON_POSITIVE, DialogStubActivity.buttonIndex);
124 
125         DialogStubActivity.buttonIndex = 0;
126         performClick(mNeutralButton);
127         assertEquals(DialogInterface.BUTTON_NEUTRAL, DialogStubActivity.buttonIndex);
128 
129         DialogStubActivity.buttonIndex = 0;
130         performClick(mNegativeButton);
131         assertEquals(DialogInterface.BUTTON_NEGATIVE, DialogStubActivity.buttonIndex);
132     }
133 
testAlertDialogAPIWithMessageDeprecated()134     public void testAlertDialogAPIWithMessageDeprecated() throws Throwable {
135         testAlertDialogAPIWithMessage(true);
136     }
137 
testAlertDialogAPIWithMessageNotDeprecated()138     public void testAlertDialogAPIWithMessageNotDeprecated() throws Throwable {
139         testAlertDialogAPIWithMessage(false);
140     }
141 
performClick(final Button button)142     private void performClick(final Button button) throws Throwable {
143         runTestOnUiThread(() -> button.performClick());
144         mInstrumentation.waitForIdleSync();
145     }
146 
testCustomAlertDialog()147     public void testCustomAlertDialog() {
148         startDialogActivity(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG);
149         assertTrue(mActivity.getDialog().isShowing());
150     }
151 
testCustomAlertDialogView()152     public void testCustomAlertDialogView() {
153         startDialogActivity(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG_VIEW);
154         assertTrue(mActivity.getDialog().isShowing());
155     }
156 
testCallback()157     public void testCallback() {
158         startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_CALLBACK);
159         assertTrue(mActivity.onCreateCalled);
160 
161         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
162         assertTrue(mActivity.onKeyDownCalled);
163         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
164         assertTrue(mActivity.onKeyUpCalled);
165     }
166 
testAlertDialogTheme()167     public void testAlertDialogTheme() throws Exception {
168         startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_THEME);
169         assertTrue(mActivity.getDialog().isShowing());
170     }
171 
testAlertDialogCancelable()172     public void testAlertDialogCancelable() throws Exception {
173         startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_CANCELABLE);
174         assertTrue(mActivity.getDialog().isShowing());
175         assertFalse(mActivity.onCancelCalled);
176         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
177         mInstrumentation.waitForIdleSync();
178         assertTrue(mActivity.onCancelCalled);
179     }
180 
testAlertDialogNotCancelable()181     public void testAlertDialogNotCancelable() throws Exception {
182         startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_NOT_CANCELABLE);
183         assertTrue(mActivity.getDialog().isShowing());
184         assertFalse(mActivity.onCancelCalled);
185         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
186         assertFalse(mActivity.onCancelCalled);
187     }
188 
testAlertDialogIconDrawable()189     public void testAlertDialogIconDrawable() {
190         startDialogActivity(DialogStubActivity.TEST_ALERT_DIALOG_ICON_DRAWABLE);
191         assertTrue(mActivity.getDialog().isShowing());
192     }
193 
testAlertDialogIconAttribute()194     public void testAlertDialogIconAttribute() {
195         startDialogActivity(DialogStubActivity.TEST_ALERT_DIALOG_ICON_ATTRIBUTE);
196         assertTrue(mActivity.getDialog().isShowing());
197     }
198 }
199