Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker
Joe Moore

Android Tidbits 6/13/2011: Great Expectations

Joe Moore
Tuesday, June 14, 2011

Great Expectations

Most of our Android projects are using great-expectations, which brings Jasmine-style test assertions. Thanks, Xian, for writing this!

Robolectric Enhancements — Stay Tuned

We have a bunch of Robolectric enhancements, including the ability to wire up BroadcastReceivers by just declaring them in AndroidManifest.xml. We’ll have to put some pull-requests together soon.

Roboguice uses Robolectric!

We use Roboguice on most of our Android projects for dependency injection. We discovered that Roboguice is using Robolectric for unit testing. Awesome!

Roboguice using Robolectric

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Tyler Schultz

Android: Unit testing custom views

Tyler Schultz
Saturday, June 11, 2011

Android Activity classes can become gigantic and unwieldy if you’re not careful. Testing complex Activities requires complex setup. To reduce that pain we try our best to keep the Activity lean. This means getting logic out of the Activity. One technique we use is to create custom views that we can test in isolation.

Here is an example of a view class that starts out with a ProgressBar spinner, and when the text is set, the spinner is hidden, and the text becomes visible:

public class LoadingTextView extends RelativeLayout {
    ...
    public void stopLoadingAndSetText(int text_res_id) {
        TextView textView = (TextView) findViewById(loading_text_text_view);
        textView.setText(text_res_id);
        textView.setVisibility(VISIBLE);

        findViewById(loading_text_spinner).setVisibility(View.GONE);
    }
}

Here is the layout xml that is associated with the custom view class. Notice the reference to the LoadingTextView class in the outermost tag (which has lost its formatting!!!):

<?xml version="1.0" encoding="utf-8"?>

<com.pivotallabs.views.loadingtextview android:layout_height="wrap_content" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

    <linearlayout android:gravity="center" android:padding="10dip" android:orientation="horizontal" android:layout_height="wrap_content" android:id="@+id/loading_text_spinner" android:layout_width="fill_parent">
        <progressbar android:layout_height="wrap_content" android:layout_width="wrap_content" />
        <textview android:paddingLeft="10dip" android:text="Loading..." android:layout_height="wrap_content" android:layout_width="wrap_content" />
    </linearlayout>

    <textview android:visibility="invisible" android:layout_height="wrap_content" android:id="@+id/loading_text_text_view" android:layout_width="fill_parent" />

</com.pivotallabs.views.loadingtextview>

Robolectric will allow you to instantiate the Android subclasses in the JVM, but the problem is that newing the custom view class will not inflate the elements the view class expects to be present. To get around this you can take advantage of Robolectric’s simulated view inflating functionality. Use the LayoutInflator.from() method to get an inflator which can be used to inflate your view:

@RunWith(RobolectricTestRunner.class)
public class LoadingTextViewTest {
    private LoadingTextView loadingTextView;
    private View loadingSpinner;
    private TextView loadingTextTextView;

    @Before
    public void setUp() throws Exception {
        loadingTextView = (LoadingTextView) LayoutInflater.from(new Activity()).inflate(R.layout.loading_text, null);
        loadingSpinner = loadingTextView.findViewById(R.id.loading_text_spinner);
        loadingTextTextView = (TextView) loadingTextView.findViewById(R.id.loading_text_text_view);
    }

    @Test
    public void testStopLoadingAndSetTextShouldHideTheSpinnerAndShowTheTextView() throws Exception {
        assertThat(loadingSpinner.getVisibility(), equalTo(View.VISIBLE));
        assertThat(loadingTextTextView.getVisibility(), equalTo(View.INVISIBLE));

        loadingTextView.stopLoadingAndSetText(R.string.unit_tests_ftw);

        assertThat(loadingSpinner.getVisibility(), equalTo(View.GONE));
        assertThat(loadingTextTextView.getVisibility(), equalTo(View.VISIBLE));
    }

    @Test
    public void testStopLoadingAndSEtTextShouldSetTheTextOnTheTextView() {
        loadingTextView.stopLoadingAndSetText(R.string.unit_tests_ftw);

        assertThat((String) loadingTextTextView.getText(), equalTo("Unit Tests FTW!!!"));
    }
}

You can run these example tests yourself by checking out the RobolectricSample project on github.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Joe Moore

Android Tidbits 6/9/2011: CD2M Resources

Joe Moore
Friday, June 10, 2011

C2DM

