| 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 | |
| 14 | package org.eclipse.linuxtools.tmf.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.IWizardPage; |
| 28 | import org.eclipse.jface.wizard.Wizard; |
| 29 | import org.eclipse.jface.wizard.WizardDialog; |
| 30 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 31 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
| 32 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; |
| 33 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; |
| 34 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 35 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; |
| 36 | import org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.BatchImportTraceWizard; |
| 37 | import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers; |
| 38 | import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramView; |
| 39 | import org.eclipse.linuxtools.tmf.ui.views.statistics.TmfStatisticsView; |
| 40 | import org.eclipse.swt.widgets.Shell; |
| 41 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
| 42 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
| 43 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| 44 | import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 45 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
| 46 | import org.eclipse.swtbot.swt.finder.waits.Conditions; |
| 47 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; |
| 48 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; |
| 49 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
| 50 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; |
| 51 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton; |
| 52 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
| 53 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 54 | import org.eclipse.ui.IEditorPart; |
| 55 | import org.eclipse.ui.IEditorReference; |
| 56 | import org.eclipse.ui.IPageLayout; |
| 57 | import org.eclipse.ui.IViewPart; |
| 58 | import org.eclipse.ui.IViewReference; |
| 59 | import org.eclipse.ui.IWorkbench; |
| 60 | import org.eclipse.ui.IWorkbenchWindow; |
| 61 | import org.eclipse.ui.PlatformUI; |
| 62 | import org.eclipse.ui.views.properties.PropertySheet; |
| 63 | import org.junit.BeforeClass; |
| 64 | import org.junit.Test; |
| 65 | |
| 66 | /** |
| 67 | * SWTBot Smoke test. base for other tests |
| 68 | * |
| 69 | * @author Matthew Khouzam |
| 70 | */ |
| 71 | public class ImportAndReadSmokeTest { |
| 72 | |
| 73 | |
| 74 | private static final String TRACE_PROJECT_NAME = "test"; |
| 75 | private static final String TRACE_NAME = "synthetic-trace"; |
| 76 | private static final String TRACE_TYPE_NAME = "Generic CTF Trace"; |
| 77 | private static final CtfTmfTestTrace fTrace = CtfTmfTestTrace.SYNTHETIC_TRACE; |
| 78 | |
| 79 | private static SWTWorkbenchBot fBot; |
| 80 | private static Wizard fWizard; |
| 81 | |
| 82 | /** The Log4j logger instance. */ |
| 83 | private 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 | * Main test case |
| 104 | */ |
| 105 | @Test |
| 106 | public void test() { |
| 107 | createProject(); |
| 108 | |
| 109 | batchImportOpenWizard(); |
| 110 | batchImportSelecTraceType(); |
| 111 | batchImportAddDirectory(); |
| 112 | batchImportSelectTrace(); |
| 113 | batchImportFinish(); |
| 114 | |
| 115 | TmfEventsEditor tmfEd = openEditor(); |
| 116 | |
| 117 | testHistogramView(getViewPart("Histogram"), tmfEd); |
| 118 | testPropertyView(getViewPart("Properties")); |
| 119 | testStatisticsView(getViewPart("Statistics")); |
| 120 | |
| 121 | deleteProject(); |
| 122 | } |
| 123 | |
| 124 | private static void createProject() { |
| 125 | SWTBotUtil.focusMainWindow(fBot.shells()); |
| 126 | fBot.menu("File").menu("New").menu("Project...").click(); |
| 127 | |
| 128 | fBot.waitUntil(Conditions.shellIsActive("New Project")); |
| 129 | SWTBotTree tree = fBot.tree(); |
| 130 | assertNotNull(tree); |
| 131 | final String tracingKey = "Tracing"; |
| 132 | fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(tracingKey, tree)); |
| 133 | final SWTBotTreeItem tracingNode = tree.expandNode(tracingKey); |
| 134 | |
| 135 | tracingNode.select(); |
| 136 | final String projectKey = "Tracing Project"; |
| 137 | fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(projectKey, tracingNode)); |
| 138 | final SWTBotTreeItem tracingProject = tracingNode.getNode(projectKey); |
| 139 | assertNotNull(tracingProject); |
| 140 | |
| 141 | tracingProject.select(); |
| 142 | tracingProject.click(); |
| 143 | |
| 144 | SWTBotButton nextButton = fBot.button("Next >"); |
| 145 | fBot.waitUntil(Conditions.widgetIsEnabled(nextButton)); |
| 146 | nextButton.click(); |
| 147 | fBot.waitUntil(Conditions.shellIsActive("Tracing Project")); |
| 148 | |
| 149 | final SWTBotText text = fBot.text(); |
| 150 | text.setText(TRACE_PROJECT_NAME); |
| 151 | |
| 152 | fBot.button("Finish").click(); |
| 153 | SWTBotUtil.waitForJobs(); |
| 154 | } |
| 155 | |
| 156 | private static void batchImportOpenWizard() { |
| 157 | fWizard = new BatchImportTraceWizard(); |
| 158 | |
| 159 | UIThreadRunnable.asyncExec(new VoidResult() { |
| 160 | @Override |
| 161 | public void run() { |
| 162 | final IWorkbench workbench = PlatformUI.getWorkbench(); |
| 163 | // Fire the Import Trace Wizard |
| 164 | if (workbench != null) { |
| 165 | final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow(); |
| 166 | Shell shell = activeWorkbenchWindow.getShell(); |
| 167 | assertNotNull(shell); |
| 168 | ((BatchImportTraceWizard) fWizard).init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY); |
| 169 | WizardDialog dialog = new WizardDialog(shell, fWizard); |
| 170 | dialog.open(); |
| 171 | } |
| 172 | } |
| 173 | }); |
| 174 | |
| 175 | fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard)); |
| 176 | } |
| 177 | |
| 178 | private static void batchImportSelecTraceType() { |
| 179 | final SWTBotTree tree = fBot.tree(); |
| 180 | final String ctfId = "Common Trace Format"; |
| 181 | fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(ctfId, tree)); |
| 182 | fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(TRACE_TYPE_NAME, tree.getTreeItem(ctfId))); |
| 183 | tree.getTreeItem(ctfId).getNode(TRACE_TYPE_NAME).check(); |
| 184 | batchImportClickNext(); |
| 185 | } |
| 186 | |
| 187 | private static void batchImportAddDirectory() { |
| 188 | UIThreadRunnable.syncExec(new VoidResult() { |
| 189 | @Override |
| 190 | public void run() { |
| 191 | ((BatchImportTraceWizard) fWizard).addFileToScan(fTrace.getPath()); |
| 192 | } |
| 193 | }); |
| 194 | final SWTBotButton removeButton = fBot.button("Remove"); |
| 195 | fBot.waitUntil(Conditions.widgetIsEnabled(removeButton)); |
| 196 | removeButton.click(); |
| 197 | fBot.waitUntil(Conditions.tableHasRows(fBot.table(), 1)); |
| 198 | |
| 199 | batchImportClickNext(); |
| 200 | } |
| 201 | |
| 202 | private static void batchImportSelectTrace() { |
| 203 | SWTBotTree tree = fBot.tree(); |
| 204 | fBot.waitUntil(Conditions.widgetIsEnabled(tree)); |
| 205 | final SWTBotTreeItem genericCtfTreeItem = tree.getTreeItem(TRACE_TYPE_NAME); |
| 206 | fBot.waitUntil(Conditions.widgetIsEnabled(genericCtfTreeItem)); |
| 207 | genericCtfTreeItem.expand(); |
| 208 | genericCtfTreeItem.check(); |
| 209 | batchImportClickNext(); |
| 210 | } |
| 211 | |
| 212 | private static void batchImportClickNext() { |
| 213 | IWizardPage currentPage = fWizard.getContainer().getCurrentPage(); |
| 214 | IWizardPage desiredPage = fWizard.getNextPage(currentPage); |
| 215 | SWTBotButton nextButton = fBot.button("Next >"); |
| 216 | nextButton.click(); |
| 217 | fBot.waitUntil(ConditionHelpers.isWizardOnPage(fWizard, desiredPage)); |
| 218 | } |
| 219 | |
| 220 | private static void batchImportFinish() { |
| 221 | SWTBotShell shell = fBot.activeShell(); |
| 222 | final SWTBotButton finishButton = fBot.button("Finish"); |
| 223 | finishButton.click(); |
| 224 | fBot.waitUntil(Conditions.shellCloses(shell)); |
| 225 | SWTBotUtil.waitForJobs(); |
| 226 | } |
| 227 | |
| 228 | private static TmfEventsEditor openEditor() { |
| 229 | final SWTBotView projectExplorerBot = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER); |
| 230 | projectExplorerBot.setFocus(); |
| 231 | |
| 232 | final SWTBotTree tree = fBot.tree(); |
| 233 | final SWTBotTreeItem treeItem = tree.getTreeItem(TRACE_PROJECT_NAME); |
| 234 | treeItem.expand(); |
| 235 | |
| 236 | List<String> nodes = treeItem.getNodes(); |
| 237 | String nodeName = ""; |
| 238 | for (String node : nodes) { |
| 239 | if (node.startsWith("Traces")) { |
| 240 | nodeName = node; |
| 241 | } |
| 242 | } |
| 243 | fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, treeItem)); |
| 244 | treeItem.getNode(nodeName).expand(); |
| 245 | fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(TRACE_NAME, treeItem.getNode(nodeName))); |
| 246 | treeItem.getNode(nodeName).getNode(TRACE_NAME).select(); |
| 247 | treeItem.getNode(nodeName).getNode(TRACE_NAME).doubleClick(); |
| 248 | SWTBotUtil.delay(1000); |
| 249 | SWTBotUtil.waitForJobs(); |
| 250 | |
| 251 | final IEditorPart iep[] = new IEditorPart[1]; |
| 252 | UIThreadRunnable.syncExec(new VoidResult() { |
| 253 | @Override |
| 254 | public void run() { |
| 255 | IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences(); |
| 256 | assertNotNull(ieds); |
| 257 | iep[0] = null; |
| 258 | for (IEditorReference ied : ieds) { |
| 259 | if (ied.getTitle().equals(TRACE_NAME)) { |
| 260 | iep[0] = ied.getEditor(true); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | }); |
| 266 | assertNotNull(iep[0]); |
| 267 | return (TmfEventsEditor) iep[0]; |
| 268 | } |
| 269 | |
| 270 | private static void deleteProject() { |
| 271 | try { |
| 272 | ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME).refreshLocal(IResource.DEPTH_INFINITE, null); |
| 273 | } catch (CoreException e) { |
| 274 | } |
| 275 | |
| 276 | SWTBotUtil.waitForJobs(); |
| 277 | |
| 278 | final SWTBotView projectViewBot = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER); |
| 279 | projectViewBot.setFocus(); |
| 280 | |
| 281 | SWTBotTree treeBot = fBot.tree(); |
| 282 | SWTBotTreeItem treeItem = treeBot.getTreeItem(TRACE_PROJECT_NAME); |
| 283 | SWTBotMenu contextMenu = treeItem.contextMenu("Delete"); |
| 284 | contextMenu.click(); |
| 285 | |
| 286 | String shellText = "Delete Resources"; |
| 287 | fBot.waitUntil(Conditions.shellIsActive(shellText)); |
| 288 | final SWTBotButton okButton = fBot.button("OK"); |
| 289 | fBot.waitUntil(Conditions.widgetIsEnabled(okButton)); |
| 290 | okButton.click(); |
| 291 | |
| 292 | SWTBotUtil.waitForJobs(); |
| 293 | } |
| 294 | |
| 295 | // --------------------------------------------- |
| 296 | // Helpers for testing views |
| 297 | // --------------------------------------------- |
| 298 | |
| 299 | private static void testPropertyView(IViewPart vp) { |
| 300 | PropertySheet pv = (PropertySheet) vp; |
| 301 | assertNotNull(pv); |
| 302 | } |
| 303 | |
| 304 | private static void testHistogramView(IViewPart vp, final TmfEventsEditor tmfEd) { |
| 305 | final CtfTmfEvent desiredEvent1 = getEvent(100); |
| 306 | UIThreadRunnable.syncExec(new VoidResult() { |
| 307 | @Override |
| 308 | public void run() { |
| 309 | tmfEd.setFocus(); |
| 310 | tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(desiredEvent1))); |
| 311 | } |
| 312 | }); |
| 313 | |
| 314 | SWTBotUtil.waitForJobs(); |
| 315 | SWTBotUtil.delay(1000); |
| 316 | |
| 317 | final CtfTmfEvent desiredEvent2 = getEvent(10000); |
| 318 | SWTBotView hvBot = fBot.viewById(HistogramView.ID); |
| 319 | List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons(); |
| 320 | for (SWTBotToolbarButton hvTool : hvTools) { |
| 321 | if (hvTool.getToolTipText().toLowerCase().contains("lost")) { |
| 322 | hvTool.click(); |
| 323 | } |
| 324 | } |
| 325 | HistogramView hv = (HistogramView) vp; |
| 326 | final TmfTimeSynchSignal signal = new TmfTimeSynchSignal(hv, desiredEvent1.getTimestamp()); |
| 327 | final TmfTimeSynchSignal signal2 = new TmfTimeSynchSignal(hv, desiredEvent2.getTimestamp()); |
| 328 | hv.updateTimeRange(100000); |
| 329 | SWTBotUtil.waitForJobs(); |
| 330 | hv.currentTimeUpdated(signal); |
| 331 | hv.broadcast(signal); |
| 332 | SWTBotUtil.waitForJobs(); |
| 333 | SWTBotUtil.delay(1000); |
| 334 | |
| 335 | hv.updateTimeRange(1000000000); |
| 336 | SWTBotUtil.waitForJobs(); |
| 337 | hv.currentTimeUpdated(signal2); |
| 338 | hv.broadcast(signal2); |
| 339 | SWTBotUtil.waitForJobs(); |
| 340 | SWTBotUtil.delay(1000); |
| 341 | assertNotNull(hv); |
| 342 | } |
| 343 | |
| 344 | private static void testStatisticsView(IViewPart vp) { |
| 345 | TmfStatisticsView sv = (TmfStatisticsView) vp; |
| 346 | assertNotNull(sv); |
| 347 | } |
| 348 | |
| 349 | // --------------------------------------------- |
| 350 | // Trace helpers |
| 351 | // --------------------------------------------- |
| 352 | |
| 353 | private static CtfTmfEvent getEvent(int rank) { |
| 354 | CtfTmfTrace trace = fTrace.getTrace(); |
| 355 | if (trace == null) { |
| 356 | return null; |
| 357 | } |
| 358 | ITmfContext ctx = trace.seekEvent(0); |
| 359 | for (int i = 0; i < rank; i++) { |
| 360 | trace.getNext(ctx); |
| 361 | } |
| 362 | final CtfTmfEvent retVal = trace.getNext(ctx); |
| 363 | trace.dispose(); |
| 364 | return retVal; |
| 365 | } |
| 366 | |
| 367 | private static IViewPart getViewPart(final String viewTile) { |
| 368 | final IViewPart[] vps = new IViewPart[1]; |
| 369 | UIThreadRunnable.syncExec(new VoidResult() { |
| 370 | @Override |
| 371 | public void run() { |
| 372 | IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences(); |
| 373 | for (IViewReference viewRef : viewRefs) { |
| 374 | IViewPart vp = viewRef.getView(true); |
| 375 | if (vp.getTitle().equals(viewTile)) { |
| 376 | vps[0] = vp; |
| 377 | return; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | }); |
| 382 | |
| 383 | return vps[0]; |
| 384 | } |
| 385 | } |