by David Britch

Introduction

The purpose of this technical article is to demonstrate how to build a testable Windows Phone application that consumes accelerometer data. The approach adopted here for building a testable Windows Phone application is to:

1. Abstract the Windows Phone SDK class that provides the desired functionality for the application.

2. Consume the wrapped class in an application.

3. Write unit tests that test application business logic.

The key to building testable Windows Phone applications is to abstract the classes in the Windows Phone 7.1 SDK. An interface can be generated for the required class in the Windows Phone 7.1 SDK that contains the desired functionality for your application. Adapters, which are wrapper classes that implement the generated interface, can also be generated. Adapter classes pass parameters and return values to and from the underlying Windows Phone 7.1 SDK class. The adapter translates calls to its interface into calls to the original interface. Creating adapters enables you to write loosely coupled testable code.

Example

The Accelerometer class provides access to the device's accelerometer sensor, which will detect the force of gravity along with any forces resulting from the movement of the phone. However, this class is not easily testable. To create a testable version of this class you should adapt the class, and then create a mock for it.

The interface for the Accelerometer class is not available in the Windows Phone 7.1 SDK. Therefore, the IAccelerometer interface can be generated from the Accelerometer SDK class by using a Reflector add-in named Doubler. Doubler is a code generator which can generate a wrapper class and its interface for a chosen type in an assembly. The generated IAccelerometer interface contains the properties, the method signatures and the event implemented by the Accelerometer SDK class.

The IAccelerometer interface can then be implemented by the AccelerometerAdapter class, which can also be generated by Doubler. The AccelerometerAdapter class adapts the Accelerometer class from the Windows Phone 7.1 SDK. The following illustration shows how the AccelerometerAdapter class adapts the Accelerometer SDK class.

image

The AccelerometerAdapter class implements the IAccelerometer interface. Any classes that want to consume the Accelerometer class to obtain sensor data should consume the AccelerometerAdapter class instead.

These classes are available in the Microsoft.Practices.Phone.Adapters Visual Studio project, which contains interfaces, adapters and facades for Windows Phone API functionality. This project is available from NuGet.

The right-hand side of the illustration shows the MockAccelerometer class, which also implements the IAccelerometer interface and is used in unit tests instead of the AccelerometerAdapter class. The MockAccelerometer class is an example of how a general-purpose mock can be given behaviour using delegates. Delegate accepting properties enable the execution of any desired behaviour necessary for the unit test. Unit tests can then be written that use the mock to test aspects of the application's business logic, contained in view models.

The Microsoft.Practices.Phone.Testing Visual Studio project contains mock implementation of the Windows Phone 7.1 SDK adapter and facade classes contained in the Microsoft.Practices.Phone.Adapters project.

The accompanying TestableAccelerometer.Tests project uses the Silverlight unit test framework for Windows Phone and Silverlight 4. To run the TestableAccelerometer.Tests project, first build and then deploy the project either to the Windows Phone emulator or to a real device. On the Windows Phone device, you can now launch an application named TestableAccelerometer.Tests, and then run the unit tests.

Summary

This article has demonstrated how to build a testable Windows Phone application that consumes accelerometer data. The AccelerometerAdapter class adapts the Accelerometer class, with the AccelerometerAdapter class then being consumed by an application. The MockAccelerometer class is used by unit tests to test business logic contained in the view model class.

To find out more, see Building Testable Windows Phone Applications on MSDN.

Resources

Developing a Windows Phone Application from Start to Finish

Developing a Windows Phone Application using the MVVM Pattern

A Case Study for Building Advanced Windows Phone Applications

Building Testable Windows Phone Applications

David Britch
Senior Technologist, Content Master