tmf: batch import wizard smoke test
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ui / swtbot / tests / ImportAndReadSmokeTest.java
CommitLineData
4b451cbe
MK
1/*******************************************************************************
2 * Copyright (c) 2013 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 * Marc-Andre Laperle
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui.swtbot.tests;
15
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.fail;
18
19import java.util.List;
20
21import org.apache.log4j.Logger;
22import org.apache.log4j.varia.NullAppender;
23import org.eclipse.core.resources.IResource;
24import org.eclipse.core.resources.ResourcesPlugin;
25import org.eclipse.core.runtime.CoreException;
26import org.eclipse.core.runtime.jobs.Job;
27import org.eclipse.jface.viewers.SelectionChangedEvent;
28import org.eclipse.jface.viewers.StructuredSelection;
29import org.eclipse.jface.wizard.IWizardPage;
30import org.eclipse.jface.wizard.Wizard;
31import org.eclipse.jface.wizard.WizardDialog;
32import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
33import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
34import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
35import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace;
36import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
37import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
38import org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.BatchImportTraceWizard;
39import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
40import org.eclipse.linuxtools.tmf.ui.views.TracingPerspectiveFactory;
41import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramView;
42import org.eclipse.linuxtools.tmf.ui.views.statistics.TmfStatisticsView;
43import org.eclipse.swt.widgets.Display;
44import org.eclipse.swt.widgets.Shell;
45import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
46import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
47import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
48import org.eclipse.swtbot.swt.finder.results.VoidResult;
49import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
50import org.eclipse.swtbot.swt.finder.waits.Conditions;
51import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
52import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
53import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
54import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
55import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
56import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
57import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
58import org.eclipse.ui.IEditorPart;
59import org.eclipse.ui.IEditorReference;
60import org.eclipse.ui.IPageLayout;
61import org.eclipse.ui.IViewPart;
62import org.eclipse.ui.IViewReference;
63import org.eclipse.ui.IWorkbench;
64import org.eclipse.ui.IWorkbenchWindow;
65import org.eclipse.ui.PlatformUI;
66import org.eclipse.ui.WorkbenchException;
67import org.eclipse.ui.views.properties.PropertySheet;
68import org.junit.BeforeClass;
69import org.junit.Test;
70
71/**
72 * SWTBot Smoke test. base for other tests
73 *
74 * @author Matthew Khouzam
75 */
76public class ImportAndReadSmokeTest {
77
78 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
79 private static final String TRACE_PROJECT_NAME = "test";
80 private static final String TRACE_NAME = "synthetic-trace";
81 private static final String TRACE_TYPE_NAME = "Generic CTF Trace";
82 private static final CtfTmfTestTrace fTrace = CtfTmfTestTrace.SYNTHETIC_TRACE;
83
84 private static SWTWorkbenchBot fBot;
85 private static Wizard fWizard;
86
87 /** The Log4j logger instance. */
88 private static final Logger fLogger = Logger.getRootLogger();
89
90 /** Test Class setup */
91 @BeforeClass
92 public static void init() {
93 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
94 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"
95 + " that useUIThread is set to false in the pom.xml");
96 }
97
98 /* set up for swtbot */
99 SWTBotPreferences.TIMEOUT = 50000; /* 50 second timeout */
100 fLogger.addAppender(new NullAppender());
101 fBot = new SWTWorkbenchBot();
102
103 final List<SWTBotView> openViews = fBot.views();
104 for (SWTBotView view : openViews) {
105 if (view.getTitle().equals("Welcome")) {
106 view.close();
107 fBot.waitUntil(ConditionHelpers.ViewIsClosed(view));
108 }
109 }
110
111 switchToTracingPerspective();
112 /* finish waiting for eclipse to load */
113 waitForJobs();
114 }
115
116 private static void switchToTracingPerspective() {
117 UIThreadRunnable.syncExec(new VoidResult() {
118 @Override
119 public void run() {
120 try {
121 PlatformUI.getWorkbench().showPerspective(TRACING_PERSPECTIVE_ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
122 } catch (WorkbenchException e) {
123 fail(e.getMessage());
124 }
125 }
126 });
127 }
128
129 private static void focusMainWindow() {
130 for (SWTBotShell shellBot : fBot.shells()) {
131 if (shellBot.getText().toLowerCase().contains("eclipse")) {
132 shellBot.activate();
133 }
134 }
135 }
136
137 /**
138 * Waits for all Eclipse jobs to finish
139 */
140 protected static void waitForJobs() {
141 while (!Job.getJobManager().isIdle()) {
142 delay(100);
143 }
144 }
145
146 /**
147 * Sleeps current thread for a given time.
148 *
149 * @param waitTimeMillis
150 * time in milliseconds to wait
151 */
152 protected static void delay(final long waitTimeMillis) {
153 try {
154 Thread.sleep(waitTimeMillis);
155 } catch (final InterruptedException e) {
156 // Ignored
157 }
158 }
159
160 /**
161 * Main test case
162 */
163 @Test
164 public void test() {
165 createProject();
166
167 batchImportOpenWizard();
168 batchImportSelecTraceType();
169 batchImportAddDirectory();
170 batchImportSelectTrace();
171 batchImportFinish();
172
173 TmfEventsEditor tmfEd = openEditor();
174
175 testHistogramView(getViewPart("Histogram"), tmfEd);
176 testPropertyView(getViewPart("Properties"));
177 testStatisticsView(getViewPart("Statistics"));
178
179 deleteProject();
180 }
181
182 private static void createProject() {
183 focusMainWindow();
184 fBot.menu("File").menu("New").menu("Project...").click();
185
186 fBot.waitUntil(Conditions.shellIsActive("New Project"));
187 SWTBotTree tree = fBot.tree();
188 assertNotNull(tree);
189 final String tracingKey = "Tracing";
190 fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(tracingKey, tree));
191 final SWTBotTreeItem tracingNode = tree.expandNode(tracingKey);
192
193 tracingNode.select();
194 final String projectKey = "Tracing Project";
195 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(projectKey, tracingNode));
196 final SWTBotTreeItem tracingProject = tracingNode.getNode(projectKey);
197 assertNotNull(tracingProject);
198
199 tracingProject.select();
200 tracingProject.click();
201
202 SWTBotButton nextButton = fBot.button("Next >");
203 fBot.waitUntil(Conditions.widgetIsEnabled(nextButton));
204 nextButton.click();
205 fBot.waitUntil(Conditions.shellIsActive("Tracing Project"));
206
207 final SWTBotText text = fBot.text();
208 text.setText(TRACE_PROJECT_NAME);
209
210 fBot.button("Finish").click();
211 waitForJobs();
212 }
213
214 private static void batchImportOpenWizard() {
215 fWizard = new BatchImportTraceWizard();
216
217 UIThreadRunnable.asyncExec(new VoidResult() {
218 @Override
219 public void run() {
220 final IWorkbench workbench = PlatformUI.getWorkbench();
221 // Fire the Import Trace Wizard
222 if (workbench != null) {
223 final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
224 Shell shell = activeWorkbenchWindow.getShell();
225 assertNotNull(shell);
226 ((BatchImportTraceWizard) fWizard).init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
227 WizardDialog dialog = new WizardDialog(shell, fWizard);
228 dialog.open();
229 }
230 }
231 });
232
233 fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard));
234 }
235
236 private static void batchImportSelecTraceType() {
237 final SWTBotTree tree = fBot.tree();
238 final String ctfId = "Common Trace Format";
239 fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(ctfId, tree));
240 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(TRACE_TYPE_NAME, tree.getTreeItem(ctfId)));
241 tree.getTreeItem(ctfId).getNode(TRACE_TYPE_NAME).check();
242 batchImportClickNext();
243 }
244
245 private static void batchImportAddDirectory() {
246 UIThreadRunnable.syncExec(new VoidResult() {
247 @Override
248 public void run() {
249 ((BatchImportTraceWizard) fWizard).addFileToScan(fTrace.getPath());
250 }
251 });
252 final SWTBotButton removeButton = fBot.button("Remove");
253 fBot.waitUntil(Conditions.widgetIsEnabled(removeButton));
254 removeButton.click();
255 fBot.waitUntil(Conditions.tableHasRows(fBot.table(), 1));
256
257 batchImportClickNext();
258 }
259
260 private static void batchImportSelectTrace() {
261 SWTBotTree tree = fBot.tree();
262 fBot.waitUntil(Conditions.widgetIsEnabled(tree));
263 final SWTBotTreeItem genericCtfTreeItem = tree.getTreeItem(TRACE_TYPE_NAME);
264 fBot.waitUntil(Conditions.widgetIsEnabled(genericCtfTreeItem));
265 genericCtfTreeItem.expand();
266 genericCtfTreeItem.check();
267 batchImportClickNext();
268 }
269
270 private static void batchImportClickNext() {
271 IWizardPage currentPage = fWizard.getContainer().getCurrentPage();
272 IWizardPage desiredPage = fWizard.getNextPage(currentPage);
273 SWTBotButton nextButton = fBot.button("Next >");
274 nextButton.click();
275 fBot.waitUntil(ConditionHelpers.isWizardOnPage(fWizard, desiredPage));
276 }
277
278 private static void batchImportFinish() {
279 SWTBotShell shell = fBot.activeShell();
280 final SWTBotButton finishButton = fBot.button("Finish");
281 finishButton.click();
282 fBot.waitUntil(Conditions.shellCloses(shell));
283 waitForJobs();
284 }
285
286 private static TmfEventsEditor openEditor() {
287 final SWTBotView projectExplorerBot = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
288 projectExplorerBot.setFocus();
289
290 final SWTBotTree tree = fBot.tree();
291 final SWTBotTreeItem treeItem = tree.getTreeItem(TRACE_PROJECT_NAME);
292 treeItem.expand();
293
294 List<String> nodes = treeItem.getNodes();
295 String nodeName = "";
296 for (String node : nodes) {
297 if (node.startsWith("Traces")) {
298 nodeName = node;
299 }
300 }
301 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, treeItem));
302 treeItem.getNode(nodeName).expand();
303 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(TRACE_NAME, treeItem.getNode(nodeName)));
304 treeItem.getNode(nodeName).getNode(TRACE_NAME).select();
305 treeItem.getNode(nodeName).getNode(TRACE_NAME).doubleClick();
306 delay(1000);
307 waitForJobs();
308
309 final IEditorPart iep[] = new IEditorPart[1];
310 UIThreadRunnable.syncExec(new VoidResult() {
311 @Override
312 public void run() {
313 IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
314 assertNotNull(ieds);
315 iep[0] = null;
316 for (IEditorReference ied : ieds) {
317 if (ied.getTitle().equals(TRACE_NAME)) {
318 iep[0] = ied.getEditor(true);
319 break;
320 }
321 }
322 }
323 });
324 assertNotNull(iep[0]);
325 return (TmfEventsEditor) iep[0];
326 }
327
328 private static void deleteProject() {
329 try {
330 ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME).refreshLocal(IResource.DEPTH_INFINITE, null);
331 } catch (CoreException e) {
332 }
333
334 waitForJobs();
335
336 final SWTBotView projectViewBot = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
337 projectViewBot.setFocus();
338
339 SWTBotTree treeBot = fBot.tree();
340 SWTBotTreeItem treeItem = treeBot.getTreeItem(TRACE_PROJECT_NAME);
341 SWTBotMenu contextMenu = treeItem.contextMenu("Delete");
342 contextMenu.click();
343
344 String shellText = "Delete Resources";
345 fBot.waitUntil(Conditions.shellIsActive(shellText));
346 final SWTBotButton okButton = fBot.button("OK");
347 fBot.waitUntil(Conditions.widgetIsEnabled(okButton));
348 okButton.click();
349
350 waitForJobs();
351 }
352
353 // ---------------------------------------------
354 // Helpers for testing views
355 // ---------------------------------------------
356
357 private static void testPropertyView(IViewPart vp) {
358 PropertySheet pv = (PropertySheet) vp;
359 assertNotNull(pv);
360 }
361
362 private static void testHistogramView(IViewPart vp, final TmfEventsEditor tmfEd) {
363 final CtfTmfEvent desiredEvent1 = getEvent(100);
364 UIThreadRunnable.syncExec(new VoidResult() {
365 @Override
366 public void run() {
367 tmfEd.setFocus();
368 tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(desiredEvent1)));
369 }
370 });
371
372 waitForJobs();
373 delay(1000);
374
375 final CtfTmfEvent desiredEvent2 = getEvent(10000);
376 SWTBotView hvBot = fBot.viewById(HistogramView.ID);
377 List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons();
378 for (SWTBotToolbarButton hvTool : hvTools) {
379 if (hvTool.getToolTipText().toLowerCase().contains("lost")) {
380 hvTool.click();
381 }
382 }
383 HistogramView hv = (HistogramView) vp;
384 final TmfTimeSynchSignal signal = new TmfTimeSynchSignal(hv, desiredEvent1.getTimestamp());
385 final TmfTimeSynchSignal signal2 = new TmfTimeSynchSignal(hv, desiredEvent2.getTimestamp());
386 hv.updateTimeRange(100000);
387 waitForJobs();
388 hv.currentTimeUpdated(signal);
389 hv.broadcast(signal);
390 waitForJobs();
391 delay(1000);
392
393 hv.updateTimeRange(1000000000);
394 waitForJobs();
395 hv.currentTimeUpdated(signal2);
396 hv.broadcast(signal2);
397 waitForJobs();
398 delay(1000);
399 assertNotNull(hv);
400 }
401
402 private static void testStatisticsView(IViewPart vp) {
403 TmfStatisticsView sv = (TmfStatisticsView) vp;
404 assertNotNull(sv);
405 }
406
407 // ---------------------------------------------
408 // Trace helpers
409 // ---------------------------------------------
410
411 private static CtfTmfEvent getEvent(int rank) {
412 CtfTmfTrace trace = fTrace.getTrace();
413 if (trace == null) {
414 return null;
415 }
416 ITmfContext ctx = trace.seekEvent(0);
417 for (int i = 0; i < rank; i++) {
418 trace.getNext(ctx);
419 }
420 final CtfTmfEvent retVal = trace.getNext(ctx);
421 trace.dispose();
422 return retVal;
423 }
424
425 private static IViewPart getViewPart(final String viewTile) {
426 final IViewPart[] vps = new IViewPart[1];
427 UIThreadRunnable.syncExec(new VoidResult() {
428 @Override
429 public void run() {
430 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
431 for (IViewReference viewRef : viewRefs) {
432 IViewPart vp = viewRef.getView(true);
433 if (vp.getTitle().equals(viewTile)) {
434 vps[0] = vp;
435 return;
436 }
437 }
438 }
439 });
440
441 return vps[0];
442 }
443}
This page took 0.058305 seconds and 5 git commands to generate.