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
.Ignore
;
41 import org
.junit
.Test
;
42 import org
.junit
.runner
.RunWith
;
45 * Test reading a trace in raw and event modes.
47 @RunWith(SWTBotJunit4ClassRunner
.class)
48 public class RawTextEditorTest
{
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";
56 private static final long INITIAL_NB_EVENTS
= 100;
57 private static final long SECOND_TO_MILLISECOND
= 1000;
59 private static final String TRACE_LOCATION
= TmfTraceManager
.getTemporaryDirPath() + File
.separator
+ "test.txt";
60 private static SWTWorkbenchBot fBot
;
62 private long fNbWrittenEvents
= 0;
64 /** Test Class setup */
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();
73 SWTBotUtils
.closeView("welcome", fBot
);
75 SWTBotUtils
.switchToTracingPerspective();
76 /* finish waiting for eclipse to load */
77 WaitUtils
.waitForJobs();
87 public void before() throws Exception
{
88 SWTBotUtils
.createProject(PROJECT_NAME
);
89 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
90 WaitUtils
.waitForJobs();
98 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
102 * Test going to raw and back
106 public void testRead() {
107 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
109 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
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();
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() {
124 public boolean test() throws Exception
{
125 return !HIGHLIGHT_COLOR
.equals(rawViewer
.getLineBackground(0));
129 public String
getFailureMessage() {
133 final ICondition colorIsHighlight
= new DefaultCondition() {
135 public boolean test() {
136 return HIGHLIGHT_COLOR
.equals(rawViewer
.getLineBackground(0));
140 public String
getFailureMessage() {
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());
172 private static class NumberOfEventsCondition
extends DefaultCondition
{
174 private ITmfTrace fTrace
;
175 private long fNbEvents
;
177 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
179 fNbEvents
= nbEvents
;
183 public boolean test() throws Exception
{
184 return fTrace
.getNbEvents() == fNbEvents
;
188 public String
getFailureMessage() {
189 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events. Current: " + fTrace
.getNbEvents();
194 * Create a trace with a number of events.
197 * the number of events to generate
198 * @return the path to the created trace
202 protected String
createTrace(long nbEvents
) throws Exception
{
203 writeToTrace(nbEvents
, false);
204 return TRACE_LOCATION
;
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
) {
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");
224 * Get the trace type for the test.
226 * @return the trace type
228 protected String
getTraceType() {
229 return TRACE_TYPE_SYSLOG
;