tmf.test: Use SWTBotJunit4ClassRunner in every SWTBot test
[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;
9afd04ae 30import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
d87a9633
MK
31import org.eclipse.swtbot.swt.finder.results.Result;
32import org.eclipse.swtbot.swt.finder.results.VoidResult;
33import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
34import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
35import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
36import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
37import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
38import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode;
39import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
40import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
41import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSetting;
42import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSettingsManager;
43import org.eclipse.tracecompass.tmf.ui.views.colors.ColorsView;
44import org.junit.After;
45import org.junit.AfterClass;
46import org.junit.Before;
47import org.junit.BeforeClass;
48import org.junit.Test;
9afd04ae 49import org.junit.runner.RunWith;
d87a9633
MK
50
51/**
52 * Test for Colors views in trace compass
53 */
9afd04ae 54@RunWith(SWTBotJunit4ClassRunner.class)
d87a9633
MK
55public class ColorsViewTest {
56
57 private static final class PassAll implements ITmfFilterTreeNode {
58 @Override
59 public boolean matches(ITmfEvent event) {
60 return true;
61 }
62
d87a9633
MK
63 @Override
64 public void setParent(ITmfFilterTreeNode parent) {
65
66 }
67
68 @Override
69 public ITmfFilterTreeNode replaceChild(int index, ITmfFilterTreeNode node) {
70 return null;
71 }
72
73 @Override
74 public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) {
75 return null;
76 }
77
78 @Override
79 public ITmfFilterTreeNode remove() {
80 return null;
81 }
82
83 @Override
84 public boolean hasChildren() {
85 return false;
86 }
87
88 @Override
89 public List<String> getValidChildren() {
90 return Collections.EMPTY_LIST;
91 }
92
93 @Override
94 public ITmfFilterTreeNode getParent() {
95 return null;
96 }
97
98 @Override
99 public String getNodeName() {
100 return "YES";
101 }
102
103 @Override
104 public int getChildrenCount() {
105 return 0;
106 }
107
108 @Override
aa353506 109 public @NonNull ITmfFilterTreeNode[] getChildren() {
d87a9633
MK
110 return null;
111 }
112
113 @Override
114 public ITmfFilterTreeNode getChild(int index) {
115 return null;
116 }
117
118 @Override
119 public int addChild(ITmfFilterTreeNode node) {
120 return 0;
121 }
122
123 @Override
124 public ITmfFilterTreeNode clone() {
125 return null;
126 }
e883975e
PT
127
128 @Override
129 public String toString(boolean explicit) {
130 return toString();
131 }
132
133 @Override
134 public String toString() {
135 return getNodeName();
136 }
d87a9633
MK
137 }
138
139 private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
140
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>";
146
147 private static final String PROJECT_NAME = "TestForFiltering";
148
149 /** The Log4j logger instance. */
150 private static final Logger fLogger = Logger.getRootLogger();
151 private static SWTWorkbenchBot fBot;
152
153 private static String makeEvent(int ts, int val) {
154 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
155 }
156
157 private static File fFileLocation;
158
159 /**
160 * Initialization, creates a temp trace
161 *
162 * @throws IOException
163 * should not happen
164 */
165 @BeforeClass
166 public static void init() throws IOException {
5785ab49 167 SWTBotUtils.initialize();
d87a9633
MK
168 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
169 /* set up for swtbot */
170 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
f10c30a0 171 fLogger.removeAllAppenders();
d87a9633
MK
172 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
173 fBot = new SWTWorkbenchBot();
174
175 SWTBotUtils.closeView("welcome", fBot);
176
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));
185 }
186 braf.writeBytes(TRACE_END);
187 }
188 }
189
190 /**
191 * Open a trace in an editor
192 */
193 @Before
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);
200 }
201
202 /**
203 * Delete the file
204 */
205 @AfterClass
206 public static void cleanUp() {
f10c30a0 207 fLogger.removeAllAppenders();
d87a9633
MK
208 fFileLocation.delete();
209 }
210
211 /**
212 * Close the editor
213 */
214 @After
215 public void tearDown() {
216 fBot.closeAllEditors();
217 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
e4d15418 218 SWTBotUtils.closeViewById(ColorsView.ID, fBot);
d87a9633
MK
219 }
220
221 /**
222 * Test color by making all events yellow
223 */
224 @Test
225 public void testYellow() {
226 SWTBotView viewBot = fBot.viewById(ColorsView.ID);
227 viewBot.setFocus();
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);
c776a34f 245 // Simulate the side effects of picking a color because we cannot
d87a9633
MK
246 // control native Color picker dialog in SWTBot.
247 final ColorSetting[] cs = new ColorSetting[1];
248 UIThreadRunnable.syncExec(new VoidResult() {
249 @Override
250 public void run() {
251 cs[0] = new ColorSetting(foreground, background, foreground, new PassAll());
252 ColorSettingsManager.setColorSettings(cs);
253 }
254 });
a345ad25 255 final SWTBotTable eventsEditor = SWTBotUtils.activeEventsEditor(fBot).bot().table();
c776a34f
MK
256 // should fix race condition of loading the trace
257 SWTBotUtils.waitForJobs();
d87a9633
MK
258 eventsEditor.select(2);
259 final SWTBotTableItem tableItem = eventsEditor.getTableItem(2);
260 RGB fgc = UIThreadRunnable.syncExec(new Result<RGB>() {
261 @Override
262 public RGB run() {
263 return tableItem.widget.getForeground().getRGB();
264 }
265 });
266 RGB bgc = UIThreadRunnable.syncExec(new Result<RGB>() {
267 @Override
268 public RGB run() {
269 return tableItem.widget.getBackground().getRGB();
270 }
271 });
272 assertEquals("Fg", foreground, fgc);
273 assertEquals("Bg", background, bgc);
c776a34f
MK
274 // reset color settings
275 UIThreadRunnable.syncExec(new VoidResult() {
276 @Override
277 public void run() {
278 ColorSettingsManager.setColorSettings(new ColorSetting[0]);
279 }
280 });
d87a9633
MK
281 }
282}
This page took 0.115519 seconds and 5 git commands to generate.