Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error style to result display #9

Merged
merged 14 commits into from
Feb 19, 2019
Merged
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private CommandResult executeCommand(String commandText) throws CommandException
try {
CommandResult commandResult = logic.execute(commandText);
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());
resultDisplay.setFeedbackSuccessToUser(commandResult.getFeedbackToUser());

if (commandResult.isShowHelp()) {
handleHelp();
Expand All @@ -194,7 +194,7 @@ private CommandResult executeCommand(String commandText) throws CommandException
return commandResult;
} catch (CommandException | ParseException e) {
logger.info("Invalid command: " + commandText);
resultDisplay.setFeedbackToUser(e.getMessage());
resultDisplay.setFeedbackErrorToUser(e.getMessage());
throw e;
}
}
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/seedu/address/ui/ResultDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Region;
Expand All @@ -11,6 +12,8 @@
*/
public class ResultDisplay extends UiPart<Region> {

public static final String ERROR_STYLE_CLASS = "error";

private static final String FXML = "ResultDisplay.fxml";

@FXML
Expand All @@ -20,7 +23,37 @@ public ResultDisplay() {
super(FXML);
}

public void setFeedbackToUser(String feedbackToUser) {
/**
* Sets the command box style to use the default style.
*/
private void setStyleToDefault() {
resultDisplay.getStyleClass().remove(ERROR_STYLE_CLASS);
}

/**
* Sets the command box style to indicate a failed command.
*/
private void setStyleToIndicateCommandFailure() {
ObservableList<String> styleClass = resultDisplay.getStyleClass();

if (styleClass.contains(ERROR_STYLE_CLASS)) {
return;
}

styleClass.add(ERROR_STYLE_CLASS);
}

public void setFeedbackErrorToUser(String feedbackToUser) {
setStyleToIndicateCommandFailure();
setFeedbackToUser(feedbackToUser);
}

public void setFeedbackSuccessToUser(String feedbackToUser) {
setStyleToDefault();
setFeedbackToUser(feedbackToUser);
}

private void setFeedbackToUser(String feedbackToUser) {
epicfailname marked this conversation as resolved.
Show resolved Hide resolved
requireNonNull(feedbackToUser);
resultDisplay.setText(feedbackToUser);
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/guitests/guihandles/ResultDisplayHandle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package guitests.guihandles;

import javafx.collections.ObservableList;
import javafx.scene.control.TextArea;

/**
Expand All @@ -19,4 +20,11 @@ public ResultDisplayHandle(TextArea resultDisplayNode) {
public String getText() {
return getRootNode().getText();
}

/**
* Returns the list of style classes present in the command box.
*/
public ObservableList<String> getStyleClass() {
return getRootNode().getStyleClass();
}
}
75 changes: 72 additions & 3 deletions src/test/java/seedu/address/ui/ResultDisplayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -12,24 +14,91 @@ public class ResultDisplayTest extends GuiUnitTest {
private ResultDisplay resultDisplay;
private ResultDisplayHandle resultDisplayHandle;

private ArrayList<String> defaultStyleOfResultDisplay;
private ArrayList<String> errorStyleOfResultDisplay;

@Before
public void setUp() {
resultDisplay = new ResultDisplay();
uiPartRule.setUiPart(resultDisplay);

resultDisplayHandle = new ResultDisplayHandle(getChildNode(resultDisplay.getRoot(),
ResultDisplayHandle.RESULT_DISPLAY_ID));

defaultStyleOfResultDisplay = new ArrayList<>(resultDisplayHandle.getStyleClass());

errorStyleOfResultDisplay = new ArrayList<>(defaultStyleOfResultDisplay);
errorStyleOfResultDisplay.add(CommandBox.ERROR_STYLE_CLASS);
}

@Test
public void display() {
public void defaultDisplay_emptyDisplay() {
// default result text
guiRobot.pauseForHuman();
assertEquals("", resultDisplayHandle.getText());
}

@Test
public void emptyFeedback_emptyDisplay() {
guiRobot.interact(() -> resultDisplay.setFeedbackSuccessToUser(""));
guiRobot.pauseForHuman();
assertEquals("", resultDisplayHandle.getText());
assertEquals(defaultStyleOfResultDisplay, resultDisplayHandle.getStyleClass());
}
@Test
epicfailname marked this conversation as resolved.
Show resolved Hide resolved
public void successfulFeedback() {
guiRobot.interact(() -> resultDisplay.setFeedbackSuccessToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForSuccessfulFeedback(resultDisplayHandle);
}

@Test
public void errorFeedback() {
guiRobot.interact(() -> resultDisplay.setFeedbackErrorToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForErrorFeedback(resultDisplayHandle);
}

@Test
public void consecutiveFeedBack() {
// new error result received
guiRobot.interact(() -> resultDisplay.setFeedbackErrorToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForErrorFeedback(resultDisplayHandle);

// new error result received
guiRobot.interact(() -> resultDisplay.setFeedbackErrorToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForErrorFeedback(resultDisplayHandle);

// new success result received
guiRobot.interact(() -> resultDisplay.setFeedbackSuccessToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForSuccessfulFeedback(resultDisplayHandle);

// new success result received
guiRobot.interact(() -> resultDisplay.setFeedbackSuccessToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForSuccessfulFeedback(resultDisplayHandle);

// new result received
guiRobot.interact(() -> resultDisplay.setFeedbackToUser("Dummy feedback to user"));
// new error result received
guiRobot.interact(() -> resultDisplay.setFeedbackErrorToUser("Dummy feedback to user"));
guiRobot.pauseForHuman();
assertEquals("Dummy feedback to user", resultDisplayHandle.getText());
assertStyleForErrorFeedback(resultDisplayHandle);
}

private void assertStyleForSuccessfulFeedback(ResultDisplayHandle resultDisplayHandle) {
assertEquals(defaultStyleOfResultDisplay, resultDisplayHandle.getStyleClass());
}

private void assertStyleForErrorFeedback(ResultDisplayHandle resultDisplayHandle) {
assertEquals(errorStyleOfResultDisplay, resultDisplayHandle.getStyleClass());
}
}