merging in latest changes from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / test / ContextMenuHelper.java
1 package eu.etaxonomy.taxeditor.test;
2
3 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
4 import static org.hamcrest.Matchers.allOf;
5 import static org.hamcrest.Matchers.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 public MenuItem run() {
38 MenuItem menuItem = null;
39 Control control = (Control) bot.widget;
40 Menu menu = control.getMenu();
41 for (String text : texts) {
42 Matcher<?> matcher = allOf(instanceOf(MenuItem.class),
43 withMnemonic(text));
44 menuItem = show(menu, matcher);
45 if (menuItem != null) {
46 menu = menuItem.getMenu();
47 } else {
48 hide(menu);
49 break;
50 }
51 }
52
53 return menuItem;
54 }
55 });
56 if (menuItem == null) {
57 throw new WidgetNotFoundException("Could not find menu: "
58 + Arrays.asList(texts));
59 }
60
61 // click
62 click(menuItem);
63
64 // hide
65 UIThreadRunnable.syncExec(new VoidResult() {
66 public void run() {
67 hide(menuItem.getParent());
68 }
69 });
70 }
71
72 private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
73 if (menu != null) {
74 menu.notifyListeners(SWT.Show, new Event());
75 MenuItem[] items = menu.getItems();
76 for (final MenuItem menuItem : items) {
77 if (matcher.matches(menuItem)) {
78 return menuItem;
79 }
80 }
81 menu.notifyListeners(SWT.Hide, new Event());
82 }
83 return null;
84 }
85
86 private static void click(final MenuItem menuItem) {
87 final Event event = new Event();
88 event.time = (int) System.currentTimeMillis();
89 event.widget = menuItem;
90 event.display = menuItem.getDisplay();
91 event.type = SWT.Selection;
92
93 UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {
94 public void run() {
95 menuItem.notifyListeners(SWT.Selection, event);
96 }
97 });
98 }
99
100 private static void hide(final Menu menu) {
101 menu.notifyListeners(SWT.Hide, new Event());
102 if (menu.getParentMenu() != null) {
103 hide(menu.getParentMenu());
104 }
105 }
106 }