tmf: Make CtfTmfTestTrace null-friendly
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ctf / ui / swtbot / tests / AbstractImportAndReadSmokeTest.java
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
14 package org.eclipse.linuxtools.tmf.ctf.ui.swtbot.tests;
15
16 import static org.junit.Assert.assertNotNull;
17
18 import java.util.List;
19
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.varia.NullAppender;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.wizard.Wizard;
28 import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
30 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
31 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
32 import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
33 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
34 import org.eclipse.linuxtools.tmf.ui.swtbot.tests.SWTBotUtil;
35 import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
36 import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramView;
37 import org.eclipse.linuxtools.tmf.ui.views.statistics.TmfStatisticsView;
38 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
39 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
40 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
41 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
42 import org.eclipse.swtbot.swt.finder.results.VoidResult;
43 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
44 import org.eclipse.swtbot.swt.finder.waits.Conditions;
45 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
46 import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
48 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
49 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
50 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
51 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
52 import org.eclipse.ui.IEditorPart;
53 import org.eclipse.ui.IEditorReference;
54 import org.eclipse.ui.IPageLayout;
55 import org.eclipse.ui.IViewPart;
56 import org.eclipse.ui.IViewReference;
57 import org.eclipse.ui.PlatformUI;
58 import org.eclipse.ui.views.properties.PropertySheet;
59 import org.junit.BeforeClass;
60 import 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)
69 public 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 ITmfContext ctx = trace.seekEvent(0);
315 for (int i = 0; i < rank; i++) {
316 trace.getNext(ctx);
317 }
318 final CtfTmfEvent retVal = trace.getNext(ctx);
319 trace.dispose();
320 return retVal;
321 }
322
323 /**
324 * Gets a view part based on view title
325 * @param viewTile
326 * a view title
327 * @return the view part
328 */
329 protected IViewPart getViewPart(final String viewTile) {
330 final IViewPart[] vps = new IViewPart[1];
331 UIThreadRunnable.syncExec(new VoidResult() {
332 @Override
333 public void run() {
334 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
335 for (IViewReference viewRef : viewRefs) {
336 IViewPart vp = viewRef.getView(true);
337 if (vp.getTitle().equals(viewTile)) {
338 vps[0] = vp;
339 return;
340 }
341 }
342 }
343 });
344
345 return vps[0];
346 }
347 }
This page took 0.042418 seconds and 6 git commands to generate.