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