Join Chiu-Ki Chan for an in-depth discussion in this video Refactor: Show recipe, part of Effective Android Testing for Mobile Developers.
- [Instructor] In step four, when the recipe is not null, we will show the recipe. We do that by setting the title text, the title selector state, and also the text for the description. But remember, the activity does not know about the recipe any more so we need to go through the presenter. Open RecipePresenter. In Line 21, we are handling the case when the recipe is null. Add an else block in line 23 to handle the case when the recipe is not null.
What do we do here? When the recipe is not null, we would like to update the title view and the description view. And those things are Android things, meaning that we cannot do it directly inside the presenter. We will need to go through the view. View, dot, Set Title, and then we will use recipe dot title. Right now, the view interface does not have set title yet. So move your cursor over there and press Alt Enter to create it.
Go back to the presenter. We will do the same for the description. View dot setDescription, and then recipe dot description. Once again, move your cursor to the red part, Alt Enter to create the method. Go back to the presenter. Next, we will need to set the selector state. To do that, we will need to know whether this recipe is favorite. How do we determine that? If you go back to recipe activity, in line 41, you can see that we are getting the Boolean favorite out of the object favorites.
But right now, the presenter does not have a handle on this object. So we need to pass it in in the constructor. Scroll up. We create the presenter in line 33. Let's copy and paste the favorite before this line so that we can pass it into the constructor. Cut the line 39 and 40. Paste it before the presenter constructor in line 33. And then, as the third parameter of the constructor, add favorites.
This is not going to compile, because we need to update the constructor. So press Alt Enter and add favorites as a third parameter. Let's Command Click through and see what is going on inside the presenter. Inside the presenter, favorites has been added as a parameter. But we haven't done anything with it yet. Let's stash it away as a variable. After line 17, add this dot favorites equals favorites.
Go to the red part, Alt Enter to create a field. Now that we have favorites inside the presenter, let's go back to the point when we are showing the recipe. Scroll down. After line 28, we can set the title selector state depending on whether this recipe is a favorite. Just like the lines above it, we are going to go through the view. View dot setFavorites, and then favorites dot get.
Get takes an ID. In this case, we can get the ID from the recipe. So recipe dot ID. SetFavorite is not defined yet, so let's put your cursor there, press Alt Enter to create the method. Change the name of the Boolean from B to Favorite. Let's go back to the activity. If you scroll up, in line 16, you can see that the definition of the class is not happy.
If you put your cursor there and press Alt Enter, you can see that we need to implement the new method that we have created inside the contract. So let's select implement methods. Set title, set description, and set favorite are all preselected and we'll keep it that way. Scroll down to see the new functions. What are we going to do here? If we scroll up back to step four, we can see that in line 43 is where we set the title.
So let's cut that line, scroll down, and paste it in setTitle in line 64. Instead of recipe dot title, we will use the parameter title passed into the function. Repeat for set description. Cut the line 44, scroll down, paste it in line 68. Once again, we will be using description directly from the parameter.
Finally, we need to set favorite. If you scroll back up to step four, you can see that it first tried to get favorite from the favorites object. We don't need to do that here any more, because we are doing that inside the presenter. Let's go take a quick look. Inside the presenter, in line 29, we call favorites dot get, which, if you go back to the activity, is exactly the same as line 41. Therefore, we can delete this line.
The one last thing that's remaining is that for right now, it's setting the selector state of the title view. We are not going to do it here. Instead, we will cut this line, scroll down, and paste it in line 70 inside the setSelected function. We got lucky that the Boolean favorite is matching the name so we don't need to change it. So if you scroll back up, now step four is empty, because once again, this is done in the presenter.
Let's walk through that, because there's a lot of back and forth between the activity and the presenter. If you go to line 36 and Command Click through Load Recipe, we can follow it from here. Let's do that for the setTitle function. If the recipe is not null, we are going to call view dot setTitle. Command Click will bring us to the contract. And then if you go to the gutter, you can click on the implementation, which is in the activity.
So this is kind of a roundabout way to set the title, but the reason why we are doing that is so that we can keep the presenter pure Java, having zero Android functionality. All right, we have finished step four. Let's go back to the activity and see what we need to do next. Next, for step five, we will need to take care of toggling the favorites.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP