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