简体   繁体   中英

Different drawable loading from app resources versus test resources

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM