tmf: Add initial SWTBot test for ImportTraceWizard
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ctf / ui / swtbot / tests / AbstractImportAndReadSmokeTest.java
CommitLineData
2f7b3dd7
BH
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 * Bernd Hufmann - Initial API and implementation
11 * (Extracted from ImportAndReadSmokeTest.java)
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ctf.ui.swtbot.tests;
15
16import static org.junit.Assert.assertNotNull;
17
18import java.util.List;
19
20import org.apache.log4j.Logger;
21import org.apache.log4j.varia.NullAppender;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.ResourcesPlugin;
24import org.eclipse.core.runtime.CoreException;
25import org.eclipse.jface.viewers.SelectionChangedEvent;
26import org.eclipse.jface.viewers.StructuredSelection;
27import org.eclipse.jface.wizard.Wizard;
28import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
29import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
30import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
31import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
32import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
33import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
34import org.eclipse.linuxtools.tmf.ui.swtbot.tests.SWTBotUtil;
35import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
36import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramView;
37import org.eclipse.linuxtools.tmf.ui.views.statistics.TmfStatisticsView;
38import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
39import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
40import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
41import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
42import org.eclipse.swtbot.swt.finder.results.VoidResult;
43import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
44import org.eclipse.swtbot.swt.finder.waits.Conditions;
45import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
46import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
47import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
48import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
49import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
50import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
51import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
52import org.eclipse.ui.IEditorPart;
53import org.eclipse.ui.IEditorReference;
54import org.eclipse.ui.IPageLayout;
55import org.eclipse.ui.IViewPart;
56import org.eclipse.ui.IViewReference;
57import org.eclipse.ui.PlatformUI;
58import org.eclipse.ui.views.properties.PropertySheet;
59import org.junit.BeforeClass;
60import org.junit.runner.RunWith;
61
62/**
63 * Abstract SWTBot Smoke test class.
64 *
65 * @author Matthew Khouzam
66 * @author Bernd Hufmann
67 */
68@RunWith(SWTBotJunit4ClassRunner.class)
69public abstract class AbstractImportAndReadSmokeTest {
70
71 /** Trace name */
72 protected static final String TRACE_NAME = "synthetic-trace";
73 /** Trace type name for generic CTF traces */
74 protected static final String TRACE_TYPE_NAME = "Generic CTF Trace";
75 /** A Generic CTF Trace*/
76 protected static final CtfTmfTestTrace fTrace = CtfTmfTestTrace.SYNTHETIC_TRACE;
77 /** SWT BOT workbench reference */
78 protected static SWTWorkbenchBot fBot;
79 /** Wizard to use */
80 protected static Wizard fWizard;
81
82 /** The Log4j logger instance. */
83 protected static final Logger fLogger = Logger.getRootLogger();
84
85 /** Test Class setup */
86 @BeforeClass
87 public static void init() {
88 SWTBotUtil.failIfUIThread();
89
90 /* set up for swtbot */
91 SWTBotPreferences.TIMEOUT = 50000; /* 50 second timeout */
92 fLogger.addAppender(new NullAppender());
93 fBot = new SWTWorkbenchBot();
94
95 SWTBotUtil.closeView("welcome", fBot);
96
97 SWTBotUtil.switchToTracingPerspective();
98 /* finish waiting for eclipse to load */
99 SWTBotUtil.waitForJobs();
100 }
101
102 /**
103 * Creates a tracing projects
104 */
105 protected void createProject() {
106 SWTBotUtil.focusMainWindow(fBot.shells());
107 fBot.menu("File").menu("New").menu("Project...").click();
108
109 fBot.waitUntil(Conditions.shellIsActive("New Project"));
110 SWTBotTree tree = fBot.tree();
111 assertNotNull(tree);
112 final String tracingKey = "Tracing";
113 fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(tracingKey, tree));
114 final SWTBotTreeItem tracingNode = tree.expandNode(tracingKey);
115
116 tracingNode.select();
117 final String projectKey = "Tracing Project";
118 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(projectKey, tracingNode));
119 final SWTBotTreeItem tracingProject = tracingNode.getNode(projectKey);
120 assertNotNull(tracingProject);
121
122 tracingProject.select();
123 tracingProject.click();
124
125 SWTBotButton nextButton = fBot.button("Next >");
126 fBot.waitUntil(Conditions.widgetIsEnabled(nextButton));
127 nextButton.click();
128 fBot.waitUntil(Conditions.shellIsActive("Tracing Project"));
129
130 final SWTBotText text = fBot.text();
131 text.setText(getProjectName());
132
133 fBot.button("Finish").click();
134 SWTBotUtil.waitForJobs();
135 }
136
137 /**
138 * Opens and get the TmfEventsEditor
139 * @return TmfEventsEditor
140 */
141 protected TmfEventsEditor openEditor() {
142 final SWTBotView projectExplorerBot = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
143 projectExplorerBot.setFocus();
144
145 final SWTBotTree tree = fBot.tree();
146 final SWTBotTreeItem treeItem = tree.getTreeItem(getProjectName());
147 treeItem.expand();
148
149 List<String> nodes = treeItem.getNodes();
150 String nodeName = "";
151 for (String node : nodes) {
152 if (node.startsWith("Traces")) {
153 nodeName = node;
154 }
155 }
156 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, treeItem));
157 treeItem.getNode(nodeName).expand();
158 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(TRACE_NAME, treeItem.getNode(nodeName)));
159 treeItem.getNode(nodeName).getNode(TRACE_NAME).select();
160 treeItem.getNode(nodeName).getNode(TRACE_NAME).doubleClick();
161 SWTBotUtil.delay(1000);
162 SWTBotUtil.waitForJobs();
163
164 final IEditorPart iep[] = new IEditorPart[1];
165 UIThreadRunnable.syncExec(new VoidResult() {
166 @Override
167 public void run() {
168 IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
169 assertNotNull(ieds);
170 iep[0] = null;
171 for (IEditorReference ied : ieds) {
172 if (ied.getTitle().equals(TRACE_NAME)) {
173 iep[0] = ied.getEditor(true);
174 break;
175 }
176 }
177 }
178 });
179 assertNotNull(iep[0]);
180 return (TmfEventsEditor) iep[0];
181 }
182
183 /**
184 * Deletes the tracing project
185 */
186 protected void deleteProject() {
187 // Wait for any analysis to complete because it might create supplementary files
188 SWTBotUtil.waitForJobs();
189 try {
190 ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName()).refreshLocal(IResource.DEPTH_INFINITE, null);
191 } catch (CoreException e) {
192 }
193
194 SWTBotUtil.waitForJobs();
195
196 final SWTBotView projectViewBot = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
197 projectViewBot.setFocus();
198
199 SWTBotTree treeBot = fBot.tree();
200 SWTBotTreeItem treeItem = treeBot.getTreeItem(getProjectName());
201 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
202 contextMenu.click();
203
204 String shellText = "Delete Resources";
205 fBot.waitUntil(Conditions.shellIsActive(shellText));
206 final SWTBotButton okButton = fBot.button("OK");
207 fBot.waitUntil(Conditions.widgetIsEnabled(okButton));
208 okButton.click();
209
210 SWTBotUtil.waitForJobs();
211 }
212
213 /**
214 * Finishes the wizard
215 */
216 protected void importFinish() {
217 SWTBotShell shell = fBot.activeShell();
218 final SWTBotButton finishButton = fBot.button("Finish");
219 finishButton.click();
220 fBot.waitUntil(Conditions.shellCloses(shell));
221 SWTBotUtil.waitForJobs();
222 }
223
224 /**
225 * Gets the project Name
226 * @return the project name
227 */
228 protected abstract String getProjectName();
229
230 // ---------------------------------------------
231 // Helpers for testing views
232 // ---------------------------------------------
233
234 /**
235 * Verifies the properties view for a given view part
236 *
237 * @param vp
238 * a view part
239 */
240 protected void testPropertyView(IViewPart vp) {
241 PropertySheet pv = (PropertySheet) vp;
242 assertNotNull(pv);
243 }
244
245 /**
246 * Verifies the Histogram View
247 * @param vp
248 * the view part
249 * @param tmfEd
250 * the events editor
251 */
252 protected void testHistogramView(IViewPart vp, final TmfEventsEditor tmfEd) {
253 final CtfTmfEvent desiredEvent1 = getEvent(100);
254 UIThreadRunnable.syncExec(new VoidResult() {
255 @Override
256 public void run() {
257 tmfEd.setFocus();
258 tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(desiredEvent1)));
259 }
260 });
261
262 SWTBotUtil.waitForJobs();
263 SWTBotUtil.delay(1000);
264
265 final CtfTmfEvent desiredEvent2 = getEvent(10000);
266 SWTBotView hvBot = fBot.viewById(HistogramView.ID);
267 List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons();
268 for (SWTBotToolbarButton hvTool : hvTools) {
269 if (hvTool.getToolTipText().toLowerCase().contains("lost")) {
270 hvTool.click();
271 }
272 }
273 HistogramView hv = (HistogramView) vp;
274 final TmfTimeSynchSignal signal = new TmfTimeSynchSignal(hv, desiredEvent1.getTimestamp());
275 final TmfTimeSynchSignal signal2 = new TmfTimeSynchSignal(hv, desiredEvent2.getTimestamp());
276 hv.updateTimeRange(100000);
277 SWTBotUtil.waitForJobs();
278 hv.currentTimeUpdated(signal);
279 hv.broadcast(signal);
280 SWTBotUtil.waitForJobs();
281 SWTBotUtil.delay(1000);
282
283 hv.updateTimeRange(1000000000);
284 SWTBotUtil.waitForJobs();
285 hv.currentTimeUpdated(signal2);
286 hv.broadcast(signal2);
287 SWTBotUtil.waitForJobs();
288 SWTBotUtil.delay(1000);
289 assertNotNull(hv);
290 }
291
292 /**
293 * Verifies the statistics view
294 * @param vp
295 * the view part
296 */
297 protected void testStatisticsView(IViewPart vp) {
298 TmfStatisticsView sv = (TmfStatisticsView) vp;
299 assertNotNull(sv);
300 }
301
302 // ---------------------------------------------
303 // Trace helpers
304 // ---------------------------------------------
305
306 /**
307 * Gets an event at a given rank
308 * @param rank
309 * a rank
310 * @return the event at given rank
311 */
312 protected CtfTmfEvent getEvent(int rank) {
313 CtfTmfTrace trace = fTrace.getTrace();
314 if (trace == null) {
315 return null;
316 }
317 ITmfContext ctx = trace.seekEvent(0);
318 for (int i = 0; i < rank; i++) {
319 trace.getNext(ctx);
320 }
321 final CtfTmfEvent retVal = trace.getNext(ctx);
322 trace.dispose();
323 return retVal;
324 }
325
326 /**
327 * Gets a view part based on view title
328 * @param viewTile
329 * a view title
330 * @return the view part
331 */
332 protected IViewPart getViewPart(final String viewTile) {
333 final IViewPart[] vps = new IViewPart[1];
334 UIThreadRunnable.syncExec(new VoidResult() {
335 @Override
336 public void run() {
337 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
338 for (IViewReference viewRef : viewRefs) {
339 IViewPart vp = viewRef.getView(true);
340 if (vp.getTitle().equals(viewTile)) {
341 vps[0] = vp;
342 return;
343 }
344 }
345 }
346 });
347
348 return vps[0];
349 }
350}
This page took 0.038534 seconds and 5 git commands to generate.