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
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.swt
.graphics
.RGB
;
27 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
28 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
29 import org
.eclipse
.swtbot
.swt
.finder
.finders
.UIThreadRunnable
;
30 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
31 import org
.eclipse
.swtbot
.swt
.finder
.results
.Result
;
32 import org
.eclipse
.swtbot
.swt
.finder
.results
.VoidResult
;
33 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
34 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
35 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTableItem
;
36 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
40 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorSetting
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorSettingsManager
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorsView
;
45 import org
.junit
.After
;
46 import org
.junit
.AfterClass
;
47 import org
.junit
.Before
;
48 import org
.junit
.BeforeClass
;
49 import org
.junit
.Test
;
50 import org
.junit
.runner
.RunWith
;
53 * Test for Colors views in trace compass
55 @RunWith(SWTBotJunit4ClassRunner
.class)
56 public class ColorsViewTest
{
58 private static final class PassAll
implements ITmfFilterTreeNode
{
60 public boolean matches(ITmfEvent event
) {
65 public void setParent(ITmfFilterTreeNode parent
) {
70 public ITmfFilterTreeNode
replaceChild(int index
, ITmfFilterTreeNode node
) {
75 public ITmfFilterTreeNode
removeChild(ITmfFilterTreeNode node
) {
80 public ITmfFilterTreeNode
remove() {
85 public boolean hasChildren() {
90 public List
<String
> getValidChildren() {
91 return Collections
.EMPTY_LIST
;
95 public ITmfFilterTreeNode
getParent() {
100 public String
getNodeName() {
105 public int getChildrenCount() {
110 public @NonNull ITmfFilterTreeNode
[] getChildren() {
115 public ITmfFilterTreeNode
getChild(int index
) {
120 public int addChild(ITmfFilterTreeNode node
) {
125 public ITmfFilterTreeNode
clone() {
130 public String
toString(boolean explicit
) {
135 public String
toString() {
136 return getNodeName();
140 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
142 private static final String TRACE_START
= "<trace>";
143 private static final String EVENT_BEGIN
= "<event timestamp=\"";
144 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
145 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
146 private static final String TRACE_END
= "</trace>";
148 private static final String PROJECT_NAME
= "TestForFiltering";
150 /** The Log4j logger instance. */
151 private static final Logger fLogger
= Logger
.getRootLogger();
152 private static SWTWorkbenchBot fBot
;
154 private static String
makeEvent(int ts
, int val
) {
155 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
158 private static File fFileLocation
;
161 * Initialization, creates a temp trace
163 * @throws IOException
167 public static void init() throws IOException
{
168 SWTBotUtils
.initialize();
169 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
170 /* set up for swtbot */
171 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
172 fLogger
.removeAllAppenders();
173 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
174 fBot
= new SWTWorkbenchBot();
176 SWTBotUtils
.closeView("welcome", fBot
);
178 SWTBotUtils
.switchToTracingPerspective();
179 /* finish waiting for eclipse to load */
180 WaitUtils
.waitForJobs();
181 fFileLocation
= File
.createTempFile("sample", ".xml");
182 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
183 braf
.writeBytes(TRACE_START
);
184 for (int i
= 0; i
< 100; i
++) {
185 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
187 braf
.writeBytes(TRACE_END
);
192 * Open a trace in an editor
195 public void beforeTest() {
196 SWTBotUtils
.createProject(PROJECT_NAME
);
197 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
198 assertNotNull(treeItem
);
199 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
200 SWTBotUtils
.openView(ColorsView
.ID
);
207 public static void cleanUp() {
208 fLogger
.removeAllAppenders();
209 fFileLocation
.delete();
216 public void tearDown() {
217 fBot
.closeAllEditors();
218 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
219 SWTBotUtils
.closeViewById(ColorsView
.ID
, fBot
);
223 * Test color by making all events yellow
226 public void testYellow() {
227 SWTBotView viewBot
= fBot
.viewById(ColorsView
.ID
);
229 final String insert
= "Insert new color setting";
230 final String increasePriority
= "Increase priority";
231 final String decreasePriority
= "Decrease priority";
232 final String delete
= "Delete color setting";
233 viewBot
.toolbarButton(insert
).click();
234 viewBot
.toolbarButton(insert
).click();
235 viewBot
.toolbarButton(insert
).click();
236 viewBot
.toolbarButton(insert
).click();
237 viewBot
.toolbarButton(increasePriority
).click();
238 viewBot
.toolbarButton(decreasePriority
).click();
239 viewBot
.toolbarButton(delete
).click();
240 viewBot
.bot().label(0).setFocus();
241 viewBot
.toolbarButton(delete
).click();
242 viewBot
.bot().label(0).setFocus();
243 viewBot
.toolbarButton(delete
).click();
244 final RGB foreground
= new RGB(0, 0, 0);
245 final RGB background
= new RGB(255, 255, 0);
246 // Simulate the side effects of picking a color because we cannot
247 // control native Color picker dialog in SWTBot.
248 final ColorSetting
[] cs
= new ColorSetting
[1];
249 UIThreadRunnable
.syncExec(new VoidResult() {
252 cs
[0] = new ColorSetting(foreground
, background
, foreground
, new PassAll());
253 ColorSettingsManager
.setColorSettings(cs
);
256 final SWTBotTable eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
).bot().table();
257 // should fix race condition of loading the trace
258 WaitUtils
.waitForJobs();
259 eventsEditor
.select(2);
260 final SWTBotTableItem tableItem
= eventsEditor
.getTableItem(2);
261 RGB fgc
= UIThreadRunnable
.syncExec(new Result
<RGB
>() {
264 return tableItem
.widget
.getForeground().getRGB();
267 RGB bgc
= UIThreadRunnable
.syncExec(new Result
<RGB
>() {
270 return tableItem
.widget
.getBackground().getRGB();
273 assertEquals("Fg", foreground
, fgc
);
274 assertEquals("Bg", background
, bgc
);
275 // reset color settings
276 UIThreadRunnable
.syncExec(new VoidResult() {
279 ColorSettingsManager
.setColorSettings(new ColorSetting
[0]);