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.RuntimeEnvironment; 10 import org.robolectric.annotation.Implements; 11 import org.robolectric.annotation.RealObject; 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 /** 29 * @return the most recently created {@code AlertDialog}, or null if none has been created during this test run 30 */ getLatestAlertDialog()31 public static AlertDialog getLatestAlertDialog() { 32 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 33 ShadowAlertDialog dialog = shadowApplication.getLatestAlertDialog(); 34 return dialog == null ? null : dialog.realAlertDialog; 35 } 36 getCustomView()37 public FrameLayout getCustomView() { 38 if (custom == null) { 39 custom = new FrameLayout(context); 40 } 41 return custom; 42 } 43 44 /** 45 * Resets the tracking of the most recently created {@code AlertDialog} 46 */ reset()47 public static void reset() { 48 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 49 shadowApplication.setLatestAlertDialog(null); 50 } 51 52 /** 53 * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently 54 * checked and calls listeners appropriately. 55 * 56 * @param index the index of the item to click on 57 */ clickOnItem(int index)58 public void clickOnItem(int index) { 59 ShadowListView shadowListView = Shadow.extract(realAlertDialog.getListView()); 60 shadowListView.performItemClick(index); 61 } 62 getTitle()63 @Override 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 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 95 shadowApplication.setLatestAlertDialog(this); 96 } 97 98 /** 99 * @return the view set with {@link AlertDialog.Builder#setView(View)} 100 */ getView()101 public View getView() { 102 return getShadowAlertController().getView(); 103 } 104 105 /** 106 * @return the icon set with {@link AlertDialog.Builder#setIcon(int)} 107 */ getIconId()108 public int getIconId() { 109 return getShadowAlertController().getIconId(); 110 } 111 112 /** 113 * @return return the view set with {@link AlertDialog.Builder#setCustomTitle(View)} 114 */ getCustomTitleView()115 public View getCustomTitleView() { 116 return getShadowAlertController().getCustomTitleView(); 117 } 118 getShadowAlertController()119 private ShadowAlertController getShadowAlertController() { 120 AlertController alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert"); 121 return (ShadowAlertController) Shadow.extract(alertController); 122 } 123 124 @Implements(AlertDialog.Builder.class) 125 public static class ShadowBuilder { 126 } 127 } 128