Skip to content

Package: SWTTestUtil$2

SWTTestUtil$2

nameinstructionbranchcomplexitylinemethod
testCondition(Control)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.test.common.swt;
15:
16: import static org.junit.Assert.fail;
17:
18: import java.util.NoSuchElementException;
19:
20: import org.eclipse.swt.SWT;
21: import org.eclipse.swt.custom.StackLayout;
22: import org.eclipse.swt.widgets.Button;
23: import org.eclipse.swt.widgets.Composite;
24: import org.eclipse.swt.widgets.Control;
25: import org.eclipse.swt.widgets.Display;
26: import org.eclipse.swt.widgets.Event;
27: import org.eclipse.swt.widgets.Layout;
28: import org.eclipse.swt.widgets.Text;
29: import org.eclipse.swt.widgets.Widget;
30:
31: /**
32: * Util class for SWT Tests.
33: *
34: * @author jfaltermeier
35: *
36: */
37: public final class SWTTestUtil {
38:
39:         private SWTTestUtil() {
40:                 // util
41:         }
42:
43:         /**
44:          * <p>
45:          * Iterates over the hierarchy of the given {@link Control} and looks for a {@link Composite} with a
46:          * {@link StackLayout}. The index specifies which composite with a stack layout should be returned.
47:          * </p>
48:          * <p>
49:          * This method uses a depth-first-search.
50:          * </p>
51:          *
52:          * @param control the parent control
53:          * @param index the index of the layout
54:          * @return the layout
55:          * @throws NoSuchElementException if no layout with the index can be found
56:          */
57:         public static StackLayout findStackLayout(Control control, int index) throws NoSuchElementException {
58:                 final Control result = find(control, index, new Counter(), new ControlTest() {
59:                         @Override
60:                         public boolean testCondition(Control control) {
61:                                 if (!Composite.class.isInstance(control)) {
62:                                         return false;
63:                                 }
64:                                 final Composite composite = Composite.class.cast(control);
65:                                 final Layout layout = composite.getLayout();
66:                                 if (layout == null) {
67:                                         return false;
68:                                 }
69:                                 if (StackLayout.class != layout.getClass()) {
70:                                         return false;
71:                                 }
72:                                 return true;
73:                         }
74:                 });
75:                 if (result == null) {
76:                         throw new NoSuchElementException("No composite with stack layout and index " + index + " found."); //$NON-NLS-1$//$NON-NLS-2$
77:                 }
78:                 final Composite composite = (Composite) result;
79:                 return (StackLayout) composite.getLayout();
80:         }
81:
82:         /**
83:          * <p>
84:          * Iterates over the hierarchy of the given {@link Control} and looks for a control of the given class. The index
85:          * specifies which control should be returned.
86:          * </p>
87:          * <p>
88:          * This method uses a depth-first-search.
89:          * </p>
90:          *
91:          * @param control the parent control
92:          * @param index the index of the control to find
93:          * @param <T> the type of the control to find
94:          * @param clazz the class of the control to find
95:          * @return the control
96:          * @throws NoSuchElementException if no control with the index can be found
97:          */
98:         public static <T extends Control> T findControl(Control control, int index, final Class<T> clazz)
99:                 throws NoSuchElementException {
100:                 final Control result = find(control, index, new Counter(), new ControlTest() {
101:                         @Override
102:                         public boolean testCondition(Control control) {
103:•                                if (control.getClass() != clazz) {
104:                                         return false;
105:                                 }
106:                                 return true;
107:                         }
108:                 });
109:                 if (result == null) {
110:                         throw new NoSuchElementException("No control of type " + clazz.getName() + " with index " + index //$NON-NLS-1$ //$NON-NLS-2$
111:                                 + " found."); //$NON-NLS-1$
112:                 }
113:                 return clazz.cast(result);
114:         }
115:
116:         private static Control find(Control control, int index, Counter found, ControlTest test) {
117:                 final boolean success = test.testCondition(control);
118:                 if (success) {
119:                         found.inc();
120:                 }
121:                 if (index + 1 == found.count) {
122:                         return control;
123:                 }
124:                 if (!Composite.class.isInstance(control)) {
125:                         return null;
126:                 }
127:                 final Composite composite = Composite.class.cast(control);
128:                 for (final Control child : composite.getChildren()) {
129:                         final Control childResult = find(child, index, found, test);
130:                         if (childResult != null) {
131:                                 return childResult;
132:                         }
133:                 }
134:                 return null;
135:         }
136:
137:         /**
138:          * Waits for the ui thread to complete its work. Fails a testcase after 5 seconds.
139:          */
140:         public static void waitForUIThread() {
141:                 final long maxTime = System.currentTimeMillis() + 5000;
142:                 while (Display.getDefault().readAndDispatch()) {
143:                         if (System.currentTimeMillis() > maxTime) {
144:                                 fail("Timeout");
145:                         }
146:                 }
147:         }
148:
149:         /**
150:          * Simulates a click on the given {@link Button}.
151:          *
152:          * @param button the button to press
153:          */
154:         public static void clickButton(Button button) {
155:                 selectWidget(button);
156:         }
157:
158:         /**
159:          * Selects the given widget.
160:          *
161:          * @param widget the control to select.
162:          */
163:         public static void selectWidget(Widget widget) {
164:                 widget.notifyListeners(SWT.Selection, new Event());
165:         }
166:
167:         /**
168:          * Simulates a key down and key up event on the given {@link Control}.
169:          *
170:          * @param control the control
171:          * @param keyCode the key code
172:          */
173:         public static void pressAndReleaseKey(Control control, int keyCode) {
174:                 final Event eventDown = new Event();
175:                 eventDown.keyCode = keyCode;
176:                 control.notifyListeners(SWT.KeyDown, eventDown);
177:                 final Event eventUp = new Event();
178:                 eventUp.keyCode = keyCode;
179:                 control.notifyListeners(SWT.KeyUp, eventDown);
180:         }
181:
182:         /**
183:          * Sets the given string on the {@link Text} and simulates a focus out event.
184:          *
185:          * @param text the text
186:          * @param string the string to enter in the text
187:          */
188:         public static void typeAndFocusOut(Text text, String string) {
189:                 text.setText(string);
190:                 text.notifyListeners(SWT.FocusOut, new Event());
191:         }
192:
193:         /**
194:          * Interface for a tester used by the {@link SWTTestUtil#find(Control, int, Counter, ControlTest)} method.
195:          *
196:          * @author jfaltermeier
197:          *
198:          */
199:         private interface ControlTest {
200:
201:                 /**
202:                  * Tests a condition on the given control.
203:                  *
204:                  * @param control the control to test
205:                  * @return <code>true</code> if condition fulfilled, <code>false</code> otherwise
206:                  */
207:                 boolean testCondition(Control control);
208:         }
209:
210:         /**
211:          * Simple counter class.
212:          *
213:          * @author jfaltermeier
214:          *
215:          */
216:         private static class Counter {
217:                 private int count;
218:
219:                 /**
220:                  * Increases the counter.
221:                  */
222:                 public void inc() {
223:                         count = count + 1;
224:                 }
225:         }
226: }