Friday, August 19, 2011

Using Robolectric with Real Android Jars

Overview

"Why doesn't Robolectric just use the real Android jars?" This one of the most commonly asked questions. The short answer is that it's hard: hard to get the real Android jars, hard to hook them up, hard to deal with native code, hard to know what the heck is going to happen when real code is called, hard to change from the Shadow-way of doing things, hard hard hard.

But, many of you know that we have a branch at http://github.com/pivotal/robolectric named "reviscerated" that backs Robolectric with real Android implementations. This branch has languished due to lack of time and resources.

This week we spent time reviving this branch, merging the master branch into it and getting it working with APK 10/2.3.3 jars.

Robolectric tests pass in both SDK and real-jars mode, as do the tests in RobolectricSample. Now we need you to help us test this code. If you find it valuable then help us fix what's broken.

So, what happens?

All non-Shadowed entities, such as methods and classes, call through to the real Android implementations. No more "Stub!" exceptions from Android, no more null return values or no-op implementations from Robolectric -- if you call Android code, you get Android code. Note that any methods implemented in a Shadow are still called -- shadowed things win.

Essential Android Links:

Terminology:

  • SDK jars: These are the jars installed by android using AVD or command line tools. Implementations replaced with "Stub!" exceptions and entities annotated @hide are either non-public or otherwise unavailable.
  • real jars: Jars generated by manually building Android. Full implementations present including entities annotated @hide.

Getting The Real Jars

Google does not distribute the real Android jars -- you must build them yourself.


Setting up your Workstation

Follow Mac setup instructions here: http://source.android.com/source/initializing.html

After create and mount (double-click) the sparse image, make a directory specifically for the version of Android you want to build.

/Volumes/android $ ls
gingerbread-233 honeycomb-30

Note: once you have built one version of android it is is hard to use that same dir to build another version. I don't know how to clean up from the last version. It's hit-or-miss.

# choose a specific branch found on http://source.android.com/source/build-numbers.html
/Volumes/android/gingerbread-233 $ repo init -u git://android.git.kernel.org/platform/manifest.git -b android-2.3.3_r1
/Volumes/android/gingerbread-233 $ repo sync # give this 40+ minutes

Compiling, Building

See the build instructions here: http://source.android.com/source/building.html

Note: make sure the lunch output looks right! If the numbers don't match what you expect from http://source.android.com/source/build-numbers.html then you have synced to the wrong version and will need to repo sync -b [some other tag]

$ lunch full-eng

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.3.3 <===== gingerbread
TARGET_PRODUCT=full
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=false
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=darwin
HOST_BUILD_TYPE=release
BUILD_ID=GRI40 <===== matches gingerbread on http://source.android.com/source/build-numbers.html
============================================

Make It

$ make -j8 # 4-cpu, 8-core machine


Don't Wait: Watch for classes.jar


The build takes about 20-30+ minutes, but you don't have to wait for the full build. Watch for out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar. When it appears you can grab it and move it somewhere handy:

