
org.robolectric.shadows.ShadowAlertDialog Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shadows-framework Show documentation
Show all versions of shadows-framework Show documentation
An alternative Android testing framework.
The newest version!
package org.robolectric.shadows;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Adapter;
import android.widget.FrameLayout;
import com.android.internal.app.AlertController;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
@SuppressWarnings({"UnusedDeclaration"})
@Implements(AlertDialog.class)
public class ShadowAlertDialog extends ShadowDialog {
@RealObject private AlertDialog realAlertDialog;
private CharSequence[] items;
private DialogInterface.OnClickListener clickListener;
private boolean isMultiItem;
private boolean isSingleItem;
private DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener;
private FrameLayout custom;
private static ShadowAlertDialog latestAlertDialog;
/**
* @return the most recently created {@code AlertDialog}, or null if none has been created during
* this test run
*/
public static AlertDialog getLatestAlertDialog() {
return latestAlertDialog == null ? null : latestAlertDialog.realAlertDialog;
}
public FrameLayout getCustomView() {
if (custom == null) {
custom = new FrameLayout(context);
}
return custom;
}
/** Resets the tracking of the most recently created {@code AlertDialog} */
@Resetter
public static void reset() {
latestAlertDialog = null;
}
/**
* Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi-
* and single-choice dialogs, tracks which items are currently checked and calls listeners
* appropriately.
*
* @param index the index of the item to click on
*/
public void clickOnItem(int index) {
ShadowListView shadowListView = Shadow.extract(realAlertDialog.getListView());
shadowListView.performItemClick(index);
}
@Override
public CharSequence getTitle() {
return getShadowAlertController().getTitle();
}
/**
* @return the items that are available to be clicked on
*/
public CharSequence[] getItems() {
Adapter adapter = getShadowAlertController().getAdapter();
int count = adapter.getCount();
CharSequence[] items = new CharSequence[count];
for (int i = 0; i < items.length; i++) {
items[i] = (CharSequence) adapter.getItem(i);
}
return items;
}
public Adapter getAdapter() {
return getShadowAlertController().getAdapter();
}
/**
* @return the message displayed in the dialog
*/
public CharSequence getMessage() {
return getShadowAlertController().getMessage();
}
@Override
public void show() {
super.show();
latestAlertDialog = this;
}
/**
* @return the view set with {@link AlertDialog.Builder#setView(View)}
*/
public View getView() {
return getShadowAlertController().getView();
}
/**
* @return the icon set with {@link AlertDialog.Builder#setIcon(int)}
*/
public int getIconId() {
return getShadowAlertController().getIconId();
}
/**
* @return return the view set with {@link AlertDialog.Builder#setCustomTitle(View)}
*/
public View getCustomTitleView() {
return getShadowAlertController().getCustomTitleView();
}
private ShadowAlertController getShadowAlertController() {
AlertController alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
return (ShadowAlertController) Shadow.extract(alertController);
}
@Implements(AlertDialog.Builder.class)
public static class ShadowBuilder {}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy