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
.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
;
44 * Test reading a trace in raw and eventy modes.
46 @RunWith(SWTBotJunit4ClassRunner
.class)
47 public class RawTextEditorTest
{
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";
55 private static final long INITIAL_NB_EVENTS
= 100;
56 private static final long SECOND_TO_MILLISECOND
= 1000;
58 private static final String TRACE_LOCATION
= TmfTraceManager
.getTemporaryDirPath() + File
.separator
+ "test.txt";
59 private static SWTWorkbenchBot fBot
;
61 private long fNbWrittenEvents
= 0;
63 /** Test Class setup */
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();
72 SWTBotUtils
.closeView("welcome", fBot
);
74 SWTBotUtils
.switchToTracingPerspective();
75 /* finish waiting for eclipse to load */
76 WaitUtils
.waitForJobs();
86 public void before() throws Exception
{
87 SWTBotUtils
.createProject(PROJECT_NAME
);
88 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
89 WaitUtils
.waitForJobs();
97 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
101 * Test going to raw and back
104 public void testRead() {
105 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
107 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
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();
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() {
122 public boolean test() throws Exception
{
123 return !HIGHLIGHT_COLOR
.equals(rawViewer
.getLineBackground(0));
127 public String
getFailureMessage() {
131 final ICondition colorIsHighlight
= new DefaultCondition() {
133 public boolean test() {
134 return HIGHLIGHT_COLOR
.equals(rawViewer
.getLineBackground(0));
138 public String
getFailureMessage() {
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());
170 private static class NumberOfEventsCondition
extends DefaultCondition
{
172 private ITmfTrace fTrace
;
173 private long fNbEvents
;
175 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
177 fNbEvents
= nbEvents
;
181 public boolean test() throws Exception
{
182 return fTrace
.getNbEvents() == fNbEvents
;
186 public String
getFailureMessage() {
187 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events. Current: " + fTrace
.getNbEvents();
192 * Create a trace with a number of events.
195 * the number of events to generate
196 * @return the path to the created trace
200 protected String
createTrace(long nbEvents
) throws Exception
{
201 writeToTrace(nbEvents
, false);
202 return TRACE_LOCATION
;
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
) {
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");
222 * Get the trace type for the test.
224 * @return the trace type
226 protected String
getTraceType() {
227 return TRACE_TYPE_SYSLOG
;