Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a4b28e60fca4880002984928284b90b3c13d3a9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright (c) 2008, 2011 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/

package org.eclipse.emf.eef.modelingBot.helper;

import java.util.List;

import org.eclipse.emf.eef.modelingBot.menu.SWTBotContextMenu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyList;
import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyList.ListElement;
import org.hamcrest.Matcher;

/**
 * @author <a href="mailto:nathalie.lepine@obeo.fr">Nathalie Lepine</a>
 * 
 */
public class SWTBotHelper {
	/**
	 * SWTWorkbenchBot
	 */
	private static SWTWorkbenchBot bot = new SWTWorkbenchBot();

	/**
	 * Press the enter key
	 * 
	 * @param widget
	 * @throws Exception
	 */
	public static void pressEnterKey(final Widget widget) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				widget.notifyListeners(SWT.KeyDown, keyEvent(SWT.NONE, SWT.CR, SWT.Selection, widget));
			}
		});
	}

	public static void sendFocusLost(final Widget widget) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				Event event = createEvent(widget);
				event.type = SWT.FocusOut;

				widget.notifyListeners(SWT.FocusOut, event);
			}
		});
	}

	/**
	 * Press the enter key
	 * 
	 * @param widget
	 * @throws Exception
	 */
	public static void pressEnterKeyInTabContainer(final Widget widget) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				searchTabContainer(widget).notifyListeners(SWT.Traverse, keyEvent(SWT.NONE, SWT.CR, SWT.Selection, widget));
			}
		});
	}

	private static Composite searchTabContainer(final Widget wid) {
		Composite parent = ((Text) wid).getParent();
		while (parent != null) {
			if (parent instanceof Composite)
				return parent;
			parent = parent.getParent();
		}
		return null;
	}

	/**
	 * @param c
	 *            the character.
	 * @param modificationKey
	 *            the modification key.
	 * @param keyCode
	 *            the keycode.
	 * @return a key event with the specified keys.
	 * @see Event#keyCode
	 * @see Event#character
	 * @see Event#stateMask
	 * @since 1.2
	 */
	private static Event keyEvent(int modificationKey, char c, int keyCode, Widget widget) {
		Event keyEvent = createEvent(widget);
		keyEvent.stateMask = modificationKey;
		keyEvent.character = c;
		keyEvent.keyCode = keyCode;

		return keyEvent;
	}

	private static Event createEvent(Widget widget) {
		Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = widget;
		event.display = bot.getDisplay();
		return event;
	}

	/**
	 * Select the tab with the name label in the property views.
	 * 
	 * @param label
	 *            Label to find.
	 */
	// CHECKSTYLE:OFF
	@SuppressWarnings({ "restriction", "unchecked" })
	// CHECKSTYLE:ON
	public static void selectPropertyTabItem(final String label) {
		final Matcher<TabbedPropertyList> matcher = WidgetMatcherFactory.allOf(WidgetMatcherFactory.widgetOfType(TabbedPropertyList.class));
		final List<TabbedPropertyList> widgets = widget(matcher);

		UIThreadRunnable.syncExec(SWTUtils.display(), new VoidResult() {
			public void run() {
				for (final TabbedPropertyList tabbedProperty : widgets) {
					final ListElement tabItem = getTabItem(label, tabbedProperty);
					if (tabItem != null) {
						final Event mouseEvent = createEvent(tabItem, tabItem.getBounds().x, tabItem.getBounds().y, 1, SWT.BUTTON1, 1);
						tabItem.notifyListeners(SWT.MouseUp, mouseEvent);
					}
				}
			}
		});
	}

	/**
	 * Find widget.
	 * 
	 * @param matcher
	 *            the matcher used to match widgets.
	 * @return all the widgets matching the matcher.
	 */
	@SuppressWarnings("restriction")
	public static List<TabbedPropertyList> widget(final Matcher<TabbedPropertyList> matcher) {
		final WaitForObjectCondition<TabbedPropertyList> waitForWidget = Conditions.waitForWidget(matcher);
		bot.waitUntilWidgetAppears(waitForWidget);
		return waitForWidget.getAllMatches();
	}

	/**
	 * Create a event <br>
	 * 
	 * @param x
	 *            the x coordinate of the mouse event.
	 * @param y
	 *            the y coordinate of the mouse event.
	 * @param button
	 *            the mouse button that was clicked.
	 * @param stateMask
	 *            the state of the keyboard modifier keys.
	 * @param count
	 *            the number of times the mouse was clicked.
	 * @return an event that encapsulates {@link #widget} and {@link #display}
	 */
	private static Event createEvent(final Widget widget, final int x, final int y, final int button, final int stateMask, final int count) {
		final Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = widget;
		event.display = bot.getDisplay();
		event.x = x;
		event.y = y;
		event.button = button;
		event.stateMask = stateMask;
		event.count = count;
		return event;
	}

	/**
	 * Select the tab with the name label in the property views
	 * 
	 * @param label
	 */
	@SuppressWarnings("restriction")
	private static ListElement getTabItem(final String label, final TabbedPropertyList tabbedProperty) {
		for (final Object listElement : tabbedProperty.getTabList()) {
			if (listElement instanceof ListElement && ((ListElement) listElement).getTabItem().getText().equals(label)) {
				return (ListElement) listElement;
			}
		}
		return null;
	}

	/**
	 * WARNING this class should move in SWTBot one day. WARNING if the text is
	 * not found it will not failed this method to get disposed elements with
	 * the current click on context menu SWTBot method
	 * 
	 * @param treeItem
	 *            the current item
	 * @param text
	 *            the menu text
	 */
	public static void clickContextMenu(final SWTBotTreeItem treeItem, final String text) {
		UIThreadRunnable.asyncExec(new VoidResult() {

			public void run() {
				final SWTBotContextMenu menu = new SWTBotContextMenu(treeItem);
				menu.click(text);
			}
		});
	}

	/**
	 * WARNING this class should move in SWTBot one day. WARNING if the text is
	 * not found it will not failed this method to get disposed elements with
	 * the current click on context menu SWTBot method
	 * 
	 * @param tree
	 *            the tree
	 * @param text
	 *            the menu text
	 */
	public static void clickContextMenu(final SWTBotTree tree, final String text) {
		UIThreadRunnable.asyncExec(new VoidResult() {
			public void run() {
				final SWTBotContextMenu menu = new SWTBotContextMenu(tree);
				menu.click(text);
			}
		});
	}

	/**
	 * Hack method to ensure that previous call was processed fully.
	 */
	public static void waitAllUiEvents() {
		// use another sync exec to ensure that previous call was processed
		// fully
		UIThreadRunnable.syncExec(new VoidResult() {
			public void run() {
				// do nothing, just wait for all events to be processed
			}
		});
	}

}

Back to the top