1 /*******************************************************************************
2 * Copyright (c) 2015 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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
18 import java
.io
.IOException
;
19 import java
.util
.Collections
;
20 import java
.util
.List
;
22 import org
.apache
.log4j
.ConsoleAppender
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.apache
.log4j
.SimpleLayout
;
25 import org
.eclipse
.swt
.graphics
.RGB
;
26 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
27 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
28 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
29 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
30 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
31 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
33 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTableItem
;
34 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
39 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorSetting
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorSettingsManager
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorsView
;
42 import org
.junit
.After
;
43 import org
.junit
.AfterClass
;
44 import org
.junit
.Before
;
45 import org
.junit
.BeforeClass
;
46 import org
.junit
.Test
;
49 * Test for Colors views in trace compass
51 public class ColorsViewTest
{
53 private static final class PassAll
implements ITmfFilterTreeNode
{
55 public boolean matches(ITmfEvent event
) {
60 public void setParent(ITmfFilterTreeNode parent
) {
65 public ITmfFilterTreeNode
replaceChild(int index
, ITmfFilterTreeNode node
) {
70 public ITmfFilterTreeNode
removeChild(ITmfFilterTreeNode node
) {
75 public ITmfFilterTreeNode
remove() {
80 public boolean hasChildren() {
85 public List
<String
> getValidChildren() {
86 return Collections
.EMPTY_LIST
;
90 public ITmfFilterTreeNode
getParent() {
95 public String
getNodeName() {
100 public int getChildrenCount() {
105 public ITmfFilterTreeNode
[] getChildren() {
110 public ITmfFilterTreeNode
getChild(int index
) {
115 public int addChild(ITmfFilterTreeNode node
) {
120 public ITmfFilterTreeNode
clone() {
125 public String
toString(boolean explicit
) {
130 public String
toString() {
131 return getNodeName();
135 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
137 private static final String TRACE_START
= "<trace>";
138 private static final String EVENT_BEGIN
= "<event timestamp=\"";
139 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
140 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
141 private static final String TRACE_END
= "</trace>";
143 private static final String PROJECT_NAME
= "TestForFiltering";
145 /** The Log4j logger instance. */
146 private static final Logger fLogger
= Logger
.getRootLogger();
147 private static SWTWorkbenchBot fBot
;
149 private static String
makeEvent(int ts
, int val
) {
150 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
153 private static File fFileLocation
;
156 * Initialization, creates a temp trace
158 * @throws IOException
162 public static void init() throws IOException
{
163 SWTBotUtils
.failIfUIThread();
164 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
165 /* set up for swtbot */
166 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
167 fLogger
.removeAllAppenders();
168 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
169 fBot
= new SWTWorkbenchBot();
171 SWTBotUtils
.closeView("welcome", fBot
);
173 SWTBotUtils
.switchToTracingPerspective();
174 /* finish waiting for eclipse to load */
175 SWTBotUtils
.waitForJobs();
176 fFileLocation
= File
.createTempFile("sample", ".xml");
177 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
178 braf
.writeBytes(TRACE_START
);
179 for (int i
= 0; i
< 100; i
++) {
180 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
182 braf
.writeBytes(TRACE_END
);
187 * Open a trace in an editor
190 public void beforeTest() {
191 SWTBotUtils
.createProject(PROJECT_NAME
);
192 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
193 assertNotNull(treeItem
);
194 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
195 SWTBotUtils
.openView(ColorsView
.ID
);
202 public static void cleanUp() {
203 fLogger
.removeAllAppenders();
204 fFileLocation
.delete();
211 public void tearDown() {
212 fBot
.closeAllEditors();
213 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
217 * Test color by making all events yellow
220 public void testYellow() {
221 SWTBotView viewBot
= fBot
.viewById(ColorsView
.ID
);
223 final String insert
= "Insert new color setting";
224 final String increasePriority
= "Increase priority";
225 final String decreasePriority
= "Decrease priority";
226 final String delete
= "Delete color setting";
227 viewBot
.toolbarButton(insert
).click();
228 viewBot
.toolbarButton(insert
).click();
229 viewBot
.toolbarButton(insert
).click();
230 viewBot
.toolbarButton(insert
).click();
231 viewBot
.toolbarButton(increasePriority
).click();
232 viewBot
.toolbarButton(decreasePriority
).click();
233 viewBot
.toolbarButton(delete
).click();
234 viewBot
.bot().label(0).setFocus();
235 viewBot
.toolbarButton(delete
).click();
236 viewBot
.bot().label(0).setFocus();
237 viewBot
.toolbarButton(delete
).click();
238 final RGB foreground
= new RGB(0, 0, 0);
239 final RGB background
= new RGB(255, 255, 0);
240 // Simulate the side effects of picking a color because we cannot
241 // control native Color picker dialog in SWTBot.
242 final ColorSetting
[] cs
= new ColorSetting
[1];
243 UIThreadRunnable
.syncExec(new VoidResult() {
246 cs
[0] = new ColorSetting(foreground
, background
, foreground
, new PassAll());
247 ColorSettingsManager
.setColorSettings(cs
);
250 final SWTBotTable eventsEditor
= fBot
.activeEditor().bot().table();
251 // should fix race condition of loading the trace
252 SWTBotUtils
.waitForJobs();
253 eventsEditor
.select(2);
254 final SWTBotTableItem tableItem
= eventsEditor
.getTableItem(2);
255 RGB fgc
= UIThreadRunnable
.syncExec(new Result
<RGB
>() {
258 return tableItem
.widget
.getForeground().getRGB();
261 RGB bgc
= UIThreadRunnable
.syncExec(new Result
<RGB
>() {
264 return tableItem
.widget
.getBackground().getRGB();
267 assertEquals("Fg", foreground
, fgc
);
268 assertEquals("Bg", background
, bgc
);
269 // reset color settings
270 UIThreadRunnable
.syncExec(new VoidResult() {
273 ColorSettingsManager
.setColorSettings(new ColorSetting
[0]);