1
|
package eu.etaxonomy.taxeditor.test;
|
2
|
|
3
|
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
|
4
|
import static org.hamcrest.CoreMatchers.allOf;
|
5
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
6
|
|
7
|
import java.util.Arrays;
|
8
|
|
9
|
import org.eclipse.swt.SWT;
|
10
|
import org.eclipse.swt.widgets.Control;
|
11
|
import org.eclipse.swt.widgets.Event;
|
12
|
import org.eclipse.swt.widgets.Menu;
|
13
|
import org.eclipse.swt.widgets.MenuItem;
|
14
|
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
|
15
|
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
|
16
|
import org.eclipse.swtbot.swt.finder.results.VoidResult;
|
17
|
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
|
18
|
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
|
19
|
import org.hamcrest.Matcher;
|
20
|
|
21
|
public class ContextMenuHelper {
|
22
|
|
23
|
/**
|
24
|
* Clicks the context menu matching the text.
|
25
|
*
|
26
|
* @param text
|
27
|
* the text on the context menu.
|
28
|
* @throws WidgetNotFoundException
|
29
|
* if the widget is not found.
|
30
|
*/
|
31
|
public static void clickContextMenu(final AbstractSWTBot<?> bot,
|
32
|
final String... texts) {
|
33
|
|
34
|
// show
|
35
|
final MenuItem menuItem = UIThreadRunnable
|
36
|
.syncExec(new WidgetResult<MenuItem>() {
|
37
|
@Override
|
38
|
public MenuItem run() {
|
39
|
MenuItem menuItem = null;
|
40
|
Control control = (Control) bot.widget;
|
41
|
Menu menu = control.getMenu();
|
42
|
for (String text : texts) {
|
43
|
Matcher<?> matcher = allOf(instanceOf(MenuItem.class),
|
44
|
withMnemonic(text));
|
45
|
menuItem = show(menu, matcher);
|
46
|
if (menuItem != null) {
|
47
|
menu = menuItem.getMenu();
|
48
|
} else {
|
49
|
hide(menu);
|
50
|
break;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
return menuItem;
|
55
|
}
|
56
|
});
|
57
|
if (menuItem == null) {
|
58
|
throw new WidgetNotFoundException("Could not find menu: "
|
59
|
+ Arrays.asList(texts));
|
60
|
}
|
61
|
|
62
|
// click
|
63
|
click(menuItem);
|
64
|
|
65
|
// hide
|
66
|
UIThreadRunnable.syncExec(new VoidResult() {
|
67
|
@Override
|
68
|
public void run() {
|
69
|
hide(menuItem.getParent());
|
70
|
}
|
71
|
});
|
72
|
}
|
73
|
|
74
|
private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
|
75
|
if (menu != null) {
|
76
|
menu.notifyListeners(SWT.Show, new Event());
|
77
|
MenuItem[] items = menu.getItems();
|
78
|
for (final MenuItem menuItem : items) {
|
79
|
if (matcher.matches(menuItem)) {
|
80
|
return menuItem;
|
81
|
}
|
82
|
}
|
83
|
menu.notifyListeners(SWT.Hide, new Event());
|
84
|
}
|
85
|
return null;
|
86
|
}
|
87
|
|
88
|
private static void click(final MenuItem menuItem) {
|
89
|
final Event event = new Event();
|
90
|
event.time = (int) System.currentTimeMillis();
|
91
|
event.widget = menuItem;
|
92
|
event.display = menuItem.getDisplay();
|
93
|
event.type = SWT.Selection;
|
94
|
|
95
|
UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {
|
96
|
@Override
|
97
|
public void run() {
|
98
|
menuItem.notifyListeners(SWT.Selection, event);
|
99
|
}
|
100
|
});
|
101
|
}
|
102
|
|
103
|
private static void hide(final Menu menu) {
|
104
|
menu.notifyListeners(SWT.Hide, new Event());
|
105
|
if (menu.getParentMenu() != null) {
|
106
|
hide(menu.getParentMenu());
|
107
|
}
|
108
|
}
|
109
|
}
|