1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
package org.eclipse.swtbot.swt.finder.widgets; |
13 | |
|
14 | |
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; |
15 | |
|
16 | |
import java.util.ArrayList; |
17 | |
import java.util.List; |
18 | |
|
19 | |
import org.eclipse.swt.SWT; |
20 | |
import org.eclipse.swt.widgets.Event; |
21 | |
import org.eclipse.swt.widgets.MenuItem; |
22 | |
import org.eclipse.swt.widgets.ToolItem; |
23 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
24 | |
import org.eclipse.swtbot.swt.finder.SWTBot; |
25 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
26 | |
import org.eclipse.swtbot.swt.finder.Style; |
27 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
28 | |
import org.eclipse.swtbot.swt.finder.finders.EventContextMenuFinder; |
29 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
30 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
31 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
32 | |
import org.hamcrest.Matcher; |
33 | |
import org.hamcrest.SelfDescribing; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@SWTBotWidget(clasz = ToolItem.class, preferredName = "toolbarDropDownButton", style = @Style(name = "SWT.DROP_DOWN", value = SWT.DROP_DOWN), referenceBy = { |
43 | |
ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP}, returnType = SWTBotToolbarDropDownButton.class ) |
44 | |
public class SWTBotToolbarDropDownButton extends SWTBotToolbarButton { |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public SWTBotToolbarDropDownButton(ToolItem w) throws WidgetNotFoundException { |
53 | 0 | this(w, null); |
54 | 0 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public SWTBotToolbarDropDownButton(ToolItem w, SelfDescribing description) throws WidgetNotFoundException { |
64 | 9 | super(w, description); |
65 | 9 | Assert.isTrue(SWTUtils.hasStyle(w, SWT.DROP_DOWN), "Expecting a drop down button."); |
66 | |
|
67 | 9 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public SWTBotMenu menuItem(String menuItem) { |
80 | 2 | Matcher<MenuItem> withMnemonic = withMnemonic(menuItem); |
81 | 2 | return menuItem(withMnemonic); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public SWTBotMenu menuItem(Matcher<MenuItem> matcher) { |
94 | 2 | return menuItems(matcher).get(0); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public List<? extends SWTBotMenu> menuItems(Matcher<MenuItem> matcher) { |
108 | 3 | EventContextMenuFinder menuFinder = new EventContextMenuFinder(); |
109 | |
try { |
110 | 3 | menuFinder.register(); |
111 | 3 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
112 | 3 | waitForEnabled(); |
113 | 3 | notify(SWT.MouseEnter); |
114 | 3 | notify(SWT.MouseMove); |
115 | 3 | notify(SWT.Activate); |
116 | 3 | notify(SWT.FocusIn); |
117 | 3 | notify(SWT.MouseDown); |
118 | 3 | notify(SWT.MouseUp); |
119 | 3 | notify(SWT.Selection, arrowEvent()); |
120 | 3 | notify(SWT.MouseHover); |
121 | 3 | notify(SWT.MouseMove); |
122 | 3 | notify(SWT.MouseExit); |
123 | 3 | notify(SWT.Deactivate); |
124 | 3 | notify(SWT.FocusOut); |
125 | 3 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
126 | 3 | List<MenuItem> findMenus = menuFinder.findMenus(new SWTBot().activeShell().widget, matcher, true); |
127 | 6 | return toSWTBotMenuItems(matcher, findMenus); |
128 | 0 | } finally { |
129 | 3 | menuFinder.unregister(); |
130 | 0 | } |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public SWTBotToolbarDropDownButton click() { |
139 | 2 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
140 | 2 | waitForEnabled(); |
141 | 2 | sendNotifications(); |
142 | 2 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
143 | 2 | return this; |
144 | |
} |
145 | |
|
146 | |
private ArrayList<SWTBotMenu> toSWTBotMenuItems(Matcher<?> matcher, List<MenuItem> findMenus) { |
147 | 3 | ArrayList<SWTBotMenu> result = new ArrayList<SWTBotMenu>(findMenus.size()); |
148 | 15 | for (MenuItem menuItem : findMenus) { |
149 | 9 | result.add(new SWTBotMenu(menuItem, matcher)); |
150 | |
} |
151 | |
|
152 | 3 | if (result.isEmpty()) |
153 | 0 | throw new WidgetNotFoundException("Could not find a menu item"); |
154 | 3 | return result; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
private Event arrowEvent() { |
163 | 3 | Event event = createEvent(); |
164 | 3 | event.detail = SWT.ARROW; |
165 | 3 | return event; |
166 | |
} |
167 | |
} |