GriffonTestFXClassRule.java
001 /*
002  * Copyright 2008-2016 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.javafx.test;
017 
018 import griffon.core.ApplicationEvent;
019 import griffon.core.RunnableWithArgs;
020 import griffon.core.env.Environment;
021 import griffon.exceptions.GriffonException;
022 import griffon.javafx.JavaFXGriffonApplication;
023 import javafx.stage.Window;
024 import org.codehaus.griffon.runtime.core.DefaultGriffonApplication;
025 import org.codehaus.griffon.runtime.javafx.TestJavaFXGriffonApplication;
026 import org.junit.rules.TestRule;
027 import org.junit.runner.Description;
028 import org.junit.runners.model.Statement;
029 import org.testfx.api.FxToolkit;
030 
031 import javax.annotation.Nonnull;
032 import javax.annotation.Nullable;
033 import java.util.concurrent.TimeoutException;
034 
035 import static com.jayway.awaitility.Awaitility.await;
036 import static griffon.javafx.test.TestContext.getTestContext;
037 import static griffon.util.GriffonNameUtils.requireNonBlank;
038 import static java.util.Objects.requireNonNull;
039 
040 /**
041  * A JUnit Rule that starts the application once per test class.
042  *
043  @author Andres Almiray
044  @since 2.3.0
045  */
046 public class GriffonTestFXClassRule extends TestFX implements TestRule {
047     protected String windowName;
048     protected String[] startupArgs;
049     protected Class<? extends TestJavaFXGriffonApplication> applicationClass;
050     protected JavaFXGriffonApplication application;
051     private boolean failures = false;
052 
053     public GriffonTestFXClassRule(@Nonnull String windowName) {
054         this(TestJavaFXGriffonApplication.class, windowName, DefaultGriffonApplication.EMPTY_ARGS);
055     }
056 
057     public GriffonTestFXClassRule(@Nonnull Class<? extends TestJavaFXGriffonApplication> applicationClass, @Nonnull String windowName) {
058         this(applicationClass, windowName, DefaultGriffonApplication.EMPTY_ARGS);
059     }
060 
061     public GriffonTestFXClassRule(@Nonnull Class<? extends TestJavaFXGriffonApplication> applicationClass, @Nonnull String windowName, @Nonnull String[] startupArgs) {
062         this.applicationClass = requireNonNull(applicationClass, "Argument 'applicationClass' must not be null");
063         this.windowName = requireNonBlank(windowName, "Argument 'windowName' cannot be blank");
064         requireNonNull(startupArgs, "Argument 'startupArgs' must not be null");
065         this.startupArgs = new String[startupArgs.length];
066         System.arraycopy(startupArgs, 0this.startupArgs, 0, startupArgs.length);
067         if (!Environment.isSystemSet()) {
068             System.setProperty(Environment.KEY, Environment.TEST.getName());
069         }
070     }
071 
072     public void setup() {
073         initialize();
074 
075         try {
076             FxToolkit.registerPrimaryStage();
077 
078             application = (JavaFXGriffonApplicationFxToolkit.setupApplication(applicationClass);
079             WindowShownHandler startingWindow = new WindowShownHandler(windowName);
080             application.getEventRouter().addEventListener(ApplicationEvent.WINDOW_SHOWN.getName(), startingWindow);
081 
082             await().until(() -> startingWindow.isShowing());
083         catch (TimeoutException e) {
084             throw new GriffonException("An error occurred while starting up the application", e);
085         }
086     }
087 
088     public void cleanup() {
089         if (application != null) {
090             application.shutdown();
091             try {
092                 FxToolkit.cleanupApplication(application);
093             catch (TimeoutException e) {
094                 throw new GriffonException("An error occurred while shutting down the application", e);
095             finally {
096                 application = null;
097             }
098         }
099     }
100 
101     @Override
102     public Statement apply(Statement base, Description description) {
103         return new Statement() {
104             @Override
105             public void evaluate() throws Throwable {
106                 setup();
107                 try {
108                     base.evaluate();
109                 finally {
110                     cleanup();
111                 }
112             }
113         };
114     }
115 
116     public void injectMembers(@Nonnull Object target) {
117         requireNonNull(target, "Argument 'target' must not be null");
118         application.getInjector().injectMembers(target);
119     }
120 
121     public boolean hasFailures() {
122         return failures;
123     }
124 
125     public void setFailures(boolean failures) {
126         this.failures = failures;
127     }
128 
129     @Nullable
130     public <W extends Window> W managedWindow(@Nonnull String name) {
131         return (Wapplication.getWindowManager().findWindow(name);
132     }
133 
134     protected void initialize() {
135         getTestContext().setWindowName(windowName);
136     }
137 
138     private static class WindowShownHandler implements RunnableWithArgs {
139         private final String windowName;
140         private boolean showing;
141 
142         private WindowShownHandler(String windowName) {
143             this.windowName = windowName;
144         }
145 
146         public boolean isShowing() {
147             return showing;
148         }
149 
150         @Override
151         public void run(Object... args) {
152             if (args != null && args.length > && args[0instanceof CharSequence) {
153                 showing = windowName.equals(String.valueOf(args[0]));
154             }
155         }
156     }
157 }