1 package org.robolectric.shadows; 2 3 import static org.robolectric.Shadows.shadowOf; 4 5 import android.app.AlertDialog; 6 import android.content.DialogInterface; 7 import android.view.View; 8 import android.widget.Adapter; 9 import android.widget.FrameLayout; 10 import com.android.internal.app.AlertController; 11 import org.robolectric.RuntimeEnvironment; 12 import org.robolectric.Shadows; 13 import org.robolectric.annotation.Implementation; 14 import org.robolectric.annotation.Implements; 15 import org.robolectric.annotation.RealObject; 16 import org.robolectric.shadow.api.Shadow; 17 import org.robolectric.util.ReflectionHelpers; 18 19 @SuppressWarnings({"UnusedDeclaration"}) 20 @Implements(AlertDialog.class) 21 public class ShadowAlertDialog extends ShadowDialog { 22 @RealObject 23 private AlertDialog realAlertDialog; 24 25 private CharSequence[] items; 26 private DialogInterface.OnClickListener clickListener; 27 private boolean isMultiItem; 28 private boolean isSingleItem; 29 private DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener; 30 private FrameLayout custom; 31 32 /** 33 * @return the most recently created {@code AlertDialog}, or null if none has been created during this test run 34 */ 35 public static AlertDialog getLatestAlertDialog() { 36 ShadowAlertDialog dialog = shadowOf(RuntimeEnvironment.application).getLatestAlertDialog(); 37 return dialog == null ? null : dialog.realAlertDialog; 38 } 39 40 public FrameLayout getCustomView() { 41 if (custom == null) { 42 custom = new FrameLayout(context); 43 } 44 return custom; 45 } 46 47 /** 48 * Resets the tracking of the most recently created {@code AlertDialog} 49 */ 50 public static void reset() { 51 shadowOf(RuntimeEnvironment.application).setLatestAlertDialog(null); 52 } 53 54 /** 55 * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently 56 * checked and calls listeners appropriately. 57 * 58 * @param index the index of the item to click on 59 */ 60 public void clickOnItem(int index) { 61 Shadows.shadowOf(realAlertDialog.getListView()).performItemClick(index); 62 } 63 64 @Override public CharSequence getTitle() { 65 return getShadowAlertController().getTitle(); 66 } 67 68 /** 69 * @return the items that are available to be clicked on 70 */ 71 public CharSequence[] getItems() { 72 Adapter adapter = getShadowAlertController().getAdapter(); 73 int count = adapter.getCount(); 74 CharSequence[] items = new CharSequence[count]; 75 for (int i = 0; i < items.length; i++) { 76 items[i] = (CharSequence) adapter.getItem(i); 77 } 78 return items; 79 } 80 81 public Adapter getAdapter() { 82 return getShadowAlertController().getAdapter(); 83 } 84 85 /** 86 * @return the message displayed in the dialog 87 */ 88 public CharSequence getMessage() { 89 return getShadowAlertController().getMessage(); 90 } 91 92 @Override @Implementation 93 public void show() { 94 super.show(); 95 shadowOf(RuntimeEnvironment.application).setLatestAlertDialog(this); 96 } 97 98 /** 99 * @return return the view set with {@link AlertDialog.Builder#setView(View)} 100 */ 101 public View getView() { 102 return getShadowAlertController().getView(); 103 } 104 105 /** 106 * @return return the view set with {@link AlertDialog.Builder#setCustomTitle(View)} 107 */ 108 public View getCustomTitleView() { 109 return getShadowAlertController().getCustomTitleView(); 110 } 111 112 public ShadowAlertController getShadowAlertController() { 113 AlertController alert = ReflectionHelpers.getField(realAlertDialog, "mAlert"); 114 return (ShadowAlertController) Shadow.extract(alert); 115 } 116 117 @Implements(AlertDialog.Builder.class) 118 public static class ShadowBuilder { 119 } 120 } 121