diff --git a/src/main/java/io/bitsquare/gui/Navigation.java b/src/main/java/io/bitsquare/gui/Navigation.java index 350d00637a..13668f3504 100644 --- a/src/main/java/io/bitsquare/gui/Navigation.java +++ b/src/main/java/io/bitsquare/gui/Navigation.java @@ -138,12 +138,16 @@ public class Navigation { void onNavigationRequested(Item... items); } + public interface FxmlResource { + String getFxmlUrl(); + } + /////////////////////////////////////////////////////////////////////////////////////////// // Enum /////////////////////////////////////////////////////////////////////////////////////////// - public static enum Item { + public static enum Item implements FxmlResource { /////////////////////////////////////////////////////////////////////////////////////////// // Application @@ -221,6 +225,7 @@ public class Navigation { this.fxmlUrl = fxmlUrl; } + @Override public String getFxmlUrl() { return fxmlUrl; } diff --git a/src/main/java/io/bitsquare/util/ViewLoader.java b/src/main/java/io/bitsquare/util/ViewLoader.java index d5d0a59c3a..cefdb4db99 100644 --- a/src/main/java/io/bitsquare/util/ViewLoader.java +++ b/src/main/java/io/bitsquare/util/ViewLoader.java @@ -55,7 +55,7 @@ public class ViewLoader { // TODO maybe add more sophisticated caching strategy with removal of rarely accessed items private static final Map cachedGUIItems = new HashMap<>(); - public ViewLoader(Navigation.Item navItem, boolean useCaching) { + public ViewLoader(Navigation.FxmlResource navItem, boolean useCaching) { this.url = ViewLoader.class.getResource(navItem.getFxmlUrl()); if (this.url == null) { throw new FatalException("'%s' could not be loaded as a resource", navItem.getFxmlUrl()); @@ -70,7 +70,7 @@ public class ViewLoader { } } - public ViewLoader(Navigation.Item navItem) { + public ViewLoader(Navigation.FxmlResource navItem) { this(navItem, true); } diff --git a/src/test/java/io/bitsquare/util/ViewLoaderTest.java b/src/test/java/io/bitsquare/util/ViewLoaderTest.java new file mode 100644 index 0000000000..9df45f531d --- /dev/null +++ b/src/test/java/io/bitsquare/util/ViewLoaderTest.java @@ -0,0 +1,75 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + +package io.bitsquare.util; + +import io.bitsquare.FatalException; +import io.bitsquare.di.BitsquareModule; +import io.bitsquare.gui.Navigation; + +import com.google.inject.Guice; +import com.google.inject.Injector; + +import javafx.application.Application; +import javafx.stage.Stage; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ViewLoaderTest { + + public static class TestApp extends Application { + @Override + public void start(Stage primaryStage) throws Exception { + } + } + + @BeforeClass + public static void initJavaFX() { + Thread t = new Thread("JavaFX Init Thread") { + public void run() { + Application.launch(TestApp.class); + } + }; + t.setDaemon(true); + t.start(); + } + + + @Before + public void setUp() { + Injector injector = Guice.createInjector(new BitsquareModule()); + ViewLoader.setInjector(injector); + } + + @After + public void tearDown() { + ViewLoader.setInjector(null); + } + + @Test(expected = FatalException.class) + public void loadingBogusFxmlResourceShouldThrow() { + new ViewLoader(() -> "a bogus fxml resource", false).load(); + } + + @Test + public void loadingValidFxmlResourceShouldNotThrow() { + new ViewLoader(Navigation.Item.ACCOUNT, false).load(); + } +} \ No newline at end of file