Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

My experience from programming on the Blackberry platform taught me that unit testing native mobile apps still has some way to go compared to desktop and web applications. So hearing that Android has its own testing framework came as welcome news.

During the past few days I’ve had a chance to work with this testing framework. Alas, as I started to experiment, excitement turned into frustration. One reason for this is that the tests I wrote were brittle. For example: if the emulator’s screen resolution changed or a new button was introduced then most of the tests failed because the layout of the buttons changed. Another reason is that to accomplish a simple test such as viewing if a menu button is visible, required considerable knowledge of Android’s internals to get it right. To make things worse, the test code I wrote was FAR from readable.

What I wanted was to write tests that were more declarative and less procedural. This is where Robotium came into play. Basically, Robotium is a facade to Android’s testing framework: it simplifies Android’s testing framework interface, making the framework easier to use. While it took me hours to write tests in Android’s testing framework, it took me only minutes to write tests in Robotium. Take a look at the two unit tests below:

public void testChangeServerAddress() throws Exception
{
    Class windowManager = Class.forName("android.view.WindowManagerImpl");
    Field viewsField = windowManager.getDeclaredField("mViews");
    Field instanceField = windowManager.getDeclaredField("mWindowManager");
    EditText serverAddress;
    View[] views;

    viewsField.setAccessible(true);
    instanceField.setAccessible(true);
    Object instance = instanceField.get(null);

    sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    sendKeys("H T T P SEMICOLON SLASH SLASH T E S T");
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

    sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

    views = (View[]) viewsField.get(instance);
    serverAddress = (EditText) ((ViewGroup) ((ViewGroup) ((ViewGroup) ((ViewGroup) ((ViewGroup) ((ViewGroup) views[1]).getChildAt(0)).getChildAt(0)).getChildAt(2)).getChildAt(0)).getChildAt(0)).getChildAt(0);

    assertEquals("http;//test", serverAddress.getText().toString());
}
public void testChangeServerAddress() throws Exception
{
    String serverAddress = "http://test";
    String serverAddressPreferenceText = "View or change the Bunya server URL";

    mPreferencesActivitySolo.clickOnText(serverAddressPreferenceText);

    mPreferencesActivitySolo.enterText(mPreferencesActivitySolo.getEditText(0), serverAddress);
    mPreferencesActivitySolo.clickOnButton("OK");

    mPreferencesActivitySolo.clickOnText(serverAddressPreferenceText);
    assertNotNull(mPreferencesActivitySolo.getEditText(serverAddress));
}

These unit tests are equivalent to each other. However, the first test is written with the test framework provided by Google, and the other is written using Robotium. The code speaks for itself… A shout out to the authors of Robotium!