1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.swt.finder.widgets; |
12 | |
|
13 | |
import java.lang.reflect.Field; |
14 | |
|
15 | |
import org.eclipse.swt.SWT; |
16 | |
import org.eclipse.swt.custom.CTabFolder; |
17 | |
import org.eclipse.swt.custom.CTabItem; |
18 | |
import org.eclipse.swt.graphics.Rectangle; |
19 | |
import org.eclipse.swt.widgets.Event; |
20 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
21 | |
import org.eclipse.swtbot.swt.finder.SWTBot; |
22 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
23 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
24 | |
import org.eclipse.swtbot.swt.finder.results.BoolResult; |
25 | |
import org.eclipse.swtbot.swt.finder.results.Result; |
26 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
27 | |
import org.eclipse.swtbot.swt.finder.results.WidgetResult; |
28 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
29 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
30 | |
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
31 | |
import org.hamcrest.SelfDescribing; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
@SWTBotWidget(clasz = CTabItem.class, preferredName = "cTabItem", referenceBy = { ReferenceBy.MNEMONIC }) |
38 | |
public class SWTBotCTabItem extends AbstractSWTBot<CTabItem> { |
39 | |
|
40 | 0 | private CTabFolder parent; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public SWTBotCTabItem(CTabItem w) throws WidgetNotFoundException { |
49 | 0 | this(w, null); |
50 | 0 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public SWTBotCTabItem(CTabItem w, SelfDescribing description) throws WidgetNotFoundException { |
60 | 1 | super(w, description); |
61 | 1 | this.parent = syncExec(new WidgetResult<CTabFolder>() { |
62 | |
public CTabFolder run() { |
63 | 1 | return widget.getParent(); |
64 | |
} |
65 | |
}); |
66 | 1 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public SWTBotCTabItem show() { |
76 | 0 | syncExec(new VoidResult() { |
77 | |
public void run() { |
78 | 0 | parent.showItem(widget); |
79 | 0 | } |
80 | |
}); |
81 | 0 | return this; |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public SWTBotCTabItem activate() throws TimeoutException { |
91 | 0 | log.trace(MessageFormat.format("Activating {0}", this)); |
92 | 0 | waitForEnabled(); |
93 | |
|
94 | 0 | asyncExec(new VoidResult() { |
95 | |
public void run() { |
96 | 0 | widget.getParent().setSelection(widget); |
97 | 0 | log.debug(MessageFormat.format("Activated {0}", this)); |
98 | 0 | } |
99 | |
}); |
100 | |
|
101 | 0 | notify(SWT.Selection, createEvent(), parent); |
102 | |
|
103 | 0 | new SWTBot().waitUntil(new DefaultCondition() { |
104 | |
public boolean test() throws Exception { |
105 | 0 | return isActive(); |
106 | |
} |
107 | |
|
108 | |
public String getFailureMessage() { |
109 | 0 | return "Timed out waiting for " + SWTUtils.toString(widget) + " to activate"; |
110 | |
} |
111 | |
}); |
112 | |
|
113 | 0 | return this; |
114 | |
} |
115 | |
|
116 | |
protected Event createEvent() { |
117 | 10 | Event event = super.createEvent(); |
118 | 10 | event.widget = parent; |
119 | 10 | event.item = widget; |
120 | 10 | return event; |
121 | |
} |
122 | |
|
123 | |
public boolean isActive() { |
124 | 0 | return syncExec(new BoolResult() { |
125 | |
public Boolean run() { |
126 | 0 | return parent.getSelection() == widget; |
127 | |
} |
128 | |
}); |
129 | |
} |
130 | |
|
131 | |
public boolean isEnabled() { |
132 | 1 | return syncExec(new BoolResult() { |
133 | |
public Boolean run() { |
134 | 1 | return widget.getParent().isEnabled(); |
135 | |
} |
136 | |
}); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public SWTBotCTabItem close() { |
145 | 1 | waitForEnabled(); |
146 | 1 | Rectangle rectangleCloseBox = syncExec(new Result<Rectangle>() { |
147 | |
public Rectangle run() { |
148 | |
try { |
149 | 1 | Field field = CTabItem.class.getDeclaredField("closeRect"); |
150 | 1 | field.setAccessible(true); |
151 | 1 | return (Rectangle) field.get(widget); |
152 | |
} |
153 | 0 | catch (Exception e) { |
154 | |
|
155 | |
} |
156 | 0 | return null; |
157 | |
} |
158 | |
}); |
159 | 1 | clickCloseButton(rectangleCloseBox.x + (rectangleCloseBox.width / 2), rectangleCloseBox.y + (rectangleCloseBox.height / 2)); |
160 | 1 | return this; |
161 | |
} |
162 | |
|
163 | |
private void notifyParent(final int eventType, final Event createEvent) { |
164 | 2 | notify(eventType, createEvent, widget.getParent()); |
165 | 2 | } |
166 | |
|
167 | |
private void notifyParent(int event) { |
168 | 10 | notify(event, createEvent(), widget.getParent()); |
169 | 10 | } |
170 | |
|
171 | |
|
172 | |
private void clickCloseButton(int x, int y) { |
173 | 1 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
174 | 1 | notifyParent(SWT.MouseEnter); |
175 | 1 | notifyParent(SWT.MouseMove); |
176 | 1 | notifyParent(SWT.Activate); |
177 | 1 | notifyParent(SWT.FocusIn); |
178 | 1 | notifyParent(SWT.MouseDown, createMouseEvent(x, y, 1, SWT.NONE, 1)); |
179 | |
|
180 | 1 | notifyParent(SWT.MouseUp, createMouseEvent(x, y, 1, SWT.BUTTON1, 1)); |
181 | 1 | notifyParent(SWT.Selection); |
182 | 1 | notifyParent(SWT.MouseHover); |
183 | 1 | notifyParent(SWT.MouseMove); |
184 | 1 | notifyParent(SWT.MouseExit); |
185 | 1 | notifyParent(SWT.Deactivate); |
186 | 1 | notifyParent(SWT.FocusOut); |
187 | 1 | log.debug(MessageFormat.format("Clicked on {0}", this)); |
188 | 1 | } |
189 | |
|
190 | |
} |