/
ImportAndReadPcapTest.java
236 lines (211 loc) · 8.71 KB
/
ImportAndReadPcapTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*******************************************************************************
* Copyright (c) 2014, 2015 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Matthew Khouzam - Initial API and implementation
*******************************************************************************/
package org.eclipse.tracecompass.tmf.pcap.ui.swtbot.tests;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace;
import org.eclipse.tracecompass.internal.tmf.pcap.ui.NetworkingPerspectiveFactory;
import org.eclipse.tracecompass.internal.tmf.pcap.ui.stream.StreamListView;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.pcap.core.tests.shared.PcapTmfTestTrace;
import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.PlatformUI;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* SWTBot Smoke test for Pcap UI.
*
* @author Matthew Khouzam
*/
@RunWith(SWTBotJunit4ClassRunner.class)
public class ImportAndReadPcapTest {
private static final String NETWORK_PERSPECTIVE_ID = NetworkingPerspectiveFactory.ID;
private static final String TRACE_PROJECT_NAME = "test";
private static SWTWorkbenchBot fBot;
private ITmfEvent fDesired1;
private static PcapTmfTestTrace pttt = PcapTmfTestTrace.BENCHMARK_TRACE;
/**
* Test Class setup
*/
@BeforeClass
public static void init() {
SWTBotUtils.initialize();
/* set up for swtbot */
SWTBotPreferences.TIMEOUT = 300000; /* 300 second timeout */
fBot = new SWTWorkbenchBot();
SWTBotUtils.closeView("welcome", fBot);
/* Switch perspectives */
SWTBotUtils.switchToPerspective(NETWORK_PERSPECTIVE_ID);
/* Finish waiting for eclipse to load */
WaitUtils.waitForJobs();
}
/**
* Main test case
*/
@Test
public void test() {
assumeTrue(pttt.exists());
SWTBotUtils.createProject(TRACE_PROJECT_NAME);
openTrace();
openEditor();
testHV(getViewPart("Histogram"));
testStreamView(getViewPartRef("Stream List"));
fBot.closeAllEditors();
SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
}
private void testStreamView(IViewReference viewPart) {
SWTBotView botView = new SWTBotView(viewPart, fBot);
StreamListView slv = (StreamListView) getViewPart("Stream List");
botView.setFocus();
SWTBotTree botTree = fBot.tree();
assertNotNull(botTree);
final TmfSelectionRangeUpdatedSignal signal = new TmfSelectionRangeUpdatedSignal(slv, fDesired1.getTimestamp());
slv.broadcast(signal);
WaitUtils.waitForJobs();
// FIXME This is a race condition:
// TmfEventsTable launches an async exec that may be run after the wait
// for jobs. This last delay catches it.
SWTBotUtils.delay(1000);
}
private static void openTrace() {
final Exception exception[] = new Exception[1];
exception[0] = null;
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
try {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME);
TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder();
String absolutePath = (new File(pttt.getTrace().getPath())).getAbsolutePath();
TmfOpenTraceHelper.openTraceFromPath(destinationFolder, absolutePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "org.eclipse.linuxtools.tmf.pcap.core.pcaptrace");
} catch (CoreException e) {
exception[0] = e;
}
}
});
if (exception[0] != null) {
fail(exception[0].getMessage());
}
SWTBotUtils.delay(1000);
WaitUtils.waitForJobs();
}
private void openEditor() {
final List<IEditorReference> editorRefs = new ArrayList<>();
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
editorRefs.addAll(Arrays.asList(ieds));
}
});
assertFalse(editorRefs.isEmpty());
IEditorPart iep = null;
for (IEditorReference ied : editorRefs) {
if (ied.getTitle().equals(pttt.getTrace().getName())) {
iep = ied.getEditor(true);
break;
}
}
assertNotNull(iep);
fDesired1 = getEvent(100);
final TmfEventsEditor tmfEd = (TmfEventsEditor) iep;
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
tmfEd.setFocus();
tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(fDesired1)));
}
});
WaitUtils.waitForJobs();
SWTBotUtils.delay(1000);
assertNotNull(tmfEd);
}
private static void testHV(IViewPart vp) {
assertNotNull(vp);
}
private static ITmfEvent getEvent(int rank) {
PcapTrace trace = pttt.getTrace();
ITmfContext ctx = trace.seekEvent(0);
for (int i = 0; i < rank; i++) {
trace.getNext(ctx);
}
ITmfEvent ret = trace.getNext(ctx);
trace.dispose();
return ret;
}
private static IViewPart getViewPart(final String viewTile) {
final IViewPart[] vps = new IViewPart[1];
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (IViewReference viewRef : viewRefs) {
IViewPart vp = viewRef.getView(true);
if (vp.getTitle().equals(viewTile)) {
vps[0] = vp;
return;
}
}
}
});
return vps[0];
}
private static IViewReference getViewPartRef(final String viewTile) {
final IViewReference[] vrs = new IViewReference[1];
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (IViewReference viewRef : viewRefs) {
IViewPart vp = viewRef.getView(true);
if (vp.getTitle().equals(viewTile)) {
vrs[0] = viewRef;
return;
}
}
}
});
return vrs[0];
}
}