Add utility method to close secondary shells after 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;
42import org.eclipse.tracecompass.tmf.ui.views.statistics.TmfStatisticsView;
43import org.eclipse.ui.IEditorReference;
1dfcd42b 44import org.junit.After;
e834a6b4
MAL
45import org.junit.AfterClass;
46import org.junit.BeforeClass;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49
50import 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)
57public class ProjectExplorerTraceActionsTest {
a4f6f73b 58 private static @NonNull TestTraceInfo CUSTOM_TEXT_LOG = new TestTraceInfo("ExampleCustomTxt.log", "Custom Text : TmfGeneric", 10, "29:52.034");
e834a6b4 59 private static final String TRACE_PROJECT_NAME = "test";
a4f6f73b 60 private static final String TRACE_NAME = CUSTOM_TEXT_LOG.getTraceName();
e834a6b4 61 private static final String RENAMED_TRACE_NAME = TRACE_NAME + 2;
e834a6b4
MAL
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();
a4f6f73b
MAL
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 }
e834a6b4
MAL
74
75 /**
76 * Test Class setup
a4f6f73b
MAL
77 *
78 * @throws IOException
79 * on error
e834a6b4
MAL
80 */
81 @BeforeClass
a4f6f73b
MAL
82 public static void init() throws IOException {
83 TestDirectoryStructureUtil.generateTraceStructure(TEST_TRACES_PATH);
84
e834a6b4
MAL
85 SWTBotUtils.initialize();
86
a4f6f73b
MAL
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();
e834a6b4 90 /* set up test trace */
a4f6f73b 91 fTestFile = new File(getPath(new Path("import").append(CUSTOM_TEXT_LOG.getTracePath()).toString()));
e834a6b4
MAL
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
1dfcd42b
MAL
122 /**
123 * Test tear down method.
124 */
125 @After
126 public void afterTest() {
127 SWTBotUtils.closeSecondaryShells(fBot);
128 }
129
e834a6b4
MAL
130 /**
131 * Test that the expected context menu items are there
bb92c458
MK
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 *
e834a6b4
MAL
139 */
140 @Test
141 public void test4_01ContextMenuPresence() {
a4f6f73b 142 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
143 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
144
a4f6f73b
MAL
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);
e834a6b4
MAL
149
150 fBot.closeAllEditors();
151 }
152
153 /**
154 * Test that the trace opens with the context menu
bb92c458
MK
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 *
e834a6b4
MAL
162 */
163 @Test
164 public void test4_02Open() {
a4f6f73b 165 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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
bb92c458
MK
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 *
e834a6b4
MAL
183 */
184 @Test
185 public void test4_03Copy() {
a4f6f73b 186 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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();
450daec8 196 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
e834a6b4
MAL
197 }
198
199 /**
200 * Test that the trace can be renamed with the context menu
bb92c458
MK
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.
e834a6b4
MAL
207 */
208 @Test
209 public void test4_04Rename() {
a4f6f73b 210 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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();
450daec8 227 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
e834a6b4
MAL
228 }
229
230 /**
231 * Test that the trace can be deleted with the context menu
bb92c458
MK
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 *
e834a6b4
MAL
239 */
240 @Test
241 public void test4_05Delete() {
a4f6f73b 242 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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));
e9a570ab 252 fBot.waitUntil(new TraceDeletedCondition());
e834a6b4
MAL
253 }
254
255 /**
256 * Test that the trace opens with the keyboard
bb92c458
MK
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 *
e834a6b4
MAL
264 *
265 * @throws WidgetNotFoundException
266 * when a widget is not found
267 */
268 @Test
269 public void test4_06OpenKeyboard() throws WidgetNotFoundException {
a4f6f73b 270 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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
bb92c458
MK
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.
e834a6b4
MAL
288 */
289 @Test
290 public void test4_07DeleteKeyboard() {
a4f6f73b 291 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4
MAL
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));
e9a570ab 301 fBot.waitUntil(new TraceDeletedCondition());
e834a6b4
MAL
302 }
303
304 /**
305 * Test that the trace opens with double-click
bb92c458
MK
306 * <p>
307 * Action : Open Trace (double click)
308 * <p>
309 * Procedure :Double-click a trace
310 * <p>
311 * Expected Results: Trace is opened
e834a6b4
MAL
312 *
313 * @throws WidgetNotFoundException
314 * when a widget is not found
315 */
316 @Test
317 public void test4_08OpenDoubleClick() throws WidgetNotFoundException {
a4f6f73b 318 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4 319 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
e834a6b4
MAL
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
bb92c458
MK
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.
e834a6b4
MAL
335 */
336 @Test
337 public void test4_09BringToTop() {
a4f6f73b 338 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), CUSTOM_TEXT_LOG.getTraceType());
e834a6b4 339 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME), TRACE_NAME);
e834a6b4
MAL
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);
e834a6b4
MAL
347 copiedItem.doubleClick();
348 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, RENAMED_TRACE_NAME));
349 SWTBotUtils.delay(1000);
e834a6b4
MAL
350 traceItem.doubleClick();
351 fBot.waitUntil(new ConditionHelpers.ActiveEventsEditor(fBot, TRACE_NAME));
352 assertTrue(originalEditor == fBot.activeEditor().getReference());
353
354 fBot.closeAllEditors();
450daec8 355 SWTBotUtils.clearTracesFolderUI(fBot, TRACE_PROJECT_NAME);
e834a6b4
MAL
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);
a4f6f73b 371 fBot.waitUntil(ConditionHelpers.numberOfEventsInTrace(TmfTraceManager.getInstance().getActiveTrace(), CUSTOM_TEXT_LOG.getNbEvents()));
e834a6b4
MAL
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 {
a4f6f73b 391 return table.selection().rowCount() == 1 && table.selection().get(0).toString().contains(CUSTOM_TEXT_LOG.getFirstEventTimestamp());
e834a6b4
MAL
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());
a4f6f73b 405 view.bot().tree().cell(0, 1).equals(Long.toString(CUSTOM_TEXT_LOG.getNbEvents()));
e834a6b4 406 }
e9a570ab
MAL
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 }
e834a6b4 419}
This page took 0.047746 seconds and 5 git commands to generate.