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
.osgi
.util
.NLS
;
30 import org
.eclipse
.swt
.dnd
.Clipboard
;
31 import org
.eclipse
.swt
.dnd
.TextTransfer
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
34 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
35 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
36 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
37 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
38 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.KeyboardFactory
;
39 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
40 import org
.eclipse
.swtbot
.swt
.finder
.results
.StringResult
;
41 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
42 import org
.eclipse
.swtbot
.swt
.finder
.waits
.DefaultCondition
;
43 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
44 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.TimeoutException
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
47 import org
.junit
.After
;
48 import org
.junit
.AfterClass
;
49 import org
.junit
.Before
;
50 import org
.junit
.BeforeClass
;
51 import org
.junit
.Test
;
52 import org
.junit
.runner
.RunWith
;
55 * SWTBot test for testing copy to clipboard.
57 @RunWith(SWTBotJunit4ClassRunner
.class)
58 public class CopyToClipboardTest
{
60 private static final String LINE_SEPARATOR
= System
.getProperty("line.separator"); //$NON-NLS-1$
61 private static final String HEADER_TEXT
= "Timestamp\tHost\tLogger\tFile\tLine\tMessage" + LINE_SEPARATOR
;
62 private static final String EVENT1_TEXT
= "01:01:01.000 000 000\tHostA\tLoggerA\tSourceFile\t4\tMessage A" + LINE_SEPARATOR
;
63 private static final String EVENT2_TEXT
= "02:02:02.000 000 000\tHostB\tLoggerB\tSourceFile\t5\tMessage B" + LINE_SEPARATOR
;
64 private static final String EVENT3_TEXT
= "03:03:03.000 000 000\tHostC\tLoggerC\tSourceFile\t6\tMessage C" + LINE_SEPARATOR
;
65 private static final Keyboard KEYBOARD
= KeyboardFactory
.getSWTKeyboard();
66 private static final String TRACE_PROJECT_NAME
= "test";
67 private static final String TRACE_NAME
= "syslog_collapse";
68 private static final String TRACE_PATH
= "testfiles/" + TRACE_NAME
;
69 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
70 private static final String COPY_TO_CLIPBOARD
= "Copy to Clipboard";
71 private static final int TIMEOUT
= 2000; /* 20 second timeout */
73 private static File fTestFile
= null;
75 private static SWTWorkbenchBot fBot
;
76 private SWTBotEditor fEditorBot
;
78 /** The Log4j logger instance. */
79 private static final Logger fLogger
= Logger
.getRootLogger();
85 public static void beforeClass() {
86 SWTBotUtils
.initialize();
88 /* set up test trace */
89 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TRACE_PATH
), null);
92 uri
= FileLocator
.toFileURL(location
).toURI();
93 fTestFile
= new File(uri
);
94 } catch (URISyntaxException
| IOException e
) {
98 assumeTrue(fTestFile
.exists());
100 /* Set up for swtbot */
101 SWTBotPreferences
.TIMEOUT
= 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();
116 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
120 * Test class tear down method.
123 public static void afterClass() {
124 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
125 fLogger
.removeAllAppenders();
132 public void before() {
133 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), TRACE_TYPE
);
134 fEditorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
141 public void after() {
142 fBot
.closeAllEditors();
146 * Test copy to clipboard with single selection
149 public void testCopySingleSelection() {
150 final SWTBotTable tableBot
= fEditorBot
.bot().table();
151 tableBot
.getTableItem(1).click();
153 tableBot
.contextMenu(COPY_TO_CLIPBOARD
).click();
154 assertClipboardContentsEquals(HEADER_TEXT
+ EVENT1_TEXT
);
158 * Test copy to clipboard with multiple selection
161 public void testCopyMultipleSelection() {
162 final SWTBotTable tableBot
= fEditorBot
.bot().table();
163 tableBot
.getTableItem(1).click();
164 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.DOWN
);
165 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.DOWN
);
167 tableBot
.contextMenu(COPY_TO_CLIPBOARD
).click();
168 assertClipboardContentsEquals(HEADER_TEXT
+ EVENT1_TEXT
+ EVENT2_TEXT
+ EVENT3_TEXT
);
172 * Test copy to clipboard not enabled when selection includes search row
175 public void testNoCopySearchRow() {
176 final SWTBotTable tableBot
= fEditorBot
.bot().table();
177 tableBot
.getTableItem(1).click();
178 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.UP
);
180 assertContextMenuAbsent(tableBot
, COPY_TO_CLIPBOARD
);
183 private static void assertContextMenuAbsent(final SWTBotTable tableBot
, final String text
) {
184 fBot
.waitUntil(new DefaultCondition() {
186 public boolean test() throws Exception
{
188 SWTBotPreferences
.TIMEOUT
= 0;
189 tableBot
.contextMenu(text
);
190 } catch (TimeoutException e
) {
192 } catch (IndexOutOfBoundsException e
) {
193 /* remove when Bug 474063 is fixed */
195 SWTBotPreferences
.TIMEOUT
= TIMEOUT
;
200 public String
getFailureMessage() {
201 return text
+ " context menu present, absent expected.";
206 private static void assertClipboardContentsEquals(final String expected
) {
207 fBot
.waitUntil(new DefaultCondition() {
210 public boolean test() throws Exception
{
211 actual
= UIThreadRunnable
.syncExec(new StringResult() {
213 public String
run() {
214 Clipboard clipboard
= new Clipboard(Display
.getDefault());
215 TextTransfer textTransfer
= TextTransfer
.getInstance();
217 return (String
) clipboard
.getContents(textTransfer
);
223 return expected
.equals(actual
);
226 public String
getFailureMessage() {
227 return NLS
.bind("Clipboard contents:\n{0}\nExpected:\n{1}",