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