Better Software - November 2008 - (Page 30) public class Presenter { private Meeting meeting; private MeetingView meetingView; private RoomScheduler roomScheduler; public Presenter(Meeting meeting, MeetingView meetingView, RoomScheduler roomScheduler) { this.meeting = meeting; this.meetingView = meetingView; this.roomScheduler = roomScheduler; } /** * Callback when the view’s capacity text box changes * * @param textField the capacity TextBox widget */ public void requiredCapacityChanged(HasText textField) { meeting.setCapacity(Integer.parseInt(textField.getText())); if (!roomScheduler.canAcceptCapacityFor(meeting)) { meetingView.disableSaveButton(); } } protected Meeting getMeeting() { return meeting; } } Listing 4 package com.google.gwt.user.client.ui; public interface HasText { /** * Gets this object’s text. */ String getText(); /** * Sets this object’s text. * * @param text the object’s new text */ void setText(String text); } Listing 5 public class FakeTextContainer implements HasText { private String text; public FakeTextContainer(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } } Listing 6 plain Java objects that don’t require any JavaScript, so these tests run quickly. Note the argument to requiredCapacityChanged is of the type HasText. This turns out to be an interface that is part of the GWT libraries, as shown in listing 5. This simple interface is used by many 30 BETTER SOFTWARE NOVEMBER 2008 GWT components and allows manipulation of a widget’s text contents, including the TextBox in our example. This interface is extremely useful for testing because we don’t need to pass in a real TextBox. Thus, we avoid instantiating a text input in the DOM, requiring our test to extend GWTTestCase to run in www.StickyMinds.com a real browser. In listing 6, I’ve made a simple, fake implementation that wraps a string. And finally, the view implementation is shown in listing 7. As you can see, there’s not much logic here. Most of the code is involved in setting up the event listeners and config- http://www.StickyMinds.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.