Friday, November 13, 2009

Android Testing: Mock Contexts

AndroidTesting: Mock Context
















This document can be read in Google Docs (http://docs.google.com/View?id=ddwc44gs_21737cgtvfj), cut and paste link if you have problems accessing it.







Continuing with our Android Testing series, we take a short detour before we proceed to create more unit and functional tests.

We have visited Mock Objects before and evaluated our Test Driven Development purism and the concerns about not using real objects, but this is another discussion.

Sometimes, introducing mock objects in our tests is recommended, desirable, useful or even unavoidable.

Android SDK provides some classes to help us in this quest in the package android.test.mock:







mock context overview


Mock Context class implements all methods in a non-functional way and throw UnsopportedOperationException.

This can be used to inject other dependencies, mocks, or monitors into the classes under testing. A fine control can be obtained extending this class.



Extend this class to provide your desired behaviour overriding the correspondent methods.





mocking file and database operations


In some cases, all we need is to be able to mock the file and database operations. For example, if we are testing the application on a real device perhaps we don't want to affect existing files during our tests.

Such cases can take advantage of another class that is not part of the android.test.mock package but of android.test instead, that is RenamingDelegatingContext. I think it belongs to the former but it is in the parent package for apparently no reason.

This class let us mock operations and files and databases are prefixed by a prefix that is specified in the constructor. All other operations are delegated to the delegating Context that you must specify in the constructor too.



Suppose our Activity under test uses some files we want to control in some way, maybe introducing specialised content to drive our tests and we don't want or we can't use the real files, in this case we create a RenamingDelegatingContext specifying a pefix, we provide that files, and our unchanged Activity will use them.



Our Activity under test, MockContextExamplesActivity, displays the content of a file inside a TextView, what we intend is to be able to display different content during a test than during normal operation of the Activity.



MockContextExamplesActivity.java


A very simple Activity. Shows the content of the file myfile.txt inside a TextView.
















package com.codtech.android.training.mockcontext.examples;



import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;



import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.widget.TextView;



public class MockContextExamplesActivity extends Activity {

    public final static String FILE_NAME = "myfile.txt";

    

    private TextView tv;

    

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        tv = (TextView) findViewById(R.id.TextView);

        final byte[] buffer = new byte[1024];

        

        try {

            final FileInputStream fis = openFileInput(FILE_NAME);

            final int n = fis.read(buffer);

            tv.setText(new String(buffer, 0, n-1));

        } catch (Exception e) {

            tv.setText(e.toString());

            tv.setTextColor(Color.RED);

        }

    }

    

    public String getText() {

        return tv.getText().toString();

    }

}






MyMockContext.java















import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;



import android.content.Context;

import android.test.RenamingDelegatingContext;

import android.util.Log;





/**

 * @author diego

 *

 */

public class MyMockContext extends RenamingDelegatingContext {

    private static final String TAG = "MyMockContext";

    

    private static final String MOCK_FILE_PREFIX = "test.";





    /**

     * @param context

     * @param filePrefix

     */

    public MyMockContext(Context context) {

        super(context, MOCK_FILE_PREFIX);

        makeExistingFilesAndDbsAccessible();

    }



    /* (non-Javadoc)

     * @see android.test.RenamingDelegatingContext#openFileInput(java.lang.String)

     */

    @Override

    public FileInputStream openFileInput(String name)

            throws FileNotFoundException {

        Log.d(TAG, "actual location of " + name +

            " is " + getFileStreamPath(name));

        return super.openFileInput(name);

    }



    /* (non-Javadoc)

     * @see android.test.RenamingDelegatingContext#openFileOutput(java.lang.String, int)

     */

    @Override

    public FileOutputStream openFileOutput(String name, int mode)

            throws FileNotFoundException {

        Log.d(TAG, "actual location of " + name +

            " is " + getFileStreamPath(name));

        return super.openFileOutput(name, mode);

    }

}









test.myfile.txt


