Custom view attributes

How can we unit test our custom view attributes to ensure they are properly parsed and applied when the view is created?

The following example comes from the the Mapzen Android SDK.

The MapView class provides two options for configuring the map controls overlay including the compass widget, zoom buttons, and current location or “finde me” button.

SDK mode configures the controls similar to other popular mapping SDKs (like the Google Maps Android SDK). Classic mode configures the controls in the style used by the Google Maps application and our own Eraser Map.

SDK mode

Classic mode

The position of the controls is configured using a custom view attribute. Based on the attribute value one of two custom layout files are inflated at runtime.

When the two argument constructor is invoked the AttributeSet is parsed and the overlayMode is stored in a private field.

Then when the view is inflated, one of two layout files is used based on the value of overlayMode.

Now we want to add unit test coverage to verify the overlayMode xml attribute is properly parsed and the correct layout file is inflated.

In order to do so we are going to rely on Mockito and a hand-written subclass of LayoutInflater.

TestLayoutInflater is a custom mock that will allow us to verify the correct layout file is inflated with the proper root view.

When we test the behavior in MapViewTest we are also going to rely on Mockito to create a number of other mocks for Android classes including Context, TypedArray, and AttributeSet.

Following the Arrange-Act-Assert convention first we arrange the collaborators by creating and configuring our mocks. Then we act by calling the MapView constructor to parse the attributes and inflate the view. Finally, we assert the correct layout file was inflated with the correct root view.

For an example of testing custom attributes applied to a Fragment take a look at MapFragment and MapFragmentTest.