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
.views
.colors
.ColorSetting
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorSettingsManager
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
.ColorsView
;
44 import org
.junit
.After
;
45 import org
.junit
.AfterClass
;
46 import org
.junit
.Before
;
47 import org
.junit
.BeforeClass
;
48 import org
.junit
.Test
;
49 import org
.junit
.runner
.RunWith
;
52 * Test for Colors views in trace compass
54 @RunWith(SWTBotJunit4ClassRunner
.class)
55 public class ColorsViewTest
{
57 private static final class PassAll
implements ITmfFilterTreeNode
{
59 public boolean matches(ITmfEvent event
) {
64 public void setParent(ITmfFilterTreeNode parent
) {
69 public ITmfFilterTreeNode
replaceChild(int index
, ITmfFilterTreeNode node
) {
74 public ITmfFilterTreeNode
removeChild(ITmfFilterTreeNode node
) {
79 public ITmfFilterTreeNode
remove() {
84 public boolean hasChildren() {
89 public List
<String
> getValidChildren() {
90 return Collections
.EMPTY_LIST
;
94 public ITmfFilterTreeNode
getParent() {
99 public String
getNodeName() {
104 public int getChildrenCount() {
109 public @NonNull ITmfFilterTreeNode
[] getChildren() {
114 public ITmfFilterTreeNode
getChild(int index
) {
119 public int addChild(ITmfFilterTreeNode node
) {
124 public ITmfFilterTreeNode
clone() {
129 public String
toString(boolean explicit
) {
134 public String
toString() {
135 return getNodeName();
139 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
141 private static final String TRACE_START
= "<trace>";
142 private static final String EVENT_BEGIN
= "<event timestamp=\"";
143 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
144 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
145 private static final String TRACE_END
= "</trace>";
147 private static final String PROJECT_NAME
= "TestForFiltering";
149 /** The Log4j logger instance. */
150 private static final Logger fLogger
= Logger
.getRootLogger();
151 private static SWTWorkbenchBot fBot
;
153 private static String
makeEvent(int ts
, int val
) {
154 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
157 private static File fFileLocation
;
160 * Initialization, creates a temp trace
162 * @throws IOException
166 public static void init() throws IOException
{
167 SWTBotUtils
.initialize();
168 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
169 /* set up for swtbot */
170 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
171 fLogger
.removeAllAppenders();
172 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
173 fBot
= new SWTWorkbenchBot();
175 SWTBotUtils
.closeView("welcome", fBot
);
177 SWTBotUtils
.switchToTracingPerspective();
178 /* finish waiting for eclipse to load */
179 SWTBotUtils
.waitForJobs();
180 fFileLocation
= File
.createTempFile("sample", ".xml");
181 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
182 braf
.writeBytes(TRACE_START
);
183 for (int i
= 0; i
< 100; i
++) {
184 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
186 braf
.writeBytes(TRACE_END
);
191 * Open a trace in an editor
194 public void beforeTest() {
195 SWTBotUtils
.createProject(PROJECT_NAME
);
196 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
197 assertNotNull(treeItem
);
198 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
199 SWTBotUtils
.openView(ColorsView
.ID
);
206 public static void cleanUp() {
207 fLogger
.removeAllAppenders();
208 fFileLocation
.delete();
215 public void tearDown() {
216 fBot
.closeAllEditors();
217 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
218 SWTBotUtils
.closeViewById(ColorsView
.ID
, fBot
);
222 * Test color by making all events yellow
225 public void testYellow() {
226 SWTBotView viewBot
= fBot
.viewById(ColorsView
.ID
);
228 final String insert
= "Insert new color setting";
229 final String increasePriority
= "Increase priority";
230 final String decreasePriority
= "Decrease priority";
231 final String delete
= "Delete color setting";
232 viewBot
.toolbarButton(insert
).click();
233 viewBot
.toolbarButton(insert
).click();
234 viewBot
.toolbarButton(insert
).click();
235 viewBot
.toolbarButton(insert
).click();
236 viewBot
.toolbarButton(increasePriority
).click();
237 viewBot
.toolbarButton(decreasePriority
).click();
238 viewBot
.toolbarButton(delete
).click();
239 viewBot
.bot().label(0).setFocus();
240 viewBot
.toolbarButton(delete
).click();
241 viewBot
.bot().label(0).setFocus();
242 viewBot
.toolbarButton(delete
).click();
243 final RGB foreground
= new RGB(0, 0, 0);
244 final RGB background
= new RGB(255, 255, 0);
245 // Simulate the side effects of picking a color because we cannot
246 // control native Color picker dialog in SWTBot.
247 final ColorSetting
[] cs
= new ColorSetting
[1];
248 UIThreadRunnable
.syncExec(new VoidResult() {
251 cs
[0] = new ColorSetting(foreground
, background
, foreground
, new PassAll());
252 ColorSettingsManager
.setColorSettings(cs
);
255 final SWTBotTable eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
).bot().table();
256 // should fix race condition of loading the trace
257 SWTBotUtils
.waitForJobs();
258 eventsEditor
.select(2);
259 final SWTBotTableItem tableItem
= eventsEditor
.getTableItem(2);
260 RGB fgc
= UIThreadRunnable
.syncExec(new Result
<RGB
>() {
263 return tableItem
.widget
.getForeground().getRGB();
266 RGB bgc
= UIThreadRunnable
.syncExec(new Result
<RGB
>() {
269 return tableItem
.widget
.getBackground().getRGB();
272 assertEquals("Fg", foreground
, fgc
);
273 assertEquals("Bg", background
, bgc
);
274 // reset color settings
275 UIThreadRunnable
.syncExec(new VoidResult() {
278 ColorSettingsManager
.setColorSettings(new ColorSetting
[0]);