Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.pcap.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / pcap / ui / swtbot / tests / ImportAndReadPcapTest.java
1 /*******************************************************************************
2 * Copyright (c) 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.pcap.ui.swtbot.tests;
14
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.fail;
18 import static org.junit.Assume.assumeTrue;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.apache.log4j.ConsoleAppender;
26 import org.apache.log4j.Logger;
27 import org.apache.log4j.SimpleLayout;
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
34 import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
36 import org.eclipse.linuxtools.tmf.pcap.core.tests.shared.PcapTmfTestTrace;
37 import org.eclipse.linuxtools.internal.tmf.pcap.core.trace.PcapTrace;
38 import org.eclipse.linuxtools.internal.tmf.pcap.ui.NetworkingPerspectiveFactory;
39 import org.eclipse.linuxtools.internal.tmf.pcap.ui.stream.StreamListView;
40 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
41 import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper;
42 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
43 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
44 import org.eclipse.linuxtools.tmf.ui.swtbot.tests.SWTBotUtil;
45 import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
46 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
47 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
48 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
49 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
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.SWTBotTree;
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 import org.junit.runner.RunWith;
63
64 /**
65 * SWTBot Smoke test for Pcap UI.
66 *
67 * @author Matthew Khouzam
68 */
69 @RunWith(SWTBotJunit4ClassRunner.class)
70 public class ImportAndReadPcapTest {
71
72 private static final String NETWORK_PERSPECTIVE_ID = NetworkingPerspectiveFactory.ID;
73 private static final String TRACE_PROJECT_NAME = "test";
74
75 private static SWTWorkbenchBot fBot;
76 private ITmfEvent fDesired1;
77 private static PcapTmfTestTrace pttt = PcapTmfTestTrace.BENCHMARK_TRACE;
78
79 /** The Log4j logger instance. */
80 private static final Logger fLogger = Logger.getRootLogger();
81
82 /**
83 * Test Class setup
84 */
85 @BeforeClass
86 public static void init() {
87
88 SWTBotUtil.failIfUIThread();
89
90 /* set up for swtbot */
91 SWTBotPreferences.TIMEOUT = 300000; /* 300 second timeout */
92 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
93 fBot = new SWTWorkbenchBot();
94
95 final List<SWTBotView> openViews = fBot.views();
96 for (SWTBotView view : openViews) {
97 if (view.getTitle().equals("Welcome")) {
98 view.close();
99 fBot.waitUntil(ConditionHelpers.ViewIsClosed(view));
100 }
101 }
102 /* Switch perspectives */
103 switchNetworkPerspective();
104 /* Finish waiting for eclipse to load */
105 SWTBotUtil.waitForJobs();
106 }
107
108 private static void switchNetworkPerspective() {
109 final Exception retE[] = new Exception[1];
110 if (!UIThreadRunnable.syncExec(new BoolResult() {
111 @Override
112 public Boolean run() {
113 try {
114 PlatformUI.getWorkbench().showPerspective(NETWORK_PERSPECTIVE_ID,
115 PlatformUI.getWorkbench().getActiveWorkbenchWindow());
116 } catch (WorkbenchException e) {
117 retE[0] = e;
118 return false;
119 }
120 return true;
121 }
122 })) {
123 fail(retE[0].getMessage());
124 }
125
126 }
127
128 /**
129 * Main test case
130 */
131 @Test
132 public void test() {
133 assumeTrue(pttt.exists());
134 SWTBotUtil.createProject(TRACE_PROJECT_NAME);
135 openTrace();
136 openEditor();
137 testHV(getViewPart("Histogram"));
138 testStreamView(getViewPartRef("Stream List"));
139 fBot.closeAllEditors();
140 SWTBotUtil.deleteProject(TRACE_PROJECT_NAME, fBot);
141 }
142
143 private void testStreamView(IViewReference viewPart) {
144 SWTBotView botView = new SWTBotView(viewPart, fBot);
145 StreamListView slv = (StreamListView) getViewPart("Stream List");
146 botView.setFocus();
147 SWTBotTree botTree = fBot.tree();
148 assertNotNull(botTree);
149 final TmfTimeSynchSignal signal = new TmfTimeSynchSignal(slv, fDesired1.getTimestamp());
150 slv.broadcast(signal);
151 SWTBotUtil.waitForJobs();
152 // FIXME This is a race condition:
153 // TmfEventsTable launches an async exec that may be run after the wait
154 // for jobs. This last delay catches it.
155 SWTBotUtil.delay(1000);
156
157 }
158
159 private static void openTrace() {
160 final Exception exception[] = new Exception[1];
161 exception[0] = null;
162 UIThreadRunnable.syncExec(new VoidResult() {
163 @Override
164 public void run() {
165 try {
166 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME);
167 TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
168 String absolutePath = (new File(pttt.getTrace().getPath())).getAbsolutePath();
169 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, absolutePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "org.eclipse.linuxtools.tmf.pcap.core.pcaptrace");
170 } catch (CoreException e) {
171 exception[0] = e;
172 }
173 }
174 });
175 if (exception[0] != null) {
176 fail(exception[0].getMessage());
177 }
178
179 SWTBotUtil.delay(1000);
180 SWTBotUtil.waitForJobs();
181 }
182
183 private void openEditor() {
184 final List<IEditorReference> editorRefs = new ArrayList<>();
185 UIThreadRunnable.syncExec(new VoidResult() {
186 @Override
187 public void run() {
188 IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
189 editorRefs.addAll(Arrays.asList(ieds));
190 }
191
192 });
193 assertFalse(editorRefs.isEmpty());
194 IEditorPart iep = null;
195 for (IEditorReference ied : editorRefs) {
196 if (ied.getTitle().equals(pttt.getTrace().getName())) {
197 iep = ied.getEditor(true);
198 break;
199 }
200 }
201 assertNotNull(iep);
202 fDesired1 = getEvent(100);
203 final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
204 UIThreadRunnable.syncExec(new VoidResult() {
205 @Override
206 public void run() {
207 tmfEd.setFocus();
208 tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(fDesired1)));
209 }
210 });
211
212 SWTBotUtil.waitForJobs();
213 SWTBotUtil.delay(1000);
214 assertNotNull(tmfEd);
215 }
216
217 private static void testHV(IViewPart vp) {
218 assertNotNull(vp);
219 }
220
221 private static ITmfEvent getEvent(int rank) {
222 try (PcapTrace trace = pttt.getTrace()) {
223 ITmfContext ctx = trace.seekEvent(0);
224 for (int i = 0; i < rank; i++) {
225 trace.getNext(ctx);
226 }
227 return trace.getNext(ctx);
228 }
229
230 }
231
232 private static IViewPart getViewPart(final String viewTile) {
233 final IViewPart[] vps = new IViewPart[1];
234 UIThreadRunnable.syncExec(new VoidResult() {
235 @Override
236 public void run() {
237 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
238 for (IViewReference viewRef : viewRefs) {
239 IViewPart vp = viewRef.getView(true);
240 if (vp.getTitle().equals(viewTile)) {
241 vps[0] = vp;
242 return;
243 }
244 }
245 }
246 });
247
248 return vps[0];
249 }
250
251 private static IViewReference getViewPartRef(final String viewTile) {
252 final IViewReference[] vrs = new IViewReference[1];
253 UIThreadRunnable.syncExec(new VoidResult() {
254 @Override
255 public void run() {
256 IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
257 for (IViewReference viewRef : viewRefs) {
258 IViewPart vp = viewRef.getView(true);
259 if (vp.getTitle().equals(viewTile)) {
260 vrs[0] = viewRef;
261 return;
262 }
263 }
264 }
265 });
266
267 return vrs[0];
268 }
269 }
This page took 0.069781 seconds and 5 git commands to generate.