We also need to provide at least the text file containing the sample text.

The directory where these files are located is created during package installation, so you may want to wait till you install the package for the first time to run this command.

Notice that we are using a "test." prefix in this file creation as we did in our MyMockContext class.

To do this just run this command:
















diego@bruce:\~$ adb shell echo "This is sample text" \

   \> /data/data/com.codtech.android.training.mockcontext.examples/files/test.myfile.txt





Remember to change package name if you are using a different one.



If you also want to create some real context to be displayed by the Activity use this command:
















diego@bruce:\~$ adb shell echo "This is REAL text" \

   \> /data/data/com.codtech.android.training.mockcontext.examples/files/myfile.txt





Notice that here there's no "test." prefix because it's the real file.





MockContextExamplesTest.java


Now when we create some tests we can use this mock context.
















import android.content.Context;

import android.content.Intent;

import android.test.ActivityUnitTestCase;



import com.codtech.android.training.mockcontext.examples.MockContextExamplesActivity;



/**

 * @author diego

 *

 */

public class MockContextExamplesTests extends ActivityUnitTestCase<MockContextExamplesActivity> {



    public MockContextExamplesTests() {

        super(MockContextExamplesActivity.class);

    }



    /**

     * @throws java.lang.Exception

     */

    protected void setUp() throws Exception {

    }



    /**

     * @throws java.lang.Exception

     */

    protected void tearDown() throws Exception {

    }



    public void testSampleTextDisplayed() {

        // mock context

        Context mockContext = new MyMockContext(getInstrumentation().getTargetContext());

        setActivityContext(mockContext);

        startActivity(new Intent(), null, null);

        final MockContextExamplesActivity activity = getActivity();

        assertNotNull(activity);

        assertEquals("This is sample text", activity.getText());

    }

    

    public void testRealTextDisplayed() {

        // real context

        setActivityContext(getInstrumentation().getTargetContext());

        startActivity(new Intent(), null, null);

        final MockContextExamplesActivity activity = getActivity();

        assertNotNull(activity);

        assertFalse("This is sample text".equals(activity.getText()));

    }

}












Conclusion


Though this is an oversimplified example it shows the most important concepts behind Mock Contexts and the way you can use Delegating Context to isolate your Activity from other parts of the system like the filesystem in this case.






Copyright © 2009 Diego Torres Milano. All rights reserved.

























































3 comments:

Unknown said...

First and foremost, I want to thank you for providing some of the best documentation on TDD+Android available on the internet. This example really gets the juices flowing.

I'm having a problem extending this example for a couple/few reasons. One is that the example source code doesn't specify package names for all classes, and so the user is left guessing if the mocked activity lives in the original android application or a separate test project. Whether or not the concept is for the mock activity to live in a 'normal' or 'test project, it would help users to provide an example android manifest(s).

Finally, a mock activity in a test project seems to have limited utility. Unless the mock activity extends an activity launched by the normal application and can be invoked from unit and instrumentation tests, it doesn't seem to make much sense.

In a roughly-related topic, I have a question about unit and instrumentation testing with MockContexts in the Google groups at http://groups.google.com/group/android-developers/browse_thread/thread/fc8210d657518f96/44b10f0b10686142?lnk=gst&q=ActivityUnitTestCase+provider#44b10f0b10686142. My question is also related to your post at http://planetandroid.vitaeblog.com/?p=2542.

Thanks again for all your help and guidance.

Diego Torres Milano said...

Hi Free,
Thanks for your positive encouraging comments. These helps this blog better providing things or extra information I neglected.
Source code of both (main and test) projects can be found at http://sites.codtech.com/android/Home/source-code.
Google Doc has also updated accordingly.
I will go with your other questions later.

Anonymous said...

Thanks, useful post even tho it's more than 2 years old!

I'm using a ServiceTestCase and passed a getSystemContext() to my RenamingDelegatingContext. I had to override getApplicationContext to stop singletons from accessing app resources