Posted on

Android Testing pt 3.3 | Espresso Image Picker


android espresso image pickerDesigned by Freepik

 

With Espresso you can test all or most of your app. Even though you don’t need to design your app’s architecture in any specific way like with unit testing – covering codebase with Espresso is harder than it might seem like

Testing is hard

The more I get into testing and figure out how to test more and more parts – I learn that testing is really hard. It could be even harder than writing app itself. Well, probably not, it’s just that it’s completely different. None of the skills you got with creating apps will help you in writing Espresso tests. You need to learn from scratch how to do everything

So here’re some topics that I’ll cover in this post

  • Picking image either from camera or gallery chooser intent
  • Check what image is loaded
  • Starting activity by class

Picking image

First, let’s see how you can pick an image from default camera app or from the gallery. Espresso doesn’t allow you to interact with other apps, like camera app. In fact, I couldn’t even interact with intent picker

android espresso image pickerVia this chain


You would need to stub image picker. When intent picker or camera app should be opened – you return result instantaneously. We can do it with Espresso Intents, with them you can provide instantaneous result return on intents that you want

Now why I said testing is hard, is because just to mock image picking you need to figure out two things:

  • What intent is called when you pick image
  • How you save picked image (from camera), so that you mock result correctly

Just those two points could discourage you from covering this part with tests at all

And those two steps vary a lot depending on how you pick images. I’m using my library, an abstraction over Image Cropper, which starts activities, returns image URLs under the hood, so if you’re using some library as well – you’ll need to get to its source code and dig some details

Setup

Activity

Here’s what my activity looks like

Test Code


Now here it depends a lot on how you pick images, save them, pass in result intent on activity result. Let’s go through this line for more details


First, you find intent which you want to stub, matched by action ACTION_CHOOSER – that’s an intent chooser for image picker. And then you return ActivityResult objectwhich has intent in it, the one that will be in onActivityResult parameters

Because Image Cropper library saves a file with that exact name into cache directory – I save stub file whose file URI I return in ActivityResult

And test methods itself just check if ImageView has a drawable

Check if correct image is loaded

Now, what if you want to check if correct image is loaded. Obviously, it’s not the image picker test, but for cases when you display existing data in your system and just want to verify that image loading is implemented correctly. Same as checking if TextView displays correct text

It’s possible if your images are accessible with urls, this way you can test ImageView’s tag with the image URL that you’re loading


You actually don’t need to wait for an image to be loaded to set a tag. The only problem with Glide was that it didn’t allow setting tags on ImageView’s. To fix that I had to add this extra code, in Application class


and in any file in res/values directory

Starting Activity by class

And one more little tip: if you want to start an activity by class from your tests – you can do it this way


 

It’s pretty cool to figure out how to test more and more things. You can get the source code here. And don’t forget to subscribe