| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 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 | |
| 10 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events; |
| 11 | |
| 12 | import static org.junit.Assert.assertEquals; |
| 13 | import static org.junit.Assert.assertFalse; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.io.FileWriter; |
| 17 | import java.io.IOException; |
| 18 | import java.text.SimpleDateFormat; |
| 19 | import java.util.Date; |
| 20 | |
| 21 | import org.apache.log4j.Logger; |
| 22 | import org.apache.log4j.varia.NullAppender; |
| 23 | import org.eclipse.swt.SWT; |
| 24 | import org.eclipse.swt.graphics.RGB; |
| 25 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
| 26 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; |
| 27 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
| 28 | import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes; |
| 29 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
| 30 | import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
| 31 | import org.eclipse.swtbot.swt.finder.waits.ICondition; |
| 32 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotStyledText; |
| 33 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 34 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 35 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 36 | import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils; |
| 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.BeforeClass; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | |
| 43 | /** |
| 44 | * Test reading a trace in raw and event modes. |
| 45 | */ |
| 46 | @RunWith(SWTBotJunit4ClassRunner.class) |
| 47 | public class RawTextEditorTest { |
| 48 | |
| 49 | private static final RGB WHITE = new RGB(255, 255, 255); |
| 50 | private static final RGB HIGHLIGHT_COLOR = new RGB(231, 246, 254); |
| 51 | private static final String PROJECT_NAME = "Test"; |
| 52 | private static final String TRACE_TYPE_SYSLOG = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog"; |
| 53 | private static final String TIMESTAMP_FORMAT = "MMM dd HH:mm:ss"; |
| 54 | |
| 55 | private static final long INITIAL_NB_EVENTS = 100; |
| 56 | private static final long SECOND_TO_MILLISECOND = 1000; |
| 57 | |
| 58 | private static final String TRACE_LOCATION = TmfTraceManager.getTemporaryDirPath() + File.separator + "test.txt"; |
| 59 | private static SWTWorkbenchBot fBot; |
| 60 | |
| 61 | private long fNbWrittenEvents = 0; |
| 62 | |
| 63 | /** Test Class setup */ |
| 64 | @BeforeClass |
| 65 | public static void init() { |
| 66 | SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US"; |
| 67 | SWTBotUtils.initialize(); |
| 68 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ |
| 69 | Logger.getRootLogger().addAppender(new NullAppender()); |
| 70 | fBot = new SWTWorkbenchBot(); |
| 71 | |
| 72 | SWTBotUtils.closeView("welcome", fBot); |
| 73 | |
| 74 | SWTBotUtils.switchToTracingPerspective(); |
| 75 | /* finish waiting for eclipse to load */ |
| 76 | WaitUtils.waitForJobs(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Test setup |
| 81 | * |
| 82 | * @throws Exception |
| 83 | * on error |
| 84 | */ |
| 85 | @Before |
| 86 | public void before() throws Exception { |
| 87 | SWTBotUtils.createProject(PROJECT_NAME); |
| 88 | SWTBotUtils.openTrace(PROJECT_NAME, createTrace(INITIAL_NB_EVENTS), getTraceType()); |
| 89 | WaitUtils.waitForJobs(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Test tear down |
| 94 | */ |
| 95 | @After |
| 96 | public void after() { |
| 97 | SWTBotUtils.deleteProject(PROJECT_NAME, fBot); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Test going to raw and back |
| 102 | */ |
| 103 | @Test |
| 104 | public void testRead() { |
| 105 | ITmfTrace activeTrace = TmfTraceManager.getInstance().getActiveTrace(); |
| 106 | |
| 107 | fBot.waitUntil(new NumberOfEventsCondition(activeTrace, INITIAL_NB_EVENTS)); |
| 108 | |
| 109 | SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot); |
| 110 | eventsEditor.setFocus(); |
| 111 | assertFalse(eventsEditor.bot().table().rowCount() == 0); |
| 112 | eventsEditor.bot().table().select(4); |
| 113 | eventsEditor.bot().table().getTableItem(4).contextMenu("Show Raw").click(); |
| 114 | eventsEditor.bot().table().getTableItem(4).click(); |
| 115 | final SWTBotStyledText rawViewer = eventsEditor.bot().styledText(); |
| 116 | |
| 117 | String selection = rawViewer.getText(); |
| 118 | assertEquals(":03 HostF LoggerF: SourceFileF:9 Message F", selection.substring(12, 54)); |
| 119 | rawViewer.pressShortcut(Keystrokes.DOWN); |
| 120 | final ICondition colorIsNotHighlight = new DefaultCondition() { |
| 121 | @Override |
| 122 | public boolean test() throws Exception { |
| 123 | return !HIGHLIGHT_COLOR.equals(rawViewer.getLineBackground(0)); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String getFailureMessage() { |
| 128 | return "Timed out"; |
| 129 | } |
| 130 | }; |
| 131 | final ICondition colorIsHighlight = new DefaultCondition() { |
| 132 | @Override |
| 133 | public boolean test() { |
| 134 | return HIGHLIGHT_COLOR.equals(rawViewer.getLineBackground(0)); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String getFailureMessage() { |
| 139 | return "Timed out"; |
| 140 | } |
| 141 | }; |
| 142 | fBot.waitUntil(colorIsNotHighlight); |
| 143 | assertEquals("Non-highlighted color", WHITE, rawViewer.getLineBackground(0)); |
| 144 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(1)); |
| 145 | rawViewer.pressShortcut(Keystrokes.UP); |
| 146 | fBot.waitUntil(colorIsHighlight); |
| 147 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(0)); |
| 148 | rawViewer.pressShortcut(Keystrokes.DOWN); |
| 149 | fBot.waitUntil(colorIsNotHighlight); |
| 150 | assertEquals("Non-highlighted color", WHITE, rawViewer.getLineBackground(0)); |
| 151 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(1)); |
| 152 | rawViewer.pressShortcut(Keystrokes.PAGE_UP); |
| 153 | fBot.waitUntil(colorIsHighlight); |
| 154 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(0)); |
| 155 | rawViewer.pressShortcut(SWT.CTRL, SWT.END, ' '); |
| 156 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(0)); |
| 157 | rawViewer.pressShortcut(Keystrokes.UP); |
| 158 | rawViewer.pressShortcut(Keystrokes.PAGE_DOWN); |
| 159 | fBot.waitUntil(colorIsNotHighlight); |
| 160 | assertEquals("Non-highlighted color", WHITE, rawViewer.getLineBackground(0)); |
| 161 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(1)); |
| 162 | rawViewer.pressShortcut(SWT.CTRL, SWT.HOME, ' '); |
| 163 | fBot.waitUntil(colorIsHighlight); |
| 164 | assertEquals("Highlighted color", HIGHLIGHT_COLOR, rawViewer.getLineBackground(0)); |
| 165 | eventsEditor.bot().table().getTableItem(5).click(); |
| 166 | eventsEditor.bot().table().getTableItem(4).contextMenu("Hide Raw").click(); |
| 167 | assertFalse(rawViewer.isActive()); |
| 168 | } |
| 169 | |
| 170 | private static class NumberOfEventsCondition extends DefaultCondition { |
| 171 | |
| 172 | private ITmfTrace fTrace; |
| 173 | private long fNbEvents; |
| 174 | |
| 175 | private NumberOfEventsCondition(ITmfTrace trace, long nbEvents) { |
| 176 | fTrace = trace; |
| 177 | fNbEvents = nbEvents; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public boolean test() throws Exception { |
| 182 | return fTrace.getNbEvents() == fNbEvents; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String getFailureMessage() { |
| 187 | return fTrace.getName() + " did not contain the expected number of " + fNbEvents + " events. Current: " + fTrace.getNbEvents(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Create a trace with a number of events. |
| 193 | * |
| 194 | * @param nbEvents |
| 195 | * the number of events to generate |
| 196 | * @return the path to the created trace |
| 197 | * @throws Exception |
| 198 | * on error |
| 199 | */ |
| 200 | protected String createTrace(long nbEvents) throws Exception { |
| 201 | writeToTrace(nbEvents, false); |
| 202 | return TRACE_LOCATION; |
| 203 | } |
| 204 | |
| 205 | private void writeToTrace(long nbEvents, boolean append) throws IOException { |
| 206 | final File file = new File(TRACE_LOCATION); |
| 207 | try (FileWriter writer = new FileWriter(file, append)) { |
| 208 | for (int i = 0; i < nbEvents; ++i) { |
| 209 | writeEvent(writer); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void writeEvent(FileWriter fw) throws IOException { |
| 215 | SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT); |
| 216 | String timeStampStr = f.format(new Date(fNbWrittenEvents * SECOND_TO_MILLISECOND)); |
| 217 | fw.write(timeStampStr + " HostF LoggerF: SourceFileF:9 Message F\n"); |
| 218 | fNbWrittenEvents++; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the trace type for the test. |
| 223 | * |
| 224 | * @return the trace type |
| 225 | */ |
| 226 | protected String getTraceType() { |
| 227 | return TRACE_TYPE_SYSLOG; |
| 228 | } |
| 229 | } |