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