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