01 /*
02 * Copyright 2008-2015 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.core.test;
17
18 import griffon.core.ApplicationBootstrapper;
19 import griffon.core.GriffonApplication;
20 import org.codehaus.griffon.runtime.core.TestApplicationBootstrapper;
21 import org.codehaus.griffon.runtime.swing.FestAwareSwingGriffonApplication;
22 import org.fest.swing.core.Robot;
23 import org.fest.swing.fixture.FrameFixture;
24
25 import javax.annotation.Nonnull;
26 import java.awt.Frame;
27 import java.lang.reflect.Field;
28
29 import static org.fest.swing.core.BasicRobot.robotWithNewAwtHierarchy;
30
31 /**
32 * @author Andres Almiray
33 * @since 2.0.0
34 */
35 public class GriffonFestRule extends GriffonUnitRule {
36 protected FrameFixture window;
37
38 public GriffonFestRule() {
39 this(FestAwareSwingGriffonApplication.EMPTY_ARGS, TestApplicationBootstrapper.class);
40 }
41
42 public GriffonFestRule(@Nonnull Class<? extends ApplicationBootstrapper> applicationBootstrapper) {
43 this(FestAwareSwingGriffonApplication.EMPTY_ARGS, applicationBootstrapper);
44 }
45
46 public GriffonFestRule(@Nonnull String[] startupArgs) {
47 this(startupArgs, TestApplicationBootstrapper.class);
48 }
49
50 public GriffonFestRule(@Nonnull String[] startupArgs, @Nonnull Class<? extends ApplicationBootstrapper> applicationBootstrapper) {
51 super(startupArgs, FestAwareSwingGriffonApplication.class, applicationBootstrapper);
52 }
53
54 @Override
55 @SuppressWarnings("ConstantConditions")
56 protected void before(@Nonnull GriffonApplication application, @Nonnull Object target) throws Throwable {
57 Robot robot = robotWithNewAwtHierarchy();
58
59 application.startup();
60 application.ready();
61 Object startingWindow = application.getWindowManager().getStartingWindow();
62 if (startingWindow != null) {
63 if (startingWindow instanceof Frame) {
64 window = new FrameFixture(robot, (Frame) startingWindow);
65 }
66 }
67
68 try {
69 Field windowField = target.getClass().getDeclaredField("window");
70 windowField.setAccessible(true);
71 windowField.set(target, window);
72 } catch (Exception e) {
73 after(application, target);
74 throw new IllegalStateException("Class " + target.getClass().getName() +
75 " does not define a field named 'window' of type " + FrameFixture.class.getName(), e);
76 }
77
78 robot.showWindow((Frame) startingWindow, null, false);
79 window.moveToFront();
80 }
81
82 @Override
83 protected void after(@Nonnull GriffonApplication application, @Nonnull Object target) {
84 if (window != null) {
85 window.cleanUp();
86 }
87 super.after(application, target);
88 }
89 }
|