59e21aae021263c5a36de4656281529ff53722a2
[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 SWTBotUtils.closeSecondaryShells(fBot);
146 }
147
148 /**
149 * Test copy to clipboard with single selection
150 */
151 @Test
152 public void testCopySingleSelection() {
153 assumeTrue(!isAffectedByBug486302());
154 final SWTBotTable tableBot = fEditorBot.bot().table();
155 tableBot.getTableItem(1).click();
156
157 tableBot.contextMenu(COPY_TO_CLIPBOARD).click();
158 assertClipboardContentsEquals(HEADER_TEXT + EVENT1_TEXT);
159 }
160
161 /**
162 * Test copy to clipboard with multiple selection
163 */
164 @Test
165 public void testCopyMultipleSelection() {
166 assumeTrue(!isAffectedByBug486302());
167 final SWTBotTable tableBot = fEditorBot.bot().table();
168 tableBot.getTableItem(1).click();
169 KEYBOARD.pressShortcut(Keystrokes.SHIFT, Keystrokes.DOWN);
170 KEYBOARD.pressShortcut(Keystrokes.SHIFT, Keystrokes.DOWN);
171
172 tableBot.contextMenu(COPY_TO_CLIPBOARD).click();
173 assertClipboardContentsEquals(HEADER_TEXT + EVENT1_TEXT + EVENT2_TEXT + EVENT3_TEXT);
174 }
175
176 /**
177 * Returns whether or not the running Eclipse is affected by Bug 486302. The
178 * bug is present in Eclipse 4.5.2 and earlier running GTK3.
179 */
180 private static boolean isAffectedByBug486302() {
181 String property = System.getProperty("org.eclipse.swt.internal.gtk.version");
182 if (property != null) {
183 @NonNull String @NonNull [] versionSegments = property.split("\\.");
184 if (versionSegments.length > 0) {
185 return SWT.getVersion() <= 4530 && versionSegments[0].equals("3");
186 }
187 }
188
189 return false;
190 }
191
192 /**
193 * Test copy to clipboard not enabled when selection includes search row
194 */
195 @Test
196 public void testNoCopySearchRow() {
197 final SWTBotTable tableBot = fEditorBot.bot().table();
198 tableBot.getTableItem(1).click();
199 KEYBOARD.pressShortcut(Keystrokes.SHIFT, Keystrokes.UP);
200
201 assertContextMenuAbsent(tableBot, COPY_TO_CLIPBOARD);
202 }
203
204 private static void assertContextMenuAbsent(final SWTBotTable tableBot, final String text) {
205 fBot.waitUntil(new DefaultCondition() {
206 @Override
207 public boolean test() throws Exception {
208 try {
209 SWTBotPreferences.TIMEOUT = 0;
210 tableBot.contextMenu(text);
211 } catch (WidgetNotFoundException e) {
212 return true;
213 } finally {
214 SWTBotPreferences.TIMEOUT = TIMEOUT;
215 }
216 return false;
217 }
218 @Override
219 public String getFailureMessage() {
220 return text + " context menu present, absent expected.";
221 }
222 });
223 }
224
225 private static void assertClipboardContentsEquals(final String expected) {
226 fBot.waitUntil(new DefaultCondition() {
227 String actual;
228 @Override
229 public boolean test() throws Exception {
230 actual = UIThreadRunnable.syncExec(new StringResult() {
231 @Override
232 public String run() {
233 Clipboard clipboard = new Clipboard(Display.getDefault());
234 TextTransfer textTransfer = TextTransfer.getInstance();
235 try {
236 return (String) clipboard.getContents(textTransfer);
237 } finally {
238 clipboard.dispose();
239 }
240 }
241 });
242 return expected.equals(actual);
243 }
244 @Override
245 public String getFailureMessage() {
246 return NLS.bind("Clipboard contents:\n{0}\nExpected:\n{1}",
247 actual, expected);
248 }
249 });
250 }
251 }
This page took 0.036889 seconds and 4 git commands to generate.