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
CommitLineData
e834a6b4
MAL
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
a4f6f73b 10package org.eclipse.tracecompass.integration.swtbot.tests.projectexplorer;
e834a6b4
MAL
11
12import static org.junit.Assert.assertEquals;
13import static org.junit.Assert.assertTrue;
e834a6b4
MAL
14
15import java.io.File;
16import java.io.IOException;
e834a6b4 17import java.util.List;
e834a6b4
MAL
18
19import org.apache.log4j.ConsoleAppender;
20import org.apache.log4j.Logger;
21import org.apache.log4j.SimpleLayout;
e9a570ab 22import org.eclipse.core.resources.ResourcesPlugin;
e834a6b4
MAL
23import org.eclipse.core.runtime.Path;
24import org.eclipse.jdt.annotation.NonNull;
e834a6b4
MAL
25import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
26import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
27import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
28import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
e834a6b4
MAL
29import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
30import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
31import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
32import org.eclipse.swtbot.swt.finder.waits.Conditions;
33import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
34import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
37import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
a4f6f73b 38import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
e834a6b4
MAL
39import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
40import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
41import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
f0beeb4a 42import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
e834a6b4
MAL
43import org.eclipse.tracecompass.tmf.ui.views.statistics.TmfStatisticsView;
44import org.eclipse.ui.IEditorReference;
1dfcd42b 45import org.junit.After;
e834a6b4
MAL
46import org.junit.AfterClass;
47import org.junit.BeforeClass;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50
51import 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)
58public class ProjectExplorerTraceActionsTest {
a4f6f73b 59 private static @NonNull TestTraceInfo CUSTOM_TEXT_LOG = new TestTraceInfo("ExampleCustomTxt.log", "Custom Text : TmfGeneric", 10, "29:52.034");
e834a6b4 60 private static final String TRACE_PROJECT_NAME = "test";
a4f6f73b 61 private static final String TRACE_NAME = CUSTOM_TEXT_LOG.getTraceName();
e834a6b4 62 private static final String RENAMED_TRACE_NAME = TRACE_NAME + 2;
e834a6b4
MAL
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();
a4f6f73b
MAL
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 }
e834a6b4
MAL
75
76 /**
77 * Test Class setup
a4f6f73b
MAL
78 *
79 * @throws IOException
80 * on error
e834a6b4
MAL
81 */
82 @BeforeClass
a4f6f73b
MAL
83 public static void init() throws IOException {
84 TestDirectoryStructureUtil.generateTraceStructure(TEST_TRACES_PATH);
85
e834a6b4
MAL
86 SWTBotUtils.initialize();
87
a4f6f73b
MAL
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();
e834a6b4 91 /* set up test trace */
a4f6f73b 92 fTestFile = new File(getPath(new Path("import").append(CUSTOM_TEXT_LOG.getTracePath()).toString()));
e834a6b4
MAL
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 */
f0beeb4a 110 WaitUtils.waitForJobs();
e834a6b4
MAL
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
1dfcd42b
MAL
123 /**
124 * Test tear down method.
125 */
126 @After
127 public void afterTest() {
128 SWTBotUtils.closeSecondaryShells(fBot);
129 }
130
e834a6b4
MAL
131 /**
132 * Test that the expected context menu items are there
bb92c458
MK
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 *
e834a6b4
MAL
140 */
141 @Test
142 public void test4_01ContextMenuPresence() {
a4f6f73b 143 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
144 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
145
a4f6f73b
MAL
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);
e834a6b4
MAL
150
151 fBot.closeAllEditors();
152 }
153
154 /**
155 * Test that the trace opens with the context menu
bb92c458
MK
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 *
e834a6b4
MAL
163 */
164 @Test
165 public void test4_02Open() {
a4f6f73b 166 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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
bb92c458
MK
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 *
e834a6b4
MAL
184 */
185 @Test
186 public void test4_03Copy() {
a4f6f73b 187 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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();
450daec8 197 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
e834a6b4
MAL
198 }
199
200 /**
201 * Test that the trace can be renamed with the context menu
bb92c458
MK
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.
e834a6b4
MAL
208 */
209 @Test
210 public void test4_04Rename() {
a4f6f73b 211 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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();
450daec8 228 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
e834a6b4
MAL
229 }
230
231 /**
232 * Test that the trace can be deleted with the context menu
bb92c458
MK
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 *
e834a6b4
MAL
240 */
241 @Test
242 public void test4_05Delete() {
a4f6f73b 243 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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));
e9a570ab 253 fBot.waitUntil(new TraceDeletedCondition());
e834a6b4
MAL
254 }
255
256 /**
257 * Test that the trace opens with the keyboard
bb92c458
MK
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 *
e834a6b4
MAL
265 *
266 * @throws WidgetNotFoundException
267 * when a widget is not found
268 */
269 @Test
270 public void test4_06OpenKeyboard() throws WidgetNotFoundException {
a4f6f73b 271 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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
bb92c458
MK
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.
e834a6b4
MAL
289 */
290 @Test
291 public void test4_07DeleteKeyboard() {
a4f6f73b 292 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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));
e9a570ab 302 fBot.waitUntil(new TraceDeletedCondition());
e834a6b4
MAL
303 }
304
305 /**
306 * Test that the trace opens with double-click
bb92c458
MK
307 * <p>
308 * Action : Open Trace (double click)
309 * <p>
310 * Procedure :Double-click a trace
311 * <p>
312 * Expected Results: Trace is opened
e834a6b4
MAL
313 *
314 * @throws WidgetNotFoundException
315 * when a widget is not found
316 */
317 @Test
318 public void test4_08OpenDoubleClick() throws WidgetNotFoundException {
a4f6f73b 319 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4 320 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
e834a6b4
MAL
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
bb92c458
MK
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.
e834a6b4
MAL
336 */
337 @Test
338 public void test4_09BringToTop() {
a4f6f73b 339 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4 340 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
e834a6b4
MAL
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);
e834a6b4
MAL
348 copiedItem.doubleClick();
349 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, RENAMED_TRACE_NAME));
350 SWTBotUtils.delay(1000);
e834a6b4
MAL
351 traceItem.doubleClick();
352 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, TRACE_NAME));
353 assertTrue(originalEditor == fBot.activeEditor().getReference());
354
355 fBot.closeAllEditors();
450daec8 356 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
e834a6b4
MAL
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);
a4f6f73b 372 fBot.waitUntil(ConditionHelpers.numberOfEventsInTrace(TmfTraceManager.getInstance().getActiveTrace(), CUSTOM_TEXT_LOG.getNbEvents()));
e834a6b4
MAL
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 {
a4f6f73b 392 return table.selection().rowCount() == 1 && table.selection().get(0).toString().contains(CUSTOM_TEXT_LOG.getFirstEventTimestamp());
e834a6b4
MAL
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());
a4f6f73b 406 view.bot().tree().cell(0, 1).equals(Long.toString(CUSTOM_TEXT_LOG.getNbEvents()));
e834a6b4 407 }
e9a570ab
MAL
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 }
e834a6b4 420}
This page took 0.046604 seconds and 5 git commands to generate.