Commit | Line | Data |
---|---|---|
0be56baa | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 Ericsson |
0be56baa MK |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.pcap.ui.swtbot.tests; |
0be56baa MK |
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; | |
0be56baa MK |
33 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
34 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
35 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
36 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; | |
37 | import org.eclipse.swtbot.swt.finder.results.BoolResult; | |
38 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
39 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
40 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; | |
2bdf0193 AM |
41 | import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace; |
42 | import org.eclipse.tracecompass.internal.tmf.pcap.ui.NetworkingPerspectiveFactory; | |
43 | import org.eclipse.tracecompass.internal.tmf.pcap.ui.stream.StreamListView; | |
44 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
97c71024 | 45 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
2bdf0193 AM |
46 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
47 | import org.eclipse.tracecompass.tmf.pcap.core.tests.shared.PcapTmfTestTrace; | |
48 | import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor; | |
49 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper; | |
50 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry; | |
51 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; | |
fa24d78b AM |
52 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
53 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
f0beeb4a | 54 | import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils; |
0be56baa MK |
55 | import org.eclipse.ui.IEditorPart; |
56 | import org.eclipse.ui.IEditorReference; | |
57 | import org.eclipse.ui.IViewPart; | |
58 | import org.eclipse.ui.IViewReference; | |
59 | import org.eclipse.ui.PlatformUI; | |
60 | import org.eclipse.ui.WorkbenchException; | |
f10c30a0 | 61 | import org.junit.AfterClass; |
0be56baa MK |
62 | import org.junit.BeforeClass; |
63 | import org.junit.Test; | |
64 | import org.junit.runner.RunWith; | |
65 | ||
66 | /** | |
67 | * SWTBot Smoke test for Pcap UI. | |
68 | * | |
69 | * @author Matthew Khouzam | |
70 | */ | |
71 | @RunWith(SWTBotJunit4ClassRunner.class) | |
72 | public class ImportAndReadPcapTest { | |
73 | ||
74 | private static final String NETWORK_PERSPECTIVE_ID = NetworkingPerspectiveFactory.ID; | |
75 | private static final String TRACE_PROJECT_NAME = "test"; | |
76 | ||
77 | private static SWTWorkbenchBot fBot; | |
78 | private ITmfEvent fDesired1; | |
79 | private static PcapTmfTestTrace pttt = PcapTmfTestTrace.BENCHMARK_TRACE; | |
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() { | |
89 | ||
5785ab49 | 90 | SWTBotUtils.initialize(); |
0be56baa MK |
91 | |
92 | /* set up for swtbot */ | |
93 | SWTBotPreferences.TIMEOUT = 300000; /* 300 second timeout */ | |
f10c30a0 | 94 | fLogger.removeAllAppenders(); |
0be56baa MK |
95 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); |
96 | fBot = new SWTWorkbenchBot(); | |
97 | ||
98 | final List<SWTBotView> openViews = fBot.views(); | |
99 | for (SWTBotView view : openViews) { | |
100 | if (view.getTitle().equals("Welcome")) { | |
101 | view.close(); | |
102 | fBot.waitUntil(ConditionHelpers.ViewIsClosed(view)); | |
103 | } | |
104 | } | |
105 | /* Switch perspectives */ | |
106 | switchNetworkPerspective(); | |
107 | /* Finish waiting for eclipse to load */ | |
f0beeb4a | 108 | WaitUtils.waitForJobs(); |
0be56baa MK |
109 | } |
110 | ||
f10c30a0 MK |
111 | /** |
112 | * Test Class teardown | |
113 | */ | |
114 | @AfterClass | |
115 | public static void terminate() { | |
116 | fLogger.removeAllAppenders(); | |
117 | } | |
118 | ||
0be56baa MK |
119 | private static void switchNetworkPerspective() { |
120 | final Exception retE[] = new Exception[1]; | |
121 | if (!UIThreadRunnable.syncExec(new BoolResult() { | |
122 | @Override | |
123 | public Boolean run() { | |
124 | try { | |
125 | PlatformUI.getWorkbench().showPerspective(NETWORK_PERSPECTIVE_ID, | |
126 | PlatformUI.getWorkbench().getActiveWorkbenchWindow()); | |
127 | } catch (WorkbenchException e) { | |
128 | retE[0] = e; | |
129 | return false; | |
130 | } | |
131 | return true; | |
132 | } | |
133 | })) { | |
134 | fail(retE[0].getMessage()); | |
135 | } | |
136 | ||
137 | } | |
138 | ||
139 | /** | |
140 | * Main test case | |
141 | */ | |
142 | @Test | |
143 | public void test() { | |
144 | assumeTrue(pttt.exists()); | |
fa24d78b | 145 | SWTBotUtils.createProject(TRACE_PROJECT_NAME); |
0be56baa MK |
146 | openTrace(); |
147 | openEditor(); | |
148 | testHV(getViewPart("Histogram")); | |
149 | testStreamView(getViewPartRef("Stream List")); | |
150 | fBot.closeAllEditors(); | |
fa24d78b | 151 | SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot); |
0be56baa MK |
152 | } |
153 | ||
154 | private void testStreamView(IViewReference viewPart) { | |
155 | SWTBotView botView = new SWTBotView(viewPart, fBot); | |
156 | StreamListView slv = (StreamListView) getViewPart("Stream List"); | |
157 | botView.setFocus(); | |
158 | SWTBotTree botTree = fBot.tree(); | |
159 | assertNotNull(botTree); | |
97c71024 | 160 | final TmfSelectionRangeUpdatedSignal signal = new TmfSelectionRangeUpdatedSignal(slv, fDesired1.getTimestamp()); |
0be56baa | 161 | slv.broadcast(signal); |
f0beeb4a | 162 | WaitUtils.waitForJobs(); |
0be56baa MK |
163 | // FIXME This is a race condition: |
164 | // TmfEventsTable launches an async exec that may be run after the wait | |
165 | // for jobs. This last delay catches it. | |
fa24d78b | 166 | SWTBotUtils.delay(1000); |
0be56baa MK |
167 | |
168 | } | |
169 | ||
170 | private static void openTrace() { | |
171 | final Exception exception[] = new Exception[1]; | |
172 | exception[0] = null; | |
173 | UIThreadRunnable.syncExec(new VoidResult() { | |
174 | @Override | |
175 | public void run() { | |
176 | try { | |
177 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME); | |
178 | TmfTraceFolder destinationFolder = TmfProjectRegistry.getProject(project, true).getTracesFolder(); | |
179 | String absolutePath = (new File(pttt.getTrace().getPath())).getAbsolutePath(); | |
180 | TmfOpenTraceHelper.openTraceFromPath(destinationFolder, absolutePath, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "org.eclipse.linuxtools.tmf.pcap.core.pcaptrace"); | |
181 | } catch (CoreException e) { | |
182 | exception[0] = e; | |
183 | } | |
184 | } | |
185 | }); | |
186 | if (exception[0] != null) { | |
187 | fail(exception[0].getMessage()); | |
188 | } | |
189 | ||
fa24d78b | 190 | SWTBotUtils.delay(1000); |
f0beeb4a | 191 | WaitUtils.waitForJobs(); |
0be56baa MK |
192 | } |
193 | ||
194 | private void openEditor() { | |
195 | final List<IEditorReference> editorRefs = new ArrayList<>(); | |
196 | UIThreadRunnable.syncExec(new VoidResult() { | |
197 | @Override | |
198 | public void run() { | |
199 | IEditorReference[] ieds = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences(); | |
200 | editorRefs.addAll(Arrays.asList(ieds)); | |
201 | } | |
202 | ||
203 | }); | |
204 | assertFalse(editorRefs.isEmpty()); | |
205 | IEditorPart iep = null; | |
206 | for (IEditorReference ied : editorRefs) { | |
207 | if (ied.getTitle().equals(pttt.getTrace().getName())) { | |
208 | iep = ied.getEditor(true); | |
209 | break; | |
210 | } | |
211 | } | |
212 | assertNotNull(iep); | |
213 | fDesired1 = getEvent(100); | |
214 | final TmfEventsEditor tmfEd = (TmfEventsEditor) iep; | |
215 | UIThreadRunnable.syncExec(new VoidResult() { | |
216 | @Override | |
217 | public void run() { | |
218 | tmfEd.setFocus(); | |
219 | tmfEd.selectionChanged(new SelectionChangedEvent(tmfEd, new StructuredSelection(fDesired1))); | |
220 | } | |
221 | }); | |
222 | ||
f0beeb4a | 223 | WaitUtils.waitForJobs(); |
fa24d78b | 224 | SWTBotUtils.delay(1000); |
0be56baa MK |
225 | assertNotNull(tmfEd); |
226 | } | |
227 | ||
228 | private static void testHV(IViewPart vp) { | |
229 | assertNotNull(vp); | |
230 | } | |
231 | ||
232 | private static ITmfEvent getEvent(int rank) { | |
61cf93ec AM |
233 | PcapTrace trace = pttt.getTrace(); |
234 | ITmfContext ctx = trace.seekEvent(0); | |
235 | for (int i = 0; i < rank; i++) { | |
236 | trace.getNext(ctx); | |
0be56baa | 237 | } |
61cf93ec AM |
238 | ITmfEvent ret = trace.getNext(ctx); |
239 | trace.dispose(); | |
240 | return ret; | |
0be56baa MK |
241 | } |
242 | ||
243 | private static IViewPart getViewPart(final String viewTile) { | |
244 | final IViewPart[] vps = new IViewPart[1]; | |
245 | UIThreadRunnable.syncExec(new VoidResult() { | |
246 | @Override | |
247 | public void run() { | |
248 | IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences(); | |
249 | for (IViewReference viewRef : viewRefs) { | |
250 | IViewPart vp = viewRef.getView(true); | |
251 | if (vp.getTitle().equals(viewTile)) { | |
252 | vps[0] = vp; | |
253 | return; | |
254 | } | |
255 | } | |
256 | } | |
257 | }); | |
258 | ||
259 | return vps[0]; | |
260 | } | |
261 | ||
262 | private static IViewReference getViewPartRef(final String viewTile) { | |
263 | final IViewReference[] vrs = new IViewReference[1]; | |
264 | UIThreadRunnable.syncExec(new VoidResult() { | |
265 | @Override | |
266 | public void run() { | |
267 | IViewReference[] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences(); | |
268 | for (IViewReference viewRef : viewRefs) { | |
269 | IViewPart vp = viewRef.getView(true); | |
270 | if (vp.getTitle().equals(viewTile)) { | |
271 | vrs[0] = viewRef; | |
272 | return; | |
273 | } | |
274 | } | |
275 | } | |
276 | }); | |
277 | ||
278 | return vrs[0]; | |
279 | } | |
280 | } |