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:
Chris Beams 2014-11-03 18:41:12 +01:00
parent ce9aca0d1e
commit 6f830d20c9
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
3 changed files with 83 additions and 3 deletions

View File

@ -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;
}

View File

@ -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);
}

View 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();
}
}