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