1 /******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.integration
.swtbot
.tests
.projectexplorer
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertTrue
;
16 import java
.io
.IOException
;
17 import java
.util
.List
;
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
;
51 import com
.google
.common
.collect
.ImmutableList
;
54 * SWTBot test for testing Project Explorer Trace actions (context-menus,
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;
64 private static File fTestFile
= null;
66 private static SWTWorkbenchBot fBot
;
68 /** The Log4j logger instance. */
69 private static final Logger fLogger
= Logger
.getRootLogger();
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();
83 public static void init() throws IOException
{
84 TestDirectoryStructureUtil
.generateTraceStructure(TEST_TRACES_PATH
);
86 SWTBotUtils
.initialize();
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()));
94 assertTrue(fTestFile
.exists());
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();
103 /* Close welcome view */
104 SWTBotUtils
.closeView("Welcome", fBot
);
106 /* Switch perspectives */
107 SWTBotUtils
.switchToTracingPerspective();
109 /* Finish waiting for eclipse to load */
110 WaitUtils
.waitForJobs();
111 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
115 * Test class tear down method.
118 public static void tearDown() {
119 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
120 fLogger
.removeAllAppenders();
124 * Test tear down method.
127 public void afterTest() {
128 SWTBotUtils
.closeSecondaryShells(fBot
);
132 * Test that the expected context menu items are there
134 * Action : Trace menu
136 * Procedure :Select an LTTng trace and open its context menu
138 * Expected Results: Correct menu opens (Open , Copy, Rename, …)
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
);
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
);
151 fBot
.closeAllEditors();
155 * Test that the trace opens with the context menu
157 * Action : Open trace
159 * Procedure :Select the Open menu
161 * Expected Results: Trace is opened and views are populated
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
);
169 traceItem
.contextMenu().menu("Open").click();
170 testEventsTable(TRACE_NAME
);
171 testStatisticsView();
172 fBot
.closeAllEditors();
176 * Test that the trace can be copied with the context menu
178 * Action : Copy trace
180 * Procedure :Select the Copy menu and provide a new name. Open.
182 * Expected Results: Trace is replicated under the new name
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
);
190 createCopy(traceItem
);
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
);
201 * Test that the trace can be renamed with the context menu
203 * Action : Rename trace
205 * Procedure :Select the Rename menu and provide a new name. Reopen.
207 * Expected Results: Trace is renamed. The trace editor is closed.
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
);
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));
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
);
232 * Test that the trace can be deleted with the context menu
234 * Action : Delete trace
236 * Procedure :Select the Delete menu and confirm deletion
238 * Expected Results: Trace is deleted. The trace editor is closed.
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
);
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());
257 * Test that the trace opens with the keyboard
259 * Action : Open Trace (Accelerator)
261 * Procedure :Select trace and press Enter
263 * Expected Results: Trace is opened
266 * @throws WidgetNotFoundException
267 * when a widget is not found
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
);
274 fBot
.activeShell().pressShortcut(Keystrokes
.CR
);
276 testEventsTable(TRACE_NAME
);
277 testStatisticsView();
278 fBot
.closeAllEditors();
282 * Test that the trace can be deleted with the keyboard
284 * Action : Delete Trace (Accelerator)
286 * Procedure :Select trace and press Delete and confirm deletion
288 * Expected Results: Trace is deleted. The trace editor is closed.
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
);
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());
306 * Test that the trace opens with double-click
308 * Action : Open Trace (double click)
310 * Procedure :Double-click a trace
312 * Expected Results: Trace is opened
314 * @throws WidgetNotFoundException
315 * when a widget is not found
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();
323 testEventsTable(TRACE_NAME
);
324 testStatisticsView();
325 fBot
.closeAllEditors();
329 * Test that the trace is brought to top if already opened
331 * Action : Open Trace (already open)
333 * Procedure :Open two traces. Open the first trace again.
335 * Expected Results: The first trace editor is simply brought to front.
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();
345 createCopy(traceItem
);
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());
355 fBot
.closeAllEditors();
356 SWTBotUtils
.clearTracesFolderUI(fBot
, TRACE_PROJECT_NAME
);
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
));
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()));
374 SWTBotTable table
= editor
.bot().table();
375 fBot
.waitUntil(new DefaultCondition() {
377 public boolean test() throws Exception
{
378 return table
.rowCount() > 1;
382 public String
getFailureMessage() {
383 return "No items in table";
386 // Select first event (skip filter/search row)
387 table
.getTableItem(1).select();
389 editor
.bot().waitUntil(new DefaultCondition() {
391 public boolean test() throws Exception
{
392 return table
.selection().rowCount() == 1 && table
.selection().get(0).toString().contains(CUSTOM_TEXT_LOG
.getFirstEventTimestamp());
396 public String
getFailureMessage() {
397 return "First event not selected";
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()));
409 private final class TraceDeletedCondition
extends DefaultCondition
{
411 public boolean test() throws Exception
{
412 return ResourcesPlugin
.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME
).findMember(new Path("Traces/" + TRACE_NAME
)) == null;
416 public String
getFailureMessage() {
417 return TRACE_NAME
+ " was not deleted successfully.";