a35085cc7fb42f2f5152c8c09aea6f4eece1b5c3
[deliverable/tracecompass.git] / releng / org.eclipse.tracecompass.integration.swtbot.tests / src / org / eclipse / tracecompass / integration / swtbot / tests / projectexplorer / ProjectExplorerTraceActionsTest.java
1 /******************************************************************************
2 * Copyright (c) 2016 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
10 package org.eclipse.tracecompass.integration.swtbot.tests.projectexplorer;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.List;
18
19 import org.apache.log4j.ConsoleAppender;
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.SimpleLayout;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
26 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
27 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
28 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
29 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
30 import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
31 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
32 import org.eclipse.swtbot.swt.finder.waits.Conditions;
33 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
34 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
35 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
36 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
37 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
38 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
39 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
40 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
41 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
42 import org.eclipse.tracecompass.tmf.ui.views.statistics.TmfStatisticsView;
43 import org.eclipse.ui.IEditorReference;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49
50 import com.google.common.collect.ImmutableList;
51
52 /**
53 * SWTBot test for testing Project Explorer Trace actions (context-menus,
54 * keyboard)
55 */
56 @RunWith(SWTBotJunit4ClassRunner.class)
57 public class ProjectExplorerTraceActionsTest {
58 private static @NonNull TestTraceInfo CUSTOM_TEXT_LOG = new TestTraceInfo("ExampleCustomTxt.log", "Custom Text : TmfGeneric", 10, "29:52.034");
59 private static final String TRACE_PROJECT_NAME = "test";
60 private static final String TRACE_NAME = CUSTOM_TEXT_LOG.getTraceName();
61 private static final String RENAMED_TRACE_NAME = TRACE_NAME + 2;
62
63 private static File fTestFile = null;
64
65 private static SWTWorkbenchBot fBot;
66
67 /** The Log4j logger instance. */
68 private static final Logger fLogger = Logger.getRootLogger();
69
70 private static final File TEST_TRACES_PATH = new File(new Path(TmfTraceManager.getTemporaryDirPath()).append("testtraces").toOSString());
71 private static String getPath(String relativePath) {
72 return new Path(TEST_TRACES_PATH.getAbsolutePath()).append(relativePath).toOSString();
73 }
74
75 /**
76 * Test Class setup
77 *
78 * @throws IOException
79 * on error
80 */
81 @BeforeClass
82 public static void init() throws IOException {
83 TestDirectoryStructureUtil.generateTraceStructure(TEST_TRACES_PATH);
84
85 SWTBotUtils.initialize();
86
87 // FIXME: We can't use Manage Custom Parsers > Import because it uses a native dialog. We'll still check that they show up in the dialog
88 CustomTxtTraceDefinition[] txtDefinitions = CustomTxtTraceDefinition.loadAll(getPath("customParsers/ExampleCustomTxtParser.xml"));
89 txtDefinitions[0].save();
90 /* set up test trace */
91 fTestFile = new File(getPath(new Path("import").append(CUSTOM_TEXT_LOG.getTracePath()).toString()));
92
93 assertTrue(fTestFile.exists());
94
95 /* Set up for swtbot */
96 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
97 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
98 fLogger.removeAllAppenders();
99 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
100 fBot = new SWTWorkbenchBot();
101
102 /* Close welcome view */
103 SWTBotUtils.closeView("Welcome", fBot);
104
105 /* Switch perspectives */
106 SWTBotUtils.switchToTracingPerspective();
107
108 /* Finish waiting for eclipse to load */
109 SWTBotUtils.waitForJobs();
110 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
111 }
112
113 /**
114 * Test class tear down method.
115 */
116 @AfterClass
117 public static void tearDown() {
118 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
119 fLogger.removeAllAppenders();
120 }
121
122 /**
123 * Test tear down method.
124 */
125 @After
126 public void afterTest() {
127 SWTBotUtils.closeSecondaryShells(fBot);
128 }
129
130 /**
131 * Test that the expected context menu items are there
132 * <p>
133 * Action : Trace menu
134 * <p>
135 * Procedure :Select an LTTng trace and open its context menu
136 * <p>
137 * Expected Results: Correct menu opens (Open , Copy, Rename, …)
138 *
139 */
140 @Test
141 public void test4_01ContextMenuPresence() {
142 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
143 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
144
145 final List<String> EXPECTED_MENU_LABELS = ImmutableList.of("Open", "Open With", "", "Copy...", "Rename...", "Delete", "", "Delete Supplementary Files...", "", "Export Trace Package...", "", "Select Trace Type...", "", "Apply Time Offset...",
146 "Clear Time Offset", "", "Refresh");
147 List<String> menuItems = traceItem.contextMenu().menuItems();
148 assertEquals(EXPECTED_MENU_LABELS, menuItems);
149
150 fBot.closeAllEditors();
151 }
152
153 /**
154 * Test that the trace opens with the context menu
155 * <p>
156 * Action : Open trace
157 * <p>
158 * Procedure :Select the Open menu
159 * <p>
160 * Expected Results: Trace is opened and views are populated
161 *
162 */
163 @Test
164 public void test4_02Open() {
165 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
166 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
167
168 traceItem.contextMenu().menu("Open").click();
169 testEventsTable(TRACE_NAME);
170 testStatisticsView();
171 fBot.closeAllEditors();
172 }
173
174 /**
175 * Test that the trace can be copied with the context menu
176 * <p>
177 * Action : Copy trace
178 * <p>
179 * Procedure :Select the Copy menu and provide a new name. Open.
180 * <p>
181 * Expected Results: Trace is replicated under the new name
182 *
183 */
184 @Test
185 public void test4_03Copy() {
186 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
187 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
188
189 createCopy(traceItem);
190
191 fBot.closeAllEditors();
192 SWTBotTreeItem copiedItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), RENAMED_TRACE_NAME);
193 copiedItem.contextMenu().menu("Open").click();
194 testEventsTable(RENAMED_TRACE_NAME);
195 fBot.closeAllEditors();
196 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
197 }
198
199 /**
200 * Test that the trace can be renamed with the context menu
201 * <p>
202 * Action : Rename trace
203 * <p>
204 * Procedure :Select the Rename menu and provide a new name. Reopen.
205 * <p>
206 * Expected Results: Trace is renamed. The trace editor is closed.
207 */
208 @Test
209 public void test4_04Rename() {
210 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
211 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
212
213 traceItem.contextMenu().menu("Rename...").click();
214 final String RENAME_TRACE_DIALOG_TITLE = "Rename Trace";
215 fBot.waitUntil(Conditions.shellIsActive(RENAME_TRACE_DIALOG_TITLE));
216 SWTBotShell shell = fBot.shell(RENAME_TRACE_DIALOG_TITLE);
217 SWTBotText text = shell.bot().textWithLabel("New Trace name:");
218 text.setText(RENAMED_TRACE_NAME);
219 shell.bot().button("OK").click();
220 fBot.waitUntil(Conditions.shellCloses(shell));
221 fBot.waitWhile(new ConditionHelpers.ActiveEventsEditor(fBot, null));
222
223 SWTBotTreeItem copiedItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), RENAMED_TRACE_NAME);
224 copiedItem.contextMenu().menu("Open").click();
225 testEventsTable(RENAMED_TRACE_NAME);
226 fBot.closeAllEditors();
227 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
228 }
229
230 /**
231 * Test that the trace can be deleted with the context menu
232 * <p>
233 * Action : Delete trace
234 * <p>
235 * Procedure :Select the Delete menu and confirm deletion
236 * <p>
237 * Expected Results: Trace is deleted. The trace editor is closed.
238 *
239 */
240 @Test
241 public void test4_05Delete() {
242 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
243 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
244
245 traceItem.contextMenu().menu("Delete").click();
246 final String DELETE_TRACE_DIALOG_TITLE = "Confirm Delete";
247 fBot.waitUntil(Conditions.shellIsActive(DELETE_TRACE_DIALOG_TITLE));
248 SWTBotShell shell = fBot.shell(DELETE_TRACE_DIALOG_TITLE);
249 shell.bot().button("Yes").click();
250 fBot.waitUntil(Conditions.shellCloses(shell));
251 fBot.waitWhile(new ConditionHelpers.ActiveEventsEditor(fBot, null));
252 fBot.waitUntil(new TraceDeletedCondition());
253 }
254
255 /**
256 * Test that the trace opens with the keyboard
257 * <p>
258 * Action : Open Trace (Accelerator)
259 * <p>
260 * Procedure :Select trace and press Enter
261 * <p>
262 * Expected Results: Trace is opened
263 *
264 *
265 * @throws WidgetNotFoundException
266 * when a widget is not found
267 */
268 @Test
269 public void test4_06OpenKeyboard() throws WidgetNotFoundException {
270 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
271 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
272 traceItem.select();
273 fBot.activeShell().pressShortcut(Keystrokes.CR);
274
275 testEventsTable(TRACE_NAME);
276 testStatisticsView();
277 fBot.closeAllEditors();
278 }
279
280 /**
281 * Test that the trace can be deleted with the keyboard
282 * <p>
283 * Action : Delete Trace (Accelerator)
284 * <p>
285 * Procedure :Select trace and press Delete and confirm deletion
286 * <p>
287 * Expected Results: Trace is deleted. The trace editor is closed.
288 */
289 @Test
290 public void test4_07DeleteKeyboard() {
291 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
292 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
293 traceItem.select();
294 fBot.activeShell().pressShortcut(Keystrokes.DELETE);
295 final String DELETE_TRACE_DIALOG_TITLE = "Confirm Delete";
296 fBot.waitUntil(Conditions.shellIsActive(DELETE_TRACE_DIALOG_TITLE));
297 SWTBotShell shell = fBot.shell(DELETE_TRACE_DIALOG_TITLE);
298 shell.bot().button("Yes").click();
299 fBot.waitUntil(Conditions.shellCloses(shell));
300 fBot.waitWhile(new ConditionHelpers.ActiveEventsEditor(fBot, null));
301 fBot.waitUntil(new TraceDeletedCondition());
302 }
303
304 /**
305 * Test that the trace opens with double-click
306 * <p>
307 * Action : Open Trace (double click)
308 * <p>
309 * Procedure :Double-click a trace
310 * <p>
311 * Expected Results: Trace is opened
312 *
313 * @throws WidgetNotFoundException
314 * when a widget is not found
315 */
316 @Test
317 public void test4_08OpenDoubleClick() throws WidgetNotFoundException {
318 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
319 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
320 traceItem.doubleClick();
321
322 testEventsTable(TRACE_NAME);
323 testStatisticsView();
324 fBot.closeAllEditors();
325 }
326
327 /**
328 * Test that the trace is brought to top if already opened
329 * <p>
330 * Action : Open Trace (already open)
331 * <p>
332 * Procedure :Open two traces. Open the first trace again.
333 * <p>
334 * Expected Results: The first trace editor is simply brought to front.
335 */
336 @Test
337 public void test4_09BringToTop() {
338 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
339 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
340 traceItem.doubleClick();
341 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, TRACE_NAME));
342 IEditorReference originalEditor = fBot.activeEditor().getReference();
343
344 createCopy(traceItem);
345
346 SWTBotTreeItem copiedItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), RENAMED_TRACE_NAME);
347 copiedItem.doubleClick();
348 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, RENAMED_TRACE_NAME));
349 SWTBotUtils.delay(1000);
350 traceItem.doubleClick();
351 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, TRACE_NAME));
352 assertTrue(originalEditor == fBot.activeEditor().getReference());
353
354 fBot.closeAllEditors();
355 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
356 }
357
358 private static void createCopy(SWTBotTreeItem traceItem) {
359 traceItem.contextMenu().menu("Copy...").click();
360 final String COPY_TRACE_DIALOG_TITLE = "Copy Trace";
361 fBot.waitUntil(Conditions.shellIsActive(COPY_TRACE_DIALOG_TITLE));
362 SWTBotShell shell = fBot.shell(COPY_TRACE_DIALOG_TITLE);
363 SWTBotText text = shell.bot().textWithLabel("New Trace name:");
364 text.setText(RENAMED_TRACE_NAME);
365 shell.bot().button("OK").click();
366 fBot.waitUntil(Conditions.shellCloses(shell));
367 }
368
369 private static void testEventsTable(String editorName) {
370 SWTBotEditor editor = SWTBotUtils.activeEventsEditor(fBot, editorName);
371 fBot.waitUntil(ConditionHelpers.numberOfEventsInTrace(TmfTraceManager.getInstance().getActiveTrace(), CUSTOM_TEXT_LOG.getNbEvents()));
372
373 SWTBotTable table = editor.bot().table();
374 fBot.waitUntil(new DefaultCondition() {
375 @Override
376 public boolean test() throws Exception {
377 return table.rowCount() > 1;
378 }
379
380 @Override
381 public String getFailureMessage() {
382 return "No items in table";
383 }
384 });
385 // Select first event (skip filter/search row)
386 table.getTableItem(1).select();
387
388 editor.bot().waitUntil(new DefaultCondition() {
389 @Override
390 public boolean test() throws Exception {
391 return table.selection().rowCount() == 1 && table.selection().get(0).toString().contains(CUSTOM_TEXT_LOG.getFirstEventTimestamp());
392 }
393
394 @Override
395 public String getFailureMessage() {
396 return "First event not selected";
397 }
398 });
399 }
400
401 private static void testStatisticsView() {
402 SWTBotUtils.openView(TmfStatisticsView.ID);
403 SWTBotView view = fBot.viewById(TmfStatisticsView.ID);
404 assertTrue(view.bot().tree().hasItems());
405 view.bot().tree().cell(0, 1).equals(Long.toString(CUSTOM_TEXT_LOG.getNbEvents()));
406 }
407
408 private final class TraceDeletedCondition extends DefaultCondition {
409 @Override
410 public boolean test() throws Exception {
411 return ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME).findMember(new Path("Traces/" + TRACE_NAME)) == null;
412 }
413
414 @Override
415 public String getFailureMessage() {
416 return TRACE_NAME + " was not deleted successfully.";
417 }
418 }
419 }
This page took 0.039315 seconds and 4 git commands to generate.