Friday, April 5, 2013

The Test Lifecycle in 2.0

Starting with Robolectric 2.0 alpha 3, there are some changes in how Robolectric prepares to run your tests.

Finding Your Application

By default, Robolectric first looks in your AndroidManifest.xml for the class to load as your application:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp">
  <application android:name=".Application"/>
</manifest>

That will cause a class called com.myapp.Application to be loaded. However, if you create a class with the same name and package, but with "Test" prepended to the class name, Robolectric will load that instead.

package com.myapp;
class TestApplication extends Application {
}

The Application Lifecycle

Robolectric now automatically calls an application's onCreate() method before the test runs, and onTerminate() after the test runs. Note that this a change from earlier versions. If you want to prevent your application's onCreate() method from being called, you can override it in a test class and have it do nothing.

If your test application implements the TestLifecycleApplication interface, it will get called with a few more useful events:

class TestApplication extends Application
    implements TestLifecycleApplication {
  @Override public void beforeTest(Method method) {
  }

  @Override public void prepareTest(Object test) {
  }

  @Override public void afterTest(Method method) {
  }
}

The overall steps taken when running each test, then, are:
  1. Create your application.
  2. Call application.onCreate().
  3. Call application.beforeTest().
  4. Call application.prepareTest().
  5. Run the test.
  6. Call application.onTerminate().
  7. Call application.afterTest().

These changes should make it possible for many people to avoid overriding RobolectricTestRunner altogether. Note that you can still change this by using your own subclass of RobolectricTestRunner, which provides a different TestLifecycle class.

Tuesday, March 5, 2013

Using ActionBarSherlock with Robolectric 2

Hey everybody! You look great today!

We're getting closer to seamless integration with ActionBarSherlock in Robolectric 2, but it's not quite there yet.

To use the ActionBarSherlock library with our 2.0-alpha-2 release, you'll need to 


Tuesday, February 26, 2013

Robolectric 2.0 alpha 2

I'm happy to announce the second alpha version of Robolectric 2!

As before, this release is alpha-quality, and targeted at early adopters. We expect that there'll be bugs. We expect APIs to change before final release. Please use this alpha only if you can deal with some churn.

Big thanks to Square and Pivotal for giving us time to work on this.


Changes since alpha 1:

  • Classes in the android support lib are now fully enabled without the use of shadows.
  • Resource loading is better than ever. Library project support is much more robust.
  • Internally we've switched from Javassist to ASM for the bytecode manipulation that works Robolectric's magic. This and a couple other optimizations give us a huge performance win: one project's test suite here at Square dropped from 163 seconds to 76.
  • We report our Build.VERSION.SDK_INT as 8 (Froyo) for now.
  • Should get along well with ActionBarSherlock.


Changes coming soon:

  • Removing even more shadows (most non-JNI Android classes should work without much shadowing) and enabling more of the SDK.
  • Easier TestRunner setup and configuration.
  • Performance and memory use improvements.
  • Ability to switch Android SDK level on a per-test basis.


Use it!

If you've been using Robolectric 1.x, see my blog post on migrating to Robolectric 2.0.

If you're new to Robolectric, just add this to your pom:
<dependency>
  <groupId>org.robolectric</groupId>
  <artifactId>robolectric</artifactId>
  <version>2.0-alpha-2</version>
</dependency>
Add a @RunWith to your test class and you're ready to go!
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
  ...
}

Caveats:

Currently only android.support.v4's Fragments and LruCache are supported. Support for the core android Fragments and LruCache from later SDK versions will be coming soon. As a workaround, you should be able to use the support versions for now.


Help:

If you find bugs, make an issue on our github project and label it with robolectric-2.

Migrating to Robolectric 2.0

There are several changes in Robolectric 2.0 that will require modifications to existing projects that use the 1.x versions. Here are some quick notes on migrating your Robolectric 1.x-based test suite to 2.0-alpha-2.

Update your pom.xml

Robolectric's maven groupId changed from com.pivotallabs to org.robolectric:
  <dependency>
    <groupId>org.robolectric</groupId>
    <artifactId>robolectric</artifactId>
    <version>2.0-alpha-2</version>
  </dependency>

Package rename

Robolectric moved from com.xtremelabs.robolectric to org.robolectric. A simple search and replace in your code should do the trick:

$ cd my-project-dir
$ find . -name \*.java | xargs -n 10 \
  sed -i.bak 's/com\.xtremelabs\.robolectric/org.robolectric/g'


