diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElementTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElementTest.java new file mode 100644 index 0000000..ad4b19e --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElementTest.java @@ -0,0 +1,107 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.view; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; +import javafx.scene.control.Button; +import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; +import org.junit.jupiter.api.Test; +import org.testfx.framework.junit5.ApplicationTest; + +class ViewElementTest extends ApplicationTest { + + /** + * View element instance used for testing. + * */ + private ViewElementTest.GenericViewElement testViewElement; + + /** + * Root of generic view instance. + * */ + private Pane rootPane; + + @Override + public void start(final Stage stage) { + rootPane = new Pane(); + testViewElement = new ViewElementTest.GenericViewElement(rootPane); + } + + @Test + void constructorSetsValuesAsExpected() { + assertEquals(rootPane, testViewElement.getRootPane()); + } + + @Test + void constructorThrowsExceptionWhenIllegalArguments() { + assertDoesNotThrow( + () -> new ViewElementTest.GenericViewElement(new VBox()) + ); + + assertThrows(IllegalArgumentException.class, + () -> new ViewElementTest.GenericViewElement(null) + ); + } + + @Test + void setOnActionThrowsExceptionOnIllegalArguments() { + assertThrows(IllegalArgumentException.class, + () -> testViewElement.setOnAction( + GenericViewActions.UNUSED_TEST_ACTION, + () -> testViewElement.setButtonPressed() + ) + ); + } + + private enum GenericViewActions { + /** + * Action used for testing purposes. + * */ + TEST_ACTION, + + /** + * Unused test action to check exception throwing. + * */ + UNUSED_TEST_ACTION + } + + /** + * Test class meant for simulating a view element instance. + * + * @see ViewElement + * */ + private static class GenericViewElement extends ViewElement { + private boolean buttonPressed = false; + private Button interactableButton; + + protected GenericViewElement(final Pane rootPane) { + super(rootPane, ViewElementTest.GenericViewActions.class); + } + + @Override + protected void initLayout() { + interactableButton = new Button("Click me!"); + registerButton(ViewElementTest.GenericViewActions.TEST_ACTION, interactableButton); + } + + public Button getInteractableButton() { + return interactableButton; + } + + public boolean getButtonPressed() { + return buttonPressed; + } + + public void setButtonPressed() { + buttonPressed = true; + } + + @Override + protected void initStyling() { + // Empty + } + } +} \ No newline at end of file