| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011 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.lttng.ui.tests.control.model.component; |
| 13 | |
| 14 | import org.eclipse.core.commands.Command; |
| 15 | import org.eclipse.core.commands.ExecutionException; |
| 16 | import org.eclipse.core.commands.NotEnabledException; |
| 17 | import org.eclipse.core.commands.NotHandledException; |
| 18 | import org.eclipse.core.commands.common.NotDefinedException; |
| 19 | import org.eclipse.core.runtime.jobs.Job; |
| 20 | import org.eclipse.linuxtools.lttng.ui.views.control.ControlView; |
| 21 | import org.eclipse.swt.widgets.Display; |
| 22 | import org.eclipse.ui.IViewPart; |
| 23 | import org.eclipse.ui.PartInitException; |
| 24 | import org.eclipse.ui.PlatformUI; |
| 25 | import org.eclipse.ui.commands.ICommandService; |
| 26 | import org.eclipse.ui.handlers.IHandlerService; |
| 27 | |
| 28 | /** |
| 29 | * Singleton class to facilitate the test cases. Creates UML2SD view and loader objects as well as provides |
| 30 | * utility methods for interacting with the loader/view. |
| 31 | */ |
| 32 | public class TraceControlTestFacility { |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | // Constants |
| 36 | // ------------------------------------------------------------------------ |
| 37 | |
| 38 | final static public int WAIT_FOR_JOBS_DELAY = 1000; |
| 39 | final static public int GUI_REFESH_DELAY = 500; |
| 40 | |
| 41 | final static public String COMMAND_CATEGORY_PREFIX = "org.eclipse.linuxtools.lttng.ui.commands.control."; //$NON-NLS-1$ |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Attributes |
| 45 | // ------------------------------------------------------------------------ |
| 46 | private static TraceControlTestFacility fInstance = null; |
| 47 | private ControlView fControlView = null; |
| 48 | private boolean fIsInitialized = false; |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Constructors |
| 52 | // ------------------------------------------------------------------------ |
| 53 | private TraceControlTestFacility() { |
| 54 | } |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Operations |
| 58 | // ------------------------------------------------------------------------ |
| 59 | public static TraceControlTestFacility getInstance() { |
| 60 | if (fInstance == null) { |
| 61 | fInstance = new TraceControlTestFacility(); |
| 62 | } |
| 63 | return fInstance; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Initial the test facility. |
| 68 | */ |
| 69 | public void init() { |
| 70 | |
| 71 | if (!fIsInitialized) { |
| 72 | |
| 73 | IViewPart view; |
| 74 | try { |
| 75 | view = PlatformUI.getWorkbench() |
| 76 | .getActiveWorkbenchWindow() |
| 77 | .getActivePage() |
| 78 | .showView(ControlView.ID); |
| 79 | |
| 80 | } catch (PartInitException e) { |
| 81 | throw new RuntimeException(e); |
| 82 | } |
| 83 | |
| 84 | fControlView = (ControlView) view; |
| 85 | |
| 86 | delay(3000); |
| 87 | fIsInitialized = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | public void dispose() { |
| 93 | if (fIsInitialized) { |
| 94 | waitForJobs(); |
| 95 | |
| 96 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView); |
| 97 | fIsInitialized = false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public void delay(long waitTimeMillis) { |
| 102 | Display display = Display.getCurrent(); |
| 103 | if (display != null) { |
| 104 | long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; |
| 105 | while(System.currentTimeMillis() < endTimeMillis) { |
| 106 | if (!display.readAndDispatch()) { |
| 107 | display.sleep(); |
| 108 | } |
| 109 | display.update(); |
| 110 | } |
| 111 | } else { |
| 112 | try { |
| 113 | Thread.sleep(waitTimeMillis); |
| 114 | } catch (InterruptedException e) { |
| 115 | // Ignored |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Waits for all Eclipse jobs to finish |
| 122 | */ |
| 123 | public void waitForJobs() { |
| 124 | while (!Job.getJobManager().isIdle()) { |
| 125 | delay(WAIT_FOR_JOBS_DELAY); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * @return current control view |
| 132 | */ |
| 133 | public ControlView getControlView() { |
| 134 | return fControlView; |
| 135 | } |
| 136 | |
| 137 | public void executeCommand(String commandId) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { |
| 138 | // ICommandService commandService = (ICommandService) fControlView.getSite().getService(ICommandService.class); |
| 139 | IHandlerService handlerService = (IHandlerService) fControlView.getSite().getService(IHandlerService.class); |
| 140 | handlerService.executeCommand(COMMAND_CATEGORY_PREFIX + commandId, null); |
| 141 | } |
| 142 | |
| 143 | } |