swtbot: Make SWTBotTimeGraph/Entry.getEntry() wait for entry
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotTimeGraphEntry.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import java.util.concurrent.atomic.AtomicReference;
19
20 import org.eclipse.jface.viewers.ITableLabelProvider;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swtbot.swt.finder.SWTBot;
28 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
29 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
30 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
31 import org.eclipse.swtbot.swt.finder.results.ListResult;
32 import org.eclipse.swtbot.swt.finder.results.Result;
33 import org.eclipse.swtbot.swt.finder.results.VoidResult;
34 import org.eclipse.swtbot.swt.finder.waits.Conditions;
35 import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
36 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;
37 import org.eclipse.swtbot.swt.finder.widgets.SWTBotRootMenu;
38 import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
39 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
40 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.ITimeDataProvider;
41 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
42
43 /**
44 * SWTBot class representing a time graph entry
45 */
46 public class SWTBotTimeGraphEntry extends AbstractSWTBotControl<TimeGraphControl> {
47
48 private final ITimeGraphEntry fEntry;
49
50 /**
51 * Constructor
52 *
53 * @param w
54 * the widget
55 * @param entry
56 * the time graph entry
57 *
58 * @throws WidgetNotFoundException
59 * if the widget is <code>null</code> or widget has been
60 * disposed.
61 */
62 public SWTBotTimeGraphEntry(TimeGraphControl w, ITimeGraphEntry entry) throws WidgetNotFoundException {
63 super(w);
64 fEntry = entry;
65 }
66
67 @Override
68 protected SWTBotRootMenu contextMenu(final Control control) throws WidgetNotFoundException {
69 UIThreadRunnable.syncExec(new VoidResult() {
70 @Override
71 public void run() {
72 Rectangle bounds = widget.getItemBounds(fEntry);
73 if (bounds == null) {
74 return;
75 }
76 Point location = widget.toDisplay(bounds.x, bounds.y);
77 final Event event = new Event();
78 event.time = (int) System.currentTimeMillis();
79 event.display = control.getDisplay();
80 event.widget = control;
81 event.x = location.x + widget.getTimeDataProvider().getNameSpace() / 2;
82 event.y = location.y + bounds.height / 2;
83 control.notifyListeners(SWT.MenuDetect, event);
84 }
85 });
86 select();
87
88 WaitForObjectCondition<Menu> waitForMenu = Conditions.waitForPopupMenu(control);
89 new SWTBot().waitUntilWidgetAppears(waitForMenu);
90 return new SWTBotRootMenu(waitForMenu.get(0));
91 }
92
93 /**
94 * Get the child entries of this entry
95 *
96 * @return the array of child entries
97 */
98 public SWTBotTimeGraphEntry[] getEntries() {
99 return syncExec(new ArrayResult<SWTBotTimeGraphEntry>() {
100 @Override
101 public SWTBotTimeGraphEntry[] run() {
102 List<SWTBotTimeGraphEntry> entries = new ArrayList<>();
103 for (ITimeGraphEntry entry : widget.getExpandedElements()) {
104 if (fEntry.equals(entry.getParent())) {
105 entries.add(new SWTBotTimeGraphEntry(widget, entry));
106 }
107 }
108 return entries.toArray(new SWTBotTimeGraphEntry[0]);
109 }
110 });
111 }
112
113 /**
114 * Click on the entry at a timestamp
115 *
116 * @param time
117 * the timestamp to click at.
118 */
119 public void click(long time) {
120 final Point p = getPointForTime(time);
121 if (p == null) {
122 return;
123 }
124 clickXY(p.x, p.y);
125 }
126
127 /**
128 * Double-click on the entry at a timestamp
129 *
130 * @param time
131 * the timestamp to double-click at.
132 */
133 public void doubleClick(long time) {
134 final Point p = getPointForTime(time);
135 if (p == null) {
136 return;
137 }
138 doubleClickXY(p.x, p.y);
139 }
140
141 private Point getPointForTime(long time) {
142 return UIThreadRunnable.syncExec((Result<Point>) () -> {
143 ITimeDataProvider timeDataProvider = widget.getTimeDataProvider();
144 if (timeDataProvider.getTime0() > time || timeDataProvider.getTime1() < time) {
145 return null;
146 }
147 int x = widget.getXForTime(time);
148 Rectangle bounds = widget.getItemBounds(fEntry);
149 int y = bounds.y + bounds.height / 2;
150 return new Point(x, y);
151 });
152 }
153
154 /**
155 * Get the child entry of this entry with the given name
156 *
157 * @param name
158 * the name of the entry
159 *
160 * @return the child entry
161 */
162 public SWTBotTimeGraphEntry getEntry(String name) {
163 AtomicReference<ITimeGraphEntry> found = new AtomicReference<>();
164 WaitUtils.waitUntil(timegraph -> {
165 List<ITimeGraphEntry> entries = syncExec(new ListResult<ITimeGraphEntry>() {
166 @Override
167 public List<ITimeGraphEntry> run() {
168 return Arrays.asList(timegraph.getExpandedElements());
169 }
170 });
171 ITableLabelProvider labelProvider = timegraph.getLabelProvider();
172 for (ITimeGraphEntry entry : entries) {
173 if (fEntry.equals(entry.getParent())) {
174 String label = labelProvider == null ? entry.getName() : labelProvider.getColumnText(entry, 0);
175 if (name.equals(label)) {
176 found.set(entry);
177 return true;
178 }
179 }
180 }
181 return false;
182 }, widget, "Timed out waiting for time graph entry " + name);
183 return new SWTBotTimeGraphEntry(widget, found.get());
184 }
185
186 /**
187 * Get the text of this entry
188 *
189 * @return the text
190 */
191 @Override
192 public String getText() {
193 return getText(0);
194 }
195
196 /**
197 * Get the text of this entry for the given column index
198 *
199 * @param column
200 * the column index
201 * @return the column text
202 */
203 public String getText(int column) {
204 ITableLabelProvider labelProvider = widget.getLabelProvider();
205 return labelProvider != null ? labelProvider.getColumnText(fEntry, column) : column == 0 ? fEntry.getName() : "";
206 }
207
208 /**
209 * Select this time graph entry
210 *
211 * @return itself
212 */
213 public SWTBotTimeGraphEntry select() {
214 syncExec(new VoidResult() {
215 @Override
216 public void run() {
217 widget.setFocus();
218 widget.selectItem(fEntry, false);
219 widget.fireSelectionChanged();
220 }
221 });
222 return this;
223 }
224 }
This page took 0.040142 seconds and 5 git commands to generate.