When using Robolectric, _com.mikepenz.materialize.util.UIUtils_#getSelectableBackgroundRes(Context ctx) always returns 0 thus crashing the RobolectricGradleTestRunner test runner.
It is coming from these lines :
public static int getSelectableBackgroundRes(Context ctx) {
if (Build.VERSION.SDK_INT >= 11) {
// (...)
ctx.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
return outValue.resourceId;
} else {
// (...) sdk version < 11 are not supported by Robolectric anyway...
}
}
// app/gradle.build
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.issues.temp"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile('com.mikepenz:materialdrawer:5.3.0@aar') {
transitive = true
}
testCompile "org.robolectric:robolectric:3.1"
// Fix missing javax/microedition/khronos/opengles/GL for Roboelectric
// https://github.com/robolectric/robolectric/issues/1932#issuecomment-219796474
testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
}
// MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerBuilder builder = new DrawerBuilder()
.withActivity(this)
.addDrawerItems(new PrimaryDrawerItem().withName("An item"));
}
}
}
// MainActivityTest.java
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest {
@Test
public void test() {
// throws android.content.res.Resources$NotFoundException: unknown resource 0
Robolectric.setupActivity(MainActivity.class);
}
}
android.content.res.Resources$NotFoundException: unknown resource 0
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:369)
at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:114)
at android.content.res.AssetManager.getResourceValue(AssetManager.java)
at android.content.res.Resources.getValue(Resources.java:1347)
at android.content.res.Resources.getDrawable(Resources.java:804)
at android.content.Context.getDrawable(Context.java:458)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at com.mikepenz.materialize.util.UIUtils.getSelectableBackground(UIUtils.java:353)
at com.mikepenz.materialize.util.UIUtils.getSelectableBackground(UIUtils.java:285)
at com.mikepenz.materialdrawer.model.BasePrimaryDrawerItem.bindViewHelper(BasePrimaryDrawerItem.java:81)
at com.mikepenz.materialdrawer.model.PrimaryDrawerItem.bindView(PrimaryDrawerItem.java:69)
at com.mikepenz.materialdrawer.model.PrimaryDrawerItem.bindView(PrimaryDrawerItem.java:18)
at com.mikepenz.fastadapter.FastAdapter$OnBindViewHolderListenerImpl.onBindViewHolder(FastAdapter.java:1651)
at com.mikepenz.fastadapter.FastAdapter.onBindViewHolder(FastAdapter.java:485)
<!-- styles.xml -->
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- Add item name="selectableItemBackground" -->
<item name="selectableItemBackground">@color/colorPrimaryLight</item>
</style>
</resources>
unknown
@dieze seems like an issue from Robolectric to me as it does not correctly set up the android attributes.
I could add a normal "fallback" into the getSelectableBackgroundRes to return a full color, but your proposed solution above is not possible as it will provide a completely different resource than the one provided by the system.
for the styles with ActionBar where you have no solution, shouldn't it be enough if you just remove the NoActionBar from the style?
Hi mike.
I also wondered if I should post this issue here or at Robolectric. I understand this is a Robolectric issue, i will post this issue on Robolectric too.
The solution i provided (adding style for selectableItemBackground) is sufficent for my project, because it avoids Robolectric crash. And i use NoActionBar style. The section about using styles with ActionBar was just for completeness.
I lost a good amount of time tracking that bug. Better save that time to others.
Most helpful comment
Hi mike.
I also wondered if I should post this issue here or at Robolectric. I understand this is a Robolectric issue, i will post this issue on Robolectric too.
The solution i provided (adding style for selectableItemBackground) is sufficent for my project, because it avoids Robolectric crash. And i use NoActionBar style. The section about using styles with ActionBar was just for completeness.
I lost a good amount of time tracking that bug. Better save that time to others.