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
.finders
.UIThreadRunnable
;
38 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
39 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
40 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.KeyboardFactory
;
41 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
42 import org
.eclipse
.swtbot
.swt
.finder
.results
.StringResult
;
43 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
44 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
45 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
46 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
49 import org
.junit
.After
;
50 import org
.junit
.AfterClass
;
51 import org
.junit
.Before
;
52 import org
.junit
.BeforeClass
;
53 import org
.junit
.Test
;
54 import org
.junit
.runner
.RunWith
;
57 * SWTBot test for testing copy to clipboard.
59 @RunWith(SWTBotJunit4ClassRunner
.class)
60 public class CopyToClipboardTest
{
62 private static final String LINE_SEPARATOR
= System
.getProperty("line.separator"); //$NON-NLS-1$
63 private static final String HEADER_TEXT
= "Timestamp\tHost\tLogger\tFile\tLine\tMessage" + LINE_SEPARATOR
;
64 private static final String EVENT1_TEXT
= "01:01:01.000 000 000\tHostA\tLoggerA\tSourceFile\t4\tMessage A" + LINE_SEPARATOR
;
65 private static final String EVENT2_TEXT
= "02:02:02.000 000 000\tHostB\tLoggerB\tSourceFile\t5\tMessage B" + LINE_SEPARATOR
;
66 private static final String EVENT3_TEXT
= "03:03:03.000 000 000\tHostC\tLoggerC\tSourceFile\t6\tMessage C" + LINE_SEPARATOR
;
67 private static final Keyboard KEYBOARD
= KeyboardFactory
.getSWTKeyboard();
68 private static final String TRACE_PROJECT_NAME
= "test";
69 private static final String TRACE_NAME
= "syslog_collapse";
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 private static final String COPY_TO_CLIPBOARD
= "Copy to Clipboard";
73 private static final int TIMEOUT
= 2000; /* 20 second timeout */
75 private static File fTestFile
= null;
77 private static SWTWorkbenchBot fBot
;
78 private SWTBotEditor fEditorBot
;
80 /** The Log4j logger instance. */
81 private static final Logger fLogger
= Logger
.getRootLogger();
87 public static void beforeClass() {
88 SWTBotUtils
.initialize();
90 /* set up test trace */
91 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TRACE_PATH
), null);
94 uri
= FileLocator
.toFileURL(location
).toURI();
95 fTestFile
= new File(uri
);
96 } catch (URISyntaxException
| IOException e
) {
100 assumeTrue(fTestFile
.exists());
102 /* Set up for swtbot */
103 SWTBotPreferences
.TIMEOUT
= TIMEOUT
;
104 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
105 fLogger
.removeAllAppenders();
106 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
107 fBot
= new SWTWorkbenchBot();
109 /* Close welcome view */
110 SWTBotUtils
.closeView("Welcome", fBot
);
112 /* Switch perspectives */
113 SWTBotUtils
.switchToTracingPerspective();
115 /* Finish waiting for eclipse to load */
116 SWTBotUtils
.waitForJobs();
118 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
122 * Test class tear down method.
125 public static void afterClass() {
126 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
127 fLogger
.removeAllAppenders();
134 public void before() {
135 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
136 fEditorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
143 public void after() {
144 fBot
.closeAllEditors();
148 * Test copy to clipboard with single selection
151 public void testCopySingleSelection() {
152 assumeTrue(!isAffectedByBug486302());
153 final SWTBotTable tableBot
= fEditorBot
.bot().table();
154 tableBot
.getTableItem(1).click();
156 tableBot
.contextMenu(COPY_TO_CLIPBOARD
).click();
157 assertClipboardContentsEquals(HEADER_TEXT
+ EVENT1_TEXT
);
161 * Test copy to clipboard with multiple selection
164 public void testCopyMultipleSelection() {
165 assumeTrue(!isAffectedByBug486302());
166 final SWTBotTable tableBot
= fEditorBot
.bot().table();
167 tableBot
.getTableItem(1).click();
168 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.DOWN
);
169 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.DOWN
);
171 tableBot
.contextMenu(COPY_TO_CLIPBOARD
).click();
172 assertClipboardContentsEquals(HEADER_TEXT
+ EVENT1_TEXT
+ EVENT2_TEXT
+ EVENT3_TEXT
);
176 * Returns whether or not the running Eclipse is affected by Bug 486302. The
177 * bug is present in Eclipse 4.5.2 and earlier running GTK3.
179 private static boolean isAffectedByBug486302() {
180 String property
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
181 if (property
!= null) {
182 @NonNull String
@NonNull [] versionSegments
= property
.split("\\.");
183 if (versionSegments
.length
> 0) {
184 return SWT
.getVersion() <= 4530 && versionSegments
[0].equals("3");
192 * Test copy to clipboard not enabled when selection includes search row
195 public void testNoCopySearchRow() {
196 final SWTBotTable tableBot
= fEditorBot
.bot().table();
197 tableBot
.getTableItem(1).click();
198 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.UP
);
200 assertContextMenuAbsent(tableBot
, COPY_TO_CLIPBOARD
);
203 private static void assertContextMenuAbsent(final SWTBotTable tableBot
, final String text
) {
204 fBot
.waitUntil(new DefaultCondition() {
206 public boolean test() throws Exception
{
208 SWTBotPreferences
.TIMEOUT
= 0;
209 tableBot
.contextMenu(text
);
210 } catch (TimeoutException e
) {
212 } catch (IndexOutOfBoundsException e
) {
213 /* remove when Bug 474063 is fixed */
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}",