Wednesday, October 9, 2013

Robolectric 2.2 Released!

We are happy to announce that the latest version of Robolectric, 2.2, has been released. This release represents several months of work by the Robolectric team and includes some notable improvements. Most interesting is that we've moved one step closer to "Android Reality" by removing one of the main fake pieces of Robolectric — the RoboWindow. We now use the real implementation of Window from the AOSP sources. This has far-reaching consequences but the main one is that we now support the native ActionBar. Your code can now call getActionBar() without getting back a null, and you'll be able to interact with an ActionBar in your tests as well.

There are many other features and fixes added in 2.2. Here's a list of them. Please give Robolectric 2.2 a try and let us know how it goes by directing pull requests and bugfixes to our GitHub repository.

We are aware that Robolectric's documentation is a bit out of date, and we are working on it. Expect documentation updates in the next few days.

Thanks — now get back to testing!

Features

  • Robolectric now does not have a fake implementation of Window (RoboWindow). It has been removed and we now use the internal PhoneWindow class. This allows the getActionBar()method in Activity to return a real ActionBar implementation in tests.
  • To create and manage Activity instances, you must now use the ActivityController interface (see Robolectric.buildActivity())
  • Added visible() method to ActivityControllervisible() must be called on an activity in order for View.isShown() to report back anything but false, since it hooks up the Activity's view hierarchy to the WindowManager.
  • Some shadows were removed (ShadowListActivity)

Bug Fixes

  • Animation.hasEnded() works as expected
  • AsyncTasks no longer swallow exceptions that are thrown during onPostExecute()
  • Context.startActivity() calls can now optionally check if the specified Activity is in thePackageManager using Robolectric.checkActivities(true)
  • Many many issues fixed
  • Better support for Windows development environments
  • Documentation updates!

Wednesday, May 29, 2013

Robolectric 2.1

Robolectric 2.1 is released!

Features

  • Layout inflation is now handled by real Android OS code.
  • Improved support for “style” attribute and theme queries in layout attributes.
  • Implemented AssetManager.openXmlResourceParser().
  • Support for using disk-based SQLite databases.

Bug Fixes

  • ActionBarSherlock should work, pretty much.
  • Fixed bugs in Handler.sendMessageAtTime(), and various timing issues.
  • Numerous other fixes and improvements from the Robolectric community! Thank you contributors!

Known Issues

  • Animations aren’t well-supported.

From the creator of Robolectric...

If Burning Man, or LGBT stuff, or a little playground humor turn you off, you can safely skip this post. Depending on your work environment, this may be mildly NSFW.

Shameless plug alert! I have another sheep-related project. Total coincidence. This one is an art piece bound for Burning Man: a giant mobile sheep-bus with a big soundsystem, a social message, some crazy awesome light effects, an unusual mode of entry, and a bunch of other nifty stuff. Oh, and he just happens to be a gay sheep. Not that it makes a difference, right?

Our Kickstarter campaign is finishing in a couple days, and we'd love to make it to $15K. If Robolectric has made your life a little better, consider kicking in some bucks to help us make Robolectric's real-world cousin amazingly awesome!

Don't worry, I don't fancy myself a DJ: we're corralling some really top-notch DJs and live performers, both acclaimed and underground, from around the world to entertain and make magic. This project is bringing together an incredible team of artists, engineers, builders, planners, and performers to create a rolling party. He'll debut at Burning Man 2013, but he'll be making appearances at events off-playa as well.

