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
.views
.statistics
.TmfStatisticsView
;
43 import org
.eclipse
.ui
.IEditorReference
;
44 import org
.junit
.After
;
45 import org
.junit
.AfterClass
;
46 import org
.junit
.BeforeClass
;
47 import org
.junit
.Test
;
48 import org
.junit
.runner
.RunWith
;
50 import com
.google
.common
.collect
.ImmutableList
;
53 * SWTBot test for testing Project Explorer Trace actions (context-menus,
56 @RunWith(SWTBotJunit4ClassRunner
.class)
57 public class ProjectExplorerTraceActionsTest
{
58 private static @NonNull TestTraceInfo CUSTOM_TEXT_LOG
= new TestTraceInfo("ExampleCustomTxt.log", "Custom Text : TmfGeneric", 10, "29:52.034");
59 private static final String TRACE_PROJECT_NAME
= "test";
60 private static final String TRACE_NAME
= CUSTOM_TEXT_LOG
.getTraceName();
61 private static final String RENAMED_TRACE_NAME
= TRACE_NAME
+ 2;
63 private static File fTestFile
= null;
65 private static SWTWorkbenchBot fBot
;
67 /** The Log4j logger instance. */
68 private static final Logger fLogger
= Logger
.getRootLogger();
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();
82 public static void init() throws IOException
{
83 TestDirectoryStructureUtil
.generateTraceStructure(TEST_TRACES_PATH
);
85 SWTBotUtils
.initialize();
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();
90 /* set up test trace */
91 fTestFile
= new File(getPath(new Path("import").append(CUSTOM_TEXT_LOG
.getTracePath()).toString()));
93 assertTrue(fTestFile
.exists());
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();
102 /* Close welcome view */
103 SWTBotUtils
.closeView("Welcome", fBot
);
105 /* Switch perspectives */
106 SWTBotUtils
.switchToTracingPerspective();
108 /* Finish waiting for eclipse to load */
109 SWTBotUtils
.waitForJobs();
110 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
114 * Test class tear down method.
117 public static void tearDown() {
118 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
119 fLogger
.removeAllAppenders();
123 * Test tear down method.
126 public void afterTest() {
127 SWTBotUtils
.closeSecondaryShells(fBot
);
131 * Test that the expected context menu items are there
133 * Action : Trace menu
135 * Procedure :Select an LTTng trace and open its context menu
137 * Expected Results: Correct menu opens (Open , Copy, Rename, …)
141 public void test4_01ContextMenuPresence() {
142 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
143 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
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
);
150 fBot
.closeAllEditors();
154 * Test that the trace opens with the context menu
156 * Action : Open trace
158 * Procedure :Select the Open menu
160 * Expected Results: Trace is opened and views are populated
164 public void test4_02Open() {
165 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
166 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
168 traceItem
.contextMenu().menu("Open").click();
169 testEventsTable(TRACE_NAME
);
170 testStatisticsView();
171 fBot
.closeAllEditors();
175 * Test that the trace can be copied with the context menu
177 * Action : Copy trace
179 * Procedure :Select the Copy menu and provide a new name. Open.
181 * Expected Results: Trace is replicated under the new name
185 public void test4_03Copy() {
186 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
187 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
189 createCopy(traceItem
);
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();
196 SWTBotUtils
.clearTracesFolderUI(fBot
, TRACE_PROJECT_NAME
);
200 * Test that the trace can be renamed with the context menu
202 * Action : Rename trace
204 * Procedure :Select the Rename menu and provide a new name. Reopen.
206 * Expected Results: Trace is renamed. The trace editor is closed.
209 public void test4_04Rename() {
210 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
211 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
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));
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();
227 SWTBotUtils
.clearTracesFolderUI(fBot
, TRACE_PROJECT_NAME
);
231 * Test that the trace can be deleted with the context menu
233 * Action : Delete trace
235 * Procedure :Select the Delete menu and confirm deletion
237 * Expected Results: Trace is deleted. The trace editor is closed.
241 public void test4_05Delete() {
242 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
243 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
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));
252 fBot
.waitUntil(new TraceDeletedCondition());
256 * Test that the trace opens with the keyboard
258 * Action : Open Trace (Accelerator)
260 * Procedure :Select trace and press Enter
262 * Expected Results: Trace is opened
265 * @throws WidgetNotFoundException
266 * when a widget is not found
269 public void test4_06OpenKeyboard() throws WidgetNotFoundException
{
270 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
271 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
273 fBot
.activeShell().pressShortcut(Keystrokes
.CR
);
275 testEventsTable(TRACE_NAME
);
276 testStatisticsView();
277 fBot
.closeAllEditors();
281 * Test that the trace can be deleted with the keyboard
283 * Action : Delete Trace (Accelerator)
285 * Procedure :Select trace and press Delete and confirm deletion
287 * Expected Results: Trace is deleted. The trace editor is closed.
290 public void test4_07DeleteKeyboard() {
291 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
292 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
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));
301 fBot
.waitUntil(new TraceDeletedCondition());
305 * Test that the trace opens with double-click
307 * Action : Open Trace (double click)
309 * Procedure :Double-click a trace
311 * Expected Results: Trace is opened
313 * @throws WidgetNotFoundException
314 * when a widget is not found
317 public void test4_08OpenDoubleClick() throws WidgetNotFoundException
{
318 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
319 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
320 traceItem
.doubleClick();
322 testEventsTable(TRACE_NAME
);
323 testStatisticsView();
324 fBot
.closeAllEditors();
328 * Test that the trace is brought to top if already opened
330 * Action : Open Trace (already open)
332 * Procedure :Open two traces. Open the first trace again.
334 * Expected Results: The first trace editor is simply brought to front.
337 public void test4_09BringToTop() {
338 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), CUSTOM_TEXT_LOG
.getTraceType());
339 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
340 traceItem
.doubleClick();
341 fBot
.waitUntil(new ConditionHelpers
.ActiveEventsEditor(fBot
, TRACE_NAME
));
342 IEditorReference originalEditor
= fBot
.activeEditor().getReference();
344 createCopy(traceItem
);
346 SWTBotTreeItem copiedItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), RENAMED_TRACE_NAME
);
347 copiedItem
.doubleClick();
348 fBot
.waitUntil(new ConditionHelpers
.ActiveEventsEditor(fBot
, RENAMED_TRACE_NAME
));
349 SWTBotUtils
.delay(1000);
350 traceItem
.doubleClick();
351 fBot
.waitUntil(new ConditionHelpers
.ActiveEventsEditor(fBot
, TRACE_NAME
));
352 assertTrue(originalEditor
== fBot
.activeEditor().getReference());
354 fBot
.closeAllEditors();
355 SWTBotUtils
.clearTracesFolderUI(fBot
, TRACE_PROJECT_NAME
);
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
));
369 private static void testEventsTable(String editorName
) {
370 SWTBotEditor editor
= SWTBotUtils
.activeEventsEditor(fBot
, editorName
);
371 fBot
.waitUntil(ConditionHelpers
.numberOfEventsInTrace(TmfTraceManager
.getInstance().getActiveTrace(), CUSTOM_TEXT_LOG
.getNbEvents()));
373 SWTBotTable table
= editor
.bot().table();
374 fBot
.waitUntil(new DefaultCondition() {
376 public boolean test() throws Exception
{
377 return table
.rowCount() > 1;
381 public String
getFailureMessage() {
382 return "No items in table";
385 // Select first event (skip filter/search row)
386 table
.getTableItem(1).select();
388 editor
.bot().waitUntil(new DefaultCondition() {
390 public boolean test() throws Exception
{
391 return table
.selection().rowCount() == 1 && table
.selection().get(0).toString().contains(CUSTOM_TEXT_LOG
.getFirstEventTimestamp());
395 public String
getFailureMessage() {
396 return "First event not selected";
401 private static void testStatisticsView() {
402 SWTBotUtils
.openView(TmfStatisticsView
.ID
);
403 SWTBotView view
= fBot
.viewById(TmfStatisticsView
.ID
);
404 assertTrue(view
.bot().tree().hasItems());
405 view
.bot().tree().cell(0, 1).equals(Long
.toString(CUSTOM_TEXT_LOG
.getNbEvents()));
408 private final class TraceDeletedCondition
extends DefaultCondition
{
410 public boolean test() throws Exception
{
411 return ResourcesPlugin
.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME
).findMember(new Path("Traces/" + TRACE_NAME
)) == null;
415 public String
getFailureMessage() {
416 return TRACE_NAME
+ " was not deleted successfully.";