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.assertNotNull; 21 import static org.junit.Assert.assertTrue; 22 import static org.mockito.Mockito.any; 23 import static org.mockito.Mockito.eq; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.times; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.verifyNoMoreInteractions; 28 29 import android.app.AlertDialog; 30 import android.app.AlertDialog.Builder; 31 import android.app.stubs.R; 32 import android.content.Context; 33 import android.content.DialogInterface; 34 import android.content.DialogInterface.OnCancelListener; 35 import android.content.DialogInterface.OnClickListener; 36 import android.content.DialogInterface.OnDismissListener; 37 import android.content.DialogInterface.OnKeyListener; 38 import android.content.DialogInterface.OnMultiChoiceClickListener; 39 import android.content.res.TypedArray; 40 import android.graphics.drawable.Drawable; 41 import android.os.SystemClock; 42 import android.view.KeyEvent; 43 import android.view.LayoutInflater; 44 import android.view.View; 45 import android.widget.AdapterView.OnItemSelectedListener; 46 import android.widget.ArrayAdapter; 47 import android.widget.Button; 48 import android.widget.ListAdapter; 49 import android.widget.ListView; 50 51 import androidx.test.filters.SmallTest; 52 53 import com.android.compatibility.common.util.PollingCheck; 54 55 import org.junit.Test; 56 import org.junit.runner.RunWith; 57 import org.junit.runners.JUnit4; 58 import org.mockito.ArgumentCaptor; 59 60 @SmallTest 61 @RunWith(JUnit4.class) 62 public final class AlertDialog_BuilderTest extends AlertDialog_BuilderTestBase { 63 64 private Builder mBuilder; 65 private final CharSequence mTitle = "title"; 66 private Drawable mDrawable; 67 private AlertDialog mDialog; 68 private Button mButton; 69 private CharSequence mSelectedItem; 70 71 private View mView; 72 private ListView mListView; 73 74 private OnClickListener mOnClickListener = mock(OnClickListener.class); 75 76 private OnCancelListener mOnCancelListener = mock(OnCancelListener.class); 77 78 private OnDismissListener mOnDismissListener = mock(OnDismissListener.class); 79 80 private OnKeyListener mOnKeyListener = mock(OnKeyListener.class); 81 82 private OnItemSelectedListener mOnItemSelectedListener = mock(OnItemSelectedListener.class); 83 84 private OnMultiChoiceClickListener mOnMultiChoiceClickListener = 85 mock(OnMultiChoiceClickListener.class); 86 87 @Test testConstructor()88 public void testConstructor() { 89 new AlertDialog.Builder(mDialogActivity); 90 } 91 92 @Test testConstructorWithThemeId()93 public void testConstructorWithThemeId() { 94 mBuilder = new AlertDialog.Builder(mDialogActivity, R.style.DialogTheme_Test); 95 96 // Get the context from the builder and attempt to resolve a custom attribute 97 // set on our theme. This way we verify that our theme has been applied to the 98 // builder. 99 final Context themedContext = mBuilder.getContext(); 100 int[] attrs = new int[]{R.attr.themeInteger}; 101 TypedArray ta = themedContext.obtainStyledAttributes(attrs); 102 assertEquals(20, ta.getInt(0, 0)); 103 } 104 105 @Test testSetIconWithParamInt()106 public void testSetIconWithParamInt() { 107 mActivityRule.getScenario().onActivity(activity -> { 108 mDrawable = activity.getResources().getDrawable(android.R.drawable.btn_default); 109 mBuilder = new AlertDialog.Builder(activity); 110 mBuilder.setIcon(android.R.drawable.btn_default); 111 mDialog = mBuilder.show(); 112 }); 113 mInstrumentation.waitForIdleSync(); 114 } 115 116 @Test testSetIconWithParamDrawable()117 public void testSetIconWithParamDrawable() { 118 mActivityRule.getScenario().onActivity(activity -> { 119 mDrawable = activity.getResources().getDrawable(android.R.drawable.btn_default); 120 mBuilder = new AlertDialog.Builder(activity); 121 mBuilder.setIcon(mDrawable); 122 mDialog = mBuilder.show(); 123 }); 124 mInstrumentation.waitForIdleSync(); 125 } 126 127 @Test testSetIconAttribute()128 public void testSetIconAttribute() { 129 mActivityRule.getScenario().onActivity(activity -> { 130 mDrawable = activity.getResources().getDrawable(android.R.drawable.btn_default); 131 mBuilder = new AlertDialog.Builder(activity); 132 mBuilder.setIconAttribute(android.R.attr.alertDialogIcon); 133 mDialog = mBuilder.show(); 134 }); 135 mInstrumentation.waitForIdleSync(); 136 } 137 138 @Test testSetPositiveButtonWithParamInt()139 public void testSetPositiveButtonWithParamInt() { 140 mActivityRule.getScenario().onActivity(activity -> { 141 mBuilder = new AlertDialog.Builder(activity); 142 mBuilder.setPositiveButton(android.R.string.ok, mOnClickListener); 143 mBuilder.setOnDismissListener(mOnDismissListener); 144 mDialog = mBuilder.show(); 145 mButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE); 146 mButton.performClick(); 147 }); 148 mInstrumentation.waitForIdleSync(); 149 150 assertEquals(mDialogActivity.getText(android.R.string.ok), mButton.getText()); 151 verify(mOnClickListener, times(1)).onClick(mDialog, DialogInterface.BUTTON_POSITIVE); 152 verifyNoMoreInteractions(mOnClickListener); 153 // Button click should also dismiss the dialog and notify the listener 154 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 155 verifyNoMoreInteractions(mOnDismissListener); 156 } 157 158 @Test testSetPositiveButtonWithParamCharSequence()159 public void testSetPositiveButtonWithParamCharSequence() { 160 mActivityRule.getScenario().onActivity(activity -> { 161 mBuilder = new AlertDialog.Builder(activity); 162 mBuilder.setPositiveButton(android.R.string.ok, mOnClickListener); 163 mBuilder.setOnDismissListener(mOnDismissListener); 164 mDialog = mBuilder.show(); 165 mButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE); 166 mButton.performClick(); 167 }); 168 mInstrumentation.waitForIdleSync(); 169 assertEquals(mDialogActivity.getText(android.R.string.ok), mButton.getText()); 170 verify(mOnClickListener, times(1)).onClick(mDialog, DialogInterface.BUTTON_POSITIVE); 171 verifyNoMoreInteractions(mOnClickListener); 172 // Button click should also dismiss the dialog and notify the listener 173 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 174 verifyNoMoreInteractions(mOnDismissListener); 175 } 176 177 @Test testSetNegativeButtonWithParamCharSequence()178 public void testSetNegativeButtonWithParamCharSequence() { 179 mActivityRule.getScenario().onActivity(activity -> { 180 mBuilder = new AlertDialog.Builder(activity); 181 mBuilder.setNegativeButton(mTitle, mOnClickListener); 182 mBuilder.setOnDismissListener(mOnDismissListener); 183 mDialog = mBuilder.show(); 184 mButton = mDialog.getButton(DialogInterface.BUTTON_NEGATIVE); 185 mButton.performClick(); 186 }); 187 mInstrumentation.waitForIdleSync(); 188 assertEquals(mTitle, mButton.getText()); 189 verify(mOnClickListener, times(1)).onClick(mDialog, DialogInterface.BUTTON_NEGATIVE); 190 verifyNoMoreInteractions(mOnClickListener); 191 // Button click should also dismiss the dialog and notify the listener 192 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 193 verifyNoMoreInteractions(mOnDismissListener); 194 } 195 196 @Test testSetNegativeButtonWithParamInt()197 public void testSetNegativeButtonWithParamInt() { 198 mActivityRule.getScenario().onActivity(activity -> { 199 mBuilder = new AlertDialog.Builder(activity); 200 mBuilder.setNegativeButton(R.string.notify, mOnClickListener); 201 mBuilder.setOnDismissListener(mOnDismissListener); 202 mDialog = mBuilder.show(); 203 mButton = mDialog.getButton(DialogInterface.BUTTON_NEGATIVE); 204 mButton.performClick(); 205 }); 206 mInstrumentation.waitForIdleSync(); 207 assertEquals(mDialogActivity.getText(R.string.notify), mButton.getText()); 208 verify(mOnClickListener, times(1)).onClick(mDialog, DialogInterface.BUTTON_NEGATIVE); 209 verifyNoMoreInteractions(mOnClickListener); 210 // Button click should also dismiss the dialog and notify the listener 211 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 212 verifyNoMoreInteractions(mOnDismissListener); 213 } 214 215 @Test testSetNeutralButtonWithParamInt()216 public void testSetNeutralButtonWithParamInt() { 217 mActivityRule.getScenario().onActivity(activity -> { 218 mBuilder = new AlertDialog.Builder(activity); 219 mBuilder.setNeutralButton(R.string.notify, mOnClickListener); 220 mBuilder.setOnDismissListener(mOnDismissListener); 221 mDialog = mBuilder.show(); 222 mButton = mDialog.getButton(DialogInterface.BUTTON_NEUTRAL); 223 mButton.performClick(); 224 }); 225 mInstrumentation.waitForIdleSync(); 226 assertEquals(mDialogActivity.getText(R.string.notify), mButton.getText()); 227 verify(mOnClickListener, times(1)).onClick(mDialog, DialogInterface.BUTTON_NEUTRAL); 228 verifyNoMoreInteractions(mOnClickListener); 229 // Button click should also dismiss the dialog and notify the listener 230 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 231 verifyNoMoreInteractions(mOnDismissListener); 232 } 233 234 @Test testSetNeutralButtonWithParamCharSequence()235 public void testSetNeutralButtonWithParamCharSequence() { 236 mActivityRule.getScenario().onActivity(activity -> { 237 mBuilder = new AlertDialog.Builder(activity); 238 mBuilder.setNeutralButton(mTitle, mOnClickListener); 239 mBuilder.setOnDismissListener(mOnDismissListener); 240 mDialog = mBuilder.show(); 241 mButton = mDialog.getButton(DialogInterface.BUTTON_NEUTRAL); 242 mButton.performClick(); 243 }); 244 mInstrumentation.waitForIdleSync(); 245 assertEquals(mTitle, mButton.getText()); 246 verify(mOnClickListener, times(1)).onClick(mDialog, DialogInterface.BUTTON_NEUTRAL); 247 verifyNoMoreInteractions(mOnClickListener); 248 // Button click should also dismiss the dialog and notify the listener 249 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 250 verifyNoMoreInteractions(mOnDismissListener); 251 } 252 testCancelable(final boolean cancelable)253 private void testCancelable(final boolean cancelable) { 254 mActivityRule.getScenario().onActivity(activity -> { 255 mBuilder = new AlertDialog.Builder(activity); 256 mBuilder.setCancelable(cancelable); 257 mDialog = mBuilder.show(); 258 259 }); 260 mInstrumentation.waitForIdleSync(); 261 PollingCheck.waitFor(mDialog::isShowing); 262 sendKeySync(KeyEvent.KEYCODE_BACK); 263 mInstrumentation.waitForIdleSync(); 264 new PollingCheck() { 265 @Override 266 protected boolean check() { 267 boolean showing = mDialog.isShowing(); 268 if (cancelable) { 269 // if the dialog is cancelable, then pressing back 270 // should cancel it. Thus it should not be showing 271 return !showing; 272 } else { 273 // if the dialog is not cancelable, pressing back 274 // should so nothing and it should still be showing 275 return showing; 276 } 277 } 278 }.run(); 279 } 280 281 @Test testSetCancelable()282 public void testSetCancelable() { 283 testCancelable(true); 284 } 285 286 @Test testDisableCancelable()287 public void testDisableCancelable() { 288 testCancelable(false); 289 } 290 291 @Test testSetOnCancelListener()292 public void testSetOnCancelListener() { 293 mActivityRule.getScenario().onActivity(activity -> { 294 mBuilder = new AlertDialog.Builder(activity); 295 mBuilder.setOnCancelListener(mOnCancelListener); 296 mDialog = mBuilder.show(); 297 mDialog.cancel(); 298 }); 299 mInstrumentation.waitForIdleSync(); 300 verify(mOnCancelListener, times(1)).onCancel(mDialog); 301 verifyNoMoreInteractions(mOnCancelListener); 302 } 303 304 @Test testSetOnDismissListener()305 public void testSetOnDismissListener() { 306 mActivityRule.getScenario().onActivity(activity -> { 307 mBuilder = new AlertDialog.Builder(activity); 308 mBuilder.setOnDismissListener(mOnDismissListener); 309 mDialog = mBuilder.show(); 310 mDialog.dismiss(); 311 }); 312 mInstrumentation.waitForIdleSync(); 313 verify(mOnDismissListener, times(1)).onDismiss(mDialog); 314 verifyNoMoreInteractions(mOnDismissListener); 315 } 316 317 @Test testSetOnKeyListener()318 public void testSetOnKeyListener() { 319 mActivityRule.getScenario().onActivity(activity -> { 320 mBuilder = new AlertDialog.Builder(activity); 321 mBuilder.setOnKeyListener(mOnKeyListener); 322 mDialog = mBuilder.show(); 323 }); 324 mInstrumentation.waitForIdleSync(); 325 sendKeySync(KeyEvent.KEYCODE_0); 326 sendKeySync(KeyEvent.KEYCODE_1); 327 mInstrumentation.waitForIdleSync(); 328 // Use Mockito captures so that we can verify that each "sent" key code resulted 329 // in one DOWN event and one UP event. 330 ArgumentCaptor<KeyEvent> keyEvent0Captor = ArgumentCaptor.forClass(KeyEvent.class); 331 ArgumentCaptor<KeyEvent> keyEvent1Captor = ArgumentCaptor.forClass(KeyEvent.class); 332 verify(mOnKeyListener, times(2)).onKey(eq(mDialog), eq(KeyEvent.KEYCODE_0), 333 keyEvent0Captor.capture()); 334 verify(mOnKeyListener, times(2)).onKey(eq(mDialog), eq(KeyEvent.KEYCODE_1), 335 keyEvent1Captor.capture()); 336 verifyNoMoreInteractions(mOnKeyListener); 337 assertEquals(KeyEvent.ACTION_DOWN, keyEvent0Captor.getAllValues().get(0).getAction()); 338 assertEquals(KeyEvent.ACTION_UP, keyEvent0Captor.getAllValues().get(1).getAction()); 339 assertEquals(KeyEvent.ACTION_DOWN, keyEvent1Captor.getAllValues().get(0).getAction()); 340 assertEquals(KeyEvent.ACTION_UP, keyEvent1Captor.getAllValues().get(1).getAction()); 341 } 342 343 @Test testSetItemsWithParamInt()344 public void testSetItemsWithParamInt() { 345 mActivityRule.getScenario().onActivity(activity -> { 346 mBuilder = new AlertDialog.Builder(activity); 347 mBuilder.setItems(R.array.difficultyLevel, mOnClickListener); 348 mDialog = mBuilder.show(); 349 mListView = mDialog.getListView(); 350 }); 351 mInstrumentation.waitForIdleSync(); 352 353 final CharSequence[] levels = mDialogActivity.getResources().getTextArray( 354 R.array.difficultyLevel); 355 assertEquals(levels[0], mListView.getItemAtPosition(0)); 356 } 357 358 @Test testSetItemsWithParamCharSequence()359 public void testSetItemsWithParamCharSequence() { 360 final CharSequence[] expect = mDialogActivity.getResources().getTextArray( 361 R.array.difficultyLevel); 362 363 mActivityRule.getScenario().onActivity(activity -> { 364 mBuilder = new AlertDialog.Builder(activity); 365 mBuilder.setItems(expect, mOnClickListener); 366 mDialog = mBuilder.show(); 367 mListView = mDialog.getListView(); 368 }); 369 mInstrumentation.waitForIdleSync(); 370 assertEquals(expect[0], mListView.getItemAtPosition(0)); 371 } 372 373 @Test testSetAdapter()374 public void testSetAdapter() { 375 final ListAdapter adapter = new AdapterTest(); 376 mActivityRule.getScenario().onActivity(activity -> { 377 mBuilder = new AlertDialog.Builder(activity); 378 mBuilder.setAdapter(adapter, mOnClickListener); 379 mDialog = mBuilder.show(); 380 mListView = mDialog.getListView(); 381 }); 382 mInstrumentation.waitForIdleSync(); 383 assertEquals(adapter, mListView.getAdapter()); 384 } 385 386 @Test testSetMultiChoiceItemsWithParamInt()387 public void testSetMultiChoiceItemsWithParamInt() { 388 final CharSequence[] items = mDialogActivity.getResources().getTextArray( 389 R.array.difficultyLevel); 390 391 mActivityRule.getScenario().onActivity(activity -> { 392 mBuilder = new AlertDialog.Builder(activity); 393 mBuilder.setMultiChoiceItems(R.array.difficultyLevel, null, 394 mOnMultiChoiceClickListener); 395 mDialog = mBuilder.show(); 396 mListView = mDialog.getListView(); 397 }); 398 if (mListView.isInTouchMode()) { 399 reAttachListViewAdapter(mListView); 400 } 401 mActivityRule.getScenario().onActivity(unused -> { 402 mSelectedItem = (CharSequence) mListView.getSelectedItem(); 403 mListView.performItemClick(null, 0, 0); 404 mListView.performItemClick(null, 1, 0); 405 }); 406 mInstrumentation.waitForIdleSync(); 407 assertEquals(items[0], mSelectedItem); 408 verify(mOnMultiChoiceClickListener, times(1)).onClick(mDialog, 0, true); 409 verify(mOnMultiChoiceClickListener, times(1)).onClick(mDialog, 1, true); 410 verifyNoMoreInteractions(mOnMultiChoiceClickListener); 411 assertEquals(items[0], mListView.getItemAtPosition(0)); 412 } 413 414 @Test testSetMultiChoiceItemsWithParamCharSequence()415 public void testSetMultiChoiceItemsWithParamCharSequence() { 416 final CharSequence[] items = mDialogActivity.getResources().getTextArray( 417 R.array.difficultyLevel); 418 419 mActivityRule.getScenario().onActivity(activity -> { 420 mBuilder = new AlertDialog.Builder(activity); 421 mBuilder.setMultiChoiceItems(items, null, mOnMultiChoiceClickListener); 422 mDialog = mBuilder.show(); 423 mListView = mDialog.getListView(); 424 }); 425 if (mListView.isInTouchMode()) { 426 reAttachListViewAdapter(mListView); 427 } 428 mActivityRule.getScenario().onActivity(unused -> { 429 mSelectedItem = (CharSequence) mListView.getSelectedItem(); 430 mListView.performItemClick(null, 0, 0); 431 mListView.performItemClick(null, 1, 0); 432 }); 433 mInstrumentation.waitForIdleSync(); 434 assertEquals(items[0], mSelectedItem); 435 verify(mOnMultiChoiceClickListener, times(1)).onClick(mDialog, 0, true); 436 verify(mOnMultiChoiceClickListener, times(1)).onClick(mDialog, 1, true); 437 verifyNoMoreInteractions(mOnMultiChoiceClickListener); 438 assertEquals(items[0], mListView.getItemAtPosition(0)); 439 } 440 441 @Test testSetSingleChoiceItemsWithParamInt()442 public void testSetSingleChoiceItemsWithParamInt() { 443 final CharSequence[] items = mDialogActivity.getResources().getTextArray( 444 R.array.difficultyLevel); 445 446 mActivityRule.getScenario().onActivity(activity -> { 447 mBuilder = new AlertDialog.Builder(mDialogActivity); 448 mBuilder.setSingleChoiceItems(R.array.difficultyLevel, 0, 449 mOnClickListener); 450 mDialog = mBuilder.show(); 451 mListView = mDialog.getListView(); 452 }); 453 if (mListView.isInTouchMode()) { 454 reAttachListViewAdapter(mListView); 455 } 456 mActivityRule.getScenario().onActivity(unused -> { 457 mSelectedItem = (CharSequence) mListView.getSelectedItem(); 458 mListView.performItemClick(null, 0, 0); 459 }); 460 461 mInstrumentation.waitForIdleSync(); 462 assertEquals(items[0], mSelectedItem); 463 assertEquals(items[0], mListView.getItemAtPosition(0)); 464 verify(mOnClickListener, times(1)).onClick(mDialog, 0); 465 verifyNoMoreInteractions(mOnClickListener); 466 } 467 468 @Test testSetSingleChoiceItemsWithParamCharSequence()469 public void testSetSingleChoiceItemsWithParamCharSequence() { 470 final CharSequence[] items = mDialogActivity.getResources().getTextArray( 471 R.array.difficultyLevel); 472 473 mActivityRule.getScenario().onActivity(activity -> { 474 mBuilder = new AlertDialog.Builder(mDialogActivity); 475 mBuilder.setSingleChoiceItems(items, 0, mOnClickListener); 476 mDialog = mBuilder.show(); 477 mListView = mDialog.getListView(); 478 479 }); 480 if (mListView.isInTouchMode()) { 481 reAttachListViewAdapter(mListView); 482 } 483 mActivityRule.getScenario().onActivity(unused -> { 484 mSelectedItem = (CharSequence) mListView.getSelectedItem(); 485 mListView.performItemClick(null, 0, 0); 486 }); 487 mInstrumentation.waitForIdleSync(); 488 assertEquals(items[0], mSelectedItem); 489 assertEquals(items[0], mListView.getItemAtPosition(0)); 490 verify(mOnClickListener, times(1)).onClick(mDialog, 0); 491 verifyNoMoreInteractions(mOnClickListener); 492 } 493 494 @Test testSetSingleChoiceItems()495 public void testSetSingleChoiceItems() { 496 final CharSequence[] items = mDialogActivity.getResources().getTextArray( 497 R.array.difficultyLevel); 498 499 mActivityRule.getScenario().onActivity(activity -> { 500 mBuilder = new AlertDialog.Builder(activity); 501 mBuilder.setSingleChoiceItems(new ArrayAdapter<>(activity, 502 android.R.layout.select_dialog_singlechoice, android.R.id.text1, 503 items), 0, 504 mOnClickListener); 505 mDialog = mBuilder.show(); 506 mListView = mDialog.getListView(); 507 }); 508 if (mListView.isInTouchMode()) { 509 reAttachListViewAdapter(mListView); 510 } 511 mActivityRule.getScenario().onActivity(unused -> { 512 mSelectedItem = (CharSequence) mListView.getSelectedItem(); 513 mListView.performItemClick(null, 0, 0); 514 }); 515 mInstrumentation.waitForIdleSync(); 516 assertEquals(items[0], mSelectedItem); 517 assertEquals(items[0], mListView.getItemAtPosition(0)); 518 verify(mOnClickListener, times(1)).onClick(mDialog, 0); 519 verifyNoMoreInteractions(mOnClickListener); 520 } 521 522 @Test testSetOnItemSelectedListener()523 public void testSetOnItemSelectedListener() { 524 mActivityRule.getScenario().onActivity(activity -> { 525 mBuilder = new AlertDialog.Builder(activity); 526 mBuilder.setOnItemSelectedListener(mOnItemSelectedListener); 527 mBuilder.setItems(R.array.difficultyLevel, mOnClickListener); 528 mDialog = mBuilder.show(); 529 mListView = mDialog.getListView(); 530 mListView.pointToPosition(0, 0); 531 }); 532 mInstrumentation.waitForIdleSync(); 533 verify(mOnItemSelectedListener, times(1)).onItemSelected(eq(mListView), any(View.class), 534 eq(0), any(Long.class)); 535 verifyNoMoreInteractions(mOnItemSelectedListener); 536 } 537 538 @Test testSetView()539 public void testSetView() { 540 final View view = new View(mDialogActivity); 541 view.setId(100); 542 mActivityRule.getScenario().onActivity(activity -> { 543 mBuilder = new AlertDialog.Builder(activity); 544 mBuilder.setView(view); 545 mDialog = mBuilder.show(); 546 mView = mDialog.getWindow().findViewById(100); 547 }); 548 mInstrumentation.waitForIdleSync(); 549 assertEquals(view, mView); 550 } 551 552 @Test testSetViewFromInflater()553 public void testSetViewFromInflater() { 554 mActivityRule.getScenario().onActivity(activity -> { 555 mBuilder = new AlertDialog.Builder(activity); 556 mBuilder.setView(LayoutInflater.from(mBuilder.getContext()).inflate( 557 R.layout.alert_dialog_text_entry_2, null, false)); 558 mDialog = mBuilder.show(); 559 mView = mDialog.getWindow().findViewById(R.id.username_form); 560 }); 561 mInstrumentation.waitForIdleSync(); 562 assertNotNull(mView); 563 assertNotNull(mView.findViewById(R.id.username_view)); 564 assertNotNull(mView.findViewById(R.id.username_edit)); 565 } 566 567 @Test testSetViewById()568 public void testSetViewById() { 569 mActivityRule.getScenario().onActivity(activity -> { 570 mBuilder = new AlertDialog.Builder(activity); 571 mBuilder.setView(R.layout.alert_dialog_text_entry_2); 572 mDialog = mBuilder.show(); 573 mView = mDialog.getWindow().findViewById(R.id.username_form); 574 }); 575 mInstrumentation.waitForIdleSync(); 576 assertNotNull(mView); 577 assertNotNull(mView.findViewById(R.id.username_view)); 578 assertNotNull(mView.findViewById(R.id.username_edit)); 579 } 580 581 @Test testSetCustomTitle()582 public void testSetCustomTitle() { 583 mActivityRule.getScenario().onActivity(activity -> { 584 mBuilder = new AlertDialog.Builder(activity); 585 mBuilder.setCustomTitle(LayoutInflater.from(mBuilder.getContext()).inflate( 586 R.layout.alertdialog_custom_title, null, false)); 587 mDialog = mBuilder.show(); 588 }); 589 mInstrumentation.waitForIdleSync(); 590 } 591 592 @Test testSetInverseBackgroundForced()593 public void testSetInverseBackgroundForced() { 594 mActivityRule.getScenario().onActivity(activity -> { 595 mBuilder = new AlertDialog.Builder(activity); 596 mBuilder.setInverseBackgroundForced(true); 597 mDialog = mBuilder.create(); 598 mDialog.show(); 599 }); 600 mInstrumentation.waitForIdleSync(); 601 } 602 603 @Test testCreate()604 public void testCreate() { 605 mActivityRule.getScenario().onActivity(activity -> { 606 mBuilder = new AlertDialog.Builder(activity); 607 mDialog = mBuilder.create(); 608 mDialog.show(); 609 }); 610 mInstrumentation.waitForIdleSync(); 611 assertNotNull(mDialog); 612 assertTrue(mDialog.isShowing()); 613 } 614 615 @Test testShow()616 public void testShow() { 617 mActivityRule.getScenario().onActivity(activity -> { 618 mBuilder = new AlertDialog.Builder(activity); 619 mDialog = mBuilder.show(); 620 }); 621 mInstrumentation.waitForIdleSync(); 622 assertTrue(mDialog.isShowing()); 623 } 624 sendKeySync(int keyCode)625 private void sendKeySync(int keyCode) { 626 final long downTime = SystemClock.uptimeMillis(); 627 final KeyEvent downEvent = 628 new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0); 629 mInstrumentation.getUiAutomation().injectInputEvent(downEvent, true /*sync*/); 630 631 final KeyEvent upEvent = 632 new KeyEvent(downTime, SystemClock.uptimeMillis(), KeyEvent.ACTION_UP, keyCode, 0); 633 mInstrumentation.getUiAutomation().injectInputEvent(upEvent, true /*sync*/); 634 } 635 636 private static class AdapterTest implements android.widget.ListAdapter { areAllItemsEnabled()637 public boolean areAllItemsEnabled() { 638 return true; 639 } 640 isEnabled(int position)641 public boolean isEnabled(int position) { 642 return false; 643 } 644 getCount()645 public int getCount() { 646 return 0; 647 } 648 getItem(int position)649 public Object getItem(int position) { 650 return null; 651 } 652 getItemId(int position)653 public long getItemId(int position) { 654 return 0; 655 } 656 getItemViewType(int position)657 public int getItemViewType(int position) { 658 return 0; 659 } 660 getView(int position, android.view.View convertView, android.view.ViewGroup parent)661 public android.view.View getView(int position, 662 android.view.View convertView, 663 android.view.ViewGroup parent) { 664 return null; 665 } 666 getViewTypeCount()667 public int getViewTypeCount() { 668 return 1; 669 } 670 hasStableIds()671 public boolean hasStableIds() { 672 return false; 673 } 674 isEmpty()675 public boolean isEmpty() { 676 return true; 677 } 678 registerDataSetObserver( android.database.DataSetObserver observer)679 public void registerDataSetObserver( 680 android.database.DataSetObserver observer) { 681 } 682 unregisterDataSetObserver( android.database.DataSetObserver observer)683 public void unregisterDataSetObserver( 684 android.database.DataSetObserver observer) { 685 } 686 } 687 } 688