1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.List
;
18 import java
.util
.concurrent
.atomic
.AtomicReference
;
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
;
44 * SWTBot class representing a time graph entry
46 public class SWTBotTimeGraphEntry
extends AbstractSWTBotControl
<TimeGraphControl
> {
48 private final ITimeGraphEntry fEntry
;
56 * the time graph entry
58 * @throws WidgetNotFoundException
59 * if the widget is <code>null</code> or widget has been
62 public SWTBotTimeGraphEntry(TimeGraphControl w
, ITimeGraphEntry entry
) throws WidgetNotFoundException
{
68 protected SWTBotRootMenu
contextMenu(final Control control
) throws WidgetNotFoundException
{
69 UIThreadRunnable
.syncExec(new VoidResult() {
72 Rectangle bounds
= widget
.getItemBounds(fEntry
);
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
);
88 WaitForObjectCondition
<Menu
> waitForMenu
= Conditions
.waitForPopupMenu(control
);
89 new SWTBot().waitUntilWidgetAppears(waitForMenu
);
90 return new SWTBotRootMenu(waitForMenu
.get(0));
94 * Get the child entries of this entry
96 * @return the array of child entries
98 public SWTBotTimeGraphEntry
[] getEntries() {
99 return syncExec(new ArrayResult
<SWTBotTimeGraphEntry
>() {
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
));
108 return entries
.toArray(new SWTBotTimeGraphEntry
[0]);
114 * Click on the entry at a timestamp
117 * the timestamp to click at.
119 public void click(long time
) {
120 final Point p
= getPointForTime(time
);
128 * Double-click on the entry at a timestamp
131 * the timestamp to double-click at.
133 public void doubleClick(long time
) {
134 final Point p
= getPointForTime(time
);
138 doubleClickXY(p
.x
, p
.y
);
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
) {
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
);
155 * Get the child entry of this entry with the given name
158 * the name of the entry
160 * @return the child entry
162 public SWTBotTimeGraphEntry
getEntry(String name
) {
163 AtomicReference
<ITimeGraphEntry
> found
= new AtomicReference
<>();
164 WaitUtils
.waitUntil(timegraph
-> {
165 List
<ITimeGraphEntry
> entries
= syncExec(new ListResult
<ITimeGraphEntry
>() {
167 public List
<ITimeGraphEntry
> run() {
168 return Arrays
.asList(timegraph
.getExpandedElements());
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
)) {
182 }, widget
, "Timed out waiting for time graph entry " + name
);
183 return new SWTBotTimeGraphEntry(widget
, found
.get());
187 * Get the text of this entry
192 public String
getText() {
197 * Get the text of this entry for the given column index
201 * @return the column text
203 public String
getText(int column
) {
204 ITableLabelProvider labelProvider
= widget
.getLabelProvider();
205 return labelProvider
!= null ? labelProvider
.getColumnText(fEntry
, column
) : column
== 0 ? fEntry
.getName() : "";
209 * Select this time graph entry
213 public SWTBotTimeGraphEntry
select() {
214 syncExec(new VoidResult() {
218 widget
.selectItem(fEntry
, false);
219 widget
.fireSelectionChanged();