| 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 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.apache.log4j.Logger; |
| 21 | import org.apache.log4j.varia.NullAppender; |
| 22 | import org.eclipse.jdt.annotation.NonNull; |
| 23 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 24 | import org.eclipse.jface.viewers.StructuredSelection; |
| 25 | import org.eclipse.jface.wizard.Wizard; |
| 26 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
| 27 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
| 28 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| 29 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
| 30 | import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 31 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
| 32 | import org.eclipse.swtbot.swt.finder.waits.Conditions; |
| 33 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; |
| 34 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
| 35 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; |
| 36 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton; |
| 37 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
| 38 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 39 | import org.eclipse.tracecompass.internal.tmf.ui.views.statistics.TmfStatisticsViewImpl; |
| 40 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
| 41 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
| 42 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
| 43 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent; |
| 44 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
| 45 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; |
| 46 | import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor; |
| 47 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
| 48 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 49 | import org.eclipse.tracecompass.tmf.ui.views.histogram.HistogramView; |
| 50 | import org.eclipse.ui.IViewPart; |
| 51 | import org.eclipse.ui.IViewReference; |
| 52 | import org.eclipse.ui.PlatformUI; |
| 53 | import org.eclipse.ui.views.properties.PropertySheet; |
| 54 | import org.junit.AfterClass; |
| 55 | import org.junit.BeforeClass; |
| 56 | import org.junit.runner.RunWith; |
| 57 | |
| 58 | /** |
| 59 | * Abstract SWTBot Smoke test class. |
| 60 | * |
| 61 | * @author Matthew Khouzam |
| 62 | * @author Bernd Hufmann |
| 63 | */ |
| 64 | @RunWith(SWTBotJunit4ClassRunner.class) |
| 65 | public abstract class AbstractImportAndReadSmokeTest { |
| 66 | |
| 67 | /** Trace name */ |
| 68 | protected static final String TRACE_NAME = "scp_dest"; |
| 69 | /** Trace folder */ |
| 70 | protected static final String TRACE_FOLDER = "synctraces"; |
| 71 | /** Trace type name for generic CTF traces */ |
| 72 | protected static final String TRACE_TYPE_NAME = "Generic CTF Trace"; |
| 73 | /** A Generic CTF Trace */ |
| 74 | protected static final @NonNull CtfTestTrace fTrace = CtfTestTrace.SYNC_DEST; |
| 75 | /** SWT BOT workbench reference */ |
| 76 | protected static SWTWorkbenchBot fBot; |
| 77 | /** Wizard to use */ |
| 78 | protected static Wizard fWizard; |
| 79 | |
| 80 | /** The Log4j logger instance. */ |
| 81 | protected static final Logger fLogger = Logger.getRootLogger(); |
| 82 | |
| 83 | /** Test Class setup */ |
| 84 | @BeforeClass |
| 85 | public static void init() { |
| 86 | SWTBotUtils.failIfUIThread(); |
| 87 | |
| 88 | /* set up for swtbot */ |
| 89 | SWTBotPreferences.TIMEOUT = 50000; /* 50 second timeout */ |
| 90 | fLogger.removeAllAppenders(); |
| 91 | fLogger.addAppender(new NullAppender()); |
| 92 | fBot = new SWTWorkbenchBot(); |
| 93 | |
| 94 | SWTBotUtils.closeView("welcome", fBot); |
| 95 | |
| 96 | SWTBotUtils.switchToTracingPerspective(); |
| 97 | /* finish waiting for eclipse to load */ |
| 98 | SWTBotUtils.waitForJobs(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Test Class teardown |
| 103 | */ |
| 104 | @AfterClass |
| 105 | public static void terminate() { |
| 106 | fLogger.removeAllAppenders(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a tracing projects |
| 111 | */ |
| 112 | protected void createProject() { |
| 113 | SWTBotUtils.focusMainWindow(fBot.shells()); |
| 114 | fBot.menu("File").menu("New").menu("Project...").click(); |
| 115 | |
| 116 | fBot.shell("New Project").setFocus(); |
| 117 | SWTBotTree tree = fBot.tree(); |
| 118 | assertNotNull(tree); |
| 119 | final String tracingKey = "Tracing"; |
| 120 | fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(tracingKey, tree)); |
| 121 | final SWTBotTreeItem tracingNode = tree.expandNode(tracingKey); |
| 122 | |
| 123 | tracingNode.select(); |
| 124 | final String projectKey = "Tracing Project"; |
| 125 | fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(projectKey, tracingNode)); |
| 126 | final SWTBotTreeItem tracingProject = tracingNode.getNode(projectKey); |
| 127 | assertNotNull(tracingProject); |
| 128 | |
| 129 | tracingProject.select(); |
| 130 | tracingProject.click(); |
| 131 | |
| 132 | SWTBotButton nextButton = fBot.button("Next >"); |
| 133 | fBot.waitUntil(Conditions.widgetIsEnabled(nextButton)); |
| 134 | nextButton.click(); |
| 135 | fBot.shell("Tracing Project").setFocus(); |
| 136 | |
| 137 | final SWTBotText text = fBot.text(); |
| 138 | text.setText(getProjectName()); |
| 139 | |
| 140 | fBot.button("Finish").click(); |
| 141 | SWTBotUtils.waitForJobs(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Finishes the wizard |
| 146 | */ |
| 147 | protected void importFinish() { |
| 148 | SWTBotShell shell = fBot.activeShell(); |
| 149 | final SWTBotButton finishButton = fBot.button("Finish"); |
| 150 | finishButton.click(); |
| 151 | fBot.waitUntil(Conditions.shellCloses(shell)); |
| 152 | SWTBotUtils.waitForJobs(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Gets the project Name |
| 157 | * |
| 158 | * @return the project name |
| 159 | */ |
| 160 | protected abstract String getProjectName(); |
| 161 | |
| 162 | // --------------------------------------------- |
| 163 | // Helpers for testing views |
| 164 | // --------------------------------------------- |
| 165 | |
| 166 | /** |
| 167 | * Verifies the properties view for a given view part |
| 168 | * |
| 169 | * @param vp |
| 170 | * a view part |
| 171 | */ |
| 172 | protected void testPropertyView(IViewPart vp) { |
| 173 | PropertySheet pv = (PropertySheet) vp; |
| 174 | assertNotNull(pv); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Verifies the Histogram View |
| 179 | * |
| 180 | * @param vp |
| 181 | * the view part |
| 182 | * @param tmfEd |
| 183 | * the events editor |
| 184 | */ |
| 185 | protected void testHistogramView(IViewPart vp, final TmfEventsEditor tmfEd) { |
| 186 | final CtfTmfEvent desiredEvent1 = getEvent(100); |
| 187 | UIThreadRunnable.syncExec(new VoidResult() { |
| 188 | @Override |
| 189 | public void run() { |
| 190 | tmfEd.setFocus(); |
| 191 | tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(desiredEvent1))); |
| 192 | } |
| 193 | }); |
| 194 | |
| 195 | SWTBotUtils.waitForJobs(); |
| 196 | SWTBotUtils.delay(1000); |
| 197 | |
| 198 | final CtfTmfEvent desiredEvent2 = getEvent(10000); |
| 199 | SWTBotView hvBot = fBot.viewById(HistogramView.ID); |
| 200 | List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons(); |
| 201 | for (SWTBotToolbarButton hvTool : hvTools) { |
| 202 | if (hvTool.getToolTipText().toLowerCase().contains("lost")) { |
| 203 | hvTool.click(); |
| 204 | } |
| 205 | } |
| 206 | HistogramView hv = (HistogramView) vp; |
| 207 | final TmfSelectionRangeUpdatedSignal signal = new TmfSelectionRangeUpdatedSignal(hv, desiredEvent1.getTimestamp()); |
| 208 | final TmfSelectionRangeUpdatedSignal signal2 = new TmfSelectionRangeUpdatedSignal(hv, desiredEvent2.getTimestamp()); |
| 209 | hv.updateTimeRange(100000); |
| 210 | SWTBotUtils.waitForJobs(); |
| 211 | hv.selectionRangeUpdated(signal); |
| 212 | hv.broadcast(signal); |
| 213 | SWTBotUtils.waitForJobs(); |
| 214 | SWTBotUtils.delay(1000); |
| 215 | |
| 216 | hv.updateTimeRange(1000000000); |
| 217 | SWTBotUtils.waitForJobs(); |
| 218 | hv.selectionRangeUpdated(signal2); |
| 219 | hv.broadcast(signal2); |
| 220 | SWTBotUtils.waitForJobs(); |
| 221 | SWTBotUtils.delay(1000); |
| 222 | assertNotNull(hv); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Verifies the statistics view |
| 227 | * |
| 228 | * @param vp |
| 229 | * the view part |
| 230 | */ |
| 231 | protected void testStatisticsView(IViewPart vp) { |
| 232 | TmfStatisticsViewImpl sv = (TmfStatisticsViewImpl) vp; |
| 233 | assertNotNull(sv); |
| 234 | } |
| 235 | |
| 236 | // --------------------------------------------- |
| 237 | // Trace helpers |
| 238 | // --------------------------------------------- |
| 239 | |
| 240 | /** |
| 241 | * Gets an event at a given rank |
| 242 | * |
| 243 | * @param rank |
| 244 | * a rank |
| 245 | * @return the event at given rank |
| 246 | */ |
| 247 | protected CtfTmfEvent getEvent(int rank) { |
| 248 | CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(fTrace); |
| 249 | ITmfContext ctx = trace.seekEvent(rank); |
| 250 | CtfTmfEvent ret = trace.getNext(ctx); |
| 251 | ctx.dispose(); |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Gets a view part based on view title |
| 257 | * |
| 258 | * @param viewTile |
| 259 | * a view title |
| 260 | * @return the view part |
| 261 | */ |
| 262 | protected IViewPart getViewPart(final String viewTile) { |
| 263 | final IViewPart[] vps = new IViewPart[1]; |
| 264 | UIThreadRunnable.syncExec(new VoidResult() { |
| 265 | @Override |
| 266 | public void run() { |
| 267 | IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences(); |
| 268 | for (IViewReference viewRef : viewRefs) { |
| 269 | IViewPart vp = viewRef.getView(true); |
| 270 | if (vp.getTitle().equals(viewTile)) { |
| 271 | vps[0] = vp; |
| 272 | return; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | }); |
| 277 | |
| 278 | return vps[0]; |
| 279 | } |
| 280 | } |