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