tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.remote.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / remote / ui / swtbot / tests / fetch / FetchRemoteTracesTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.remote.ui.swtbot.tests.fetch;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22 import java.util.List;
23
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.varia.NullAppender;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.FileLocator;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
32 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
33 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
34 import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
35 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
36 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
37 import org.eclipse.swtbot.swt.finder.waits.Conditions;
38 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
39 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
40 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
41 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
42 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
43 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
44 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
45 import org.eclipse.tracecompass.ctf.core.tests.synthetictraces.LttngKernelTraceGenerator;
46 import org.eclipse.tracecompass.tmf.remote.ui.swtbot.tests.TmfRemoteUISWTBotTestPlugin;
47 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
48 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
49 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
50 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
51 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55
56 /**
57 * Test the Fetch Remote Traces wizard.
58 */
59 @RunWith(SWTBotJunit4ClassRunner.class)
60 public class FetchRemoteTracesTest {
61
62 private static final String CONNECTION_NODE_NAME = "node1";
63 private static final String CONNECTION_NODE_TEXT = CONNECTION_NODE_NAME + " (file://)";
64 private static final String LTTNG_TRACE_FILE_PATTERN = ".*synthetic.*";
65 private static final String FETCH_COMMAND_NAME = "Fetch Remote Traces...";
66 private static final String PROFILE_NAME = "new profile";
67 private static final String PROJECT_EXPLORER = "Project Explorer";
68 private static final String PROJECT_NAME = "Test";
69 private static final String SYSLOG_FILE_PATTERN = ".*syslog";
70 private static final String TRACE_GROUP_NODE_TEXT;
71 private static final String TRACE_LOCATION;
72 private static final String TRACE_TYPE_LTTNG = "org.eclipse.linuxtools.lttng2.kernel.tracetype";
73 private static final String TRACE_TYPE_SYSLOG = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
74 private static final String WELCOME_NAME = "welcome";
75
76 private static SWTWorkbenchBot fBot;
77
78 static {
79 String traceLocation = "";
80 try {
81 IPath resourcesPath = new Path("resources");
82 File resourcesFile = getBundleFile(resourcesPath);
83 // Create a sub directory to test the trace folders at the same time
84 IPath subDirFullPath = new Path(resourcesFile.getAbsolutePath()).append("generated");
85 File subDirFile = new File(subDirFullPath.toOSString());
86 subDirFile.mkdir();
87
88 IPath generatedTraceFullPath = subDirFullPath.append("synthetic-trace");
89 File generatedTraceFile = new File(generatedTraceFullPath.toOSString());
90 LttngKernelTraceGenerator.generateLttngKernelTrace(generatedTraceFile);
91 traceLocation = new Path(resourcesFile.getAbsolutePath()).toString();
92 } catch (IOException e) {
93 e.printStackTrace();
94 } catch (URISyntaxException e) {
95 e.printStackTrace();
96 }
97 TRACE_LOCATION = traceLocation;
98 TRACE_GROUP_NODE_TEXT = TRACE_LOCATION + " (recursive)";
99 }
100
101 private static File getBundleFile(IPath relativePath) throws URISyntaxException, IOException {
102 return new File(FileLocator.toFileURL(FileLocator.find(TmfRemoteUISWTBotTestPlugin.getDefault().getBundle(), relativePath, null)).toURI());
103 }
104
105 /** Test Class setup */
106 @BeforeClass
107 public static void init() {
108 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
109 SWTBotUtils.failIfUIThread();
110 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
111 Logger.getRootLogger().addAppender(new NullAppender());
112 fBot = new SWTWorkbenchBot();
113
114 SWTBotUtils.closeView(WELCOME_NAME, fBot);
115
116 SWTBotUtils.switchToTracingPerspective();
117 /* finish waiting for eclipse to load */
118 SWTBotUtils.waitForJobs();
119 }
120
121 private static class TraceCountCondition extends DefaultCondition {
122
123 private final TmfProjectElement fProject;
124 private final int fExpectedCount;
125
126 public TraceCountCondition(TmfProjectElement project, int expectedNumber) {
127 fProject = project;
128 fExpectedCount = expectedNumber;
129 }
130
131 @Override
132 public boolean test() throws Exception {
133 return fProject.getTracesFolder().getTraces().size() == fExpectedCount;
134 }
135
136 @Override
137 public String getFailureMessage() {
138 return NLS.bind("The project {0} does not contain {1} traces.", fProject.getName(), fExpectedCount);
139 }
140 }
141
142 /**
143 * Test creating a profile, fetching all using the profile.
144 */
145 @Test
146 public void testImportAll() {
147 testImport(new Runnable() {
148 @Override
149 public void run() {
150 }
151 }, new Runnable() {
152 @Override
153 public void run() {
154 final TmfProjectElement project = TmfProjectRegistry.getProject(ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME), true);
155 fBot.waitUntil(new TraceCountCondition(project, 2));
156 List<TmfTraceElement> traces = project.getTracesFolder().getTraces();
157 assertEquals(2, traces.size());
158 testTrace(traces.get(0), CONNECTION_NODE_NAME + "/generated/synthetic-trace", TRACE_TYPE_LTTNG);
159 testTrace(traces.get(1), CONNECTION_NODE_NAME + "/syslog", TRACE_TYPE_SYSLOG);
160 }
161 });
162 }
163
164 /**
165 * Test creating a profile, fetching only one trace
166 */
167 @Test
168 public void testImportOnlyOne() {
169 testImport(new Runnable() {
170 @Override
171 public void run() {
172 SWTBotTree tree = fBot.tree();
173 fBot.button("Deselect All").click();
174 int length = tree.getAllItems().length;
175 assertTrue(length > 0);
176 // Selecting the second trace under node > traceGroup
177 SWTBotTreeItem node = getTreeItem(fBot, tree, new String[] { CONNECTION_NODE_TEXT, TRACE_GROUP_NODE_TEXT }).getNode(1);
178 assertEquals("syslog", node.getText());
179 node.check();
180 }
181 }, new Runnable() {
182 @Override
183 public void run() {
184 TmfProjectElement project = TmfProjectRegistry.getProject(ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME), true);
185 fBot.waitUntil(new TraceCountCondition(project, 1));
186 List<TmfTraceElement> traces = project.getTracesFolder().getTraces();
187 assertEquals(1, traces.size());
188 testTrace(traces.get(0), CONNECTION_NODE_NAME + "/syslog", TRACE_TYPE_SYSLOG);
189 }
190 });
191 }
192
193 /**
194 * Test creating a profile, fetching nothing
195 */
196 @Test
197 public void testImportNothing() {
198 testImport(new Runnable() {
199 @Override
200 public void run() {
201 fBot.button("Deselect All").click();
202 }
203 }, new Runnable() {
204 @Override
205 public void run() {
206 TmfProjectElement project = TmfProjectRegistry.getProject(ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME), true);
207 List<TmfTraceElement> traces = project.getTracesFolder().getTraces();
208 assertEquals(0, traces.size());
209 }
210 });
211 }
212
213 /**
214 * Test editing a profile
215 */
216 @Test
217 public void testEditProfile() {
218 openRemoteProfilePreferences();
219 createProfile();
220 openRemoteProfilePreferences();
221
222 // The first tree is the preference "categories" on the left side, we
223 // need to skip it
224 SWTBotTree tree = fBot.tree(1);
225
226 final String[] traceGroupNodePath = new String[] { PROFILE_NAME, CONNECTION_NODE_TEXT, TRACE_GROUP_NODE_TEXT };
227
228 // Initial order of traces
229 SWTBotTreeItem traceGroupNode = getTreeItem(fBot, tree, traceGroupNodePath);
230 SWTBotTreeItem[] traceNodes = traceGroupNode.getItems();
231 assertEquals(2, traceNodes.length);
232 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[0].getText());
233 assertEquals(SYSLOG_FILE_PATTERN, traceNodes[1].getText());
234
235 // Test moving down a trace element
236 SWTBotTreeItem traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
237 traceNode.select();
238 fBot.button("Move Down").click();
239 traceGroupNode = getTreeItem(fBot, tree, traceGroupNodePath);
240 traceNodes = traceGroupNode.getItems();
241 assertEquals(2, traceNodes.length);
242 assertEquals(SYSLOG_FILE_PATTERN, traceNodes[0].getText());
243 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[1].getText());
244
245 // Test moving down a trace element
246 traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
247 traceNode.select();
248 fBot.button("Move Up").click();
249 traceGroupNode = getTreeItem(fBot, tree, traceGroupNodePath);
250 traceNodes = traceGroupNode.getItems();
251 assertEquals(2, traceNodes.length);
252 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[0].getText());
253 assertEquals(SYSLOG_FILE_PATTERN, traceNodes[1].getText());
254
255 // Test Copy/Paste
256 traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
257 traceNode.select().contextMenu("Copy").click();
258 traceNode.contextMenu("Paste").click();
259 traceNodes = traceGroupNode.getItems();
260 assertEquals(3, traceNodes.length);
261 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[0].getText());
262 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[1].getText());
263 assertEquals(SYSLOG_FILE_PATTERN, traceNodes[2].getText());
264
265 // Test Cut/Paste
266 traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
267 traceNode.select().contextMenu("Cut").click();
268 traceNode = traceGroupNode.getNode(SYSLOG_FILE_PATTERN);
269 traceNode.select().contextMenu("Paste").click();
270 traceNodes = traceGroupNode.getItems();
271 assertEquals(3, traceNodes.length);
272 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[0].getText());
273 assertEquals(SYSLOG_FILE_PATTERN, traceNodes[1].getText());
274 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[2].getText());
275
276 // Test Delete
277 traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
278 traceNode.select().contextMenu("Delete").click();
279 traceNodes = traceGroupNode.getItems();
280 assertEquals(2, traceNodes.length);
281 assertEquals(SYSLOG_FILE_PATTERN, traceNodes[0].getText());
282 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[1].getText());
283 // Copy to test Paste after Delete
284 traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
285 traceNode.select().contextMenu("Copy").click();
286 traceNode = traceGroupNode.select(SYSLOG_FILE_PATTERN, LTTNG_TRACE_FILE_PATTERN);
287 traceNode.pressShortcut(Keystrokes.DELETE);
288 traceNodes = traceGroupNode.getItems();
289 assertEquals(0, traceNodes.length);
290 // Paste after Delete
291 traceGroupNode.contextMenu("Paste").click();
292 traceNodes = traceGroupNode.getItems();
293 assertEquals(1, traceNodes.length);
294 assertEquals(LTTNG_TRACE_FILE_PATTERN, traceNodes[0].getText());
295 fBot.button("OK").click();
296 deleteProfile();
297 }
298
299 private static void testImport(Runnable selectionFunctor, Runnable verifyTracesFunctor) {
300 SWTBotUtils.createProject(PROJECT_NAME);
301 SWTBotUtils.waitForJobs();
302 SWTBotView projectExplorerBot = fBot.viewByTitle(PROJECT_EXPLORER);
303 assertNotNull("Cannot find " + PROJECT_EXPLORER, projectExplorerBot);
304 projectExplorerBot.show();
305 SWTBotTreeItem treeItem = getTracesFolderTreeItem(projectExplorerBot);
306
307 treeItem.contextMenu(FETCH_COMMAND_NAME).click();
308
309 fBot.button("Manage Profiles").click();
310
311 createProfile();
312
313 assertEquals(PROFILE_NAME, fBot.comboBoxWithLabel("Profile name:").getText());
314 assertEquals(CONNECTION_NODE_TEXT, fBot.textWithLabel("Nodes:").getText());
315
316 // Make sure if we go to the next page and come back that the first page
317 // still has valid values
318 SWTBotButton button = fBot.button("Next >");
319 fBot.waitUntil(Conditions.widgetIsEnabled(button));
320 button.click();
321 button = fBot.button("< Back");
322 fBot.waitUntil(Conditions.widgetIsEnabled(button));
323 button.click();
324 assertEquals(PROFILE_NAME, fBot.comboBoxWithLabel("Profile name:").getText());
325 assertEquals(CONNECTION_NODE_TEXT, fBot.textWithLabel("Nodes:").getText());
326
327 button = fBot.button("Next >");
328 fBot.waitUntil(Conditions.widgetIsEnabled(button));
329 button.click();
330
331 selectionFunctor.run();
332
333 SWTBotShell shell = fBot.activeShell();
334
335 button = fBot.button("Finish");
336 fBot.waitUntil(Conditions.widgetIsEnabled(button));
337 button.click();
338 fBot.waitUntil(Conditions.shellCloses(shell));
339 SWTBotUtils.waitForJobs();
340
341 verifyTracesFunctor.run();
342 fBot.closeAllEditors();
343 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
344 deleteProfile();
345 }
346
347 private static void createProfile() {
348 fBot.button("Add").click();
349
350 // The first tree is the preference "categories" on the left side, we
351 // need to skip it
352 SWTBotTree tree = fBot.tree(1);
353
354 SWTBotTreeItem treeNode = getTreeItem(fBot, tree, PROFILE_NAME, "name (ssh://userinfo@host:22)");
355 treeNode.select();
356 SWTBotText uriLabel = fBot.textWithLabel("URI:");
357 uriLabel.setText("file://");
358 SWTBotText nodeNameLabel = fBot.textWithLabel("Node name:");
359 nodeNameLabel.setText(CONNECTION_NODE_NAME);
360
361 SWTBotTreeItem traceRootNode = treeNode.getNode("/rootpath");
362 traceRootNode.select();
363 SWTBotText pathLabel = fBot.textWithLabel("Root path:");
364 pathLabel.setText(TRACE_LOCATION);
365 fBot.checkBox("Recursive").select();
366
367 // Add the ctf file pattern
368 treeNode = traceRootNode.getNode(".*");
369 treeNode.select();
370 SWTBotText filePatternLabel = fBot.textWithLabel("File pattern:");
371 filePatternLabel.setText(LTTNG_TRACE_FILE_PATTERN);
372
373 // Add the syslog file pattern
374 traceRootNode.contextMenu("New Trace").click();
375 treeNode = traceRootNode.getNode(".*");
376 treeNode.select();
377 filePatternLabel = fBot.textWithLabel("File pattern:");
378 filePatternLabel.setText(SYSLOG_FILE_PATTERN);
379 SWTBotCombo combo = fBot.comboBoxWithLabel("Trace type:");
380 combo.setSelection("Test trace : Test Syslog");
381
382 fBot.button("OK").click();
383 }
384
385 private static void testTrace(TmfTraceElement tmfTraceElement, String expectedTracePath, String traceType) {
386 assertEquals(traceType, tmfTraceElement.getTraceType());
387 IPath tracePath = new Path(tmfTraceElement.getElementPath());
388 assertEquals(expectedTracePath, tracePath.toString());
389 SWTBotUtils.openEditor(fBot, PROJECT_NAME, tracePath);
390 }
391
392 private static void deleteProfile() {
393 openRemoteProfilePreferences();
394
395 // The second tree is the remote profiles tree on the right side
396 SWTBotTree tree = fBot.tree(1);
397 SWTBotTreeItem treeNode = tree.getTreeItem(PROFILE_NAME);
398 treeNode.select();
399 fBot.button("Remove").click();
400 assertEquals(0, tree.getAllItems().length);
401 fBot.button("OK").click();
402 }
403
404 private static SWTBotTreeItem getTreeItem(SWTWorkbenchBot bot, SWTBotTree tree, String... nodeNames) {
405 if (nodeNames.length == 0) {
406 return null;
407 }
408
409 SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]);
410 for (int i = 1; i < nodeNames.length; i++) {
411 String nodeName = nodeNames[i];
412 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode));
413 SWTBotTreeItem newNode = currentNode.getNode(nodeName);
414 currentNode = newNode;
415 }
416
417 return currentNode;
418 }
419
420 private static void openRemoteProfilePreferences() {
421 if (SWTUtils.isMac()) {
422 // On Mac, the Preferences menu item is under the application name.
423 fBot.menu("Eclipse").menu("Preferences").click();
424 } else {
425 fBot.menu("Window").menu("Preferences").click();
426 }
427
428 // The first tree is the preference "categories" on the left side
429 SWTBotTree tree = fBot.tree(0);
430 SWTBotTreeItem treeNode = tree.getTreeItem("Tracing");
431 treeNode.select();
432 treeNode.expand();
433 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable("Remote Profiles", treeNode));
434 treeNode = treeNode.getNode("Remote Profiles");
435 treeNode.select();
436 }
437
438 private static SWTBotTreeItem getTracesFolderTreeItem(SWTBotView projectExplorerBot) {
439 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
440 treeItem.select();
441 treeItem.expand();
442 return treeItem.getNode("Traces [0]");
443 }
444
445 }
This page took 0.067339 seconds and 5 git commands to generate.