emilsjolander / StickyListHeaders

An android library for section headers that stick to the top
Apache License 2.0
5.51k stars 1.52k forks source link

Robolectric tests failing when used on Robolectric4/Androidx #495

Open alixwar opened 5 years ago

alixwar commented 5 years ago

Problem: When my Robolectric test executes the following line it crashes (it doesn't crash on the actual device). Note: I have gotten rid of any usages of LinearLayout as suggested here: https://stackoverflow.com/a/11493550/1859486

StickyListHeadersListView.setAdapter(adapter);

My setup Plugins:

classpath 'com.android.tools.build:gradle:3.2.1'

Dependences:

implementation 'se.emilsjolander:stickylistheaders:2.7.0@aar'

Test dependencies:

testImplementation('org.robolectric:robolectric:4.1') {
        exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on'
        exclude group: 'androidx.test', module: 'monitor'
    }

Stacktrace:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

    at android.widget.ListView.clearRecycledState(ListView.java:532)
    at android.widget.ListView.resetList(ListView.java:519)
    at android.widget.ListView.setAdapter(ListView.java:462)
    at se.emilsjolander.stickylistheaders.StickyListHeadersListView.setAdapter(StickyListHeadersListView.java:722)

Update I've traced the root cause and isolated it to this line:

        listView.addFooterView(inflater.inflate(R.layout.list_footer, container, false));

Interestingly enough, it only causes failures when running @Config(sdk = Build.VERSION_CODES.KITKAT) or @Config(sdk = Build.VERSION_CODES.O). It works fine on @Config(sdk = Build.VERSION_CODES.P)

list_footer.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        >

    <Space
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_vertical_margin_large"
            />

</LinearLayout>