tmf: Add waitUntil / condition to tmf.ui.tests
[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.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;
56
57 /**
58 * Test the Fetch Remote Traces wizard.
59 */
60 @RunWith(SWTBotJunit4ClassRunner.class)
61 public class FetchRemoteTracesTest {
62
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";
76
77 private static SWTWorkbenchBot fBot;
78
79 static {
80 String traceLocation = "";
81 try {
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());
87 subDirFile.mkdir();
88
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) {
94 e.printStackTrace();
95 } catch (URISyntaxException e) {
96 e.printStackTrace();
97 }
98 TRACE_LOCATION = traceLocation;
99 TRACE_GROUP_NODE_TEXT = TRACE_LOCATION + " (recursive)";
100 }
101
102 private static File getBundleFile(IPath relativePath) throws URISyntaxException, IOException {
103 return new File(FileLocator.toFileURL(FileLocator.find(TmfRemoteUISWTBotTestPlugin.getDefault().getBundle(), relativePath, null)).toURI());
104 }
105
106 /** Test Class setup */
107 @BeforeClass
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();
114
115 SWTBotUtils.closeView(WELCOME_NAME, fBot);
116
117 SWTBotUtils.switchToTracingPerspective();
118 /* finish waiting for eclipse to load */
119 WaitUtils.waitForJobs();
120 }
121
122 private static class TraceCountCondition extends DefaultCondition {
123
124 private final TmfProjectElement fProject;
125 private final int fExpectedCount;
126
127 public TraceCountCondition(TmfProjectElement project, int expectedNumber) {
128 fProject = project;
129 fExpectedCount = expectedNumber;
130 }
131
132 @Override
133 public boolean test() throws Exception {
134 return fProject.getTracesFolder().getTraces().size() == fExpectedCount;
135 }
136
137 @Override
138 public String getFailureMessage() {
139 return NLS.bind("The project {0} does not contain {1} traces.", fProject.getName(), fExpectedCount);
140 }
141 }
142
143 /**
144 * Test creating a profile, fetching all using the profile.
145 */
146 @Test
147 public void testImportAll() {
148 testImport(new Runnable() {
149 @Override
150 public void run() {
151 }
152 }, new Runnable() {
153 @Override
154 public void run() {
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);
161 }
162 });
163 }
164
165 /**
166 * Test creating a profile, fetching only one trace
167 */
168 @Test
169 public void testImportOnlyOne() {
170 testImport(new Runnable() {
171 @Override
172 public void run() {
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());
180 node.check();
181 }
182 }, new Runnable() {
183 @Override
184 public void run() {
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);
190 }
191 });
192 }
193
194 /**
195 * Test creating a profile, fetching nothing
196 */
197 @Test
198 public void testImportNothing() {
199 testImport(new Runnable() {
200 @Override
201 public void run() {
202 fBot.button("Deselect All").click();
203 }
204 }, new Runnable() {
205 @Override
206 public void run() {
207 TmfProjectElement project = TmfProjectRegistry.getProject(ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME), true);
208 List<TmfTraceElement> traces = project.getTracesFolder().getTraces();
209 assertEquals(0, traces.size());
210 }
211 });
212 }
213
214 /**
215 * Test to verify that empty files are omitted.
216 */
217 @Test
218 public void testEmptyFile() {
219 testImport(new Runnable() {
220 @Override
221 public void run() {
222 SWTBotTree tree = fBot.tree();
223 fBot.button("Deselect All").click();
224 int length = tree.getAllItems().length;
225 assertTrue(length > 0);
226
227 SWTBotTreeItem groupNode = getTreeItem(fBot, tree, new String[] { CONNECTION_NODE_TEXT, TRACE_GROUP_NODE_TEXT });
228 /*
229 * Currently there are 3 items at the location where 1 file has 0 bytes.
230 * Verify that empty file is not shown.
231 */
232 assertEquals(2, groupNode.getItems().length);
233 }
234 }, new Runnable() {
235 @Override
236 public void run() {
237 TmfProjectElement project = TmfProjectRegistry.getProject(ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME), true);
238 List<TmfTraceElement> traces = project.getTracesFolder().getTraces();
239 assertEquals(0, traces.size());
240 }
241 });
242 }
243
244
245 /**
246 * Test editing a profile
247 */
248 @Test
249 public void testEditProfile() {
250 openRemoteProfilePreferences();
251 createProfile();
252 openRemoteProfilePreferences();
253
254 // The first tree is the preference "categories" on the left side, we
255 // need to skip it
256 SWTBotTree tree = fBot.tree(1);
257
258 final String[] traceGroupNodePath = new String[] { PROFILE_NAME, CONNECTION_NODE_TEXT, TRACE_GROUP_NODE_TEXT };
259
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());
266
267 // Test moving down a trace element
268 SWTBotTreeItem traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
269 traceNode.select();
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());
276
277 // Test moving down a trace element
278 traceNode = traceGroupNode.getNode(LTTNG_TRACE_FILE_PATTERN);
279 traceNode.select();
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());
286
287 // Test Copy/Paste
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());
296
297 // Test Cut/Paste
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());
307
308 // Test Delete
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();
328 deleteProfile();
329 }
330
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);
338
339 treeItem.contextMenu(FETCH_COMMAND_NAME).click();
340
341 fBot.button("Manage Profiles").click();
342
343 createProfile();
344
345 assertEquals(PROFILE_NAME, fBot.comboBoxWithLabel("Profile name:").getText());
346 assertEquals(CONNECTION_NODE_TEXT, fBot.textWithLabel("Nodes:").getText());
347
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));
352 button.click();
353 button = fBot.button("< Back");
354 fBot.waitUntil(Conditions.widgetIsEnabled(button));
355 button.click();
356 assertEquals(PROFILE_NAME, fBot.comboBoxWithLabel("Profile name:").getText());
357 assertEquals(CONNECTION_NODE_TEXT, fBot.textWithLabel("Nodes:").getText());
358
359 button = fBot.button("Next >");
360 fBot.waitUntil(Conditions.widgetIsEnabled(button));
361 button.click();
362
363 selectionFunctor.run();
364
365 SWTBotShell shell = fBot.activeShell();
366
367 button = fBot.button("Finish");
368 fBot.waitUntil(Conditions.widgetIsEnabled(button));
369 button.click();
370 fBot.waitUntil(Conditions.shellCloses(shell));
371 WaitUtils.waitForJobs();
372
373 verifyTracesFunctor.run();
374 fBot.closeAllEditors();
375 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
376 deleteProfile();
377 }
378
379 private static void createProfile() {
380 fBot.button("Add").click();
381
382 // The first tree is the preference "categories" on the left side, we
383 // need to skip it
384 SWTBotTree tree = fBot.tree(1);
385
386 SWTBotTreeItem treeNode = getTreeItem(fBot, tree, PROFILE_NAME, "name (ssh://userinfo@host:22)");
387 treeNode.select();
388 SWTBotText uriLabel = fBot.textWithLabel("URI:");
389 uriLabel.setText("file://");
390 SWTBotText nodeNameLabel = fBot.textWithLabel("Node name:");
391 nodeNameLabel.setText(CONNECTION_NODE_NAME);
392
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();
398
399 // Add the ctf file pattern
400 treeNode = traceRootNode.getNode(".*");
401 treeNode.select();
402 SWTBotText filePatternLabel = fBot.textWithLabel("File pattern:");
403 filePatternLabel.setText(LTTNG_TRACE_FILE_PATTERN);
404
405 // Add the syslog file pattern
406 traceRootNode.contextMenu("New Trace").click();
407 treeNode = traceRootNode.getNode(".*");
408 treeNode.select();
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");
413
414 fBot.button("OK").click();
415 }
416
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);
422 }
423
424 private static void deleteProfile() {
425 openRemoteProfilePreferences();
426
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);
430 treeNode.select();
431 fBot.button("Remove").click();
432 assertEquals(0, tree.getAllItems().length);
433 fBot.button("OK").click();
434 }
435
436 private static SWTBotTreeItem getTreeItem(SWTWorkbenchBot bot, SWTBotTree tree, String... nodeNames) {
437 if (nodeNames.length == 0) {
438 return null;
439 }
440
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;
447 }
448
449 return currentNode;
450 }
451
452 private static void openRemoteProfilePreferences() {
453 SWTBotShell preferencesShell = SWTBotUtils.openPreferences(fBot);
454
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");
459 treeNode.select();
460 treeNode.expand();
461 bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable("Remote Profiles", treeNode));
462 treeNode = treeNode.getNode("Remote Profiles");
463 treeNode.select();
464 }
465
466 private static SWTBotTreeItem getTracesFolderTreeItem(SWTBotView projectExplorerBot) {
467 SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
468 treeItem.select();
469 treeItem.expand();
470 return treeItem.getNode("Traces [0]");
471 }
472
473 }
This page took 0.042455 seconds and 5 git commands to generate.