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