SHOW THE CATEGORY IN THE GUI
Show the category in the GUI. There should be a field that gets filled in when a movie is selected.
Now that we have categories supported in Movie, we can add support for them to the interface layers. We'll start with these tests:
Test 38. Telling the logical layer that a movie is selected causes the presentation layer to be told the category to display.
Test 39. Selecting a movie in the GUI causes the category field to be updated.
To get the category to reflect on the interface, we'll start, as usual, with MovieList-Editor.
Test 38: Telling the logical layer that a movie is selected causes the presentation layer to be told the category to display
We start by extending testSelecting:
public void testSelecting() { mockView.setMovies(movies); control.setVoidCallable(1);
mockView.setNameField(starWars.getName()); control.setVoidCallable(1); mockView.setRatingField(6); control.setVoidCallable(1); mockView.setCategoryField(Category.SCIFI); control.setVoidCallable(1);
mockView.setNameField(starTrek.getName()); control.setVoidCallable(1); mockView.setRatingField(4); control.setVoidCallable(1); mockView.setCategoryField(Category.SCIFI); control.setVoidCallable(1);
mockView.setNameField(stargate.getName()); control.setVoidCallable(1); mockView.setRatingField(0); control.setVoidCallable(1); mockView.setCategoryField(Category.SCIFI); control.setVoidCallable(1);
mockView.setNameField(theShining.getName()); control.setVoidCallable(1); mockView.setRatingField(3); control.setVoidCallable(1); mockView.setCategoryField(Category.HORROR); control.setVoidCallable(1);
control.activate();
MovieListEditor editor = new MovieListEditor(movieList,mockView); editor.select(0); editor.select(1); editor.select(2); editor.select(3);
control.verify(); }
To support this, we have to update setUp() as well. Note: We don't need to create a new fixture (which would require a new TestCase), but the current fixture needs to be more complete. Here it is:
protected void setUp() throws DuplicateMovieException { starWars = new Movie("Star Wars",Category.SCIFI, 5); starTrek = new Movie("Star Trek",Category.SCIFI, 3); stargate = new Movie("Stargate",Category.SCIFI, -1); theShining = new Movie("The Shining",Category.HORROR, 2);
movies =newVector(); movies.add(starWars); movies.add(starTrek); movies.add(stargate); movies.add(theShining);
movieList=newMovieList(); movieList.add(starWars); movieList.add(starTrek); movieList.add(stargate); movieList.add(theShining);
control = EasyMock.controlFor(MovieListEditorView.class); mockView = (MovieListEditorView)control.getMock();
mockView.setEditor(null); control.setDefaultVoidCallable(); }
Now we have the test. Let's make it compile and get to the red bar as quickly as we can. We need to add a setCategoryField() method to MovieListEditorView. Now we have the red bar we were aiming for.
Exercises
4. |
Extend setCategoryField() to make testSelecting() pass.
|
5. |
What problem does this cause? Why? Fix it.
|
Green bar! To the Swing interface layer.
Test 39: Selecting a movie in the GUI causes the category field to be updated
First, we need a test that checks that selecting updates the category field as expected:
public void testSelectUpdatesCategory() { JListOperator movieList = new JListOperator(mainWindow); JTextFieldOperator categoryField = new JTextFieldOperator(mainWindow, "category");
movieList.clickOnItem(0, 1); assertEquals("wrong category from selecting starWars.", Category.SCIFI.toString(), categoryField.getText());
movieList.clickOnItem(3, 1); assertEquals("wrong category from selecting theShining.", Category.HORROR.toString(), categoryField.getText());
movieList.clickOnItem(1, 1); assertEquals("wrong category from selecting starTrek.", Category.SCIFI.toString(), categoryField.getText()); }
Now we need to add a JTextField for displaying the category:
private JTextField categoryField = null;
public void setCategoryField(Category aCategory) { categoryField.setText(aCategory.toString()); }
public void init() { setTitle(); setLayout(); initList(); initNameField(); initRatingCombo(); initCategoryField(); initAddButton(); initUpdateButton(); pack(); }
private void initCategoryField() { categoryField = new JTextField(16); categoryField.setText("category"); getContentPane().add(categoryField); }
Green bar. Next.
Exercises
6. |
We used the toString() method to get the value for the category field, as well as the value from the expected Category to compare against the field contents. What's the problem that we have with the system in its current state? (Hint: look at Category.) Fix it.
|
|
No comments:
Post a Comment