1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertFalse
;
16 import java
.io
.FileWriter
;
17 import java
.io
.IOException
;
18 import java
.text
.SimpleDateFormat
;
19 import java
.util
.Date
;
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
;
43 * Test reading a trace in raw and eventy modes.
45 @RunWith(SWTBotJunit4ClassRunner
.class)
46 public class RawTextEditorTest
{
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";
54 private static final long INITIAL_NB_EVENTS
= 100;
55 private static final long SECOND_TO_MILLISECOND
= 1000;
57 private static final String TRACE_LOCATION
= TmfTraceManager
.getTemporaryDirPath() + File
.separator
+ "test.txt";
58 private static SWTWorkbenchBot fBot
;
60 private long fNbWrittenEvents
= 0;
62 /** Test Class setup */
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();
71 SWTBotUtils
.closeView("welcome", fBot
);
73 SWTBotUtils
.switchToTracingPerspective();
74 /* finish waiting for eclipse to load */
75 SWTBotUtils
.waitForJobs();
85 public void before() throws Exception
{
86 SWTBotUtils
.createProject(PROJECT_NAME
);
87 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
88 SWTBotUtils
.waitForJobs();
96 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
100 * Test going to raw and back
103 public void testRead() {
104 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
106 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
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();
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() {
121 public boolean test() throws Exception
{
122 return !HIGHLIGHT_COLOR
.equals(rawViewer
.getLineBackground(0));
126 public String
getFailureMessage() {
130 final ICondition colorIsHighlight
= new DefaultCondition() {
132 public boolean test() {
133 return HIGHLIGHT_COLOR
.equals(rawViewer
.getLineBackground(0));
137 public String
getFailureMessage() {
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());
169 private static class NumberOfEventsCondition
extends DefaultCondition
{
171 private ITmfTrace fTrace
;
172 private long fNbEvents
;
174 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
176 fNbEvents
= nbEvents
;
180 public boolean test() throws Exception
{
181 return fTrace
.getNbEvents() == fNbEvents
;
185 public String
getFailureMessage() {
186 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events. Current: " + fTrace
.getNbEvents();
191 * Create a trace with a number of events.
194 * the number of events to generate
195 * @return the path to the created trace
199 protected String
createTrace(long nbEvents
) throws Exception
{
200 writeToTrace(nbEvents
, false);
201 return TRACE_LOCATION
;
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
) {
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");
221 * Get the trace type for the test.
223 * @return the trace type
225 protected String
getTraceType() {
226 return TRACE_TYPE_SYSLOG
;