AbstractSwingTestCase.groovy
01 /*
02  * Copyright 2007-2012 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 
17 package griffon.test
18 
19 import griffon.core.UIThreadManager
20 import java.lang.reflect.Constructor
21 import java.lang.reflect.InvocationTargetException
22 
23 /**
24  * Base classe for Swing relatedt test.
25  *
26  @author Danno Ferrin
27  @author Andres Almiray
28  */
29 public class AbstractSwingTestCase extends GroovyTestCase {
30     private static boolean headless
31 
32     /**
33      * A boolean indicating if we are running in headless mode.
34      * Check this flag if you believe your test may make use of AWT/Swing
35      * features, then simply return rather than running your test.
36      *
37      @return true if running in headless mode
38      */
39     public static boolean isHeadless() {
40         return headless
41     }
42 
43     /**
44      * Alias for isHeadless().
45      *
46      @return true if running in headless mode
47      */
48     public static boolean getHeadless() {
49         return isHeadless()
50     }
51 
52     static {
53         try {
54             final Class jframe = Class.forName("javax.swing.JFrame")
55             final Constructor constructor = jframe.getConstructor((Class[])[String])
56             constructor.newInstance((String[])["testing"])
57             headless = false
58         catch (java.awt.HeadlessException e) {
59             headless = true
60         catch (UnsatisfiedLinkError e) {
61             headless = true
62         catch (ClassNotFoundException e) {
63             headless = true
64         catch (NoClassDefFoundError e) {
65             headless = true
66         catch (IllegalAccessException e) {
67             headless = true
68         catch (InstantiationException e) {
69             headless = true
70         catch (NoSuchMethodException e) {
71             headless = true
72         catch (InvocationTargetException e) {
73             headless = true
74         }
75     }
76 
77     /** Executes code synchronously inside the UI thread */
78     def execSync = UIThreadManager.instance.&executeSync
79     /** Executes code asynchronously inside the UI thread */
80     def execAsync = UIThreadManager.instance.&executeAsync
81     /** Executes code outside the UI thread */
82     def execOutside = UIThreadManager.instance.&executeOutside
83     /** True if the current thread is the UI thread */
84     def isUIThread = UIThreadManager.instance.&isUIThread
85     /** Schedules a block of code as a Future */
86     def execFuture = Object... args ->
87         UIThreadManager.instance.executeFuture(*args)
88     }
89 }