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 private AlertDialog realAlertDialog; 19 20 private CharSequence[] items; 21 private DialogInterface.OnClickListener clickListener; 22 private boolean isMultiItem; 23 private boolean isSingleItem; 24 private DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener; 25 private FrameLayout custom; 26 27 private static ShadowAlertDialog latestAlertDialog; 28 29 /** 30 * @return the most recently created {@code AlertDialog}, or null if none has been created during 31 * 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- 52 * and single-choice dialogs, tracks which items are currently checked and calls listeners 53 * appropriately. 54 * 55 * @param index the index of the item to click on 56 */ clickOnItem(int index)57 public void clickOnItem(int index) { 58 ShadowListView shadowListView = Shadow.extract(realAlertDialog.getListView()); 59 shadowListView.performItemClick(index); 60 } 61 62 @Override getTitle()63 public CharSequence getTitle() { 64 return getShadowAlertController().getTitle(); 65 } 66 67 /** 68 * @return the items that are available to be clicked on 69 */ getItems()70 public CharSequence[] getItems() { 71 Adapter adapter = getShadowAlertController().getAdapter(); 72 int count = adapter.getCount(); 73 CharSequence[] items = new CharSequence[count]; 74 for (int i = 0; i < items.length; i++) { 75 items[i] = (CharSequence) adapter.getItem(i); 76 } 77 return items; 78 } 79 getAdapter()80 public Adapter getAdapter() { 81 return getShadowAlertController().getAdapter(); 82 } 83 84 /** 85 * @return the message displayed in the dialog 86 */ getMessage()87 public CharSequence getMessage() { 88 return getShadowAlertController().getMessage(); 89 } 90 91 @Override show()92 public void show() { 93 super.show(); 94 latestAlertDialog = this; 95 } 96 97 /** 98 * @return the view set with {@link AlertDialog.Builder#setView(View)} 99 */ getView()100 public View getView() { 101 return getShadowAlertController().getView(); 102 } 103 104 /** 105 * @return the icon set with {@link AlertDialog.Builder#setIcon(int)} 106 */ getIconId()107 public int getIconId() { 108 return getShadowAlertController().getIconId(); 109 } 110 111 /** 112 * @return return the view set with {@link AlertDialog.Builder#setCustomTitle(View)} 113 */ getCustomTitleView()114 public View getCustomTitleView() { 115 return getShadowAlertController().getCustomTitleView(); 116 } 117 getShadowAlertController()118 private ShadowAlertController getShadowAlertController() { 119 AlertController alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert"); 120 return (ShadowAlertController) Shadow.extract(alertController); 121 } 122 123 @Implements(AlertDialog.Builder.class) 124 public static class ShadowBuilder {} 125 } 126