001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.pivot.test;
019
020 import griffon.core.GriffonApplication;
021 import griffon.exceptions.GriffonException;
022 import org.apache.pivot.wtk.Component;
023 import org.apache.pivot.wtk.Window;
024 import org.codehaus.griffon.runtime.pivot.TestDesktopPivotApplication;
025
026 import javax.annotation.Nonnull;
027 import java.awt.EventQueue;
028 import java.lang.reflect.InvocationTargetException;
029 import java.util.concurrent.CountDownLatch;
030
031 import static griffon.pivot.support.PivotUtils.findComponentByName;
032 import static griffon.util.GriffonNameUtils.requireNonBlank;
033 import static java.util.Objects.requireNonNull;
034
035 /**
036 * @author Andres Almiray
037 * @since 2.0.0
038 */
039 public class GriffonPivotFuncRule extends GriffonPivotRule {
040 private static final String ERROR_RUNNABLE_NULL = "Argument 'runnable' must not be null";
041
042 private Window window;
043
044 public GriffonPivotFuncRule() {
045 }
046
047 public GriffonPivotFuncRule(@Nonnull String[] startupArgs) {
048 super(startupArgs);
049 }
050
051 @Override
052 @SuppressWarnings("ConstantConditions")
053 protected void before(@Nonnull GriffonApplication application, @Nonnull Object target) throws Throwable {
054 application.startup();
055 application.ready();
056 TestDesktopPivotApplication.getReadyLatch().await();
057
058 window = (Window) application.getWindowManager().getStartingWindow();
059 }
060
061 @Nonnull
062 public Window getWindow() {
063 return window;
064 }
065
066 @Nonnull
067 public <T> T find(@Nonnull String name, Class<T> type) {
068 requireNonBlank(name, "Argument 'name' must not be blank");
069 requireNonNull(type, "Argument 'type' must not be null");
070
071 Component component = findComponentByName(name, window);
072 if (component != null) {
073 if (type.isAssignableFrom(component.getClass())) {
074 return type.cast(component);
075 }
076 throw new IllegalArgumentException("Could not find a component name '"
077 + name + "' with type " + type.getName()
078 + "; found type " + component.getClass().getName() + " instead");
079 }
080
081 throw new IllegalArgumentException("Could not find a component name '" + name + "' with type " + type.getName());
082 }
083
084 public void runInsideUISync(@Nonnull final Runnable runnable) {
085 requireNonNull(runnable, ERROR_RUNNABLE_NULL);
086 Throwable t = null;
087 final CountDownLatch latch = new CountDownLatch(1);
088 final Runnable worker = new Runnable() {
089 @Override
090 public void run() {
091 try {
092 runnable.run();
093 } finally {
094 latch.countDown();
095 }
096 }
097 };
098 try {
099 EventQueue.invokeAndWait(worker);
100 } catch (InterruptedException e) {
101 latch.countDown();
102 t = e;
103 } catch (InvocationTargetException e) {
104 latch.countDown();
105 t = e.getTargetException();
106 }
107
108 try {
109 latch.await();
110 } catch (InterruptedException e) {
111 throw new GriffonException(e);
112 }
113
114 if (t != null) {
115 if (t instanceof AssertionError) {
116 throw (AssertionError) t;
117 } else if (t instanceof RuntimeException) {
118 throw (RuntimeException) t;
119 } else {
120 throw new GriffonException(t);
121 }
122 }
123 }
124
125 public void runInsideUIAsync(@Nonnull final Runnable runnable) {
126 requireNonNull(runnable, ERROR_RUNNABLE_NULL);
127 final Throwable[] ts = new Throwable[1];
128 final CountDownLatch latch = new CountDownLatch(1);
129 final Runnable worker = new Runnable() {
130 @Override
131 public void run() {
132 try {
133 runnable.run();
134 } catch (Throwable t) {
135 ts[0] = t;
136 } finally {
137 latch.countDown();
138 }
139 }
140 };
141
142 EventQueue.invokeLater(worker);
143
144 try {
145 latch.await();
146 } catch (InterruptedException e) {
147 throw new GriffonException(e);
148 }
149
150 if (ts[0] != null) {
151 if (ts[0] instanceof AssertionError) {
152 throw (AssertionError) ts[0];
153 } else if (ts[0] instanceof RuntimeException) {
154 throw (RuntimeException) ts[0];
155 } else {
156 throw new GriffonException(ts[0]);
157 }
158 }
159 }
160 }
|