From 0933479596d9bbdee0ce4b409669af16f4a54734 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 22 Dec 2023 11:17:40 -0500 Subject: [PATCH] Cleaned up issues, added test for history. --- pom.xml | 13 +++ .../javafx_scene_router/RouteHistory.java | 16 ++++ src/main/java/module-info.java | 3 +- .../javafx_scene_router/RouteHistoryTest.java | 89 +++++++++++++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/andrewlalis/javafx_scene_router/RouteHistoryTest.java diff --git a/pom.xml b/pom.xml index 7c01dbe..07dd66f 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,19 @@ javafx-fxml ${javafx.version} + + + org.junit.jupiter + junit-jupiter-api + 5.10.0 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.10.0 + test + diff --git a/src/main/java/com/andrewlalis/javafx_scene_router/RouteHistory.java b/src/main/java/com/andrewlalis/javafx_scene_router/RouteHistory.java index 63bb612..e9cc2b9 100644 --- a/src/main/java/com/andrewlalis/javafx_scene_router/RouteHistory.java +++ b/src/main/java/com/andrewlalis/javafx_scene_router/RouteHistory.java @@ -124,4 +124,20 @@ public class RouteHistory { } return breadCrumbs; } + + /** + * Gets an unmodifiable view of the items in this history. + * @return This history's items. + */ + public List getItems() { + return Collections.unmodifiableList(items); + } + + /** + * Gets the current item index. + * @return The current item index. + */ + public int getCurrentItemIndex() { + return currentItemIndex; + } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index acf59e9..12979d8 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -2,9 +2,8 @@ * The JavaFX-Scene-Router module. Require this to use the library. */ module com.andrewlalis.javafx_scene_router { - requires javafx.base; - requires javafx.controls; requires javafx.fxml; + requires javafx.graphics; exports com.andrewlalis.javafx_scene_router; } diff --git a/src/test/java/com/andrewlalis/javafx_scene_router/RouteHistoryTest.java b/src/test/java/com/andrewlalis/javafx_scene_router/RouteHistoryTest.java new file mode 100644 index 0000000..38d409d --- /dev/null +++ b/src/test/java/com/andrewlalis/javafx_scene_router/RouteHistoryTest.java @@ -0,0 +1,89 @@ +package com.andrewlalis.javafx_scene_router; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class RouteHistoryTest { + @Test + public void testPush() { + var history = new RouteHistory(); + assertTrue(history.getItems().isEmpty()); + assertEquals(-1, history.getCurrentItemIndex()); + history.push("test", "Hello"); + assertEquals(history.getItems().size(), 1); + assertEquals(0, history.getCurrentItemIndex()); + assertEquals("Hello", history.getCurrentContext()); + } + + @Test + public void testGetCurrentContext() { + var history = new RouteHistory(); + assertNull(history.getCurrentContext()); + history.push("test", 5); + assertEquals(Integer.valueOf(5), history.getCurrentContext()); + history.push("test2", null); + assertNull(history.getCurrentContext()); + history.back(); + assertEquals(Integer.valueOf(5), history.getCurrentContext()); + } + + @Test + public void testBack() { + var history = new RouteHistory(); + assertFalse(history.canGoBack()); + assertTrue(history.back().isEmpty()); + history.push("a", "a"); + assertFalse(history.canGoBack()); + history.push("b", "b"); + assertTrue(history.canGoBack()); + var prev = history.back(); + assertTrue(prev.isPresent()); + assertEquals(new RouteHistoryItem("a", "a"), prev.get()); + assertFalse(history.canGoBack()); + assertTrue(history.back().isEmpty()); + } + + @Test + public void testForward() { + var history = new RouteHistory(); + assertFalse(history.canGoForward()); + assertTrue(history.forward().isEmpty()); + history.push("a", "a"); + history.push("b", "b"); + history.push("c", "c"); + assertFalse(history.canGoForward()); + assertTrue(history.forward().isEmpty()); + history.back(); + assertTrue(history.canGoForward()); + var next = history.forward(); + assertTrue(next.isPresent()); + assertEquals(new RouteHistoryItem("c", "c"), next.get()); + } + + @Test + public void testClear() { + var history = new RouteHistory(); + history.push("a", "a"); + history.clear(); + assertFalse(history.canGoBack()); + assertFalse(history.canGoForward()); + assertNull(history.getCurrentContext()); + assertTrue(history.getItems().isEmpty()); + assertEquals(-1, history.getCurrentItemIndex()); + } + + @Test + public void testGetBreadCrumbs() { + var h1 = new RouteHistory(); + var b1 = h1.getBreadCrumbs(); + assertTrue(b1.isEmpty()); + + var h2 = new RouteHistory(); + h2.push("a", "a"); + var b2 = h2.getBreadCrumbs(); + assertEquals(b2.size(), 1); + assertEquals("a", b2.get(0).route()); + assertTrue(b2.get(0).current()); + } +}