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