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