TMF: Add trace stub for TMF unit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui.swtbot.tests / src / org / eclipse / linuxtools / lttng2 / kernel / ui / swtbot / tests / ImportAndReadKernelSmokeTest.java
CommitLineData
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
977ca87f 12 * Patrick Tasse - Add support for folder elements
ffa8146c
MK
13 *******************************************************************************/
14
15package org.eclipse.linuxtools.lttng2.kernel.ui.swtbot.tests;
16
ffa8146c
MK
17import static org.junit.Assert.assertNotNull;
18import static org.junit.Assert.fail;
19
ffa8146c
MK
20import java.util.List;
21
22import org.apache.log4j.ConsoleAppender;
23import org.apache.log4j.Logger;
24import org.apache.log4j.SimpleLayout;
977ca87f
PT
25import org.eclipse.core.resources.IProject;
26import org.eclipse.core.resources.ResourcesPlugin;
ffa8146c 27import org.eclipse.core.runtime.CoreException;
ffa8146c
MK
28import org.eclipse.jface.viewers.SelectionChangedEvent;
29import org.eclipse.jface.viewers.StructuredSelection;
30import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.controlflow.ControlFlowView;
31import org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources.ResourcesView;
ffa8146c
MK
32import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
33import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
ffa8146c 34import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
91e7f946
AM
35import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
36import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
37import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
ffa8146c
MK
38import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
39import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper;
977ca87f
PT
40import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
41import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
306e18d0 42import org.eclipse.linuxtools.tmf.ui.swtbot.tests.SWTBotUtil;
ffa8146c
MK
43import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
44import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramView;
ffa8146c 45import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
bf0f892f 46import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
ffa8146c
MK
47import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
48import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
cbbd323f 49import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
ffa8146c
MK
50import org.eclipse.swtbot.swt.finder.results.BoolResult;
51import org.eclipse.swtbot.swt.finder.results.VoidResult;
52import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
53import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
54import org.eclipse.ui.IEditorPart;
55import org.eclipse.ui.IEditorReference;
56import org.eclipse.ui.IViewPart;
57import org.eclipse.ui.IViewReference;
58import org.eclipse.ui.PlatformUI;
59import org.eclipse.ui.WorkbenchException;
bf0f892f 60import org.hamcrest.Matcher;
ffa8146c
MK
61import org.junit.BeforeClass;
62import org.junit.Test;
cbbd323f 63import org.junit.runner.RunWith;
ffa8146c
MK
64
65/**
66 * SWTBot Smoke test for LTTng Kernel UI.
67 *
68 * @author Matthew Khouzam
69 */
cbbd323f 70@RunWith(SWTBotJunit4ClassRunner.class)
ffa8146c
MK
71public class ImportAndReadKernelSmokeTest {
72
73 private static final String KERNEL_PERSPECTIVE_ID = "org.eclipse.linuxtools.lttng2.kernel.ui.perspective";
74 private static final String TRACE_PROJECT_NAME = "test";
75
76 private static SWTWorkbenchBot fBot;
77 private static CtfTmfTestTrace ctt = CtfTmfTestTrace.SYNTHETIC_TRACE;
78 private ITmfEvent fDesired1;
79 private ITmfEvent fDesired2;
80
81 /** The Log4j logger instance. */
82 private static final Logger fLogger = Logger.getRootLogger();
83
84 /**
85 * Test Class setup
86 */
87 @BeforeClass
88 public static void init() {
306e18d0 89 SWTBotUtil.failIfUIThread();
ffa8146c
MK
90
91 /* set up for swtbot */
bf0f892f 92 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
ffa8146c
MK
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 */
306e18d0 106 SWTBotUtil.waitForJobs();
ffa8146c
MK
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
ffa8146c
MK
129 /**
130 * Main test case
131 */
132 @Test
133 public void test() {
306e18d0 134 SWTBotUtil.createProject(TRACE_PROJECT_NAME);
ffa8146c
MK
135 openTrace();
136 openEditor();
137 testHV(getViewPart("Histogram"));
138 testCFV((ControlFlowView) getViewPart("Control Flow"));
139 testRV((ResourcesView) getViewPart("Resources"));
93c91230
MAL
140
141 fBot.closeAllEditors();
142 SWTBotUtil.deleteProject(TRACE_PROJECT_NAME, fBot);
ffa8146c
MK
143 }
144
145 private static void openTrace() {
146 final Exception exception[] = new Exception[1];
147 exception[0] = null;
148 UIThreadRunnable.syncExec(new VoidResult() {
149 @Override
150 public void run() {
151 try {
977ca87f
PT
152 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME);
153 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
154 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, ctt.getPath(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "org.eclipse.linuxtools.lttng2.kernel.tracetype");
ffa8146c
MK
155 } catch (CoreException e) {
156 exception[0] = e;
157 }
158 }
159 });
160 if (exception[0] != null) {
161 fail(exception[0].getMessage());
162 }
163
306e18d0
MK
164 SWTBotUtil.delay(1000);
165 SWTBotUtil.waitForJobs();
ffa8146c
MK
166 }
167
168 private void openEditor() {
bf0f892f
AF
169 Matcher<IEditorReference> matcher = WidgetMatcherFactory.withPartName(ctt.getTrace().getName());
170 IEditorPart iep = fBot.editor(matcher).getReference().getEditor(true);
ffa8146c
MK
171 fDesired1 = getEvent(100);
172 fDesired2 = getEvent(10000);
173 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
174 UIThreadRunnable.syncExec(new VoidResult() {
175 @Override
176 public void run() {
177 tmfEd.setFocus();
178 tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(fDesired1)));
179 }
180 });
181
306e18d0
MK
182 SWTBotUtil.waitForJobs();
183 SWTBotUtil.delay(1000);
ffa8146c
MK
184 assertNotNull(tmfEd);
185 }
186
ffa8146c
MK
187 private static void testCFV(ControlFlowView vp) {
188 assertNotNull(vp);
189 }
190
191 private void testHV(IViewPart vp) {
192 SWTBotView hvBot = (new SWTWorkbenchBot()).viewById(HistogramView.ID);
193 List<SWTBotToolbarButton> hvTools = hvBot.getToolbarButtons();
194 for (SWTBotToolbarButton hvTool : hvTools) {
195 if (hvTool.getToolTipText().toLowerCase().contains("lost")) {
196 hvTool.click();
197 }
198 }
199 HistogramView hv = (HistogramView) vp;
200 final TmfTimeSynchSignal signal = new TmfTimeSynchSignal(hv, fDesired1.getTimestamp());
201 final TmfTimeSynchSignal signal2 = new TmfTimeSynchSignal(hv, fDesired2.getTimestamp());
202 hv.updateTimeRange(100000);
306e18d0 203 SWTBotUtil.waitForJobs();
ffa8146c
MK
204 hv.currentTimeUpdated(signal);
205 hv.broadcast(signal);
306e18d0
MK
206 SWTBotUtil.waitForJobs();
207 SWTBotUtil.delay(1000);
ffa8146c
MK
208
209 hv.updateTimeRange(1000000000);
306e18d0 210 SWTBotUtil.waitForJobs();
ffa8146c
MK
211 hv.currentTimeUpdated(signal2);
212 hv.broadcast(signal2);
306e18d0
MK
213 SWTBotUtil.waitForJobs();
214 SWTBotUtil.delay(1000);
ffa8146c
MK
215 assertNotNull(hv);
216 }
217
218 private static void testRV(ResourcesView vp) {
219 assertNotNull(vp);
220 }
221
222 private static CtfTmfEvent getEvent(int rank) {
7b3eb8c0
AM
223 try (CtfTmfTrace trace = CtfTmfTestTrace.SYNTHETIC_TRACE.getTrace()) {
224 ITmfContext ctx = trace.seekEvent(0);
225 for (int i = 0; i < rank; i++) {
226 trace.getNext(ctx);
227 }
228 return trace.getNext(ctx);
ffa8146c 229 }
7b3eb8c0 230
ffa8146c
MK
231 }
232
233 private static IViewPart getViewPart(final String viewTile) {
234 final IViewPart[] vps = new IViewPart[1];
235 UIThreadRunnable.syncExec(new VoidResult() {
236 @Override
237 public void run() {
238 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
239 for (IViewReference viewRef : viewRefs) {
240 IViewPart vp = viewRef.getView(true);
241 if (vp.getTitle().equals(viewTile)) {
242 vps[0] = vp;
243 return;
244 }
245 }
246 }
247 });
248
249 return vps[0];
250 }
251}
This page took 0.045779 seconds and 5 git commands to generate.