Saturday, August 15, 2009

TotT: Testing GWT without GwtTestCase

TotT: Testing GWT without GwtTestCase: "

Because GWT (Google Web Toolkit) is new and exciting it's easy to forget the lessons on clean GUI code structure that have been accumulated over nearly thirty years.

GwtTestCase is good for testing UI-specific code in JavaScript. If you find yourself using GwtTestCase for testing non-ui client-side logic you may not have a clear View/Presenter separation. Separating the View and the Presenter allows for more modular, more easily tested code with shorter test times. Model View Presenter was introduced in another episode back in February. Here's how to apply it to a GWT app.

Defining terms:

  • Server – a completely standard backend with no dependency on GWT.
  • Model – the data model. May be shared between the client and server side, or if appropriate you might have a different model for the client side. It has no dependency on GWT.
  • View – the display. Classes in the view wrap GWT widgets, hiding them from the rest of your code. They contain no logic, no state, and are easy to mock.
  • Presenter – all the client side logic and state; it talks to the server and tells the view what to do. It uses RPC mechanisms from GWT but no widgets.

The Presenter, which contains all the interesting client-side code is fully testable in Java!

public void testRefreshPersonListButtonWasClicked() {
IMocksControl easyMockContext = EasyMock.createControl()
mockServer = easyMockContext.createMock(Server.class);
mockView = easyMockContext.createMock(View.class);
List franz = Lists.newArrayList(new Person("Franz", "Mayer"));
mockServer.getPersonList(AsyncCallbackSuccessMatcher<list<person>>reportSuccess(franz)));
mockView.clearPersonList());
mockView.addPerson(“Franz”, “Mayer”);

easyMockContext.replay();
presenter.refreshPersonListButtonClicked();
easyMockContext.verify();
}

Testing failure cases is now as easy as changing expectations. By swapping in the following expectations, the above test goes from testing success to testing that after two server failures, we show an error message.

mockServer.getPersonList(AsyncCallbackFailureMatcher<list<person>>reportFailure(failedExpn))
expectLastCall().times(2); // Ensure the presenter tries twice

mockView.showErrorMessage(“Sorry, please try again later”));

You'll still need an end-to-end test. But all your logic can be tested in small and fast tests.

The Source Code for the Matchers is open-sourced and can be downloaded here: AsyncCallbackSuccessMatcher.java - AsyncCallbackFailureMatcher.java.

Consider using Test Driven Development (TDD) to develop the presenter. It tends to result in higher test coverage, faster and more relevant tests, as well as a better code structure.


This week's episode by David Morgan, Christopher Semturs and Nicolas Wettstein based in Zürich, Switzerland – having a real Mountain View

Toilet-friendly version

AsyncCallbackSuccessMatcher.java

AsyncCallbackFailureMatcher.java.



"

Thursday, August 13, 2009

A flurry of features for feed readers

A flurry of features for feed readers: "

Since our last big launch, we've been thinking about ways to help out our users better share, discover, and consume content in Reader. Today, I'm happy to announce several new features that we hope will further improve the way you use Reader.




Send to...


Send to menuWe've made it easier to share posts you like to Blogger, Twitter, Facebook, and more, with our new 'Send to' feature. (Incidentally, Blogger is celebrating its tenth birthday this month, and we're hoping our friends there will like this little birthday present.)



Just head over to the settings page, and enable the services you want to use. If your favorite service isn't listed (and you're feeling extra geeky), you can create your own 'Send to' link with a URL template.




Send to tab on the settings page



To share an item on one of your sites, simply click the 'Send to' button and choose your service. If you're into keyboard shortcuts, 'shift-t' will do the same.




Feeds from people you follow


When we added following, we tried to make it easier to find and follow people who share similar interests. Now we've gone even further, and made it possible for you to subscribe directly to the blogs, photos, or Twitter updates that anyone you're following has included on their Google profile.




Feeds from Mihai



To quickly subscribe to these sites, click the 'From people you follow' tab on the 'Browse for stuff' page.



More control for mark all as read

Mark all as read menuWe know people can be overwhelmed by too many unread items, and sometimes only want to see recent posts. The 'Mark all as read' button now has a menu that lets you choose to only mark items as read if they're older than your specified time frame. A tip of the hat to Nick Bradbury who pioneered this 'panic button' feature.



Finally, a few small tweaks in this release:


  • When you expand an item in comment view, you now get the full set of actions, enabling you to share, like, and star items without leaving comment view.

  • We added a 'Feeds' start-page option for the iPhone/Android/Pre mobile interface, so you can see a list of your subscriptions when you sign in.

  • There is now an option to show notes when embedding your shared items on other pages as clips.



As always, if you have feedback, please head over to our help group, Twitter, or Get Satisfaction.



"