Commit | Line | Data |
---|---|---|
5453c5d4 PT |
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.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; | |
53 | ||
54 | /** | |
55 | * SWTBot test for testing copy to clipboard. | |
56 | */ | |
57 | @RunWith(SWTBotJunit4ClassRunner.class) | |
58 | public class CopyToClipboardTest { | |
59 | ||
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"; | |
59b8da2d PT |
70 | private static final String COPY_TO_CLIPBOARD = "Copy to Clipboard"; |
71 | private static final int TIMEOUT = 2000; /* 20 second timeout */ | |
5453c5d4 PT |
72 | |
73 | private static File fTestFile = null; | |
74 | ||
75 | private static SWTWorkbenchBot fBot; | |
76 | private SWTBotEditor fEditorBot; | |
77 | ||
78 | /** The Log4j logger instance. */ | |
79 | private static final Logger fLogger = Logger.getRootLogger(); | |
80 | ||
81 | /** | |
82 | * Test Class setup | |
83 | */ | |
84 | @BeforeClass | |
85 | public static void beforeClass() { | |
86 | SWTBotUtils.failIfUIThread(); | |
87 | ||
88 | /* set up test trace */ | |
89 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TRACE_PATH), null); | |
90 | URI uri; | |
91 | try { | |
92 | uri = FileLocator.toFileURL(location).toURI(); | |
93 | fTestFile = new File(uri); | |
94 | } catch (URISyntaxException | IOException e) { | |
95 | fail(e.getMessage()); | |
96 | } | |
97 | ||
98 | assumeTrue(fTestFile.exists()); | |
99 | ||
100 | /* Set up for swtbot */ | |
59b8da2d | 101 | SWTBotPreferences.TIMEOUT = TIMEOUT; |
5453c5d4 PT |
102 | SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US"; |
103 | fLogger.removeAllAppenders(); | |
104 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); | |
105 | fBot = new SWTWorkbenchBot(); | |
106 | ||
107 | /* Close welcome view */ | |
108 | SWTBotUtils.closeView("Welcome", fBot); | |
109 | ||
110 | /* Switch perspectives */ | |
111 | SWTBotUtils.switchToTracingPerspective(); | |
112 | ||
113 | /* Finish waiting for eclipse to load */ | |
114 | SWTBotUtils.waitForJobs(); | |
115 | ||
116 | SWTBotUtils.createProject(TRACE_PROJECT_NAME); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Test class tear down method. | |
121 | */ | |
122 | @AfterClass | |
123 | public static void afterClass() { | |
124 | SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot); | |
125 | fLogger.removeAllAppenders(); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Before Test | |
130 | */ | |
131 | @Before | |
132 | public void before() { | |
133 | SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), TRACE_TYPE); | |
134 | fEditorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName()); | |
135 | } | |
136 | ||
137 | /** | |
138 | * After Test | |
139 | */ | |
140 | @After | |
141 | public void after() { | |
142 | fBot.closeAllEditors(); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Test copy to clipboard with single selection | |
147 | */ | |
148 | @Test | |
149 | public void testCopySingleSelection() { | |
150 | final SWTBotTable tableBot = fEditorBot.bot().table(); | |
151 | tableBot.getTableItem(1).click(); | |
152 | ||
59b8da2d | 153 | tableBot.contextMenu(COPY_TO_CLIPBOARD).click(); |
5453c5d4 PT |
154 | assertClipboardContentsEquals(HEADER_TEXT + EVENT1_TEXT); |
155 | } | |
156 | ||
157 | /** | |
158 | * Test copy to clipboard with multiple selection | |
159 | */ | |
160 | @Test | |
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); | |
166 | ||
59b8da2d | 167 | tableBot.contextMenu(COPY_TO_CLIPBOARD).click(); |
5453c5d4 PT |
168 | assertClipboardContentsEquals(HEADER_TEXT + EVENT1_TEXT + EVENT2_TEXT + EVENT3_TEXT); |
169 | } | |
170 | ||
171 | /** | |
172 | * Test copy to clipboard not enabled when selection includes search row | |
173 | */ | |
59b8da2d | 174 | @Test |
5453c5d4 PT |
175 | public void testNoCopySearchRow() { |
176 | final SWTBotTable tableBot = fEditorBot.bot().table(); | |
177 | tableBot.getTableItem(1).click(); | |
178 | KEYBOARD.pressShortcut(Keystrokes.SHIFT, Keystrokes.UP); | |
179 | ||
59b8da2d PT |
180 | assertContextMenuAbsent(tableBot, COPY_TO_CLIPBOARD); |
181 | } | |
182 | ||
183 | private static void assertContextMenuAbsent(final SWTBotTable tableBot, final String text) { | |
184 | fBot.waitUntil(new DefaultCondition() { | |
185 | @Override | |
186 | public boolean test() throws Exception { | |
187 | try { | |
188 | SWTBotPreferences.TIMEOUT = 0; | |
189 | tableBot.contextMenu(text); | |
190 | } catch (TimeoutException e) { | |
191 | return true; | |
192 | } catch (IndexOutOfBoundsException e) { | |
193 | /* remove when Bug 474063 is fixed */ | |
194 | } finally { | |
195 | SWTBotPreferences.TIMEOUT = TIMEOUT; | |
196 | } | |
197 | return false; | |
198 | } | |
199 | @Override | |
200 | public String getFailureMessage() { | |
201 | return text + " context menu present, absent expected."; | |
202 | } | |
203 | }); | |
5453c5d4 PT |
204 | } |
205 | ||
206 | private static void assertClipboardContentsEquals(final String expected) { | |
207 | fBot.waitUntil(new DefaultCondition() { | |
208 | String actual; | |
209 | @Override | |
210 | public boolean test() throws Exception { | |
211 | actual = UIThreadRunnable.syncExec(new StringResult() { | |
212 | @Override | |
213 | public String run() { | |
214 | Clipboard clipboard = new Clipboard(Display.getDefault()); | |
215 | TextTransfer textTransfer = TextTransfer.getInstance(); | |
216 | try { | |
217 | return (String) clipboard.getContents(textTransfer); | |
218 | } finally { | |
219 | clipboard.dispose(); | |
220 | } | |
221 | } | |
222 | }); | |
223 | return expected.equals(actual); | |
224 | } | |
225 | @Override | |
226 | public String getFailureMessage() { | |
227 | return NLS.bind("Clipboard contents:\n{0}\nExpected:\n{1}", | |
228 | actual, expected); | |
229 | } | |
230 | }); | |
231 | } | |
232 | } |