Update your TestRunners

If you've subclassed RobolectricTestRunner, you'll need to remove some constructors and override some different methods than before. RobolectricTestRunner and related configuration will be overhauled significantly for 2.0-alpha-3, so if you're doing extensive customization you might want to wait until then to upgrade.

If that doesn't scare you off, here's a template for customizing the test runner in alpha-2:

public class MyTestRunner extends RobolectricTestRunner {
  public MyTestRunner(Class<?> testClass) throws InitializationError {
    super(RobolectricContext.bootstrap(MyTestRunner.class, testClass,
        new RobolectricContext.Factory() {
          @Override
          public RobolectricContext create() {
            return new RobolectricContext() {
              // override methods here to customize things...
            };
          }
        }));
  }
}

Specifying qualifiers

In Robolectric 1.x, there were a few ways to goad it into loading resources other than the defaults, including calling setLayoutQualifierSearchPath() on a ViewLoader (and occasionally it worked). In Robolectric 2, the easiest way is to put a @Values annotation on your test:

@Test @Values(qualifiers = "fr-land")
public void shouldShowBucolicFrenchLandscape() throws Exception {
  // ...
}

Wednesday, January 30, 2013

Robolectric 2.0 alpha 1

Heya Android testers! There've been whispers, intimations, and innuendo of a major rev to Robolectric for a while now. We're happy to announce the first alpha version of Robolectric 2. The main goal for 2.0 is to accomplish more realistic Android simulation with much less need for shadow code.

This release is alpha-quality, and targeted at early adopters. We expect that there'll be bugs. We expect APIs to change before final release. Please use this alpha only if you can deal with some churn.

Big thanks to Square for giving us time to work on this.


Changes so far:

  • Using real android fragment manager code from the support lib.
  • More realistic resource loading, including system resources.
  • Much better support for resource qualifiers (e.g. locale, resolution, etc.).
  • Support for library projects.
  • Groundwork to remove many shadows and use actual android sdk code.
  • Moved to org.robolectric package and maven groupId.


Changes coming soon:

  • Removing many more shadows (most non-JNI Android classes should work without much shadowing).
  • Switching from javassist to ASM for faster startup.
  • Easier TestRunner setup and configuration.
  • Performance and memory use improvements.

Use it!


Add this to your pom (note the changed groupId):
<dependency>
  <groupId>org.robolectric</groupId>
  <artifactId>robolectric</artifactId>
  <version>2.0-alpha-1</version>
</dependency>
Add a @RunWith to your test class and you're ready to go!
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
  ...
}

Upgrading:


If you're upgrading from Robolectric 1.x, you'll need to replace any reference to "com.xtremelabs.robolectric" in your project with "org.robolectric".

If you have a custom TestRunner, you'll need to change its configuration somewhat. We're expecting the custom TestRunner API to change soon.


Help:


If you find bugs, make an issue on our github project and label it with robolectric-2.

Thursday, October 25, 2012

Previewing Your Views

Working with complicated view hierarchies in Android can be a pain. Countless times I've found myself in the debugger hunting through LinearLayouts searching for the right RadioButton to toggle, or trying to figure out whether some TextView has gotten added at the right spot.


Robolectric to the rescue!

Hey, here's a thing! Call Robolectric.dump(view) on some view object, and Robolectric will spew some XML to System.out describing that view and its children:

Robolectric.dump(view) (to System.out):
  <AddressLayout>
    <EditText id="id/street"/>
    <EditText id="id/apartment"/>
    <LinearLayout id="id/city_state_row" visibility="GONE">
      <Spinner id="id/city_picker" visibility="GONE"/>
      <EditText id="id/city" visibility="GONE" text="Toronto"/>
      <StateEditor id="id/province" visibility="GONE" text="ON"/>
    </LinearLayout>
    <LinearLayout id="id/postal_country_row">
      <EditText id="id/postal" text="M4Y 1D2"/>
      <TextView id="id/country_picker" text="Canada"/>
    </LinearLayout>
  </AddressLayout>

Another handy method returns any text contained within a view. If multiple TextViews are inside a layout, their text will be appended together:

Robolectric.innerText(view) returns:
  M4Y 1D2 Canada

Note that "Toronto" and "ON" didn't appear because they weren't visible.

Note: you'll need the latest source or the latest maven 1.2-SNAPSHOT.

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.