1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.remote
.ui
.swtbot
.tests
.fetch
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.io
.IOException
;
21 import java
.net
.URISyntaxException
;
22 import java
.util
.List
;
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
.SWTBot
;
34 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
35 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
36 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
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
.shared
.LttngTraceGenerator
;
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
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
53 import org
.junit
.BeforeClass
;
54 import org
.junit
.Test
;
55 import org
.junit
.runner
.RunWith
;
58 * Test the Fetch Remote Traces wizard.
60 @RunWith(SWTBotJunit4ClassRunner
.class)
61 public class FetchRemoteTracesTest
{
63 private static final String CONNECTION_NODE_NAME
= "node1";
64 private static final String CONNECTION_NODE_TEXT
= CONNECTION_NODE_NAME
+ " (file://)";
65 private static final String LTTNG_TRACE_FILE_PATTERN
= ".*synthetic.*";
66 private static final String FETCH_COMMAND_NAME
= "Fetch Remote Traces...";
67 private static final String PROFILE_NAME
= "new profile";
68 private static final String PROJECT_EXPLORER
= "Project Explorer";
69 private static final String PROJECT_NAME
= "Test";
70 private static final String SYSLOG_FILE_PATTERN
= ".*syslog";
71 private static final String TRACE_GROUP_NODE_TEXT
;
72 private static final String TRACE_LOCATION
;
73 private static final String TRACE_TYPE_LTTNG
= "org.eclipse.linuxtools.lttng2.kernel.tracetype";
74 private static final String TRACE_TYPE_SYSLOG
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
75 private static final String WELCOME_NAME
= "welcome";
77 private static SWTWorkbenchBot fBot
;
80 String traceLocation
= "";
82 IPath resourcesPath
= new Path("resources");
83 File resourcesFile
= getBundleFile(resourcesPath
);
84 // Create a sub directory to test the trace folders at the same time
85 IPath subDirFullPath
= new Path(resourcesFile
.getAbsolutePath()).append("generated");
86 File subDirFile
= new File(subDirFullPath
.toOSString());
89 IPath generatedTraceFullPath
= subDirFullPath
.append("synthetic-trace");
90 File generatedTraceFile
= new File(generatedTraceFullPath
.toOSString());
91 LttngTraceGenerator
.generateLttngTrace(generatedTraceFile
);
92 traceLocation
= new Path(resourcesFile
.getAbsolutePath()).toString();
93 } catch (IOException e
) {
95 } catch (URISyntaxException e
) {
98 TRACE_LOCATION
= traceLocation
;
99 TRACE_GROUP_NODE_TEXT
= TRACE_LOCATION
+ " (recursive)";
102 private static File
getBundleFile(IPath relativePath
) throws URISyntaxException
, IOException
{
103 return new File(FileLocator
.toFileURL(FileLocator
.find(TmfRemoteUISWTBotTestPlugin
.getDefault().getBundle(), relativePath
, null)).toURI());
106 /** Test Class setup */
108 public static void init() {
109 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
110 SWTBotUtils
.initialize();
111 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
112 Logger
.getRootLogger().addAppender(new NullAppender());
113 fBot
= new SWTWorkbenchBot();
115 SWTBotUtils
.closeView(WELCOME_NAME
, fBot
);
117 SWTBotUtils
.switchToTracingPerspective();
118 /* finish waiting for eclipse to load */
119 WaitUtils
.waitForJobs();
122 private static class TraceCountCondition
extends DefaultCondition
{
124 private final TmfProjectElement fProject
;
125 private final int fExpectedCount
;
127 public TraceCountCondition(TmfProjectElement project
, int expectedNumber
) {
129 fExpectedCount
= expectedNumber
;
133 public boolean test() throws Exception
{
134 return fProject
.getTracesFolder().getTraces().size() == fExpectedCount
;
138 public String
getFailureMessage() {
139 return NLS
.bind("The project {0} does not contain {1} traces.", fProject
.getName(), fExpectedCount
);
144 * Test creating a profile, fetching all using the profile.
147 public void testImportAll() {
148 testImport(new Runnable() {
155 final TmfProjectElement project
= TmfProjectRegistry
.getProject(ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT_NAME
), true);
156 fBot
.waitUntil(new TraceCountCondition(project
, 2));
157 List
<TmfTraceElement
> traces
= project
.getTracesFolder().getTraces();
158 assertEquals(2, traces
.size());
159 testTrace(traces
.get(0), CONNECTION_NODE_NAME
+ "/resources/generated/synthetic-trace", TRACE_TYPE_LTTNG
);
160 testTrace(traces
.get(1), CONNECTION_NODE_NAME
+ "/resources/syslog", TRACE_TYPE_SYSLOG
);
166 * Test creating a profile, fetching only one trace
169 public void testImportOnlyOne() {
170 testImport(new Runnable() {
173 SWTBotTree tree
= fBot
.tree();
174 fBot
.button("Deselect All").click();
175 int length
= tree
.getAllItems().length
;
176 assertTrue(length
> 0);
177 // Selecting the second trace under node > traceGroup
178 SWTBotTreeItem node
= getTreeItem(fBot
, tree
, new String
[] { CONNECTION_NODE_TEXT
, TRACE_GROUP_NODE_TEXT
}).getNode(1);
179 assertEquals("syslog", node
.getText());
185 TmfProjectElement project
= TmfProjectRegistry
.getProject(ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT_NAME
), true);
186 fBot
.waitUntil(new TraceCountCondition(project
, 1));
187 List
<TmfTraceElement
> traces
= project
.getTracesFolder().getTraces();
188 assertEquals(1, traces
.size());
189 testTrace(traces
.get(0), CONNECTION_NODE_NAME
+ "/resources/syslog", TRACE_TYPE_SYSLOG
);
195 * Test creating a profile, fetching nothing
198 public void testImportNothing() {
199 testImport(new Runnable() {
202 fBot
.button("Deselect All").click();
207 TmfProjectElement project
= TmfProjectRegistry
.getProject(ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT_NAME
), true);
208 List
<TmfTraceElement
> traces
= project
.getTracesFolder().getTraces();
209 assertEquals(0, traces
.size());
215 * Test to verify that empty files are omitted.
218 public void testEmptyFile() {
219 testImport(new Runnable() {
222 SWTBotTree tree
= fBot
.tree();
223 fBot
.button("Deselect All").click();
224 int length
= tree
.getAllItems().length
;
225 assertTrue(length
> 0);
227 SWTBotTreeItem groupNode
= getTreeItem(fBot
, tree
, new String
[] { CONNECTION_NODE_TEXT
, TRACE_GROUP_NODE_TEXT
});
229 * Currently there are 3 items at the location where 1 file has 0 bytes.
230 * Verify that empty file is not shown.
232 assertEquals(2, groupNode
.getItems().length
);
237 TmfProjectElement project
= TmfProjectRegistry
.getProject(ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT_NAME
), true);
238 List
<TmfTraceElement
> traces
= project
.getTracesFolder().getTraces();
239 assertEquals(0, traces
.size());
246 * Test editing a profile
249 public void testEditProfile() {
250 openRemoteProfilePreferences();
252 openRemoteProfilePreferences();
254 // The first tree is the preference "categories" on the left side, we
256 SWTBotTree tree
= fBot
.tree(1);
258 final String
[] traceGroupNodePath
= new String
[] { PROFILE_NAME
, CONNECTION_NODE_TEXT
, TRACE_GROUP_NODE_TEXT
};
260 // Initial order of traces
261 SWTBotTreeItem traceGroupNode
= getTreeItem(fBot
, tree
, traceGroupNodePath
);
262 SWTBotTreeItem
[] traceNodes
= traceGroupNode
.getItems();
263 assertEquals(2, traceNodes
.length
);
264 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[0].getText());
265 assertEquals(SYSLOG_FILE_PATTERN
, traceNodes
[1].getText());
267 // Test moving down a trace element
268 SWTBotTreeItem traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
270 fBot
.button("Move Down").click();
271 traceGroupNode
= getTreeItem(fBot
, tree
, traceGroupNodePath
);
272 traceNodes
= traceGroupNode
.getItems();
273 assertEquals(2, traceNodes
.length
);
274 assertEquals(SYSLOG_FILE_PATTERN
, traceNodes
[0].getText());
275 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[1].getText());
277 // Test moving down a trace element
278 traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
280 fBot
.button("Move Up").click();
281 traceGroupNode
= getTreeItem(fBot
, tree
, traceGroupNodePath
);
282 traceNodes
= traceGroupNode
.getItems();
283 assertEquals(2, traceNodes
.length
);
284 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[0].getText());
285 assertEquals(SYSLOG_FILE_PATTERN
, traceNodes
[1].getText());
288 traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
289 traceNode
.select().contextMenu("Copy").click();
290 traceNode
.contextMenu("Paste").click();
291 traceNodes
= traceGroupNode
.getItems();
292 assertEquals(3, traceNodes
.length
);
293 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[0].getText());
294 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[1].getText());
295 assertEquals(SYSLOG_FILE_PATTERN
, traceNodes
[2].getText());
298 traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
299 traceNode
.select().contextMenu("Cut").click();
300 traceNode
= traceGroupNode
.getNode(SYSLOG_FILE_PATTERN
);
301 traceNode
.select().contextMenu("Paste").click();
302 traceNodes
= traceGroupNode
.getItems();
303 assertEquals(3, traceNodes
.length
);
304 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[0].getText());
305 assertEquals(SYSLOG_FILE_PATTERN
, traceNodes
[1].getText());
306 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[2].getText());
309 traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
310 traceNode
.select().contextMenu("Delete").click();
311 traceNodes
= traceGroupNode
.getItems();
312 assertEquals(2, traceNodes
.length
);
313 assertEquals(SYSLOG_FILE_PATTERN
, traceNodes
[0].getText());
314 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[1].getText());
315 // Copy to test Paste after Delete
316 traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
317 traceNode
.select().contextMenu("Copy").click();
318 traceNode
= traceGroupNode
.select(SYSLOG_FILE_PATTERN
, LTTNG_TRACE_FILE_PATTERN
);
319 traceNode
.pressShortcut(Keystrokes
.DELETE
);
320 traceNodes
= traceGroupNode
.getItems();
321 assertEquals(0, traceNodes
.length
);
322 // Paste after Delete
323 traceGroupNode
.contextMenu("Paste").click();
324 traceNodes
= traceGroupNode
.getItems();
325 assertEquals(1, traceNodes
.length
);
326 assertEquals(LTTNG_TRACE_FILE_PATTERN
, traceNodes
[0].getText());
327 fBot
.button("OK").click();
331 private static void testImport(Runnable selectionFunctor
, Runnable verifyTracesFunctor
) {
332 SWTBotUtils
.createProject(PROJECT_NAME
);
333 WaitUtils
.waitForJobs();
334 SWTBotView projectExplorerBot
= fBot
.viewByTitle(PROJECT_EXPLORER
);
335 assertNotNull("Cannot find " + PROJECT_EXPLORER
, projectExplorerBot
);
336 projectExplorerBot
.show();
337 SWTBotTreeItem treeItem
= getTracesFolderTreeItem(projectExplorerBot
);
339 treeItem
.contextMenu(FETCH_COMMAND_NAME
).click();
341 fBot
.button("Manage Profiles").click();
345 assertEquals(PROFILE_NAME
, fBot
.comboBoxWithLabel("Profile name:").getText());
346 assertEquals(CONNECTION_NODE_TEXT
, fBot
.textWithLabel("Nodes:").getText());
348 // Make sure if we go to the next page and come back that the first page
349 // still has valid values
350 SWTBotButton button
= fBot
.button("Next >");
351 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
353 button
= fBot
.button("< Back");
354 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
356 assertEquals(PROFILE_NAME
, fBot
.comboBoxWithLabel("Profile name:").getText());
357 assertEquals(CONNECTION_NODE_TEXT
, fBot
.textWithLabel("Nodes:").getText());
359 button
= fBot
.button("Next >");
360 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
363 selectionFunctor
.run();
365 SWTBotShell shell
= fBot
.activeShell();
367 button
= fBot
.button("Finish");
368 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
370 fBot
.waitUntil(Conditions
.shellCloses(shell
));
371 WaitUtils
.waitForJobs();
373 verifyTracesFunctor
.run();
374 fBot
.closeAllEditors();
375 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
379 private static void createProfile() {
380 fBot
.button("Add").click();
382 // The first tree is the preference "categories" on the left side, we
384 SWTBotTree tree
= fBot
.tree(1);
386 SWTBotTreeItem treeNode
= getTreeItem(fBot
, tree
, PROFILE_NAME
, "name (ssh://userinfo@host:22)");
388 SWTBotText uriLabel
= fBot
.textWithLabel("URI:");
389 uriLabel
.setText("file://");
390 SWTBotText nodeNameLabel
= fBot
.textWithLabel("Node name:");
391 nodeNameLabel
.setText(CONNECTION_NODE_NAME
);
393 SWTBotTreeItem traceRootNode
= treeNode
.getNode("/rootpath");
394 traceRootNode
.select();
395 SWTBotText pathLabel
= fBot
.textWithLabel("Root path:");
396 pathLabel
.setText(TRACE_LOCATION
);
397 fBot
.checkBox("Recursive").select();
399 // Add the ctf file pattern
400 treeNode
= traceRootNode
.getNode(".*");
402 SWTBotText filePatternLabel
= fBot
.textWithLabel("File pattern:");
403 filePatternLabel
.setText(LTTNG_TRACE_FILE_PATTERN
);
405 // Add the syslog file pattern
406 traceRootNode
.contextMenu("New Trace").click();
407 treeNode
= traceRootNode
.getNode(".*");
409 filePatternLabel
= fBot
.textWithLabel("File pattern:");
410 filePatternLabel
.setText(SYSLOG_FILE_PATTERN
);
411 SWTBotCombo combo
= fBot
.comboBoxWithLabel("Trace type:");
412 combo
.setSelection("Test trace : Test Syslog");
414 fBot
.button("OK").click();
417 private static void testTrace(TmfTraceElement tmfTraceElement
, String expectedTracePath
, String traceType
) {
418 assertEquals(traceType
, tmfTraceElement
.getTraceType());
419 IPath tracePath
= new Path(tmfTraceElement
.getElementPath());
420 assertEquals(expectedTracePath
, tracePath
.toString());
421 SWTBotUtils
.openEditor(fBot
, PROJECT_NAME
, tracePath
);
424 private static void deleteProfile() {
425 openRemoteProfilePreferences();
427 // The second tree is the remote profiles tree on the right side
428 SWTBotTree tree
= fBot
.tree(1);
429 SWTBotTreeItem treeNode
= tree
.getTreeItem(PROFILE_NAME
);
431 fBot
.button("Remove").click();
432 assertEquals(0, tree
.getAllItems().length
);
433 fBot
.button("OK").click();
436 private static SWTBotTreeItem
getTreeItem(SWTWorkbenchBot bot
, SWTBotTree tree
, String
... nodeNames
) {
437 if (nodeNames
.length
== 0) {
441 SWTBotTreeItem currentNode
= tree
.getTreeItem(nodeNames
[0]);
442 for (int i
= 1; i
< nodeNames
.length
; i
++) {
443 String nodeName
= nodeNames
[i
];
444 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable(nodeName
, currentNode
));
445 SWTBotTreeItem newNode
= currentNode
.getNode(nodeName
);
446 currentNode
= newNode
;
452 private static void openRemoteProfilePreferences() {
453 SWTBotShell preferencesShell
= SWTBotUtils
.openPreferences(fBot
);
455 // The first tree is the preference "categories" on the left side
456 SWTBot bot
= preferencesShell
.bot();
457 SWTBotTree tree
= bot
.tree(0);
458 SWTBotTreeItem treeNode
= tree
.getTreeItem("Tracing");
461 bot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable("Remote Profiles", treeNode
));
462 treeNode
= treeNode
.getNode("Remote Profiles");
466 private static SWTBotTreeItem
getTracesFolderTreeItem(SWTBotView projectExplorerBot
) {
467 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
470 return treeItem
.getNode("Traces [0]");