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