btf: fix order of modifiers in BtfEventPropertySource
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotUtils.java
CommitLineData
306e18d0
MK
1/*******************************************************************************
2 * Copyright (c) 2014 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 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
fa24d78b 13package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
306e18d0
MK
14
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.fail;
17
18import java.util.List;
19
20import org.eclipse.core.resources.IProject;
93c91230
MAL
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.resources.ResourcesPlugin;
23import org.eclipse.core.runtime.CoreException;
b4290931 24import org.eclipse.core.runtime.IPath;
306e18d0
MK
25import org.eclipse.core.runtime.NullProgressMonitor;
26import org.eclipse.core.runtime.jobs.Job;
306e18d0
MK
27import org.eclipse.swt.widgets.Display;
28import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
693ec829
BH
29import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
30import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
306e18d0
MK
31import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
32import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
33import org.eclipse.swtbot.swt.finder.results.VoidResult;
93c91230
MAL
34import org.eclipse.swtbot.swt.finder.waits.Conditions;
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
37import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
306e18d0 38import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
93c91230
MAL
39import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
40import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
2bdf0193
AM
41import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
42import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
43import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
44import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
b4290931 45import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
2bdf0193 46import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory;
693ec829
BH
47import org.eclipse.ui.IEditorPart;
48import org.eclipse.ui.IEditorReference;
93c91230 49import org.eclipse.ui.IPageLayout;
306e18d0
MK
50import org.eclipse.ui.PlatformUI;
51import org.eclipse.ui.WorkbenchException;
693ec829 52import org.hamcrest.Matcher;
306e18d0 53
b4290931 54
306e18d0
MK
55/**
56 * SWTBot Helper functions
57 *
58 * @author Matthew Khouzam
59 */
fa24d78b
AM
60public final class SWTBotUtils {
61
62 private SWTBotUtils() {}
63
306e18d0
MK
64 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
65
66 /**
67 * Waits for all Eclipse jobs to finish
68 */
69 public static void waitForJobs() {
70 while (!Job.getJobManager().isIdle()) {
71 delay(100);
72 }
73 }
74
75 /**
76 * Sleeps current thread for a given time.
77 *
78 * @param waitTimeMillis
79 * time in milliseconds to wait
80 */
81 public static void delay(final long waitTimeMillis) {
82 try {
83 Thread.sleep(waitTimeMillis);
84 } catch (final InterruptedException e) {
85 // Ignored
86 }
87 }
88
89 /**
90 * Create a tracing project
91 *
92 * @param projectName
93 * the name of the tracing project
94 */
95 public static void createProject(final String projectName) {
96 /*
97 * Make a new test
98 */
99 UIThreadRunnable.syncExec(new VoidResult() {
100 @Override
101 public void run() {
102 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
103 assertNotNull(project);
104 }
105 });
106
fa24d78b 107 SWTBotUtils.waitForJobs();
306e18d0
MK
108 }
109
93c91230
MAL
110 /**
111 * Deletes a tracing project
112 *
113 * @param projectName
114 * the name of the tracing project
115 * @param bot
116 * the workbench bot
117 */
118 public static void deleteProject(String projectName, SWTWorkbenchBot bot) {
7777d5f0
MK
119 // Wait for any analysis to complete because it might create
120 // supplementary files
fa24d78b 121 SWTBotUtils.waitForJobs();
93c91230
MAL
122 try {
123 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
124 } catch (CoreException e) {
125 }
126
fa24d78b 127 SWTBotUtils.waitForJobs();
93c91230
MAL
128
129 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
130 projectViewBot.setFocus();
131
693ec829 132 SWTBotTree treeBot = projectViewBot.bot().tree();
93c91230
MAL
133 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
134 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
135 contextMenu.click();
136
137 bot.shell("Delete Resources").setFocus();
138 final SWTBotCheckBox checkBox = bot.checkBox();
139 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
140 checkBox.click();
141
142 final SWTBotButton okButton = bot.button("OK");
143 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
144 okButton.click();
145
fa24d78b 146 SWTBotUtils.waitForJobs();
93c91230
MAL
147 }
148
306e18d0
MK
149 /**
150 * Focus on the main window
151 *
152 * @param shellBots
153 * swtbotshells for all the shells
154 */
155 public static void focusMainWindow(SWTBotShell[] shellBots) {
156 for (SWTBotShell shellBot : shellBots) {
157 if (shellBot.getText().toLowerCase().contains("eclipse")) {
158 shellBot.activate();
159 }
160 }
161 }
162
163 /**
164 * Close a view with a title
165 *
166 * @param title
167 * the title, like "welcome"
168 * @param bot
169 * the workbench bot
170 */
171 public static void closeView(String title, SWTWorkbenchBot bot) {
172 final List<SWTBotView> openViews = bot.views();
173 for (SWTBotView view : openViews) {
174 if (view.getTitle().equalsIgnoreCase(title)) {
175 view.close();
176 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
177 }
178 }
179 }
180
181 /**
182 * Switch to the tracing perspective
183 */
184 public static void switchToTracingPerspective() {
664fa59c
MK
185 switchToPerspective(TRACING_PERSPECTIVE_ID);
186 }
187
188 /**
189 * Switch to a given perspective
190 *
191 * @param id
192 * the perspective id (like
193 * "org.eclipse.linuxtools.tmf.ui.perspective"
194 */
195 public static void switchToPerspective(final String id) {
306e18d0
MK
196 UIThreadRunnable.syncExec(new VoidResult() {
197 @Override
198 public void run() {
199 try {
664fa59c 200 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
201 } catch (WorkbenchException e) {
202 fail(e.getMessage());
203 }
204 }
205 });
206 }
207
306e18d0
MK
208 /**
209 * If the test is running in the UI thread then fail
210 */
211 public static void failIfUIThread() {
212 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
213 fail("SWTBot test needs to run in a non-UI thread. Make sure that \"Run in UI thread\" is unchecked in your launch configuration or"
214 + " that useUIThread is set to false in the pom.xml");
215 }
216
306e18d0 217 }
7777d5f0
MK
218
219 /**
220 * Open a trace, this does not perform any validation though
221 *
222 * @param projectName
223 * The project name
224 * @param tracePath
225 * the path of the trace file (absolute or relative)
226 * @param traceType
227 * the trace canonical string (eg:
228 * org.eclipse.linuxtools.btf.trace)
229 */
230 public static void openTrace(final String projectName, final String tracePath, final String traceType) {
a3d7df19
BH
231 openTrace(projectName, tracePath, traceType, true);
232 }
233
234 /**
235 * Open a trace, this does not perform any validation though
236 *
237 * @param projectName
238 * The project name
239 * @param tracePath
240 * the path of the trace file (absolute or relative)
241 * @param traceType
242 * the trace canonical string (eg:
243 * org.eclipse.linuxtools.btf.trace)
244 * @param delay
245 * delay and wait for jobs
246 */
247 public static void openTrace(final String projectName, final String tracePath, final String traceType, boolean delay) {
7777d5f0
MK
248 final Exception exception[] = new Exception[1];
249 exception[0] = null;
250 UIThreadRunnable.syncExec(new VoidResult() {
251 @Override
252 public void run() {
253 try {
254 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
255 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
256 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, tracePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), traceType);
257 } catch (CoreException e) {
258 exception[0] = e;
259 }
260 }
261 });
262 if (exception[0] != null) {
263 fail(exception[0].getMessage());
264 }
265
a3d7df19
BH
266 if (delay) {
267 delay(1000);
268 waitForJobs();
269 }
7777d5f0 270 }
693ec829
BH
271
272 /**
b4290931 273 * Finds an editor and sets focus to the editor
693ec829
BH
274 *
275 * @param bot
276 * the workbench bot
277 * @param editorName
278 * the editor name
279 * @return the corresponding SWTBotEditor
280 */
b4290931 281 public static SWTBotEditor activateEditor(SWTWorkbenchBot bot, String editorName) {
693ec829
BH
282 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(editorName);
283 final SWTBotEditor editorBot = bot.editor(matcher);
284 IEditorPart iep = editorBot.getReference().getEditor(true);
285 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
286 editorBot.show();
287 UIThreadRunnable.syncExec(new VoidResult() {
288 @Override
289 public void run() {
290 tmfEd.setFocus();
291 }
292 });
293
fa24d78b
AM
294 SWTBotUtils.waitForJobs();
295 SWTBotUtils.delay(1000);
693ec829
BH
296 assertNotNull(tmfEd);
297 return editorBot;
298 }
b4290931
MAL
299
300 /**
301 * Opens a trace in an editor and get the TmfEventsEditor
302 *
303 * @param bot
304 * the workbench bot
305 * @param projectName
306 * the name of the project that contains the trace
307 * @param elementPath
308 * the trace element path (relative to Traces folder)
309 * @return TmfEventsEditor the opened editor
310 */
311 public static TmfEventsEditor openEditor(SWTWorkbenchBot bot, String projectName, IPath elementPath) {
312 final SWTBotView projectExplorerBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
313 projectExplorerBot.setFocus();
314
315 final SWTBotTree tree = bot.tree();
316 final SWTBotTreeItem treeItem = tree.getTreeItem(projectName);
317 treeItem.expand();
318
319 String nodeName = getFullNodeName(treeItem, TmfTracesFolder.TRACES_FOLDER_NAME);
320 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, treeItem));
321 SWTBotTreeItem tracesNode = treeItem.getNode(nodeName);
322 tracesNode.expand();
323
324 SWTBotTreeItem currentNode = tracesNode;
325 for (String segment : elementPath.segments()) {
326 String fullNodeName = getFullNodeName(currentNode, segment);
327 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(fullNodeName, currentNode));
328 SWTBotTreeItem newNode = currentNode.getNode(fullNodeName);
329 newNode.select();
330 newNode.doubleClick();
331 currentNode = newNode;
332 }
333
fa24d78b
AM
334 SWTBotUtils.delay(1000);
335 SWTBotUtils.waitForJobs();
b4290931
MAL
336 final String expectedTitle = elementPath.toString();
337
338 final IEditorPart iep[] = new IEditorPart[1];
339 UIThreadRunnable.syncExec(new VoidResult() {
340 @Override
341 public void run() {
342 IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
343 assertNotNull(ieds);
344 iep[0] = null;
345 for (IEditorReference ied : ieds) {
346 if (ied.getTitle().equals(expectedTitle)) {
347 iep[0] = ied.getEditor(true);
348 break;
349 }
350 }
351 }
352 });
353 assertNotNull(iep[0]);
354 return (TmfEventsEditor) iep[0];
355 }
356
357 private static String getFullNodeName(final SWTBotTreeItem treeItem, String prefix) {
358 List<String> nodes = treeItem.getNodes();
359 String nodeName = "";
360 for (String node : nodes) {
361 if (node.startsWith(prefix)) {
362 nodeName = node;
363 }
364 }
365 return nodeName;
366 }
306e18d0 367}
This page took 0.059523 seconds and 5 git commands to generate.