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
.tmf
.ui
.swtbot
.tests
.projectexplorer
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertTrue
;
14 import static org
.junit
.Assert
.fail
;
17 import java
.io
.IOException
;
19 import java
.net
.URISyntaxException
;
21 import java
.util
.List
;
22 import java
.util
.stream
.Collectors
;
24 import org
.apache
.log4j
.ConsoleAppender
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.apache
.log4j
.SimpleLayout
;
27 import org
.eclipse
.core
.runtime
.FileLocator
;
28 import org
.eclipse
.core
.runtime
.Path
;
29 import org
.eclipse
.jdt
.annotation
.NonNull
;
30 import org
.eclipse
.swt
.widgets
.MenuItem
;
31 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
32 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
33 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
34 import org
.eclipse
.swtbot
.swt
.finder
.exceptions
.WidgetNotFoundException
;
35 import org
.eclipse
.swtbot
.swt
.finder
.finders
.ContextMenuFinder
;
36 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
37 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
38 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
39 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
40 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
41 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
42 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotShell
;
43 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
44 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotText
;
45 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.statistics
.TmfStatisticsView
;
51 import org
.eclipse
.ui
.IEditorReference
;
52 import org
.hamcrest
.core
.IsAnything
;
53 import org
.junit
.AfterClass
;
54 import org
.junit
.BeforeClass
;
55 import org
.junit
.Test
;
56 import org
.junit
.runner
.RunWith
;
58 import com
.google
.common
.collect
.ImmutableList
;
61 * SWTBot test for testing Project Explorer Trace actions (context-menus,
64 @RunWith(SWTBotJunit4ClassRunner
.class)
65 public class ProjectExplorerTraceActionsTest
{
66 private static final String TRACE_PROJECT_NAME
= "test";
67 private static final String TRACE_NAME
= "syslog_collapse";
68 private static final String RENAMED_TRACE_NAME
= TRACE_NAME
+ 2;
69 private static final String TRACE_PATH
= "testfiles/" + TRACE_NAME
;
70 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
72 private static File fTestFile
= null;
74 private static SWTWorkbenchBot fBot
;
76 /** The Log4j logger instance. */
77 private static final Logger fLogger
= Logger
.getRootLogger();
78 private static final long NB_EVENTS
= 22;
84 public static void init() {
85 SWTBotUtils
.initialize();
87 /* set up test trace */
88 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TRACE_PATH
), null);
91 uri
= FileLocator
.toFileURL(location
).toURI();
92 fTestFile
= new File(uri
);
93 } catch (URISyntaxException
| IOException e
) {
98 assertTrue(fTestFile
.exists());
100 /* Set up for swtbot */
101 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
102 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
103 fLogger
.removeAllAppenders();
104 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
105 fBot
= new SWTWorkbenchBot();
107 /* Close welcome view */
108 SWTBotUtils
.closeView("Welcome", fBot
);
110 /* Switch perspectives */
111 SWTBotUtils
.switchToTracingPerspective();
113 /* Finish waiting for eclipse to load */
114 SWTBotUtils
.waitForJobs();
115 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
119 * Test class tear down method.
122 public static void tearDown() {
123 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
124 fLogger
.removeAllAppenders();
128 * Test that the expected context menu items are there
131 public void test4_01ContextMenuPresence() {
132 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
133 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
135 final List
<String
> EXPECTED_MENU_LABELS
= ImmutableList
.of(
136 "&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", "Refresh\tF5");
138 // TODO: SWTBot needs a better way to do this
139 ContextMenuFinder finder
= new ContextMenuFinder(fBot
.tree().widget
);
140 List
<MenuItem
> menuItems
= finder
.findMenus(traceItem
.contextMenu().widget
, new IsAnything
<>(), false);
142 List
<String
> menuLabels
= menuItems
.stream().map((item
) -> {
143 return UIThreadRunnable
.syncExec(() -> item
.getText());
144 }).collect(Collectors
.toList());
145 assertEquals(EXPECTED_MENU_LABELS
, menuLabels
);
147 fBot
.closeAllEditors();
151 * Test that the trace opens with the context menu
154 public void test4_02Open() {
155 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
156 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
158 traceItem
.contextMenu().menu("Open").click();
159 testEventsTable(TRACE_NAME
);
160 testStatisticsView();
161 fBot
.closeAllEditors();
165 * Test that the trace can be copied with the context menu
168 public void test4_03Copy() {
169 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
170 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
172 createCopy(traceItem
);
174 fBot
.closeAllEditors();
175 SWTBotTreeItem copiedItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), RENAMED_TRACE_NAME
);
176 copiedItem
.contextMenu().menu("Open").click();
177 testEventsTable(RENAMED_TRACE_NAME
);
178 fBot
.closeAllEditors();
179 SWTBotUtils
.clearTracesFolder(fBot
, TRACE_PROJECT_NAME
);
183 * Test that the trace can be renamed with the context menu
186 public void test4_04Rename() {
187 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
188 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
190 traceItem
.contextMenu().menu("Rename...").click();
191 final String RENAME_TRACE_DIALOG_TITLE
= "Rename Trace";
192 fBot
.waitUntil(Conditions
.shellIsActive(RENAME_TRACE_DIALOG_TITLE
));
193 SWTBotShell shell
= fBot
.shell(RENAME_TRACE_DIALOG_TITLE
);
194 SWTBotText text
= shell
.bot().textWithLabel("New Trace name:");
195 text
.setText(RENAMED_TRACE_NAME
);
196 shell
.bot().button("OK").click();
197 fBot
.waitUntil(Conditions
.shellCloses(shell
));
198 fBot
.waitWhile(new ConditionHelpers
.ActiveEventsEditor(fBot
, null));
200 SWTBotTreeItem copiedItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), RENAMED_TRACE_NAME
);
201 copiedItem
.contextMenu().menu("Open").click();
202 testEventsTable(RENAMED_TRACE_NAME
);
203 fBot
.closeAllEditors();
204 SWTBotUtils
.clearTracesFolder(fBot
, TRACE_PROJECT_NAME
);
208 * Test that the trace can be deleted with the context menu
211 public void test4_05Delete() {
212 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
213 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
215 traceItem
.contextMenu().menu("Delete").click();
216 final String DELETE_TRACE_DIALOG_TITLE
= "Confirm Delete";
217 fBot
.waitUntil(Conditions
.shellIsActive(DELETE_TRACE_DIALOG_TITLE
));
218 SWTBotShell shell
= fBot
.shell(DELETE_TRACE_DIALOG_TITLE
);
219 shell
.bot().button("Yes").click();
220 fBot
.waitUntil(Conditions
.shellCloses(shell
));
221 fBot
.waitWhile(new ConditionHelpers
.ActiveEventsEditor(fBot
, null));
225 * Test that the trace opens with the keyboard
227 * @throws WidgetNotFoundException
228 * when a widget is not found
231 public void test4_06OpenKeyboard() throws WidgetNotFoundException
{
232 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
233 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
235 fBot
.activeShell().pressShortcut(Keystrokes
.CR
);
237 testEventsTable(TRACE_NAME
);
238 testStatisticsView();
239 fBot
.closeAllEditors();
243 * Test that the trace can be deleted with the keyboard
246 public void test4_07DeleteKeyboard() {
247 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
248 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
250 fBot
.activeShell().pressShortcut(Keystrokes
.DELETE
);
251 final String DELETE_TRACE_DIALOG_TITLE
= "Confirm Delete";
252 fBot
.waitUntil(Conditions
.shellIsActive(DELETE_TRACE_DIALOG_TITLE
));
253 SWTBotShell shell
= fBot
.shell(DELETE_TRACE_DIALOG_TITLE
);
254 shell
.bot().button("Yes").click();
255 fBot
.waitUntil(Conditions
.shellCloses(shell
));
256 fBot
.waitWhile(new ConditionHelpers
.ActiveEventsEditor(fBot
, null));
260 * Test that the trace opens with double-click
262 * @throws WidgetNotFoundException
263 * when a widget is not found
266 public void test4_08OpenDoubleClick() throws WidgetNotFoundException
{
267 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
268 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
270 traceItem
.doubleClick();
272 testEventsTable(TRACE_NAME
);
273 testStatisticsView();
274 fBot
.closeAllEditors();
278 * Test that the trace is brought to top if already opened
281 public void test4_09BringToTop() {
282 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
283 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), TRACE_NAME
);
285 traceItem
.doubleClick();
286 fBot
.waitUntil(new ConditionHelpers
.ActiveEventsEditor(fBot
, TRACE_NAME
));
287 IEditorReference originalEditor
= fBot
.activeEditor().getReference();
289 createCopy(traceItem
);
291 SWTBotTreeItem copiedItem
= SWTBotUtils
.getTraceProjectItem(fBot
, SWTBotUtils
.selectTracesFolder(fBot
, TRACE_PROJECT_NAME
), RENAMED_TRACE_NAME
);
293 copiedItem
.doubleClick();
294 copiedItem
.doubleClick();
295 fBot
.waitUntil(new ConditionHelpers
.ActiveEventsEditor(fBot
, RENAMED_TRACE_NAME
));
296 SWTBotUtils
.delay(1000);
298 traceItem
.doubleClick();
299 fBot
.waitUntil(new ConditionHelpers
.ActiveEventsEditor(fBot
, TRACE_NAME
));
300 assertTrue(originalEditor
== fBot
.activeEditor().getReference());
302 fBot
.closeAllEditors();
303 SWTBotUtils
.clearTracesFolder(fBot
, TRACE_PROJECT_NAME
);
306 private static void createCopy(SWTBotTreeItem traceItem
) {
307 traceItem
.contextMenu().menu("Copy...").click();
308 final String COPY_TRACE_DIALOG_TITLE
= "Copy Trace";
309 fBot
.waitUntil(Conditions
.shellIsActive(COPY_TRACE_DIALOG_TITLE
));
310 SWTBotShell shell
= fBot
.shell(COPY_TRACE_DIALOG_TITLE
);
311 SWTBotText text
= shell
.bot().textWithLabel("New Trace name:");
312 text
.setText(RENAMED_TRACE_NAME
);
313 shell
.bot().button("OK").click();
314 fBot
.waitUntil(Conditions
.shellCloses(shell
));
317 private static void testEventsTable(String editorName
) {
318 SWTBotEditor editor
= SWTBotUtils
.activeEventsEditor(fBot
, editorName
);
319 fBot
.waitUntil(ConditionHelpers
.numberOfEventsInTrace(TmfTraceManager
.getInstance().getActiveTrace(), NB_EVENTS
));
321 SWTBotTable table
= editor
.bot().table();
322 fBot
.waitUntil(new DefaultCondition() {
324 public boolean test() throws Exception
{
325 return table
.rowCount() > 1;
329 public String
getFailureMessage() {
330 return "No items in table";
333 // Select first event (skip filter/search row)
334 table
.getTableItem(1).select();
336 editor
.bot().waitUntil(new DefaultCondition() {
338 public boolean test() throws Exception
{
339 return table
.selection().rowCount() == 1 && table
.selection().get(0).toString().contains("01:01");
343 public String
getFailureMessage() {
344 return "First event not selected";
349 private static void testStatisticsView() {
350 SWTBotUtils
.openView(TmfStatisticsView
.ID
);
351 SWTBotView view
= fBot
.viewById(TmfStatisticsView
.ID
);
352 assertTrue(view
.bot().tree().hasItems());
353 view
.bot().tree().cell(0, 1).equals(Long
.toString(NB_EVENTS
));