1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.pcap
.ui
.swtbot
.tests
;
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
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.List
;
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
;
65 * SWTBot Smoke test for Pcap UI.
67 * @author Matthew Khouzam
69 @RunWith(SWTBotJunit4ClassRunner
.class)
70 public class ImportAndReadPcapTest
{
72 private static final String NETWORK_PERSPECTIVE_ID
= NetworkingPerspectiveFactory
.ID
;
73 private static final String TRACE_PROJECT_NAME
= "test";
75 private static SWTWorkbenchBot fBot
;
76 private ITmfEvent fDesired1
;
77 private static PcapTmfTestTrace pttt
= PcapTmfTestTrace
.BENCHMARK_TRACE
;
79 /** The Log4j logger instance. */
80 private static final Logger fLogger
= Logger
.getRootLogger();
86 public static void init() {
88 SWTBotUtil
.failIfUIThread();
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();
95 final List
<SWTBotView
> openViews
= fBot
.views();
96 for (SWTBotView view
: openViews
) {
97 if (view
.getTitle().equals("Welcome")) {
99 fBot
.waitUntil(ConditionHelpers
.ViewIsClosed(view
));
102 /* Switch perspectives */
103 switchNetworkPerspective();
104 /* Finish waiting for eclipse to load */
105 SWTBotUtil
.waitForJobs();
108 private static void switchNetworkPerspective() {
109 final Exception retE
[] = new Exception
[1];
110 if (!UIThreadRunnable
.syncExec(new BoolResult() {
112 public Boolean
run() {
114 PlatformUI
.getWorkbench().showPerspective(NETWORK_PERSPECTIVE_ID
,
115 PlatformUI
.getWorkbench().getActiveWorkbenchWindow());
116 } catch (WorkbenchException e
) {
123 fail(retE
[0].getMessage());
133 assumeTrue(pttt
.exists());
134 SWTBotUtil
.createProject(TRACE_PROJECT_NAME
);
137 testHV(getViewPart("Histogram"));
138 testStreamView(getViewPartRef("Stream List"));
139 fBot
.closeAllEditors();
140 SWTBotUtil
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
143 private void testStreamView(IViewReference viewPart
) {
144 SWTBotView botView
= new SWTBotView(viewPart
, fBot
);
145 StreamListView slv
= (StreamListView
) getViewPart("Stream List");
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);
159 private static void openTrace() {
160 final Exception exception
[] = new Exception
[1];
162 UIThreadRunnable
.syncExec(new VoidResult() {
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
) {
175 if (exception
[0] != null) {
176 fail(exception
[0].getMessage());
179 SWTBotUtil
.delay(1000);
180 SWTBotUtil
.waitForJobs();
183 private void openEditor() {
184 final List
<IEditorReference
> editorRefs
= new ArrayList
<>();
185 UIThreadRunnable
.syncExec(new VoidResult() {
188 IEditorReference
[] ieds
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
189 editorRefs
.addAll(Arrays
.asList(ieds
));
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);
202 fDesired1
= getEvent(100);
203 final TmfEventsEditor tmfEd
= (TmfEventsEditor
) iep
;
204 UIThreadRunnable
.syncExec(new VoidResult() {
208 tmfEd
.selectionChanged(new SelectionChangedEvent(tmfEd
, new StructuredSelection(fDesired1
)));
212 SWTBotUtil
.waitForJobs();
213 SWTBotUtil
.delay(1000);
214 assertNotNull(tmfEd
);
217 private static void testHV(IViewPart vp
) {
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
++) {
227 return trace
.getNext(ctx
);
232 private static IViewPart
getViewPart(final String viewTile
) {
233 final IViewPart
[] vps
= new IViewPart
[1];
234 UIThreadRunnable
.syncExec(new VoidResult() {
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
)) {
251 private static IViewReference
getViewPartRef(final String viewTile
) {
252 final IViewReference
[] vrs
= new IViewReference
[1];
253 UIThreadRunnable
.syncExec(new VoidResult() {
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
)) {