Fix some null warnings
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / ColorsViewTest.java
CommitLineData
d87a9633
MK
1/*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertNotNull;
16
17import java.io.File;
18import java.io.IOException;
19import java.util.Collections;
20import java.util.List;
21
22import org.apache.log4j.ConsoleAppender;
23import org.apache.log4j.Logger;
24import org.apache.log4j.SimpleLayout;
aa353506 25import org.eclipse.jdt.annotation.NonNull;
d87a9633
MK
26import org.eclipse.swt.graphics.RGB;
27import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
30import org.eclipse.swtbot.swt.finder.results.Result;
31import org.eclipse.swtbot.swt.finder.results.VoidResult;
32import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
33import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
34import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
36import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
37import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode;
38import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
39import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
40import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSetting;
41import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSettingsManager;
42import org.eclipse.tracecompass.tmf.ui.views.colors.ColorsView;
43import org.junit.After;
44import org.junit.AfterClass;
45import org.junit.Before;
46import org.junit.BeforeClass;
47import org.junit.Test;
48
49/**
50 * Test for Colors views in trace compass
51 */
52public class ColorsViewTest {
53
54 private static final class PassAll implements ITmfFilterTreeNode {
55 @Override
56 public boolean matches(ITmfEvent event) {
57 return true;
58 }
59
d87a9633
MK
60 @Override
61 public void setParent(ITmfFilterTreeNode parent) {
62
63 }
64
65 @Override
66 public ITmfFilterTreeNode replaceChild(int index, ITmfFilterTreeNode node) {
67 return null;
68 }
69
70 @Override
71 public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) {
72 return null;
73 }
74
75 @Override
76 public ITmfFilterTreeNode remove() {
77 return null;
78 }
79
80 @Override
81 public boolean hasChildren() {
82 return false;
83 }
84
85 @Override
86 public List<String> getValidChildren() {
87 return Collections.EMPTY_LIST;
88 }
89
90 @Override
91 public ITmfFilterTreeNode getParent() {
92 return null;
93 }
94
95 @Override
96 public String getNodeName() {
97 return "YES";
98 }
99
100 @Override
101 public int getChildrenCount() {
102 return 0;
103 }
104
105 @Override
aa353506 106 public @NonNull ITmfFilterTreeNode[] getChildren() {
d87a9633
MK
107 return null;
108 }
109
110 @Override
111 public ITmfFilterTreeNode getChild(int index) {
112 return null;
113 }
114
115 @Override
116 public int addChild(ITmfFilterTreeNode node) {
117 return 0;
118 }
119
120 @Override
121 public ITmfFilterTreeNode clone() {
122 return null;
123 }
e883975e
PT
124
125 @Override
126 public String toString(boolean explicit) {
127 return toString();
128 }
129
130 @Override
131 public String toString() {
132 return getNodeName();
133 }
d87a9633
MK
134 }
135
136 private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
137
138 private static final String TRACE_START = "<trace>";
139 private static final String EVENT_BEGIN = "<event timestamp=\"";
140 private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\"";
141 private static final String EVENT_END = "\" type=\"int\" />" + "</event>";
142 private static final String TRACE_END = "</trace>";
143
144 private static final String PROJECT_NAME = "TestForFiltering";
145
146 /** The Log4j logger instance. */
147 private static final Logger fLogger = Logger.getRootLogger();
148 private static SWTWorkbenchBot fBot;
149
150 private static String makeEvent(int ts, int val) {
151 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
152 }
153
154 private static File fFileLocation;
155
156 /**
157 * Initialization, creates a temp trace
158 *
159 * @throws IOException
160 * should not happen
161 */
162 @BeforeClass
163 public static void init() throws IOException {
5785ab49 164 SWTBotUtils.initialize();
d87a9633
MK
165 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
166 /* set up for swtbot */
167 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
f10c30a0 168 fLogger.removeAllAppenders();
d87a9633
MK
169 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
170 fBot = new SWTWorkbenchBot();
171
172 SWTBotUtils.closeView("welcome", fBot);
173
174 SWTBotUtils.switchToTracingPerspective();
175 /* finish waiting for eclipse to load */
176 SWTBotUtils.waitForJobs();
177 fFileLocation = File.createTempFile("sample", ".xml");
178 try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fFileLocation, "rw")) {
179 braf.writeBytes(TRACE_START);
180 for (int i = 0; i < 100; i++) {
181 braf.writeBytes(makeEvent(i * 100, i % 4));
182 }
183 braf.writeBytes(TRACE_END);
184 }
185 }
186
187 /**
188 * Open a trace in an editor
189 */
190 @Before
191 public void beforeTest() {
192 SWTBotUtils.createProject(PROJECT_NAME);
193 SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(fBot, PROJECT_NAME);
194 assertNotNull(treeItem);
195 SWTBotUtils.openTrace(PROJECT_NAME, fFileLocation.getAbsolutePath(), XMLSTUB_ID);
196 SWTBotUtils.openView(ColorsView.ID);
197 }
198
199 /**
200 * Delete the file
201 */
202 @AfterClass
203 public static void cleanUp() {
f10c30a0 204 fLogger.removeAllAppenders();
d87a9633
MK
205 fFileLocation.delete();
206 }
207
208 /**
209 * Close the editor
210 */
211 @After
212 public void tearDown() {
213 fBot.closeAllEditors();
214 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
e4d15418 215 SWTBotUtils.closeViewById(ColorsView.ID, fBot);
d87a9633
MK
216 }
217
218 /**
219 * Test color by making all events yellow
220 */
221 @Test
222 public void testYellow() {
223 SWTBotView viewBot = fBot.viewById(ColorsView.ID);
224 viewBot.setFocus();
225 final String insert = "Insert new color setting";
226 final String increasePriority = "Increase priority";
227 final String decreasePriority = "Decrease priority";
228 final String delete = "Delete color setting";
229 viewBot.toolbarButton(insert).click();
230 viewBot.toolbarButton(insert).click();
231 viewBot.toolbarButton(insert).click();
232 viewBot.toolbarButton(insert).click();
233 viewBot.toolbarButton(increasePriority).click();
234 viewBot.toolbarButton(decreasePriority).click();
235 viewBot.toolbarButton(delete).click();
236 viewBot.bot().label(0).setFocus();
237 viewBot.toolbarButton(delete).click();
238 viewBot.bot().label(0).setFocus();
239 viewBot.toolbarButton(delete).click();
240 final RGB foreground = new RGB(0, 0, 0);
241 final RGB background = new RGB(255, 255, 0);
c776a34f 242 // Simulate the side effects of picking a color because we cannot
d87a9633
MK
243 // control native Color picker dialog in SWTBot.
244 final ColorSetting[] cs = new ColorSetting[1];
245 UIThreadRunnable.syncExec(new VoidResult() {
246 @Override
247 public void run() {
248 cs[0] = new ColorSetting(foreground, background, foreground, new PassAll());
249 ColorSettingsManager.setColorSettings(cs);
250 }
251 });
a345ad25 252 final SWTBotTable eventsEditor = SWTBotUtils.activeEventsEditor(fBot).bot().table();
c776a34f
MK
253 // should fix race condition of loading the trace
254 SWTBotUtils.waitForJobs();
d87a9633
MK
255 eventsEditor.select(2);
256 final SWTBotTableItem tableItem = eventsEditor.getTableItem(2);
257 RGB fgc = UIThreadRunnable.syncExec(new Result<RGB>() {
258 @Override
259 public RGB run() {
260 return tableItem.widget.getForeground().getRGB();
261 }
262 });
263 RGB bgc = UIThreadRunnable.syncExec(new Result<RGB>() {
264 @Override
265 public RGB run() {
266 return tableItem.widget.getBackground().getRGB();
267 }
268 });
269 assertEquals("Fg", foreground, fgc);
270 assertEquals("Bg", background, bgc);
c776a34f
MK
271 // reset color settings
272 UIThreadRunnable.syncExec(new VoidResult() {
273 @Override
274 public void run() {
275 ColorSettingsManager.setColorSettings(new ColorSetting[0]);
276 }
277 });
d87a9633
MK
278 }
279}
This page took 0.055149 seconds and 5 git commands to generate.