TMF: Add trace stub for TMF unit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ui / swtbot / tests / SWTBotUtil.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
13package org.eclipse.linuxtools.tmf.ui.swtbot.tests;
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;
306e18d0
MK
24import org.eclipse.core.runtime.NullProgressMonitor;
25import org.eclipse.core.runtime.jobs.Job;
26import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
27import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
28import org.eclipse.linuxtools.tmf.ui.views.TracingPerspectiveFactory;
29import org.eclipse.swt.widgets.Display;
30import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
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;
41import org.eclipse.ui.IPageLayout;
306e18d0
MK
42import org.eclipse.ui.PlatformUI;
43import org.eclipse.ui.WorkbenchException;
44
45/**
46 * SWTBot Helper functions
47 *
48 * @author Matthew Khouzam
49 */
50public abstract class SWTBotUtil {
51 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
52
53 /**
54 * Waits for all Eclipse jobs to finish
55 */
56 public static void waitForJobs() {
57 while (!Job.getJobManager().isIdle()) {
58 delay(100);
59 }
60 }
61
62 /**
63 * Sleeps current thread for a given time.
64 *
65 * @param waitTimeMillis
66 * time in milliseconds to wait
67 */
68 public static void delay(final long waitTimeMillis) {
69 try {
70 Thread.sleep(waitTimeMillis);
71 } catch (final InterruptedException e) {
72 // Ignored
73 }
74 }
75
76 /**
77 * Create a tracing project
78 *
79 * @param projectName
80 * the name of the tracing project
81 */
82 public static void createProject(final String projectName) {
83 /*
84 * Make a new test
85 */
86 UIThreadRunnable.syncExec(new VoidResult() {
87 @Override
88 public void run() {
89 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
90 assertNotNull(project);
91 }
92 });
93
94 SWTBotUtil.waitForJobs();
95 }
96
93c91230
MAL
97 /**
98 * Deletes a tracing project
99 *
100 * @param projectName
101 * the name of the tracing project
102 * @param bot
103 * the workbench bot
104 */
105 public static void deleteProject(String projectName, SWTWorkbenchBot bot) {
106 // Wait for any analysis to complete because it might create supplementary files
107 SWTBotUtil.waitForJobs();
108 try {
109 ResourcesPlugin.getWorkspace().getRoot().getProject(projectName).refreshLocal(IResource.DEPTH_INFINITE, null);
110 } catch (CoreException e) {
111 }
112
113 SWTBotUtil.waitForJobs();
114
115 final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
116 projectViewBot.setFocus();
117
118 SWTBotTree treeBot = bot.tree();
119 SWTBotTreeItem treeItem = treeBot.getTreeItem(projectName);
120 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
121 contextMenu.click();
122
123 bot.shell("Delete Resources").setFocus();
124 final SWTBotCheckBox checkBox = bot.checkBox();
125 bot.waitUntil(Conditions.widgetIsEnabled(checkBox));
126 checkBox.click();
127
128 final SWTBotButton okButton = bot.button("OK");
129 bot.waitUntil(Conditions.widgetIsEnabled(okButton));
130 okButton.click();
131
132 SWTBotUtil.waitForJobs();
133 }
134
306e18d0
MK
135 /**
136 * Focus on the main window
137 *
138 * @param shellBots
139 * swtbotshells for all the shells
140 */
141 public static void focusMainWindow(SWTBotShell[] shellBots) {
142 for (SWTBotShell shellBot : shellBots) {
143 if (shellBot.getText().toLowerCase().contains("eclipse")) {
144 shellBot.activate();
145 }
146 }
147 }
148
149 /**
150 * Close a view with a title
151 *
152 * @param title
153 * the title, like "welcome"
154 * @param bot
155 * the workbench bot
156 */
157 public static void closeView(String title, SWTWorkbenchBot bot) {
158 final List<SWTBotView> openViews = bot.views();
159 for (SWTBotView view : openViews) {
160 if (view.getTitle().equalsIgnoreCase(title)) {
161 view.close();
162 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
163 }
164 }
165 }
166
167 /**
168 * Switch to the tracing perspective
169 */
170 public static void switchToTracingPerspective() {
664fa59c
MK
171 switchToPerspective(TRACING_PERSPECTIVE_ID);
172 }
173
174 /**
175 * Switch to a given perspective
176 *
177 * @param id
178 * the perspective id (like
179 * "org.eclipse.linuxtools.tmf.ui.perspective"
180 */
181 public static void switchToPerspective(final String id) {
306e18d0
MK
182 UIThreadRunnable.syncExec(new VoidResult() {
183 @Override
184 public void run() {
185 try {
664fa59c 186 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
306e18d0
MK
187 } catch (WorkbenchException e) {
188 fail(e.getMessage());
189 }
190 }
191 });
192 }
193
306e18d0
MK
194 /**
195 * If the test is running in the UI thread then fail
196 */
197 public static void failIfUIThread() {
198 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
199 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"
200 + " that useUIThread is set to false in the pom.xml");
201 }
202
306e18d0
MK
203 }
204}
This page took 0.036464 seconds and 5 git commands to generate.