Different drawable loading from app resources versus test resources

329 views Asked by At

I am writing some UI tests for the app that I'm working on and I need to switch an image based on a push notification. To write the test without push notifications, I'm placing a png file in the androidTest res directory and then loading it with ContextCompat.getDrawable() using the resource id from the test resources. I'm also loading the same image from the app resources using the id from the app resources.

Here's the code that loads the resources:

Drawable drawable1 = ContextCompat.getDrawable(setupRule.getActivity().getApplicationContext(), R.drawable.my_contact_pic);
Drawable drawable2 = ContextCompat.getDrawable(setupRule.getActivity().getApplicationContext(), com.mycompany.myapp.devenv.test.R.drawable.my_contact_pic);

drawable1 is an instance of BitmapDrawable, drawable2 is an instance of NinePatchDrawable. Since the underlying PNG for drawable2 is a copy of the PNG for drawable1, I would expect them both to be BitmapDrawables, I can code around this but would like to understand why this happens for future reference.

1

There are 1 answers

0
Michael Markley On

So I finally figured this out, to load the png from the androidTest resources, you have to specify InstrumentationRegister.getContext(), otherwise it will load from the application resources. It was a coincidence that the resource ID that I specified pointed to a NinePatch file.