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