Join Chiu-Ki Chan for an in-depth discussion in this video View Interface and Presenter, part of Effective Android Testing for Mobile Developers.
- Lets refactor recipe activity into Model View Presenter One thing that is confusing about the Model View Presenter is that the view here is not the same as the view in Android Instead, it is a generic term for the UI In Android, this usually corresponds to an activity, or a fragment. For our app, it is recipe activity. Now that we know what is our view, we need to find a view interface, which does not contain any Android classes.
We will create an interface called Recipe Contract for that. This contract is going to be in the same folder as the recepie activity. Scroll on your project view, and locate recepie activity under Main Java UI recepie. Right click, new, Java Class We will call it recipe contract. Change it from class to interface. Press OK.
Inside the recipe contract, we will have two interfaces: View, and listener The view contract allows the presenter to talk to the view and the listener allows the view to talk to the presenter. How do we decide what goes into these interfaces? The presenter should contain the logic. So let's look at recipe activity, and see what logic is in it. The first thing it does is to set up the UI.
You can see that from line 21 to line 25. I'm going to add some comments so we can break it up into steps. Step one is to set up the UI. After setting up the UI, we will be loading the recipe from the store. You can see that from line 28 to 30. So let's add a comment here. Step two: load recipe from store. Step three is from line 33 to 37.
If the recipe is null, we will show error. So let's add a comment. Step three: if recipe is null, show error. After the return line in line 37, we know that the recipe is not null. In that case, we are going to show the recipe on the UI. To do that, we first load the favorite, which is done from line 40-42. With the favorite we are able to update the title views, select to stay in line 45.
We are also going to be displaying title in line 44 and also displaying the description in line 53. All these are updating the UI, so let's group them together, by cutting the description line from line 53, and pasting it after the set-selected in line 45. So now this block from line 40 to line 46 is step four for us.
Step four: if recipe is not null, we will show the recipe. And finally, from line 49 to 54, we are setting up a click listener. So that will be our step five. Step five: when the title is clicked, what are we doing when the title is clicked? Ah, we are toggling the favorites: toggle, favorites. Now that we have broken down the activity into five different items, we need to decide which ones are from the view to the presenter, and which ones are from the presenter to the view.
Remember, the presenter is the logic, the pure java part. Next, let's look at each item and ask: what is the logic? We will use that to guide our refactoring.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP