244cb8422a5211f3dca15ecffc022e1c253ba839
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / CopyToClipboardTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
14
15 import static org.junit.Assert.fail;
16 import static org.junit.Assume.assumeTrue;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.net.URL;
23
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.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;
55
56 /**
57 * SWTBot test for testing copy to clipboard.
58 */
59 @RunWith(SWTBotJunit4ClassRunner.class)
60 public class CopyToClipboardTest {
61
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 */
74
75 private static File fTestFile = null;
76
77 private static SWTWorkbenchBot fBot;
78 private SWTBotEditor fEditorBot;
79
80 /** The Log4j logger instance. */
81 private static final Logger fLogger = Logger.getRootLogger();
82
83 /**
84 * Test Class setup
85 */
86 @BeforeClass
87 public static void beforeClass() {
88 SWTBotUtils.initialize();
89
90 /* set up test trace */
91 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TRACE_PATH), null);
92 URI uri;
93 try {
94 uri = FileLocator.toFileURL(location).toURI();
95 fTestFile = new File(uri);
96 } catch (URISyntaxException | IOException e) {
97 fail(e.getMessage());
98 }
99
100 assumeTrue(fTestFile.exists());
101
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();
108
109 /* Close welcome view */
110 SWTBotUtils.closeView("Welcome", fBot);
111
112 /* Switch perspectives */
113 SWTBotUtils.switchToTracingPerspective();
114
115 /* Finish waiting for eclipse to load */
116 SWTBotUtils.waitForJobs();
117
118 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
119 }
120
121 /**
122 * Test class tear down method.
123 */
124 @AfterClass
125 public static void afterClass() {
126 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
127 fLogger.removeAllAppenders();
128 }
129
130 /**
131 * Before Test
132 */
133 @Before
134 public void before() {
135 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), TRACE_TYPE);
136 fEditorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName());
137 }
138
139 /**
140 * After Test
141 */
142 @After
143 public void after() {
144 fBot.closeAllEditors();
145 }
146
147 /**
148 * Test copy to clipboard with single selection
149 */
150 @Test
151 public void testCopySingleSelection() {
152 assumeTrue(!isAffectedByBug486302());
153 final SWTBotTable tableBot = fEditorBot.bot().table();
154 tableBot.getTableItem(1).click();
155
156 tableBot.contextMenu(COPY_TO_CLIPBOARD).click();
157 assertClipboardContentsEquals(HEADER_TEXT + EVENT1_TEXT);
158 }
159
160 /**
161 * Test copy to clipboard with multiple selection
162 */
163 @Test
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);
170
171 tableBot.contextMenu(COPY_TO_CLIPBOARD).click();
172 assertClipboardContentsEquals(HEADER_TEXT + EVENT1_TEXT + EVENT2_TEXT + EVENT3_TEXT);
173 }
174
175 /**
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.
178 */
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");
185 }
186 }
187
188 return false;
189 }
190
191 /**
192 * Test copy to clipboard not enabled when selection includes search row
193 */
194 @Test
195 public void testNoCopySearchRow() {
196 final SWTBotTable tableBot = fEditorBot.bot().table();
197 tableBot.getTableItem(1).click();
198 KEYBOARD.pressShortcut(Keystrokes.SHIFT, Keystrokes.UP);
199
200 assertContextMenuAbsent(tableBot, COPY_TO_CLIPBOARD);
201 }
202
203 private static void assertContextMenuAbsent(final SWTBotTable tableBot, final String text) {
204 fBot.waitUntil(new DefaultCondition() {
205 @Override
206 public boolean test() throws Exception {
207 try {
208 SWTBotPreferences.TIMEOUT = 0;
209 tableBot.contextMenu(text);
210 } catch (WidgetNotFoundException e) {
211 return true;
212 } finally {
213 SWTBotPreferences.TIMEOUT = TIMEOUT;
214 }
215 return false;
216 }
217 @Override
218 public String getFailureMessage() {
219 return text + " context menu present, absent expected.";
220 }
221 });
222 }
223
224 private static void assertClipboardContentsEquals(final String expected) {
225 fBot.waitUntil(new DefaultCondition() {
226 String actual;
227 @Override
228 public boolean test() throws Exception {
229 actual = UIThreadRunnable.syncExec(new StringResult() {
230 @Override
231 public String run() {
232 Clipboard clipboard = new Clipboard(Display.getDefault());
233 TextTransfer textTransfer = TextTransfer.getInstance();
234 try {
235 return (String) clipboard.getContents(textTransfer);
236 } finally {
237 clipboard.dispose();
238 }
239 }
240 });
241 return expected.equals(actual);
242 }
243 @Override
244 public String getFailureMessage() {
245 return NLS.bind("Clipboard contents:\n{0}\nExpected:\n{1}",
246 actual, expected);
247 }
248 });
249 }
250 }
This page took 0.03562 seconds and 4 git commands to generate.