GriffonFestRule.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.core.test;
19 
20 import griffon.core.ApplicationBootstrapper;
21 import griffon.core.GriffonApplication;
22 import org.codehaus.griffon.runtime.core.TestApplicationBootstrapper;
23 import org.codehaus.griffon.runtime.swing.FestAwareSwingGriffonApplication;
24 import org.fest.swing.core.Robot;
25 import org.fest.swing.fixture.FrameFixture;
26 
27 import javax.annotation.Nonnull;
28 import java.awt.Frame;
29 import java.lang.reflect.Field;
30 
31 import static org.fest.swing.core.BasicRobot.robotWithNewAwtHierarchy;
32 
33 /**
34  @author Andres Almiray
35  @since 2.0.0
36  */
37 public class GriffonFestRule extends GriffonUnitRule {
38     protected FrameFixture window;
39 
40     public GriffonFestRule() {
41         this(FestAwareSwingGriffonApplication.EMPTY_ARGS, TestApplicationBootstrapper.class);
42     }
43 
44     public GriffonFestRule(@Nonnull Class<? extends ApplicationBootstrapper> applicationBootstrapper) {
45         this(FestAwareSwingGriffonApplication.EMPTY_ARGS, applicationBootstrapper);
46     }
47 
48     public GriffonFestRule(@Nonnull String[] startupArgs) {
49         this(startupArgs, TestApplicationBootstrapper.class);
50     }
51 
52     public GriffonFestRule(@Nonnull String[] startupArgs, @Nonnull Class<? extends ApplicationBootstrapper> applicationBootstrapper) {
53         super(startupArgs, FestAwareSwingGriffonApplication.class, applicationBootstrapper);
54     }
55 
56     @Override
57     @SuppressWarnings("ConstantConditions")
58     protected void before(@Nonnull GriffonApplication application, @Nonnull Object targetthrows Throwable {
59         Robot robot = robotWithNewAwtHierarchy();
60 
61         application.startup();
62         application.ready();
63         Object startingWindow = application.getWindowManager().getStartingWindow();
64         if (startingWindow != null && startingWindow instanceof Frame) {
65             window = new FrameFixture(robot, (FramestartingWindow);
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((FramestartingWindow, 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 }