Commit | Line | Data |
---|---|---|
ffa8146c MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013, 2014 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 | * Matthew Khouzam - Initial API and implementation | |
11 | * Marc-Andre Laperle | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.lttng2.kernel.ui.swtbot.tests; | |
15 | ||
16 | import static org.junit.Assert.assertFalse; | |
17 | import static org.junit.Assert.assertNotNull; | |
18 | import static org.junit.Assert.fail; | |
19 | ||
20 | import java.util.ArrayList; | |
21 | import java.util.Arrays; | |
22 | import java.util.List; | |
23 | ||
24 | import org.apache.log4j.ConsoleAppender; | |
25 | import org.apache.log4j.Logger; | |
26 | import org.apache.log4j.SimpleLayout; | |
27 | import org.eclipse.core.resources.IProject; | |
28 | import org.eclipse.core.runtime.CoreException; | |
29 | import org.eclipse.core.runtime.NullProgressMonitor; | |
30 | import org.eclipse.core.runtime.jobs.Job; | |
31 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
32 | import org.eclipse.jface.viewers.StructuredSelection; | |
33 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.controlflow.ControlFlowView; | |
34 | import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesView; | |
35 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; | |
36 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; | |
37 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
38 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; | |
39 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; | |
40 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
41 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; | |
42 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper; | |
43 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; | |
44 | import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers; | |
45 | import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramView; | |
46 | import org.eclipse.swt.widgets.Display; | |
47 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
48 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
49 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
50 | import org.eclipse.swtbot.swt.finder.results.BoolResult; | |
51 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
52 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
53 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton; | |
54 | import org.eclipse.ui.IEditorPart; | |
55 | import org.eclipse.ui.IEditorReference; | |
56 | import org.eclipse.ui.IViewPart; | |
57 | import org.eclipse.ui.IViewReference; | |
58 | import org.eclipse.ui.PlatformUI; | |
59 | import org.eclipse.ui.WorkbenchException; | |
60 | import org.junit.BeforeClass; | |
61 | import org.junit.Test; | |
62 | ||
63 | /** | |
64 | * SWTBot Smoke test for LTTng Kernel UI. | |
65 | * | |
66 | * @author Matthew Khouzam | |
67 | */ | |
68 | public class ImportAndReadKernelSmokeTest { | |
69 | ||
70 | private static final String KERNEL_PERSPECTIVE_ID = "org.eclipse.linuxtools.lttng2.kernel.ui.perspective"; | |
71 | private static final String TRACE_PROJECT_NAME = "test"; | |
72 | ||
73 | private static SWTWorkbenchBot fBot; | |
74 | private static CtfTmfTestTrace ctt = CtfTmfTestTrace.SYNTHETIC_TRACE; | |
75 | private ITmfEvent fDesired1; | |
76 | private ITmfEvent fDesired2; | |
77 | ||
78 | /** The Log4j logger instance. */ | |
79 | private static final Logger fLogger = Logger.getRootLogger(); | |
80 | ||
81 | /** | |
82 | * Test Class setup | |
83 | */ | |
84 | @BeforeClass | |
85 | public static void init() { | |
86 | if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) { | |
87 | fail("SWTBot test needs to run in a non-UI thread. Make sure that \"Run in UI thread\" is unchecked in your launch configuration or" | |
88 | + " that useUIThread is set to false in the pom.xml"); | |
89 | } | |
90 | ||
91 | /* set up for swtbot */ | |
92 | SWTBotPreferences.TIMEOUT = 300000; /* 300 second timeout */ | |
93 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); | |
94 | fBot = new SWTWorkbenchBot(); | |
95 | ||
96 | final List<SWTBotView> openViews = fBot.views(); | |
97 | for (SWTBotView view : openViews) { | |
98 | if (view.getTitle().equals("Welcome")) { | |
99 | view.close(); | |
100 | fBot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
101 | } | |
102 | } | |
103 | /* Switch perspectives */ | |
104 | switchKernelPerspective(); | |
105 | /* Finish waiting for eclipse to load */ | |
106 | waitForJobs(); | |
107 | } | |
108 | ||
109 | private static void switchKernelPerspective() { | |
110 | final Exception retE[] = new Exception[1]; | |
111 | if (!UIThreadRunnable.syncExec(new BoolResult() { | |
112 | @Override | |
113 | public Boolean run() { | |
114 | try { | |
115 | PlatformUI.getWorkbench().showPerspective(KERNEL_PERSPECTIVE_ID, | |
116 | PlatformUI.getWorkbench().getActiveWorkbenchWindow()); | |
117 | } catch (WorkbenchException e) { | |
118 | retE[0] = e; | |
119 | return false; | |
120 | } | |
121 | return true; | |
122 | } | |
123 | })) { | |
124 | fail(retE[0].getMessage()); | |
125 | } | |
126 | ||
127 | } | |
128 | ||
129 | /** | |
130 | * Waits for all Eclipse jobs to finish | |
131 | */ | |
132 | public static void waitForJobs() { | |
133 | while (!Job.getJobManager().isIdle()) { | |
134 | delay(100); | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Sleeps current thread for a given time. | |
140 | * | |
141 | * @param waitTimeMillis | |
142 | * time in milliseconds to wait | |
143 | */ | |
144 | protected static void delay(final long waitTimeMillis) { | |
145 | try { | |
146 | Thread.sleep(waitTimeMillis); | |
147 | } catch (final InterruptedException e) { | |
148 | // Ignored | |
149 | } | |
150 | } | |
151 | ||
152 | /** | |
153 | * Main test case | |
154 | */ | |
155 | @Test | |
156 | public void test() { | |
157 | createProject(); | |
158 | openTrace(); | |
159 | openEditor(); | |
160 | testHV(getViewPart("Histogram")); | |
161 | testCFV((ControlFlowView) getViewPart("Control Flow")); | |
162 | testRV((ResourcesView) getViewPart("Resources")); | |
163 | } | |
164 | ||
165 | private static void openTrace() { | |
166 | final Exception exception[] = new Exception[1]; | |
167 | exception[0] = null; | |
168 | UIThreadRunnable.syncExec(new VoidResult() { | |
169 | @Override | |
170 | public void run() { | |
171 | try { | |
172 | (new TmfOpenTraceHelper()).openTraceFromPath("test", ctt.getPath(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "org.eclipse.linuxtools.lttng2.kernel.tracetype"); | |
173 | } catch (CoreException e) { | |
174 | exception[0] = e; | |
175 | } | |
176 | } | |
177 | }); | |
178 | if (exception[0] != null) { | |
179 | fail(exception[0].getMessage()); | |
180 | } | |
181 | ||
182 | delay(1000); | |
183 | waitForJobs(); | |
184 | } | |
185 | ||
186 | private void openEditor() { | |
187 | final List<IEditorReference> editorRefs = new ArrayList<>(); | |
188 | UIThreadRunnable.syncExec(new VoidResult() { | |
189 | @Override | |
190 | public void run() { | |
191 | IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences(); | |
192 | editorRefs.addAll(Arrays.asList(ieds)); | |
193 | } | |
194 | ||
195 | }); | |
196 | assertFalse(editorRefs.isEmpty()); | |
197 | IEditorPart iep = null; | |
198 | for (IEditorReference ied : editorRefs) { | |
199 | if (ied.getTitle().equals(ctt.getTrace().getName())) { | |
200 | iep = ied.getEditor(true); | |
201 | break; | |
202 | } | |
203 | } | |
204 | assertNotNull(iep); | |
205 | fDesired1 = getEvent(100); | |
206 | fDesired2 = getEvent(10000); | |
207 | final TmfEventsEditor tmfEd = (TmfEventsEditor) iep; | |
208 | UIThreadRunnable.syncExec(new VoidResult() { | |
209 | @Override | |
210 | public void run() { | |
211 | tmfEd.setFocus(); | |
212 | tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(fDesired1))); | |
213 | } | |
214 | }); | |
215 | ||
216 | waitForJobs(); | |
217 | delay(1000); | |
218 | assertNotNull(tmfEd); | |
219 | } | |
220 | ||
221 | private static void createProject() { | |
222 | /* | |
223 | * Make a new test | |
224 | */ | |
225 | UIThreadRunnable.syncExec(new VoidResult() { | |
226 | @Override | |
227 | public void run() { | |
228 | IProject project = TmfProjectRegistry.createProject(TRACE_PROJECT_NAME, null, new NullProgressMonitor()); | |
229 | assertNotNull(project); | |
230 | } | |
231 | }); | |
232 | ||
233 | waitForJobs(); | |
234 | } | |
235 | ||
236 | private static void testCFV(ControlFlowView vp) { | |
237 | assertNotNull(vp); | |
238 | } | |
239 | ||
240 | private void testHV(IViewPart vp) { | |
241 | SWTBotView hvBot = (new SWTWorkbenchBot()).viewById(HistogramView.ID); | |
242 | List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons(); | |
243 | for (SWTBotToolbarButton hvTool : hvTools) { | |
244 | if (hvTool.getToolTipText().toLowerCase().contains("lost")) { | |
245 | hvTool.click(); | |
246 | } | |
247 | } | |
248 | HistogramView hv = (HistogramView) vp; | |
249 | final TmfTimeSynchSignal signal = new TmfTimeSynchSignal(hv, fDesired1.getTimestamp()); | |
250 | final TmfTimeSynchSignal signal2 = new TmfTimeSynchSignal(hv, fDesired2.getTimestamp()); | |
251 | hv.updateTimeRange(100000); | |
252 | waitForJobs(); | |
253 | hv.currentTimeUpdated(signal); | |
254 | hv.broadcast(signal); | |
255 | waitForJobs(); | |
256 | delay(1000); | |
257 | ||
258 | hv.updateTimeRange(1000000000); | |
259 | waitForJobs(); | |
260 | hv.currentTimeUpdated(signal2); | |
261 | hv.broadcast(signal2); | |
262 | waitForJobs(); | |
263 | delay(1000); | |
264 | assertNotNull(hv); | |
265 | } | |
266 | ||
267 | private static void testRV(ResourcesView vp) { | |
268 | assertNotNull(vp); | |
269 | } | |
270 | ||
271 | private static CtfTmfEvent getEvent(int rank) { | |
272 | CtfTmfTrace trace = CtfTmfTestTrace.SYNTHETIC_TRACE.getTrace(); | |
273 | if (trace == null) { | |
274 | return null; | |
275 | } | |
276 | ITmfContext ctx = trace.seekEvent(0); | |
277 | for (int i = 0; i < rank; i++) { | |
278 | trace.getNext(ctx); | |
279 | } | |
280 | final CtfTmfEvent retVal = trace.getNext(ctx); | |
281 | trace.dispose(); | |
282 | return retVal; | |
283 | } | |
284 | ||
285 | private static IViewPart getViewPart(final String viewTile) { | |
286 | final IViewPart[] vps = new IViewPart[1]; | |
287 | UIThreadRunnable.syncExec(new VoidResult() { | |
288 | @Override | |
289 | public void run() { | |
290 | IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences(); | |
291 | for (IViewReference viewRef : viewRefs) { | |
292 | IViewPart vp = viewRef.getView(true); | |
293 | if (vp.getTitle().equals(viewTile)) { | |
294 | vps[0] = vp; | |
295 | return; | |
296 | } | |
297 | } | |
298 | } | |
299 | }); | |
300 | ||
301 | return vps[0]; | |
302 | } | |
303 | } |