Skip to content

Commit

Permalink
Create ViewElementTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 25, 2026
1 parent c174d6e commit 757b17b
Showing 1 changed file with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Pane, ViewElementTest.GenericViewActions> {
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
}
}
}

0 comments on commit 757b17b

Please sign in to comment.