Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric InflateException when using v7 support library AlertDialog

Using a normal android.app.AlertDialog works with ShadowAlertDialog.getLatestAlertDialog(), but if you use a support library android.support.v7.app.AlertDialog, then this exception happens:

android.view.InflateException: XML file app/build/intermediates/res/qa/debug/layout/abc_alert_dialog_material.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.internal.widget.DialogTitle
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
    at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:75)
    at android.support.v7.app.AlertController.installContent(AlertController.java:216)
    at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
    at android.app.Dialog.show(Dialog.java:262)
    at org.robolectric.shadows.ShadowDialog.show(ShadowDialog.java:65)
    at android.app.Dialog.show(Dialog.java)
<snip>
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -9
    at java.lang.String.substring(String.java:1955)
    at org.robolectric.res.ResName.qualifyResName(ResName.java:51)
    at org.robolectric.res.Attribute.getStyleReference(Attribute.java:147)
    at org.robolectric.res.builder.XmlFileBuilder$XmlResourceParserImpl.getResourceId(XmlFileBuilder.java:789)

This is a known issue, but what's the best way to workaround it?
https://github.com/robolectric/robolectric/issues/1736

like image 721
Dan J Avatar asked Aug 07 '15 01:08

Dan J


2 Answers

I use a static utility to build my AlertDialog objects, and I build a android.app.AlertDialog one when I detect Robolectric on the classpath. Here's my code:

private static Boolean isRobolectricTest = null;

/**
 * Determines if we are running inside of Robolectric or not.
 */
public static boolean isARobolectricUnitTest() {
    if (isRobolectricTest == null) {
        try {
            Class.forName("org.robolectric.Robolectric");
            isRobolectricTest = true;
        } catch (ClassNotFoundException e) {
            isRobolectricTest = false;
        }
    }
    return isRobolectricTest;
}

/**
 * This utility helps us to workaround a Robolectric issue that causes our unit tests to fail
 * when an Activity/Fragment creates an AlertDialog using the v7 support library.  The
 * workaround is to use the normal android.app.AlertDialog when running Robolectric tests.
 *
 * The Robolectric bug is: https://github.com/robolectric/robolectric/issues/1736
 *   android.view.InflateException: XML file app/build/intermediates/res/qa/debug/layout/abc_alert_dialog_material.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.internal.widget.DialogTitle
 */
public static DialogInterface createAndShowDialog(Context context,
                                                  @StringRes int titleResId,
                                                  String message,
                                                  @StringRes int negativeTextResId,
                                                  DialogInterface.OnClickListener negativeClickListener,
                                                  @StringRes int neutralTextResId,
                                                  DialogInterface.OnClickListener neutralClickListener,
                                                  @StringRes int positiveTextResId,
                                                  DialogInterface.OnClickListener positiveClickListener,
                                                  boolean cancelable) {
    if (isARobolectricUnitTest()) {
        return UiUtils.createDialog(context, titleResId, message, negativeTextResId, negativeClickListener, neutralTextResId, neutralClickListener, positiveTextResId, positiveClickListener, cancelable);
    } else {
        return UiUtils.createDialogSupportV7(context, titleResId, message, negativeTextResId, negativeClickListener, neutralTextResId, neutralClickListener, positiveTextResId, positiveClickListener, cancelable);
    }
}

private static android.app.AlertDialog createDialog(Context context,
                                                    @StringRes int titleResId,
                                                    String message,
                                                    @StringRes int negativeTextResId,
                                                    DialogInterface.OnClickListener negativeClickListener,
                                                    @StringRes int neutralTextResId,
                                                    DialogInterface.OnClickListener neutralClickListener,
                                                    @StringRes int positiveTextResId,
                                                    DialogInterface.OnClickListener positiveClickListener,
                                                    boolean cancelable) {
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
    builder.setTitle(titleResId);
    builder.setMessage(message);
    builder.setNegativeButton(negativeTextResId, negativeClickListener);

    if ((neutralTextResId != -1) && (neutralClickListener != null)) {
        builder.setNeutralButton(neutralTextResId, neutralClickListener);
    }

    builder.setPositiveButton(positiveTextResId, positiveClickListener);
    builder.setCancelable(cancelable);

    android.app.AlertDialog alertDialog = builder.create();
    alertDialog.show();

    return alertDialog;
}

private static android.support.v7.app.AlertDialog createDialogSupportV7(Context context,
                                                                        @StringRes int titleResId,
                                                                        String message,
                                                                        @StringRes int negativeTextResId,
                                                                        DialogInterface.OnClickListener negativeClickListener,
                                                                        @StringRes int neutralTextResId,
                                                                        DialogInterface.OnClickListener neutralClickListener,
                                                                        @StringRes int positiveTextResId,
                                                                        DialogInterface.OnClickListener positiveClickListener,
                                                                        boolean cancelable) {
    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
    builder.setTitle(titleResId);
    builder.setMessage(message);
    builder.setNegativeButton(negativeTextResId, negativeClickListener);

    if ((neutralTextResId != -1) && (neutralClickListener != null)) {
        builder.setNeutralButton(neutralTextResId, neutralClickListener);
    }

    builder.setPositiveButton(positiveTextResId, positiveClickListener);
    builder.setCancelable(cancelable);

    android.support.v7.app.AlertDialog alertDialog = builder.create();
    alertDialog.show();

    return alertDialog;
}

It's not ideal, but it does the job, and it's easy to remove the hack later when the Robolectric issue is fixed.

