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
.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
;
57 * Test the Fetch Remote Traces wizard.
59 @RunWith(SWTBotJunit4ClassRunner
.class)
60 public class FetchRemoteTracesTest
{
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";
76 private static SWTWorkbenchBot fBot
;
79 String traceLocation
= "";
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());
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
) {
94 } catch (URISyntaxException e
) {
97 TRACE_LOCATION
= traceLocation
;
98 TRACE_GROUP_NODE_TEXT
= TRACE_LOCATION
+ " (recursive)";
101 private static File
getBundleFile(IPath relativePath
) throws URISyntaxException
, IOException
{
102 return new File(FileLocator
.toFileURL(FileLocator
.find(TmfRemoteUISWTBotTestPlugin
.getDefault().getBundle(), relativePath
, null)).toURI());
105 /** Test Class setup */
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();
114 SWTBotUtils
.closeView(WELCOME_NAME
, fBot
);
116 SWTBotUtils
.switchToTracingPerspective();
117 /* finish waiting for eclipse to load */
118 SWTBotUtils
.waitForJobs();
121 private static class TraceCountCondition
extends DefaultCondition
{
123 private final TmfProjectElement fProject
;
124 private final int fExpectedCount
;
126 public TraceCountCondition(TmfProjectElement project
, int expectedNumber
) {
128 fExpectedCount
= expectedNumber
;
132 public boolean test() throws Exception
{
133 return fProject
.getTracesFolder().getTraces().size() == fExpectedCount
;
137 public String
getFailureMessage() {
138 return NLS
.bind("The project {0} does not contain {1} traces.", fProject
.getName(), fExpectedCount
);
143 * Test creating a profile, fetching all using the profile.
146 public void testImportAll() {
147 testImport(new Runnable() {
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
);
165 * Test creating a profile, fetching only one trace
168 public void testImportOnlyOne() {
169 testImport(new Runnable() {
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());
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
);
194 * Test creating a profile, fetching nothing
197 public void testImportNothing() {
198 testImport(new Runnable() {
201 fBot
.button("Deselect All").click();
206 TmfProjectElement project
= TmfProjectRegistry
.getProject(ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT_NAME
), true);
207 List
<TmfTraceElement
> traces
= project
.getTracesFolder().getTraces();
208 assertEquals(0, traces
.size());
214 * Test editing a profile
217 public void testEditProfile() {
218 openRemoteProfilePreferences();
220 openRemoteProfilePreferences();
222 // The first tree is the preference "categories" on the left side, we
224 SWTBotTree tree
= fBot
.tree(1);
226 final String
[] traceGroupNodePath
= new String
[] { PROFILE_NAME
, CONNECTION_NODE_TEXT
, TRACE_GROUP_NODE_TEXT
};
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());
235 // Test moving down a trace element
236 SWTBotTreeItem traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
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());
245 // Test moving down a trace element
246 traceNode
= traceGroupNode
.getNode(LTTNG_TRACE_FILE_PATTERN
);
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());
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());
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());
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();
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
);
307 treeItem
.contextMenu(FETCH_COMMAND_NAME
).click();
309 fBot
.button("Manage Profiles").click();
313 assertEquals(PROFILE_NAME
, fBot
.comboBoxWithLabel("Profile name:").getText());
314 assertEquals(CONNECTION_NODE_TEXT
, fBot
.textWithLabel("Nodes:").getText());
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
));
321 button
= fBot
.button("< Back");
322 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
324 assertEquals(PROFILE_NAME
, fBot
.comboBoxWithLabel("Profile name:").getText());
325 assertEquals(CONNECTION_NODE_TEXT
, fBot
.textWithLabel("Nodes:").getText());
327 button
= fBot
.button("Next >");
328 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
331 selectionFunctor
.run();
333 SWTBotShell shell
= fBot
.activeShell();
335 button
= fBot
.button("Finish");
336 fBot
.waitUntil(Conditions
.widgetIsEnabled(button
));
338 fBot
.waitUntil(Conditions
.shellCloses(shell
));
339 SWTBotUtils
.waitForJobs();
341 verifyTracesFunctor
.run();
342 fBot
.closeAllEditors();
343 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
347 private static void createProfile() {
348 fBot
.button("Add").click();
350 // The first tree is the preference "categories" on the left side, we
352 SWTBotTree tree
= fBot
.tree(1);
354 SWTBotTreeItem treeNode
= getTreeItem(fBot
, tree
, PROFILE_NAME
, "name (ssh://userinfo@host:22)");
356 SWTBotText uriLabel
= fBot
.textWithLabel("URI:");
357 uriLabel
.setText("file://");
358 SWTBotText nodeNameLabel
= fBot
.textWithLabel("Node name:");
359 nodeNameLabel
.setText(CONNECTION_NODE_NAME
);
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();
367 // Add the ctf file pattern
368 treeNode
= traceRootNode
.getNode(".*");
370 SWTBotText filePatternLabel
= fBot
.textWithLabel("File pattern:");
371 filePatternLabel
.setText(LTTNG_TRACE_FILE_PATTERN
);
373 // Add the syslog file pattern
374 traceRootNode
.contextMenu("New Trace").click();
375 treeNode
= traceRootNode
.getNode(".*");
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");
382 fBot
.button("OK").click();
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
);
392 private static void deleteProfile() {
393 openRemoteProfilePreferences();
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
);
399 fBot
.button("Remove").click();
400 assertEquals(0, tree
.getAllItems().length
);
401 fBot
.button("OK").click();
404 private static SWTBotTreeItem
getTreeItem(SWTWorkbenchBot bot
, SWTBotTree tree
, String
... nodeNames
) {
405 if (nodeNames
.length
== 0) {
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
;
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();
425 fBot
.menu("Window").menu("Preferences").click();
428 // The first tree is the preference "categories" on the left side
429 SWTBotTree tree
= fBot
.tree(0);
430 SWTBotTreeItem treeNode
= tree
.getTreeItem("Tracing");
433 fBot
.waitUntil(ConditionHelpers
.IsTreeChildNodeAvailable("Remote Profiles", treeNode
));
434 treeNode
= treeNode
.getNode("Remote Profiles");
438 private static SWTBotTreeItem
getTracesFolderTreeItem(SWTBotView projectExplorerBot
) {
439 SWTBotTreeItem treeItem
= projectExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
442 return treeItem
.getNode("Traces [0]");