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