like image 74
Dan J Avatar answered Nov 14 '22 13:11

Dan J


I ported the existing shadows and the stuff I needed to use worked for me

@Suppress("unused")
@Implements(AlertDialog::class)
open class ShadowAlertDialog : ShadowDialog() {

    @RealObject
    private lateinit var realAlertDialog: AlertDialog

    private val items: Array<CharSequence>? = null
    private val clickListener: DialogInterface.OnClickListener? = null
    private val isMultiItem: Boolean = false
    private val isSingleItem: Boolean = false
    private val multiChoiceClickListener: DialogInterface.OnMultiChoiceClickListener? = null

    private var custom: FrameLayout? = null

    val customView: FrameLayout
        get() = custom ?: FrameLayout(realAlertDialog.context).apply { custom = this }

    val adapter: Adapter?
        get() = shadowAlertController.adapter

    /**
     * @return the message displayed in the dialog
     */
    open val message: CharSequence
        get() = shadowAlertController.getMessage()

    /**
     * @return the view set with [AlertDialog.Builder.setView]
     */
    val view: View?
        get() = shadowAlertController.view

    /**
     * @return the icon set with [AlertDialog.Builder.setIcon]
     */
    val iconId: Int
        get() = shadowAlertController.iconId

    /**
     * @return return the view set with [AlertDialog.Builder.setCustomTitle]
     */
    val customTitleView: View?
        get() = shadowAlertController.customTitleView

    private val shadowAlertController: ShadowAlertController
        get() {
            val alertController = ReflectionHelpers.getField<Any>(realAlertDialog, "mAlert")
            return Shadow.extract<ShadowAlertController>(alertController)
        }

    /**
     * Simulates a click on the `Dialog` item indicated by `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
     */
    fun clickOnItem(index: Int) {
        val shadowListView = Shadow.extract<ShadowListView>(realAlertDialog.listView)
        shadowListView.performItemClick(index)
    }

    override fun getTitle(): CharSequence {
        return shadowAlertController.getTitle()
    }

    /**
     * @return the items that are available to be clicked on
     */
    fun getItems(): Array<CharSequence>? {
        val adapter = shadowAlertController.adapter ?: return null
        return Array(adapter.count) { adapter.getItem(it) as CharSequence }
    }

    public override fun show() {
        super.show()
        latestShadowAlertDialog = this
    }

    @Implements(AlertDialog.Builder::class)
    class ShadowBuilder

    companion object {

        private var latestShadowAlertDialog: ShadowAlertDialog? = null

        /**
         * @return the most recently created `AlertDialog`, or null if none has been created during this test run
         */
        val latestAlertDialog: AlertDialog?
            get() = latestShadowAlertDialog?.realAlertDialog

        /**
         * Resets the tracking of the most recently created `AlertDialog`
         */
        fun reset() {
            latestShadowAlertDialog = null
        }
    }
}

@Suppress("unused")
@Implements(className = ShadowAlertController.clazzName, isInAndroidSdk = false)
class ShadowAlertController {

    companion object {

        const val clazzName = "androidx.appcompat.app.AlertController"
    }

    @RealObject
    private lateinit var realAlertController: Any

    private var title: CharSequence? = null
    private var message: CharSequence? = null

    var view: View? = null
        @Implementation
        set(view) {
            field = view
            directlyOn<Any>(realAlertController, clazzName, "setView", ReflectionHelpers.ClassParameter(View::class.java, view))
        }

    var customTitleView: View? = null
        private set

    var iconId: Int = 0
        private set

    val adapter: Adapter?
        get() = ReflectionHelpers.callInstanceMethod<ListView>(realAlertController, "getListView").adapter

    @Implementation
    @Throws(InvocationTargetException::class, IllegalAccessException::class)
    fun setTitle(title: CharSequence) {
        this.title = title
        directlyOn<Any>(realAlertController, clazzName, "setTitle", ReflectionHelpers.ClassParameter(CharSequence::class.java, title))
    }

    fun getTitle(): CharSequence = title ?: ""

    @Implementation
    fun setCustomTitle(customTitleView: View) {
        this.customTitleView = customTitleView
        directlyOn<Any>(realAlertController, clazzName, "setCustomTitle", ReflectionHelpers.ClassParameter(View::class.java, customTitleView))
    }

    @Implementation
    fun setMessage(message: CharSequence) {
        this.message = message
        directlyOn<Any>(realAlertController, clazzName, "setMessage", ReflectionHelpers.ClassParameter(CharSequence::class.java, message))
    }

    fun getMessage(): CharSequence = message ?: ""

    @Implementation(minSdk = LOLLIPOP)
    fun setView(resourceId: Int) {
        view = LayoutInflater.from(ApplicationProvider.getApplicationContext()).inflate(resourceId, null)
    }

    @Implementation
    fun setIcon(iconId: Int) {
        this.iconId = iconId
        directlyOn<Any>(realAlertController, clazzName, "setIcon", ReflectionHelpers.ClassParameter(Int::class.java, iconId))
    }
}

Register the shadows for your tests like this

@RunWith(RobolectricTestRunner::class)
@Config(
    shadows = [ShadowAlertDialog::class, ShadowAlertController::class, ...],
)

And use these in your tests like follows

val dialog = ShadowAlertDialog.latestAlertDialog!!
val shadowDialog = Shadow.extract<ShadowAlertDialog(dialog)
like image 1
jturgeon90 Avatar answered Nov 14 '22 14:11

jturgeon90