$ mkdir -p ~/android-real-jars/gingerbread-233
$ cp /Volumes/android/gingerbread-233/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/*.jar. ~/android-real-jars/gingerbread-233/

Also: download kxml2-2.3.0.jar and put it in the same directory you moved classes.jar. http://www.findjar.com/jar/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.jar.html


Using the Jars with Robolectric

From Maven 2.0-SNAPSHOT



From Robolectric Source

$ git clone git://github.com/pivotal/robolectric.git
$ cd robolectric
$ git checkout origin/reviscerated # this is the "use real jars" branch

  • Open RobolectricTestRunner.USE_REAL_ANDROID_SOURCES and make sure it is set to true.
  • Look at RobolectricTestRunner.getDefaultLoader() and make sure the paths point to the real jars you copied above. You can also use environment variables for these. See Setting ANDROID_HOME and ANDROID_REAL_JARS_HOME below.
  • Rebuild the jars in real-jar mode:
    $ ant clean jar-all
  • Replace your robojectric.jar with target/robolectric.jar or target/robolectric-all.jar

If you are using IntelliJ you might need to fix the Project Structure => Modules => Dependencies.

Setting ANDROID_HOME and ANDROID_REAL_JARS_HOME

You need to tell Robolectric some extra information when in real-jars mode. At this time you can either hand-edit RobolectricTestRunner and change the defaults or set environment variables:

# in .bash_profile or something

export ANDROID_HOME=/Users/pivotal/android-sdk-mac_x86
export ANDROID_REAL_JARS_HOME=/Users/pivotal/android-real-jars/gingerbread-233


Intellij most likely won't see those variables. Set them in the run configruation or default run configuration for JUnit:





Issues

  • Real jars mode seems to be incompatible with Roboguice at the moment. Sometimes it works, somtimes not.
  • Running ant clean test results in MethodGenerator test failures -- javassist.NotFoundException for Views. mvn clean test succeeds.
  • We would like to find the minimum time and effort to generate real jars.
  • There are likely errors in this blog post. We will be updating it as issues are found.

Thursday, February 3, 2011

How to work on Robolectric in parallel with another project

The Robolectric framework is designed to allow us to create and install test doubles for elements of the Android API as we encounter the need for them during our test and development activities. This should come as no surprise since Robolectric was born of an Agile development environment that strives to create software artifacts on the basis of demonstrated need rather than speculation. A consequence of this approach is that the most effective way to work with Robolectric is to also work on Robolectric. When your project encounters a part of the Android API that is not yet covered by Robolectric, or which is not covered in a way that you like, it is easy to add the needed functionality to Robolectric and move forward. This is similar to writing a new mock object in order to facilitate testing except that the new functionality is added to the permanent test infrastructure rather than being inlined into the test code.

The Robolectric documentation contains instructions about how to write and modify the Shadow Classes that serve as the test doubles for the Android code, but little is said about how to integrate active Robolectric development into your working environment. This article addresses how to obtain and build the Robolectric source so that your changes are immediately visible to your tests; how to track upstream changes to Robolectric; and how to create pull-requests for code that you want to contribute to the Robolectric project. These instructions are completely compatible with the instructions for registering your own private Shadow Classes contained in the Robolectric documentation about customizing the RobolectricTestRunner. So, you will still be able to write Shadow Classes (including subclasses of existing ShadowClasses) that are specific to your own application and register them in your custom TestRunner at the same time as you are writing new classes, or other extensions to Robolectric functionality, that are meant for consumption by the wider Robolectric community.

About git
The source code for Robolectric lives in a public git repository on the GitHub website. Unfamiliarity with git should not be an obstacle to using a live version of the Robolectric source in your project. This article documents all of the commands that are needed and GitHub has excellent help for both git and GitHub including how to install git on Windows. Git does have a couple of idiosyncrasies, almost always for good reasons, and we will discuss them as we run into them.  The first of these is that it is a distributed revision control system which means that there are multiple copies of the repository. In our case there will be three repositories in addition to your working copy of the code. The first repository lives on GitHub and is the primary repository for the Robolectric code. The second repository will also live on GitHub and will be the primary repository for your organization's copy of the Robolectric source code and your gateway for sharing contributions that you wish to share with the community. The third repository lives on your personal development machine and is the one that sometimes catches people by surprise. This article will treat it as just a gateway between your machine and GitHub, but it is a fully fledged revision control system for the work done on a single instance of a project on a single machine. With that information in mind we can proceed.

Forking Robolectric
The first thing you need to do is to create a place on GitHub for the changes you want to make. You don't have to use GitHub as your revision control solution for Robolectric if you don't want to, but you do need a way to move code between your organization and the main Robolectric repository and this is the mechanism for doing that.
  1. Login or create an account on GitHub. You may want to create a separate account for your specific organization or project.
  2. Navigate to the Robolectric project page at: https://github.com/pivotal/robolectric
  3. Click the button that says "Fork" (upper right corner, third line from the top, fourth button from the right)
    You now have your own private copy of the Robolectric source code in a repository in GitHub that knows how to track changes in the main Robolectric repository. Later we'll talk about how to merge changes from the main Robolectric repository into your own, and how to request that we pull some or all of the changes you've made into the main repository so that they can be used and maintained by the rest of the community.
  4. Click on the button with the clipboard icon next to the text that says: "This URL has Read+Write access"
    This will copy a URL to your clipboard that can be used to make a local clone of the repository
  5. On the command line, go to the location where you want the Robolectric project to be created (you do not have to create the subdirectory for it), type: "git clone " and then paste in the URL you just obtained. The command should look something like this:
    git clone git@github.com:your_user_name/robolectric.git
    When this command completes you will have a local copy of the Robolectric repository (in
    robolectric/.git) as well as a working copy of the code in the newly created robolectric directory.
  6. To build and test Robolectric type the following commands:
    For Maven builds:
    ant maven-install-jars
    mvn install

    This will locate your Android development kit and install android.jar into your local Maven repository and then build Robolectric, test it, and then install the resulting jars into Maven.
    For ant builds:
    ant clean test
    Which will build the Robolectric jar and run the tests.
Adding Robolectric as a Dependency in Your Project
Maven
If you are using Maven you should take note of the Robolectric version (currently 0.9.9-SNAPSHOT) from pom.xml file and add a dependency to your projects pom.xml that looks something like this:

<dependency>
    <groupId>com.pivotallabs</groupId>
    <artifactId>robolectric</artifactId>
    <version>0.9.9-SNAPSHOT</version>
    <scope>test</scope>
</dependency>

Ant
The easiest thing to do is to include the Robolectric sources as part of your compile.tests target something like this:


        

        
            
            
            
                
                
                
                
                
            
        
    

Where the key items are the addition of <src path="..robolectric/src/main/java"> as one of the source directories and <fileset dir="../robolectric/lib/main" includes="*.jar"> in the classpath.

Eclipse and IntelliJ
As mentioned elsewhere both of these IDEs can import Robolectric using its pom.xml file. In Eclipse this is done using the M2Eclipse plugin. In the "Configure Build Path" dialog for your test project, under the Projects tab, just add Robolectric and any changes you make to the Robolectric project will be picked up. Similarly in IntelliJ, Robolectric can be added as a module by navigating to the "Project Structure" dialog, selecting "Modules" under "Project Settings" and then clicking the plus icon and navigating to the pom.xml file.

Pulling Upstream Changes
From time to time you will want to pull the changes that have been made to the main Robolectric repository into your own workspace. The following command will connect the two together in order to make that possible:


git remote add pivotal git@github.com:pivotal/robolectric.git


This makes pivotal an alias for the main repository. Now you can pull down the latest Robolectric sources and merge them with your local changes with:


git pull pivotal master


If you have made substantial changes to the Robolectric code you may need to resolve merge conflicts before proceeding further. Ensure that all of the tests run with this command:

mvn clean test

Submitting Pull Requests
Once your changes to Robolectric are complete and the tests for them are all passing, you will often want them to become a permanent part of the Robolectric codebase so that they can be used and maintained by the rest of the community. You can accomplish this by pushing your changes to your repository on GitHub and submitting a pull request to the Robolectric team.

The first step in this process is to get the code in your repository to look the way you want the code in the Robolectric repository to look. Start by pulling in any upstream changes, as described above, and ensuring that the entire test suite passes. Once everything is working the next step will be to commit your changes to your local repository in preparation for pushing them to GitHub. This is accomplished with the following command:

git commit -a -m"some commit message"


In this command the -a means "add all of the changes I've made to the list of changes to be committed" and the -m allows you to put your commit message on the command line. If it is omitted git will open an editor and ask you to enter your commit message there.


Ensure that all of your changes have made it into your repository:


git status


The resulting message should tell you which branch you are on and that there is nothing to commit.


Once your changes are in your local repository you can push them to the GitHub repository with the following command:

git push origin master


This command may fail if you have not set up an SSH key for your GitHub account. If that happens, these instructions will help you out.


Running git push makes it possible for the Robolectric team to get access to the changes that you want to add. You can request that we add these changes to the project by submitting a pull request from the GitHub website. Log in to GitHub and select the forked Robolectric repository from your list of repositories on the right-hand side of the page, and then click on the "Pull Request" button from the button bar in the upper right corner. Fill in the form to let us know what to expect from your pull-request and then click the "Send Pull Request" button. We like to use pull request titles as story names in Tracker so please make yours suitably descriptive.

We are responsive as possible to pull requests, often putting them ahead of our current priorities, in order to encourage the Robolectric community to remain active. Many factors, including the presence of passing tests, commitments of our day jobs, compatibility with our roadmap, and technical and legal considerations, can affect our ability to quickly address each pull request. Regardless of our ability to respond, we want everyone who makes a contribution to Robolectric to know that we, and the rest of the community, appreciate their efforts very much, and give our heartfelt thanks for all of the great contributions we have already received.

Tuesday, January 18, 2011

How to test HTTP requests

Many, if not most, Android applications communicate with remote servers via the HTTP protocol. The Android SDK provides facilities for accomplishing this by including the Apache HttpClient library, and Robolectric instruments this library in order to make it easier to write test code for interactions that occur over HTTP.

In most cases the remote calls that your application makes will cause some kind of work to be done that cannot be done directly on the phone. Perhaps your application will cause money to be transferred between bank accounts, an email to be sent to all of your friends, or maybe it will cause your microwave to start cooking dinner for you. During the process of developing and testing your application these calls will be made thousands of times, often incorrectly. In order to prevent application development from draining your bank account, alienating your friends and burning down your home, Robolectric prevents these calls from actually being made, and instead keeps a record of them and provides a couple of different mechanisms for simulating their results.

The following is a portion of the HttpTest class taken from the RobolectricSample application:

@RunWith(RobolectricTestRunner.class)
public class HttpTest {
    @Test
    public void testGet_FormsCorrectRequest_noBasicAuth()
            throws Exception {
        Robolectric.addPendingHttpResponse(200, "OK");

        new Http().get(
            "www.example.com",
            Maps.<String, String>newHashMap(),
            null,
            null);

        assertThat(
            ((HttpUriRequest) Robolectric.getSentHttpRequest(0)).getURI(),
            equalTo(URI.create("www.example.com")));
    }
}

This simple test demonstrates how to set up a simulated HttpResponse, make an HTTP GET request, and then retrieve and examine the contents of that request. In real life the call to new Http().get(... would actually be a call into application code that you expect to make an HTTP request, and the utility of this test would be to ensure that the request you were expecting was made and made correctly.

The call to Robolectric.addPendingResponse() is needed because in this scenario Robolectric is acting in the role of a mock server and it needs to know how to respond to the HTTP request in order for the test to proceed, if it doesn't then it throws a RuntimeException. In this test we are checking the request set up and would rather not have to set up the response which we don't care about. Robolectric allows us to set up a default response that can be shared among tests:

@RunWith(RobolectricTestRunner.class)
public class HttpTest {
    @Before
    public void setup() {
        Robolectric.setDefaultHttpResponse(200, "OK");
    }
    
    ...
}

Now tests that don't set up their own pending responses will get the default response.

Tuesday, January 11, 2011

How to create your own Shadow Classes

You may find, from time to time, that Robolectric does not have the functionality to support a test that you want to write and you will want to add that functionality on your own. This article will show you how to write and extend Shadow classes to suit your needs and then register them with Robolectric so that they will be available to your tests.

The first step is to write the Shadow class. Here is a simple Shadow for the android Point class:

import android.graphics.Point;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

@Implements(Point.class)
public class ShadowPoint {
    @RealObject private Point realPoint;

    public void __constructor__(int x, int y) {
        realPoint.x = x;
        realPoint.y = y;
    }

    public void __constructor__(Point src) {
        realPoint.x = src.x;
        realPoint.y = src.y;
    }

    @Implementation
    public void set(int x, int y) {
        realPoint.x = x;
        realPoint.y = y;
    }

    @Implementation
    public final void offset(int dx, int dy) {
        realPoint.x += dx;
        realPoint.y += dy;
    }

    @Override @Implementation
    public String toString() {
        return "Point(" + realPoint.x + ", " + realPoint.y + ")";
    }
}

This example illustrates all of the most important aspects of writing Shadows. We start with an @Implements annotation that will tell Robolectric that our class is a Shadow for Android's Point class. This annotation, along with the @Implementation annotation (as shown on the set(), offset(), and toString() methods) are the bread and butter of writing Shadow classes. The @Implementation annotation tells Robolectric that the annotated method is meant to shadow the corresponding method with the same signature on the shadowed class.

Note that ShadowPoint doesn't extend Point. Robolectric creates an association between the two classes behind the scenes based on these annotations. Shadow classes mirror the inheritance hierarchy of the classes they shadow. So, for instance, the Shadow for TextView would be a sub-class of the Shadow for View. It is important to maintain this relationship so that inheritance will work properly within the Shadow class framework.

The @RealObject annotation is used to ask Robolectric to provide instances of the Shadow class with a reference to the object that they are shadowing. The annotated field must be of the same type as the class being shadowed and will automatically be populated by Robolectric when the class is instantiated.

While Robolectric provides control over the behavior of methods, it does not provide the same kind of control over access to member variables. So, when a class such as Point exposes its member variables (which, fortunately, is rare), its Shadow class should maintain the same behavior with respect to those member variables as the original class does. In our example we make ShadowPoints do this by getting and setting values on the fields of the shadowed Point object; the @RealObject annotation is the tool used to access the Point. (Normally a Shadow will store information about the state of the shadowed object in its own fields and ignore the private fields on the shadowed object.)

The @RealObject reference is also useful in any situation where the this reference would be used, such as when an object is expected to pass itself as a parameter to other methods, return a reference to itself, or polymorphically call other methods on itself. For instance, in the following example the onClick() method expects a parameter of type View , so the Shadow must pass a reference to the object it shadows rather than itself.

@Implements(View.class)
public class ShadowView {
    @RealObject protected View realView;

    @Implementation
    public boolean performClick() {
        if (onClickListener != null) {
            onClickListener.onClick(realView);
            return true;
        } else {
            return false;
        }
    }
}

Robolectric grants access to the constructors of shadowed objects by means of the special __constructor__() methods. Shadows themselves are always instantiated via their default constructors and their initial setup can be done at that time. Calls to the constructors of the shadowed class are diverted to the __constructor__() method with the corresponding signature, where the Shadow can perform any further initialization tasks.

If a Shadow needs to gain access to the Shadow of any other object (whether of the same or of a different class), it may use the Robolectric.shadowOf_() method. This may be useful if your Shadow needs to inquire about the state of another shadowed object in a way that the Android API does not normally expose.

Once the Shadow class has been written it must be registered with Robolectric in order to become available for use in your unit tests. This is done by the RobolectricTestRunner during test initialization.

If the new Shadow class is meant to be an addition to the standard set of Robolectric Shadows then it should be added to the list of shadow classes in the getDefaultShadowClasses() method of the com.xtremelabs.Robolectric class. In this case, it is convention to also add a shadowOf() method to the same class and specialize it for the new Shadow.

If you are creating the Shadow for local use, you will need to register it in the bindShadowClasses() method of the subclass of RobolectricTestRunner that you use in the @RunWith annotation on your tests:

public class CustomTestRunner extends RobolectricTestRunner {
    public CustomTestRunner(Class testClass)
            throws InitializationError {
        super(testClass);
    }

    @Override protected void bindShadowClasses() {
        Robolectric.bindShadowClass(ShadowPoint.class);
    }
}

It's also possible to register (or change) the Shadow for an individual test calling Robolectric.bindShadowClass() within the test or set-up method; just make sure you do that before any instances of the class are instantiated by the test.