One of our projects just started implementing the Android Cloud to Device Messaging (C2DM) framework. We’ll keep you posted as we progress through the many pieces of this implementation. Various resources include:

  • Official Google code page: Google Projects for Android: C2DM
  • Sign up for the service — you’ll need a Google account, like GMail or a hosted Google account.
  • There is no official Android client library for handling these messages. There is a de-facto standard set of classes, as used in JumpNote and Google Chrome to Phone Extension. Most blog and forum posts say something like “Download those classes and tweak as needed.”
  • Wei Huang from Google posted an article about implementing C2DM.
  • Now for the Ruby part — wait, Ruby? Yes, there is a big server-side component to C2DM. Your message-pushing server must not obtain an authorization token from Google to communicate with the service, but also keep track of the authorization tokens from each device that needs to receive push notifications. We are implementing a server-side API for our devices to register their C2DM tokens. Also, the awesome folks at GroupMe have open sourced a c2dm gem for Ruby servers to both authorize with Google and post notifications.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Joe Moore

Android Tidbits 6/8/2011: Warning! Warning!

Joe Moore
Thursday, June 9, 2011

Un-shadowed Method Warnings!

In response to yesterday’s question about Un-shadowed Method Warnings, Pivot Tyler points out that you can turn these on by calling Robolectric.logMissingInvokedShadowMethods(). Thanks!

Bad cached-robolectric-classes.jar

On two workstations every unit test was failing with the following error:

// …many levels of stack trace, finally:
Caused by: com.xtremelabs.robolectric.bytecode.IgnorableClassNotFoundException: msg because of javassist.NotFoundException: android.content.DialogInterface$OnShowListener
at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:80)
at javassist.Loader.findClass(Loader.java:340)

Cause: there was an old tmp/cached-robolectric-classes.jar that was causing these errors and our tests ran successfully after deleting it. That’s two answers from Pivot Tyler!

Don’t DDOS Yourself With Your Own App

The Bump Android team wrote an article about a good idea gone wrong. Moral of the story: not all devices behave the same, and this might cripple your servers rather than the devices.

Robolectric Google Group

Join it, contribute, and learn about unit testing your Android apps.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Joe Moore

Pivotal Android Tidbits 06/07/2011

Joe Moore
Wednesday, June 8, 2011

We’re trying an experiment: since we currently have several Android projects at Pivotal Labs, and Android development and testing is hard, we are going to post to the blog the tips, tricks, gotchas, and conundrums we find.

Emulator and Orientation

Android will destroy and recreate activities upon screen orientation change. One way to prevent this is to set android:configChanges="orientation" in your AndroidManifest.xml (article here):

<activity android:name=".activities.MyActivity"
          android:configChanges="orientation"/>

This prevents android from calling onPause() => onDestroy() => onCreate() on a device, but not within the emulator; annoyingly, the Activity is still recreated within the emulator.

Un-shadowed Method Warnings?

Once again we spent about 15 minutes debugging a failing unit test only to find that there was no Robolectric implementation/shadow for one of the Android methods under test (ArrayAdapter#insert in our case). I would be nice to have a “warning mode” where Robolectric would log warning for all un-shadowed methods. I wish we knew the guys who wrote Robolectric…

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Glenn Jahnke

Android Mongo Meetup

Glenn Jahnke
Tuesday, August 31, 2010

There is meeting tonight at 7pm in San Francisco about using MongoDB for mobile backend servers. Will Shulman from MongoLab will be giving the talk. They will also be offering food and prizes to attendees.

More details can be found at:

http://www.sfandroid.org/calendar/14367370/?eventId=14367370&action=detail

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Topics

  • agile (780)
  • rails (113)
  • testing (88)
  • ruby (83)
  • ruby on rails (70)
  • jobs (62)
  • javascript (55)
  • techtalk (44)
  • rspec (38)
  • ironblogger (32)
  • productivity (30)
  • activerecord (29)
  • gogaruco (29)
  • git (28)
  • nyc (27)
  • rubymine (26)
  • bloggerdome (23)
  • mobile (22)
  • process (21)
  • pivotal tracker (20)
  • cucumber (20)
  • jasmine (19)
  • design (18)
  • ios (18)
  • webos (17)
  • objective-c (17)
  • android (16)
  • palm (16)
  • "soft" ware (16)
  • fun (15)
  • tracker ecosystem (15)
  • ci (15)
  • cedar (15)
  • rails3 (14)
  • performance (14)
  • bdd (14)
  • gem (13)
  • css (13)
  • tdd (13)
  • selenium (12)
  • goruco (12)
  • bundler (12)
  • meetup (11)
  • railsconf (11)
  • nyc-standup (11)
  • capybara (10)
  • mac (10)
  • mojo (10)
  • chef (10)
  • api (10)
Subscribe to android Feed
  1. ←
  2. 1
  3. 2
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >