mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-04-22 16:39:23 -04:00
Introduce ViewLoaderTest and FxmlResource interface
Tests basic loading of FXML resources via the ViewLoader API. Also introduces the FxmlResource abstraction, an interface which the Nagivation.Items enum now implements. This simplifies the process of testing, e.g. in this case testing a non-existent resource without having to add a bogus value to the enum itself. Note the @BeforeClass logic necessary to initialize the JavaFX platform. This is necessary in order to avoid "Toolkit not initialized" exceptions. See http://stackoverflow.com/q/11385604 for details.
This commit is contained in:
parent
ce9aca0d1e
commit
6f830d20c9
@ -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;
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public class ViewLoader {
|
||||
// TODO maybe add more sophisticated caching strategy with removal of rarely accessed items
|
||||
private static final Map<URL, Item> 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);
|
||||
}
|
||||
|
||||
|
75
src/test/java/io/bitsquare/util/ViewLoaderTest.java
Normal file
75
src/test/java/io/bitsquare/util/ViewLoaderTest.java
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user