Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Robolectric asset not found

Open tmtrademarked opened this issue 7 years ago • 4 comments

After much hassle, I found the source of an annoying problem I've had in my Robolectric tests. When inflating a TextView with no textAppearance set, Robolectric fails with the following exception:

Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff in packages [android, com.myapp]
	at org.robolectric.shadows.ShadowAssetManager.getResName(ShadowAssetManager.java:868)
	at org.robolectric.shadows.ShadowAssetManager.buildTypedValue(ShadowAssetManager.java:734)
	at org.robolectric.shadows.ShadowAssetManager.attrsToTypedArray(ShadowAssetManager.java:796)
	at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:221)
	at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:216)
	at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
	at android.content.Context.obtainStyledAttributes(Context.java:520)
	at uk.co.chrisjenx.calligraphy.CalligraphyUtils.pullFontPathFromTextAppearance(CalligraphyUtils.java:230)
	at uk.co.chrisjenx.calligraphy.CalligraphyFactory.resolveFontPath(CalligraphyFactory.java:197)
	at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onViewCreatedInternal(CalligraphyFactory.java:132)
	at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onViewCreated(CalligraphyFactory.java:114)
	at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$PrivateWrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:301)
	at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)

It looks like Robolectric is more strict about requiring a resource to exist than the normal Android framework is. This is pretty annoying for tests.

But the fix seems like it should be really simple: just wrap the final call in CalligraphyUtils.pullFontPathFromTextAppearance with a try/catch. In other words, wrap lines 230-240 in a try/catch - or just return null directly if textAppearanceId still equals -1.

Thoughts? I'd be happy to submit a pull request implementing this, if that would help.

tmtrademarked avatar Mar 05 '17 20:03 tmtrademarked

affects me too

dagi12 avatar Jan 18 '18 22:01 dagi12

Same here. @tmtrademarked have you submitted the fix by any chance?

zawadz88 avatar Nov 02 '18 14:11 zawadz88

Unfortunately, I never got around to creating a pull request for this. :-/ I think I ended up working around this in my tests, but it's been long enough that the context doesn't spring to mind immediately.

tmtrademarked avatar Nov 02 '18 15:11 tmtrademarked

The fix suggested by @tmtrademarked seems to be working :) I've published it in releases in a forked repo here if anyone is still struggling with it: https://github.com/stepstone-tech/Calligraphy/releases

zawadz88 avatar Nov 16 '18 19:11 zawadz88