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