tmf: Add waitUntil / condition to tmf.ui.tests
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ctf / ui / swtbot / tests / AbstractImportAndReadSmokeTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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.tracecompass.tmf.ctf.ui.swtbot.tests;
15
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18
19 import java.util.List;
20
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.varia.NullAppender;
23 import org.eclipse.jdt.annotation.NonNull;
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.swtbot.eclipse.finder.SWTWorkbenchBot;
28 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
30 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
31 import org.eclipse.swtbot.swt.finder.results.VoidResult;
32 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
33 import org.eclipse.swtbot.swt.finder.waits.Conditions;
34 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
35 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
36 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
37 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
38 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
39 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
40 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportConfirmation;
41 import org.eclipse.tracecompass.internal.tmf.ui.views.statistics.TmfStatisticsViewImpl;
42 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
43 import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
44 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
45 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
46 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
47 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
48 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
49 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
50 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
51 import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
52 import org.eclipse.tracecompass.tmf.ui.views.histogram.HistogramView;
53 import org.eclipse.ui.IViewPart;
54 import org.eclipse.ui.IViewReference;
55 import org.eclipse.ui.PlatformUI;
56 import org.eclipse.ui.views.properties.PropertySheet;
57 import org.junit.AfterClass;
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 @NonNull CtfTestTrace fTrace = CtfTestTrace.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 SWTBotUtils.initialize();
90
91 /* set up for swtbot */
92 SWTBotPreferences.TIMEOUT = 50000; /* 50 second timeout */
93 fLogger.removeAllAppenders();
94 fLogger.addAppender(new NullAppender());
95 fBot = new SWTWorkbenchBot();
96
97 SWTBotUtils.closeView("welcome", fBot);
98
99 SWTBotUtils.switchToTracingPerspective();
100 /* finish waiting for eclipse to load */
101 WaitUtils.waitForJobs();
102 }
103
104 /**
105 * Test Class teardown
106 */
107 @AfterClass
108 public static void terminate() {
109 fLogger.removeAllAppenders();
110 }
111
112 /**
113 * Creates a tracing projects
114 *
115 * @param traceProjectName
116 * the name of the test project
117 */
118 protected static void createProject(String traceProjectName) {
119 SWTBotUtils.focusMainWindow(fBot.shells());
120 fBot.menu("File").menu("New").menu("Project...").click();
121
122 fBot.shell("New Project").setFocus();
123 SWTBotTree tree = fBot.tree();
124 assertNotNull(tree);
125 final String tracingKey = "Tracing";
126 fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(tracingKey, tree));
127 final SWTBotTreeItem tracingNode = tree.expandNode(tracingKey);
128
129 tracingNode.select();
130 final String projectKey = "Tracing Project";
131 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(projectKey, tracingNode));
132 final SWTBotTreeItem tracingProject = tracingNode.getNode(projectKey);
133 assertNotNull(tracingProject);
134
135 tracingProject.select();
136 tracingProject.click();
137
138 SWTBotButton nextButton = fBot.button("Next >");
139 fBot.waitUntil(Conditions.widgetIsEnabled(nextButton));
140 nextButton.click();
141 fBot.shell("Tracing Project").setFocus();
142
143 final SWTBotText text = fBot.text();
144 text.setText(traceProjectName);
145
146 fBot.button("Finish").click();
147 WaitUtils.waitForJobs();
148 }
149
150 /**
151 * Finishes the wizard
152 */
153 protected void importFinish() {
154 importFinish(ImportConfirmation.CONTINUE);
155 }
156
157 /**
158 * Finishes the wizard
159 *
160 * @param confirmationMode
161 * a confirmation value
162 * Note: Only {@link ImportConfirmation#RENAME_ALL},
163 * {@link ImportConfirmation#OVERWRITE_ALL},
164 * {@link ImportConfirmation#CONTINUE} are supported
165 */
166 protected void importFinish(ImportConfirmation confirmationMode) {
167 SWTBotShell shell = fBot.activeShell();
168 final SWTBotButton finishButton = fBot.button("Finish");
169 finishButton.click();
170 if (confirmationMode == ImportConfirmation.RENAME_ALL) {
171 fBot.waitUntil(Conditions.shellIsActive("Confirmation"));
172 SWTBotShell shell2 = fBot.activeShell();
173 SWTBotButton button = shell2.bot().button("Rename All");
174 button.click();
175 } else if (confirmationMode == ImportConfirmation.OVERWRITE_ALL) {
176 fBot.waitUntil(Conditions.shellIsActive("Confirmation"));
177 SWTBotShell shell2 = fBot.activeShell();
178 SWTBotButton button = shell2.bot().button("Overwrite All");
179 button.click();
180 }
181 fBot.waitUntil(Conditions.shellCloses(shell));
182 WaitUtils.waitForJobs();
183 }
184
185 /**
186 * Checks finish button enablement
187 *
188 * @param isEnabled
189 * state to check against
190 *
191 */
192 protected void checkFinishButton(boolean isEnabled) {
193 final SWTBotButton finishButton = fBot.button("Finish");
194 assertTrue(finishButton.isEnabled() == isEnabled);
195 }
196
197 // ---------------------------------------------
198 // Helpers for testing views
199 // ---------------------------------------------
200
201 /**
202 * Verifies the properties view for a given view part
203 *
204 * @param vp
205 * a view part
206 */
207 protected void testPropertyView(IViewPart vp) {
208 PropertySheet pv = (PropertySheet) vp;
209 assertNotNull(pv);
210 }
211
212 /**
213 * Verifies the Histogram View
214 *
215 * @param vp
216 * the view part
217 * @param tmfEd
218 * the events editor
219 */
220 protected void testHistogramView(IViewPart vp, final TmfEventsEditor tmfEd) {
221 final CtfTmfEvent desiredEvent1 = getEvent(100);
222 UIThreadRunnable.syncExec(new VoidResult() {
223 @Override
224 public void run() {
225 tmfEd.setFocus();
226 tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(desiredEvent1)));
227 }
228 });
229
230 WaitUtils.waitForJobs();
231 SWTBotUtils.delay(1000);
232
233 final CtfTmfEvent desiredEvent2 = getEvent(10000);
234 SWTBotView hvBot = fBot.viewById(HistogramView.ID);
235 List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons();
236 for (SWTBotToolbarButton hvTool : hvTools) {
237 if (hvTool.getToolTipText().toLowerCase().contains("lost")) {
238 hvTool.click();
239 }
240 }
241 HistogramView hv = (HistogramView) vp;
242 final TmfSelectionRangeUpdatedSignal signal = new TmfSelectionRangeUpdatedSignal(hv, desiredEvent1.getTimestamp());
243 final TmfSelectionRangeUpdatedSignal signal2 = new TmfSelectionRangeUpdatedSignal(hv, desiredEvent2.getTimestamp());
244 hv.updateTimeRange(100000);
245 WaitUtils.waitForJobs();
246 hv.selectionRangeUpdated(signal);
247 hv.broadcast(signal);
248 WaitUtils.waitForJobs();
249 SWTBotUtils.delay(1000);
250
251 hv.updateTimeRange(1000000000);
252 WaitUtils.waitForJobs();
253 hv.selectionRangeUpdated(signal2);
254 hv.broadcast(signal2);
255 WaitUtils.waitForJobs();
256 SWTBotUtils.delay(1000);
257 assertNotNull(hv);
258 }
259
260 /**
261 * Verifies the statistics view
262 *
263 * @param vp
264 * the view part
265 */
266 protected void testStatisticsView(IViewPart vp) {
267 TmfStatisticsViewImpl sv = (TmfStatisticsViewImpl) vp;
268 assertNotNull(sv);
269 }
270
271 // ---------------------------------------------
272 // Trace helpers
273 // ---------------------------------------------
274
275 /**
276 * Gets an event at a given rank
277 *
278 * @param rank
279 * a rank
280 * @return the event at given rank
281 */
282 protected CtfTmfEvent getEvent(int rank) {
283 CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(fTrace);
284 ITmfContext ctx = trace.seekEvent(rank);
285 CtfTmfEvent ret = trace.getNext(ctx);
286 ctx.dispose();
287 return ret;
288 }
289
290 /**
291 * Gets a view part based on view title
292 *
293 * @param viewTile
294 * a view title
295 * @return the view part
296 */
297 protected IViewPart getViewPart(final String viewTile) {
298 final IViewPart[] vps = new IViewPart[1];
299 UIThreadRunnable.syncExec(new VoidResult() {
300 @Override
301 public void run() {
302 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
303 for (IViewReference viewRef : viewRefs) {
304 IViewPart vp = viewRef.getView(true);
305 if (vp.getTitle().equals(viewTile)) {
306 vps[0] = vp;
307 return;
308 }
309 }
310 }
311 });
312
313 return vps[0];
314 }
315 }
This page took 0.039672 seconds and 5 git commands to generate.