| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader; |
| 13 | |
| 14 | import java.io.File; |
| 15 | import java.io.IOException; |
| 16 | import java.net.URISyntaxException; |
| 17 | import java.net.URL; |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.eclipse.core.runtime.FileLocator; |
| 22 | import org.eclipse.core.runtime.Path; |
| 23 | import org.eclipse.core.runtime.jobs.Job; |
| 24 | import org.eclipse.swt.widgets.Display; |
| 25 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 26 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
| 27 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; |
| 28 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; |
| 29 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; |
| 30 | import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser; |
| 31 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 32 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
| 33 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; |
| 34 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; |
| 35 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; |
| 36 | import org.eclipse.tracecompass.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace; |
| 37 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDView; |
| 38 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.Criteria; |
| 39 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterCriteria; |
| 40 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.FilterListDialog; |
| 41 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.load.LoadersManager; |
| 42 | import org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader; |
| 43 | import org.eclipse.ui.IViewPart; |
| 44 | import org.eclipse.ui.PartInitException; |
| 45 | import org.eclipse.ui.PlatformUI; |
| 46 | import org.osgi.framework.FrameworkUtil; |
| 47 | |
| 48 | /** |
| 49 | * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides |
| 50 | * utility methods for interacting with the loader/view. |
| 51 | * |
| 52 | * @author Bernd Hufmann |
| 53 | */ |
| 54 | public class Uml2SDTestFacility { |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Attributes |
| 58 | // ------------------------------------------------------------------------ |
| 59 | private static Uml2SDTestFacility fInstance = null; |
| 60 | |
| 61 | private TmfUml2SDSyncLoader fLoader; |
| 62 | private SDView fSdView; |
| 63 | private TmfTraceStub fTrace = null; |
| 64 | private TmfUml2SDTestTrace fParser = null; |
| 65 | private TmfExperiment fExperiment = null; |
| 66 | |
| 67 | private volatile boolean fIsInitialized = false; |
| 68 | |
| 69 | // ------------------------------------------------------------------------ |
| 70 | // Constructors |
| 71 | // ------------------------------------------------------------------------ |
| 72 | private Uml2SDTestFacility() { |
| 73 | } |
| 74 | |
| 75 | // ------------------------------------------------------------------------ |
| 76 | // Operations |
| 77 | // ------------------------------------------------------------------------ |
| 78 | /** |
| 79 | * @return the singleton instance. |
| 80 | */ |
| 81 | public synchronized static Uml2SDTestFacility getInstance() { |
| 82 | if (fInstance == null) { |
| 83 | fInstance = new Uml2SDTestFacility(); |
| 84 | fInstance.init(); |
| 85 | } |
| 86 | return fInstance; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Initial the test facility. |
| 91 | */ |
| 92 | public void init() { |
| 93 | |
| 94 | if (!fIsInitialized) { |
| 95 | |
| 96 | fParser = new TmfUml2SDTestTrace(); |
| 97 | fTrace = setupTrace(fParser); |
| 98 | fParser.setTrace(fTrace); |
| 99 | |
| 100 | IViewPart view; |
| 101 | try { |
| 102 | // Remove welcome view to avoid interference during test execution |
| 103 | view = PlatformUI.getWorkbench() |
| 104 | .getActiveWorkbenchWindow() |
| 105 | .getActivePage() |
| 106 | .findView("org.eclipse.ui.internal.introview"); |
| 107 | |
| 108 | if (view != null) { |
| 109 | PlatformUI.getWorkbench() |
| 110 | .getActiveWorkbenchWindow() |
| 111 | .getActivePage().hideView(view); |
| 112 | } |
| 113 | |
| 114 | view = PlatformUI.getWorkbench() |
| 115 | .getActiveWorkbenchWindow() |
| 116 | .getActivePage() |
| 117 | .showView("org.eclipse.linuxtools.tmf.ui.tmfUml2SDSyncView"); |
| 118 | |
| 119 | } catch (final PartInitException e) { |
| 120 | throw new RuntimeException(e); |
| 121 | } |
| 122 | |
| 123 | fSdView = (SDView) view; |
| 124 | fLoader = (TmfUml2SDSyncLoader) LoadersManager.getInstance().createLoader( |
| 125 | "org.eclipse.tracecompass.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader", fSdView); |
| 126 | |
| 127 | delay(3000); |
| 128 | fIsInitialized = true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | private TmfTraceStub setupTrace(final ITmfEventParser parser) { |
| 134 | |
| 135 | try { |
| 136 | // Create test trace object |
| 137 | final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path("tracesets/sdEvents"), null); |
| 138 | final File test = new File(FileLocator.toFileURL(location).toURI()); |
| 139 | return new TmfTraceStub(test.getPath(), 500, true, parser); |
| 140 | } catch (final TmfTraceException e) { |
| 141 | e.printStackTrace(); |
| 142 | throw new RuntimeException(e); |
| 143 | } catch (final URISyntaxException e) { |
| 144 | e.printStackTrace(); |
| 145 | throw new RuntimeException(e); |
| 146 | } catch (final IOException e) { |
| 147 | e.printStackTrace(); |
| 148 | throw new RuntimeException(e); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Dispose the resource |
| 154 | */ |
| 155 | public void dispose() { |
| 156 | if (fIsInitialized) { |
| 157 | ITmfTrace trace = fTrace; |
| 158 | TmfExperiment experiment = fExperiment; |
| 159 | if (trace == null || experiment == null) { |
| 160 | throw new IllegalStateException(); |
| 161 | } |
| 162 | |
| 163 | trace.broadcast(new TmfTraceClosedSignal(this, experiment)); |
| 164 | experiment.dispose(); |
| 165 | |
| 166 | // Wait for all Eclipse jobs to finish |
| 167 | waitForJobs(); |
| 168 | |
| 169 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fSdView); |
| 170 | fIsInitialized = false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sleeps current thread or GUI thread for a given time. |
| 176 | * @param waitTimeMillis time in milliseconds to wait |
| 177 | */ |
| 178 | public void delay(final long waitTimeMillis) { |
| 179 | final Display display = Display.getCurrent(); |
| 180 | if (display != null) { |
| 181 | final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; |
| 182 | while(System.currentTimeMillis() < endTimeMillis) { |
| 183 | if (!display.readAndDispatch()) { |
| 184 | // We do not use Display.sleep because it might never wake up |
| 185 | // if there is no user interaction |
| 186 | try { |
| 187 | Thread.sleep(Math.min(waitTimeMillis, 10)); |
| 188 | } catch (final InterruptedException e) { |
| 189 | // Ignored |
| 190 | } |
| 191 | } |
| 192 | display.update(); |
| 193 | } |
| 194 | } else { |
| 195 | try { |
| 196 | Thread.sleep(waitTimeMillis); |
| 197 | } catch (final InterruptedException e) { |
| 198 | // Ignored |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Waits for all Eclipse jobs to finish |
| 205 | */ |
| 206 | public void waitForJobs() { |
| 207 | while (!Job.getJobManager().isIdle()) { |
| 208 | delay(IUml2SDTestConstants.WAIT_FOR_JOBS_DELAY); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @return current UML2SD loader |
| 214 | */ |
| 215 | public TmfUml2SDSyncLoader getLoader() { |
| 216 | return fLoader; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @return current SD view |
| 221 | */ |
| 222 | public SDView getSdView() { |
| 223 | return fSdView; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @return current trace |
| 228 | */ |
| 229 | public TmfTraceStub getTrace() { |
| 230 | return fTrace; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @return Trace parser |
| 235 | */ |
| 236 | public TmfUml2SDTestTrace getParser() { |
| 237 | return fParser; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @return current experiment. |
| 242 | */ |
| 243 | public TmfExperiment getExperiment() { |
| 244 | return fExperiment; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Go to next page; |
| 249 | */ |
| 250 | public void nextPage() { |
| 251 | fLoader.nextPage(); |
| 252 | fLoader.waitForCompletion(); |
| 253 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Go to previous page. |
| 258 | */ |
| 259 | public void prevPage() { |
| 260 | fLoader.prevPage(); |
| 261 | fLoader.waitForCompletion(); |
| 262 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Go to last page. |
| 267 | */ |
| 268 | public void lastPage() { |
| 269 | fLoader.lastPage(); |
| 270 | fLoader.waitForCompletion(); |
| 271 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Go to first page. |
| 276 | */ |
| 277 | public void firstPage() { |
| 278 | fLoader.firstPage(); |
| 279 | fLoader.waitForCompletion(); |
| 280 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @param page number to set |
| 285 | */ |
| 286 | public void setPage(final int page) { |
| 287 | fLoader.pageNumberChanged(page); |
| 288 | fLoader.waitForCompletion(); |
| 289 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * @see org.eclipse.tracecompass.tmf.ui.tests.views.uml2sd.loader.Uml2SDTestFacility#selectExperiment(boolean) |
| 294 | */ |
| 295 | public void selectExperiment() { |
| 296 | this.selectExperiment(true); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Selects the experiment. |
| 301 | * @param wait true to wait for indexing to finish else false |
| 302 | */ |
| 303 | public void selectExperiment(final boolean wait) { |
| 304 | fParser = new TmfUml2SDTestTrace(); |
| 305 | fTrace = setupTrace(fParser); |
| 306 | fParser.setTrace(fTrace); |
| 307 | |
| 308 | final ITmfTrace traces[] = new ITmfTrace[1]; |
| 309 | traces[0] = fTrace; |
| 310 | fExperiment = new TmfExperiment(ITmfEvent.class, "TestExperiment", |
| 311 | traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null) { |
| 312 | @Override |
| 313 | protected ITmfTraceIndexer createIndexer(int interval) { |
| 314 | return new TmfCheckpointIndexer(this, interval); |
| 315 | } |
| 316 | }; |
| 317 | fTrace.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null)); |
| 318 | fTrace.broadcast(new TmfTraceSelectedSignal(this, fExperiment)); |
| 319 | if (wait) { |
| 320 | while (fExperiment.getNbEvents() == 0) { |
| 321 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 322 | } |
| 323 | waitForJobs(); |
| 324 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Disposes the experiment. |
| 330 | */ |
| 331 | public void disposeExperiment() { |
| 332 | ITmfTrace trace = fTrace; |
| 333 | TmfExperiment experiment = fExperiment; |
| 334 | if (trace == null || experiment == null) { |
| 335 | throw new IllegalStateException(); |
| 336 | } |
| 337 | trace.broadcast(new TmfTraceClosedSignal(this, experiment)); |
| 338 | experiment.dispose(); |
| 339 | delay(IUml2SDTestConstants.GUI_REFESH_DELAY); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Creates some global filter criteria and saves them to disk. |
| 344 | */ |
| 345 | public void createFilterCriteria() { |
| 346 | // Create Filter Criteria and save tme |
| 347 | final List<FilterCriteria> filterToSave = new ArrayList<>(); |
| 348 | Criteria criteria = new Criteria(); |
| 349 | criteria.setLifeLineSelected(true); |
| 350 | criteria.setExpression(IUml2SDTestConstants.FIRST_PLAYER_NAME); |
| 351 | filterToSave.add(new FilterCriteria(criteria, true, false)); |
| 352 | |
| 353 | criteria = new Criteria(); |
| 354 | criteria.setSyncMessageSelected(true); |
| 355 | criteria.setExpression("BALL_.*"); |
| 356 | filterToSave.add(new FilterCriteria(criteria, true, false)); |
| 357 | FilterListDialog.saveFiltersCriteria(filterToSave); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | } |