1 /*******************************************************************************
2 * Copyright (c) 2015 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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
15 import static org
.junit
.Assert
.fail
;
16 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.io
.IOException
;
21 import java
.net
.URISyntaxException
;
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
.osgi
.util
.NLS
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.dnd
.Clipboard
;
33 import org
.eclipse
.swt
.dnd
.TextTransfer
;
34 import org
.eclipse
.swt
.widgets
.Display
;
35 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
36 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
37 import org
.eclipse
.swtbot
.swt
.finder
.exceptions
.WidgetNotFoundException
;
38 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
39 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
40 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
41 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.KeyboardFactory
;
42 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
43 import org
.eclipse
.swtbot
.swt
.finder
.results
.StringResult
;
44 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
45 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
46 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
50 import org
.junit
.After
;
51 import org
.junit
.AfterClass
;
52 import org
.junit
.Before
;
53 import org
.junit
.BeforeClass
;
54 import org
.junit
.Test
;
55 import org
.junit
.runner
.RunWith
;
58 * SWTBot test for testing copy to clipboard.
60 @RunWith(SWTBotJunit4ClassRunner
.class)
61 public class CopyToClipboardTest
{
63 private static final String LINE_SEPARATOR
= System
.getProperty("line.separator"); //$NON-NLS-1$
64 private static final String HEADER_TEXT
= "Timestamp\tHost\tLogger\tFile\tLine\tMessage" + LINE_SEPARATOR
;
65 private static final String EVENT1_TEXT
= "01:01:01.000 000 000\tHostA\tLoggerA\tSourceFile\t4\tMessage A" + LINE_SEPARATOR
;
66 private static final String EVENT2_TEXT
= "02:02:02.000 000 000\tHostB\tLoggerB\tSourceFile\t5\tMessage B" + LINE_SEPARATOR
;
67 private static final String EVENT3_TEXT
= "03:03:03.000 000 000\tHostC\tLoggerC\tSourceFile\t6\tMessage C" + LINE_SEPARATOR
;
68 private static final Keyboard KEYBOARD
= KeyboardFactory
.getSWTKeyboard();
69 private static final String TRACE_PROJECT_NAME
= "test";
70 private static final String TRACE_NAME
= "syslog_collapse";
71 private static final String TRACE_PATH
= "testfiles/" + TRACE_NAME
;
72 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
73 private static final String COPY_TO_CLIPBOARD
= "Copy to Clipboard";
74 private static final int TIMEOUT
= 2000; /* 20 second timeout */
76 private static File fTestFile
= null;
78 private static SWTWorkbenchBot fBot
;
79 private SWTBotEditor fEditorBot
;
81 /** The Log4j logger instance. */
82 private static final Logger fLogger
= Logger
.getRootLogger();
88 public static void beforeClass() {
89 SWTBotUtils
.initialize();
91 /* set up test trace */
92 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TRACE_PATH
), null);
95 uri
= FileLocator
.toFileURL(location
).toURI();
96 fTestFile
= new File(uri
);
97 } catch (URISyntaxException
| IOException e
) {
101 assumeTrue(fTestFile
.exists());
103 /* Set up for swtbot */
104 SWTBotPreferences
.TIMEOUT
= TIMEOUT
;
105 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
106 fLogger
.removeAllAppenders();
107 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
108 fBot
= new SWTWorkbenchBot();
110 /* Close welcome view */
111 SWTBotUtils
.closeView("Welcome", fBot
);
113 /* Switch perspectives */
114 SWTBotUtils
.switchToTracingPerspective();
116 /* Finish waiting for eclipse to load */
117 WaitUtils
.waitForJobs();
119 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
123 * Test class tear down method.
126 public static void afterClass() {
127 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
128 fLogger
.removeAllAppenders();
135 public void before() {
136 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
137 fEditorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
144 public void after() {
145 fBot
.closeAllEditors();
146 SWTBotUtils
.closeSecondaryShells(fBot
);
150 * Test copy to clipboard with single selection
153 public void testCopySingleSelection() {
154 assumeTrue(!isAffectedByBug486302());
155 final SWTBotTable tableBot
= fEditorBot
.bot().table();
156 tableBot
.getTableItem(1).click();
158 tableBot
.contextMenu(COPY_TO_CLIPBOARD
).click();
159 assertClipboardContentsEquals(HEADER_TEXT
+ EVENT1_TEXT
);
163 * Test copy to clipboard with multiple selection
166 public void testCopyMultipleSelection() {
167 assumeTrue(!isAffectedByBug486302());
168 final SWTBotTable tableBot
= fEditorBot
.bot().table();
169 tableBot
.getTableItem(1).click();
170 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.DOWN
);
171 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.DOWN
);
173 tableBot
.contextMenu(COPY_TO_CLIPBOARD
).click();
174 assertClipboardContentsEquals(HEADER_TEXT
+ EVENT1_TEXT
+ EVENT2_TEXT
+ EVENT3_TEXT
);
178 * Returns whether or not the running Eclipse is affected by Bug 486302. The
179 * bug is present in Eclipse 4.5.2 and earlier running GTK3.
181 private static boolean isAffectedByBug486302() {
182 String property
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
183 if (property
!= null) {
184 @NonNull String
@NonNull [] versionSegments
= property
.split("\\.");
185 if (versionSegments
.length
> 0) {
186 return SWT
.getVersion() <= 4530 && versionSegments
[0].equals("3");
194 * Test copy to clipboard not enabled when selection includes search row
197 public void testNoCopySearchRow() {
198 final SWTBotTable tableBot
= fEditorBot
.bot().table();
199 tableBot
.getTableItem(1).click();
200 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.UP
);
202 assertContextMenuAbsent(tableBot
, COPY_TO_CLIPBOARD
);
205 private static void assertContextMenuAbsent(final SWTBotTable tableBot
, final String text
) {
206 fBot
.waitUntil(new DefaultCondition() {
208 public boolean test() throws Exception
{
210 SWTBotPreferences
.TIMEOUT
= 0;
211 tableBot
.contextMenu(text
);
212 } catch (WidgetNotFoundException e
) {
215 SWTBotPreferences
.TIMEOUT
= TIMEOUT
;
220 public String
getFailureMessage() {
221 return text
+ " context menu present, absent expected.";
226 private static void assertClipboardContentsEquals(final String expected
) {
227 fBot
.waitUntil(new DefaultCondition() {
230 public boolean test() throws Exception
{
231 actual
= UIThreadRunnable
.syncExec(new StringResult() {
233 public String
run() {
234 Clipboard clipboard
= new Clipboard(Display
.getDefault());
235 TextTransfer textTransfer
= TextTransfer
.getInstance();
237 return (String
) clipboard
.getContents(textTransfer
);
243 return expected
.equals(actual
);
246 public String
getFailureMessage() {
247 return NLS
.bind("Clipboard contents:\n{0}\nExpected:\n{1}",