Slide 1

Slide 1 text

Testable Android Architecture (Part 2) 15 Nov 2016 / Chuck Greb

Slide 2

Slide 2 text

Clean code Software Craftsmanship

Slide 3

Slide 3 text

Art is anything you can do well. Anything you can do with Quality. “ ” - Robert M. Pirsig

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

→ MVC Model View Controller → MVP Model View Presenter → MVVM Model View ViewModel Clean Architecture Design patterns

Slide 6

Slide 6 text

Building your first app

Slide 7

Slide 7 text

Activity

Slide 8

Slide 8 text

Activity Activity Lifecycle System Services

Slide 9

Slide 9 text

Activity EditText Button Activity Lifecycle System Services

Slide 10

Slide 10 text

Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle System Services

Slide 11

Slide 11 text

Fragment Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle System Services Fragment Fragment Lifecycle

Slide 12

Slide 12 text

Fragment Activity EditText Button Web Service Storage AsyncTask Activity Lifecycle System Services Fragment Fragment Lifecycle User Input Input Validation

Slide 13

Slide 13 text

Fragment Activity Web Service Storage AsyncTask Activity Lifecycle System Services Fragment Fragment Lifecycle User Input setRetainInstance(true) EditText Button Input Validation

Slide 14

Slide 14 text

Fragment Activity Web Service Storage AsyncTask Activity Lifecycle System Services Fragment Fragment Lifecycle User Input setRetainInstance(true) Intent Parcelable Extras EditText Button Input Validation

Slide 15

Slide 15 text

Web Service Storage Activity Fragment Fragment AsyncTask setRetainInstance(true) Activity Lifecycle Fragment Lifecycle System Services User Input Intent Activity Instrumentation TestCase2 Parcelable Extras EditText Button Input Validation

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

There has to be a better way

Slide 18

Slide 18 text

Activity Logic

Slide 19

Slide 19 text

Activity Logic Data

Slide 20

Slide 20 text

Activity Logic Data View

Slide 21

Slide 21 text

View (Activity) Controller (Logic) Model (Data) View

Slide 22

Slide 22 text

View (Activity) Presenter (Logic) Model (Data) View

Slide 23

Slide 23 text

View (Activity) ViewModel (Logic) Model (Data) View

Slide 24

Slide 24 text

- Phil Karlton There are only two hard problems in computer science: cache invalidation and naming things “ ”

Slide 25

Slide 25 text

Clean Architecture Model View Presenter Controller MVP(C)

Slide 26

Slide 26 text

Controller (Activity) Presenter (Logic) Model (Data) View

Slide 27

Slide 27 text

Activity Presenter (Logic) Model (Data) View Controller

Slide 28

Slide 28 text

Web Service Storage Activity Activity Lifecycle System Services User Input Presenter View Model Controller Button EditText Thread Input Validation start()

Slide 29

Slide 29 text

Web Service Storage Activity Activity Lifecycle System Services User Input Presenter View Model Controller Button EditText Input Validation Thread start()

Slide 30

Slide 30 text

Testings Models & Presenters JUnit + Mockito

Slide 31

Slide 31 text

Mock Service Mock Storage Test Controller Presenter Model Controller JUnit TestRunner Runnable run()

Slide 32

Slide 32 text

A test is not a unit test if: → It talks to the database → It communicates across the network → It touches the file system → It can't run at the same time as any of your other unit tests → You have to do special things to your environment (such as editing config files) to run it. Michael Feathers, 2005 A Set of Unit Test Rules

Slide 33

Slide 33 text

Testing Activities & Views Robolectric

Slide 34

Slide 34 text

Activity Robolectric TestRunner Robolectric Runtime Environment Simulated Input Stub Presenter View Button EditText Input Validation

Slide 35

Slide 35 text

Demo App https://github.com/ecgreb/mvpc

Slide 36

Slide 36 text

Q & A

Slide 37

Slide 37 text

What is the best way to manage presenter scope or lifecycle? “ ”

Slide 38

Slide 38 text

→ Global singleton → Scoped singleton → View State Manager Q & A What is the best way to manage presenter scope or lifecycle?

Slide 39

Slide 39 text

Web Service Storage Activity Activity Lifecycle System Services User Input Presenter View Model Controller Button EditText Thread Input Validation start()

Slide 40

Slide 40 text

Web Service Storage Activity Activity Lifecycle System Services User Input Presenter View Model Controller Button EditText Thread Input Validation start() ViewState Manager (VSM)

Slide 41

Slide 41 text

Can a view have its own presenter? “ ”

Slide 42

Slide 42 text

→ True Model-View-Presenter → One presenter instance per view Q & A Can a view have its own presenter?

Slide 43

Slide 43 text

View Presenter Activity Activity Lifecycle System Services User Input Main Presenter View Controller Button EditText Input Validation Model

Slide 44

Slide 44 text

How do you communicate between multiple presenters? “ ”

Slide 45

Slide 45 text

→ Pass the message “around the horn” → Event bus Q & A How do you communicate between multiple presenters?

Slide 46

Slide 46 text

Search View Search Presenter Main Activity Main Presenter Navigation View Navigation Presenter

Slide 47

Slide 47 text

Search View Search Presenter Main Activity Main Presenter Navigation View Navigation Presenter Event Bus

Slide 48

Slide 48 text

Eraser Map

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

→ Choose an architecture appropriate to your use case → Write tests that work in concert with your architecture → Don’t be overly dogmatic Conclusion So which architecture and testing strategy should I use for my app?

Slide 52

Slide 52 text

Start where you are