001 /*
002 * Copyright 2008-2015 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 org.codehaus.griffon.runtime.core.DefaultGriffonApplication;
024 import org.codehaus.griffon.runtime.javafx.TestJavaFXGriffonApplication;
025 import org.junit.rules.TestRule;
026 import org.junit.runner.Description;
027 import org.junit.runners.model.Statement;
028 import org.testfx.api.FxToolkit;
029
030 import javax.annotation.Nonnull;
031 import java.util.concurrent.TimeoutException;
032
033 import static com.jayway.awaitility.Awaitility.await;
034 import static griffon.javafx.test.TestContext.getTestContext;
035 import static griffon.util.GriffonNameUtils.requireNonBlank;
036 import static java.util.Objects.requireNonNull;
037
038 /**
039 * A JUnit Rule that starts the application once per test class.
040 *
041 * @author Andres Almiray
042 * @since 2.3.0
043 */
044 public class GriffonTestFXClassRule extends TestFX implements TestRule {
045 protected String windowName;
046 protected String[] startupArgs;
047 protected Class<? extends TestJavaFXGriffonApplication> applicationClass;
048 protected JavaFXGriffonApplication application;
049
050 public GriffonTestFXClassRule(@Nonnull String windowName) {
051 this(TestJavaFXGriffonApplication.class, windowName, DefaultGriffonApplication.EMPTY_ARGS);
052 }
053
054 public GriffonTestFXClassRule(@Nonnull Class<? extends TestJavaFXGriffonApplication> applicationClass, @Nonnull String windowName) {
055 this(applicationClass, windowName, DefaultGriffonApplication.EMPTY_ARGS);
056 }
057
058 public GriffonTestFXClassRule(@Nonnull Class<? extends TestJavaFXGriffonApplication> applicationClass, @Nonnull String windowName, @Nonnull String[] startupArgs) {
059 this.applicationClass = requireNonNull(applicationClass, "Argument 'applicationClass' must not be null");
060 this.windowName = requireNonBlank(windowName, "Argument 'windowName' cannot be blank");
061 requireNonNull(startupArgs, "Argument 'startupArgs' must not be null");
062 this.startupArgs = new String[startupArgs.length];
063 System.arraycopy(startupArgs, 0, this.startupArgs, 0, startupArgs.length);
064 if (!Environment.isSystemSet()) {
065 System.setProperty(Environment.KEY, Environment.TEST.getName());
066 }
067 }
068
069 public void setup() {
070 initialize();
071
072 try {
073 FxToolkit.registerPrimaryStage();
074
075 application = (JavaFXGriffonApplication) FxToolkit.setupApplication(applicationClass);
076 WindowShownHandler startingWindow = new WindowShownHandler(windowName);
077 application.getEventRouter().addEventListener(ApplicationEvent.WINDOW_SHOWN.getName(), startingWindow);
078
079 await().until(() -> startingWindow.isShowing());
080 } catch (TimeoutException e) {
081 throw new GriffonException("An error occurred while starting up the application", e);
082 }
083 }
084
085 public void cleanup() {
086 if (application != null) {
087 application.shutdown();
088 try {
089 FxToolkit.cleanupApplication(application);
090 } catch (TimeoutException e) {
091 throw new GriffonException("An error occurred while shutting down the application",e);
092 }
093 }
094 }
095
096 @Override
097 public Statement apply(Statement base, Description description) {
098 return new Statement() {
099 @Override
100 public void evaluate() throws Throwable {
101 setup();
102 try {
103 base.evaluate();
104 } finally {
105 cleanup();
106 }
107 }
108 };
109 }
110
111 protected void initialize() {
112 getTestContext().setWindowName(windowName);
113 }
114
115 private static class WindowShownHandler implements RunnableWithArgs {
116 private final String windowName;
117 private boolean showing;
118
119 private WindowShownHandler(String windowName) {
120 this.windowName = windowName;
121 }
122
123 public boolean isShowing() {
124 return showing;
125 }
126
127 @Override
128 public void run(Object... args) {
129 if (args != null && args.length > 0 && args[0] instanceof CharSequence) {
130 showing = windowName.equals(String.valueOf(args[0]));
131 }
132 }
133 }
134 }
|