Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Presenter test done

  • Loading branch information...
commit 19929e3b8807ba36ee5ff8da218c666124a1a8a2 1 parent 413b4e6
@pinfieldharm authored
View
64 src/books/client/Presenter.java
@@ -1,10 +1,9 @@
package books.client;
-
import books.client.model.Model;
import books.client.model.ModelChangeEvent;
import books.client.view.AddButtonClickedEvent;
-import books.client.view.BookPanelImpl;
+import books.client.view.BookPanel;
import books.client.view.BookStackPanel;
import books.client.view.RemoveButtonClickedEvent;
import books.client.view.UpButtonClickedEvent;
@@ -14,7 +13,9 @@
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
-public class Presenter implements AddButtonClickedEvent.Handler, RemoveButtonClickedEvent.Handler, UpButtonClickedEvent.Handler, ModelChangeEvent.Handler {
+public class Presenter implements AddButtonClickedEvent.Handler,
+ RemoveButtonClickedEvent.Handler, UpButtonClickedEvent.Handler,
+ ModelChangeEvent.Handler {
private final HandlerManager eventBus;
private final View view;
@@ -27,18 +28,27 @@ public Presenter(final HandlerManager eventBus, final View view,
this.view = view;
this.model = model;
- /* Bind low-level UI events */
- view.getInputPanel().getAddButton().addClickHandler(new AddButtonUIClickHandler());
+ bindEventHandlers(eventBus, view);
+ }
+
+ private void bindEventHandlers(final HandlerManager eventBus,
+ final View view) {
+ view.getInputPanel().getAddButton().addClickHandler(
+ new AddButtonUIClickHandler());
- /* Bind to higher-level UI events */
eventBus.addHandler(AddButtonClickedEvent.TYPE, this);
eventBus.addHandler(RemoveButtonClickedEvent.TYPE, this);
eventBus.addHandler(UpButtonClickedEvent.TYPE, this);
- /* Bind to model events */
eventBus.addHandler(ModelChangeEvent.TYPE, this);
}
+ protected final class AddButtonUIClickHandler implements ClickHandler {
+ public void onClick(ClickEvent event) {
+ eventBus.fireEvent(new AddButtonClickedEvent());
+ }
+ }
+
public void onAddButtonClicked(AddButtonClickedEvent addButtonClickedEvent) {
String title = view.getInputPanel().getAddBox().getText().trim();
if (title.length() > 0) {
@@ -47,15 +57,14 @@ public void onAddButtonClicked(AddButtonClickedEvent addButtonClickedEvent) {
view.getInputPanel().getAddBox().setText("");
}
- public void onModelChange(ModelChangeEvent addEvent) {
+ public void onModelChange(ModelChangeEvent event) {
BookStackPanel stackPanel = view.getStackPanel();
stackPanel.clear();
-
- for (int i = 0; i < model.getTitles().size(); i++) {
- String title = model.getTitles().get(i);
- BookPanelImpl bookPanel = stackPanel.addBookPanel(title);
- bookPanel.getRemoveButton().addClickHandler(new RemoveButtonUIClickHandler(i));
- bookPanel.getUpButton().addClickHandler(new UpButtonUIClickHandler(i));
+ for (int pos = 0; pos < model.getTitles().size(); pos++) {
+ String title = model.getTitles().get(pos);
+ BookPanel bookPanel = stackPanel.addBookPanel(title);
+ bookPanel.getRemoveButton().addClickHandler(new RemoveButtonUIClickHandler(pos));
+ bookPanel.getUpButton().addClickHandler(new UpButtonUIClickHandler(pos));
}
}
@@ -63,39 +72,32 @@ public void onRemoveButtonClicked(RemoveButtonClickedEvent removeButtonClickedEv
model.removeTitle(removeButtonClickedEvent.getPosition());
}
- public void onUpButtonClicked(UpButtonClickedEvent removeButtonClickedEvent) {
- model.moveTitleUp(removeButtonClickedEvent.getPosition());
- }
-
- protected final class UpButtonUIClickHandler implements ClickHandler {
+ protected final class RemoveButtonUIClickHandler implements ClickHandler {
private final int position;
- protected UpButtonUIClickHandler(int position) {
+ protected RemoveButtonUIClickHandler(int position) {
this.position = position;
}
public void onClick(ClickEvent event) {
- eventBus.fireEvent(new UpButtonClickedEvent(position));
+ eventBus.fireEvent(new RemoveButtonClickedEvent(position));
}
}
- protected final class RemoveButtonUIClickHandler implements ClickHandler {
+ public void onUpButtonClicked(UpButtonClickedEvent removeButtonClickedEvent) {
+ model.moveTitleUp(removeButtonClickedEvent.getPosition());
+ }
+
+ protected final class UpButtonUIClickHandler implements ClickHandler {
private final int position;
- protected RemoveButtonUIClickHandler(int position) {
+ protected UpButtonUIClickHandler(int position) {
this.position = position;
}
public void onClick(ClickEvent event) {
- eventBus.fireEvent(new RemoveButtonClickedEvent(position));
- }
- }
-
- protected final class AddButtonUIClickHandler implements ClickHandler {
- public void onClick(ClickEvent event) {
- eventBus.fireEvent(new AddButtonClickedEvent());
+ eventBus.fireEvent(new UpButtonClickedEvent(position));
}
}
-
}
View
11 src/books/client/view/BookPanelImpl.java
@@ -3,11 +3,13 @@
*/
package books.client.view;
+import com.google.gwt.event.dom.client.HasClickHandlers;
+import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
-public final class BookPanelImpl extends HorizontalPanel {
+public final class BookPanelImpl extends HorizontalPanel implements BookPanel {
private Image upButton;
private Image removeButton;
@@ -32,18 +34,17 @@ public BookPanelImpl(String title) {
}
- public Image getUpButton() {
+ public HasClickHandlers getUpButton() {
return upButton;
}
- public Image getRemoveButton() {
+ public HasClickHandlers getRemoveButton() {
return removeButton;
}
- public Label getLabel() {
+ public HasText getLabel() {
return label;
}
-
}
View
2  src/books/client/view/BookStackPanel.java
@@ -4,6 +4,6 @@
void clear();
- BookPanelImpl addBookPanel(String title);
+ BookPanel addBookPanel(String title);
}
View
2  src/books/client/view/BookStackPanelImpl.java
@@ -4,7 +4,7 @@
public class BookStackPanelImpl extends VerticalPanel implements BookStackPanel {
- public BookPanelImpl addBookPanel(String title) {
+ public BookPanel addBookPanel(String title) {
BookPanelImpl bookPanel = new BookPanelImpl(title);
add(bookPanel);
return bookPanel;
View
59 test/books/client/PresenterTest.java
@@ -1,16 +1,22 @@
package books.client;
-import org.junit.Before;
-import org.junit.Test;
-
+import static java.util.Arrays.asList;
+import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.isA;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
import books.client.model.Model;
import books.client.model.ModelChangeEvent;
import books.client.view.AddButtonClickedEvent;
+import books.client.view.BookPanel;
+import books.client.view.BookStackPanel;
import books.client.view.InputPanel;
import books.client.view.RemoveButtonClickedEvent;
import books.client.view.UpButtonClickedEvent;
@@ -30,6 +36,7 @@
private HasClickHandlers addButton;
private InputPanel inputPanel;
private HasText addBox;
+ private BookStackPanel bookStackPanel;
@Before
public void setUp() {
@@ -47,6 +54,9 @@ public void setUp() {
when(addBox.getText()).thenReturn("");
when(inputPanel.getAddBox()).thenReturn(addBox);
+ bookStackPanel = mock(BookStackPanel.class);
+ when(view.getStackPanel()).thenReturn(bookStackPanel);
+
presenter = new Presenter(eventBus, view, model);
}
@@ -113,4 +123,43 @@ public void uiHandlerForAddButtonFiresEventBusEvent() {
presenter.new AddButtonUIClickHandler().onClick(mock(ClickEvent.class));
verify(eventBus).fireEvent(isA(AddButtonClickedEvent.class));
}
+
+ @Test
+ public void clearsStackPanelAndAddsEachTitleWhenModelChanges() {
+ ArrayList<String> titles = new ArrayList<String>(asList("foo", "bar", "baz"));
+ when(model.getTitles()).thenReturn(titles);
+ BookPanel bookPanel = mockBookPanel();
+ when(bookStackPanel.addBookPanel(Mockito.anyString())).thenReturn(bookPanel);
+
+ presenter.onModelChange(new ModelChangeEvent());
+
+ verify(bookStackPanel).clear();
+ verify(bookStackPanel).addBookPanel("foo");
+ verify(bookStackPanel).addBookPanel("bar");
+ verify(bookStackPanel).addBookPanel("baz");
+ }
+
+ @Test
+ public void addsPresenterAsEventHandlerToNewPanelsWhenModelChanges() {
+ ArrayList<String> titles = new ArrayList<String>(asList("foo"));
+ when(model.getTitles()).thenReturn(titles);
+ BookPanel bookPanel = mockBookPanel();
+ when(bookStackPanel.addBookPanel(Mockito.anyString())).thenReturn(bookPanel);
+
+ presenter.onModelChange(new ModelChangeEvent());
+
+ verify(bookPanel.getRemoveButton()).addClickHandler(isA(Presenter.RemoveButtonUIClickHandler.class));
+ verify(bookPanel.getUpButton()).addClickHandler(isA(Presenter.UpButtonUIClickHandler.class));
+ }
+
+ private BookPanel mockBookPanel() {
+ BookPanel bookPanel = mock(BookPanel.class);
+ HasClickHandlers removeButton = mock(HasClickHandlers.class);
+ when(bookPanel.getRemoveButton()).thenReturn(removeButton);
+ HasClickHandlers addButton = mock(HasClickHandlers.class);
+ when(bookPanel.getUpButton()).thenReturn(addButton);
+ return bookPanel;
+ }
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.