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