BAAAHS Kickstarter (I'm the guy at the start of the video)
BAAAHS Facebook page

Thursday, May 23, 2013

Configuring Robolectric 2.0

In Robolectric 1.x, it was necessary to extend RobolectricTestRunner if you wanted to configure things differently.

From 2.0 on, most configuration can be accomplished using either the @Config annotation or by adding a special file on your classpath.

You can add the @Config annotation to individual test methods, or entire test classes.

If you'd like to apply a configuration to your entire project, create a file called "org.robolectric.Config.properties", and place it on your classpath. If you're using maven, you could put it in src/test/resources.

Example:

@Config(qualifiers="fr-land")
public void shouldDisplayThingsInFrench() {
  ...
}

Configurable values currently include which manifest to use, the SDK level and qualifiers to report, and the list of additional shadows to enable. See the @Config annotation documentation for details.

Also, remember that Robolectric will now look for a test version of your Application class, where you can do additional configuration which used to require subclassing RobolectricTestRunner.

Wednesday, May 15, 2013

Robolectric 2.0 final!


Robolectric 2.0 is, at long last, finished! Thanks for all the bug reports over the last week.

We'll have some blog posts in the coming week talking about new features.


Use it!

If you've been using Robolectric 1.x, see the 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</version>
</dependency>
Add a @RunWith to your test class and you're ready to go!
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
  ...
}

Help:

If you find bugs, make an issue on our github project.

Monday, May 13, 2013

Robolectric 2.0 RC2



Robolectric 2.0 is very nearly finalized! Thanks for all the bug reports over the last week.


Unless we find any showstopper bugs, I'll redeploy this as 2.0 final shortly.

Changes since alpha 3:

  • Bug fixes.
  • Performance improvements.

    Use it!

    If you've been using Robolectric 1.x, see the 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-rc2</version>
    </dependency>
    Add a @RunWith to your test class and you're ready to go!
    @RunWith(RobolectricTestRunner.class)
    public class MyActivityTest {
      ...
    }

    Help:

    If you find bugs, make an issue on our github project.

    Thursday, May 9, 2013

    Announcing the Robolectric 2.0 release (candidate)!

    We're very, very pleased to announce that Robolectric 2.0 is now feature complete and nearly ready to be minted (note the subtle lamb pun there)!

    Thank you to Square for giving us Day 4 of the Seven Days of Open Source campaign of free software awesomeness: Announcing Robolectric 2.0 RC.

    Robolectric has a new home!


    To give us more flexible management of access for community contributors, Robolectric is moving to its own GitHub org.

    From now on you'll find the main Robolectric GitHub project here: http://github.com/robolectric/robolectric

    You can update your git repos with this command:
    $ git remote set-url origin git://github.com/robolectric/robolectric.git

    Robolectric has a new home, but the folks behind it remain the same: big thanks to Pivotal Labs, Square, and other backers for time and support!

    Wednesday, May 8, 2013

    Robolectric 2.0 alpha 3


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

    We're now feature-complete on Robolectric 2.0! As before, this release is alpha-quality, and targeted at early adopters. We expect that there'll be bugs. APIs may change somewhat 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 2:

    • Massive realism upgrade: we removed many, many shadows, particularly for views and widgets, so they'll behave much more realistically.
    • Dramatically improved resource loading, with support for themes and styles.
    • Easier TestRunner setup and configuration.
    • The new @Config annotation to specify config for a test class or test method:
      • The SDK level to report: @Config(reportSdk=Build.VERSION_CODES.FROYO)
      • Qualifiers for resource loading: @Config(qualifiers="fr-port-hdpi")
      • Custom shadow classes to enable.
    • For classes and methods that aren't explicitly shadowed, Robolectric now calls through to the real SDK code.
    • Views now complain if you try to pass in a null Context. Don't do it!
    • SDK method execution is now much much faster, especially in cases where we use the actual SDK code rather than a shadow (which will increasingly be the normal case).
    • Performance and memory use improvements.

      Changes coming soon:

      • Ability to switch Android SDK level on a per-test basis. The @Config annotation will allow you to specify which version of the SDK to emulate: @Config(emulateSdk=Build.VERSION_CODES.FROYO)
      • Performance and memory use improvements.

        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-3</version>
        </dependency>
        Add a @RunWith to your test class and you're ready to go!
        @RunWith(RobolectricTestRunner.class)
        public class MyActivityTest {
          ...
        }

        Help:

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

        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.