analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / ColorsViewTest.java
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 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.List;
21
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;
47
48 /**
49 * Test for Colors views in trace compass
50 */
51 public class ColorsViewTest {
52
53 private static final class PassAll implements ITmfFilterTreeNode {
54 @Override
55 public boolean matches(ITmfEvent event) {
56 return true;
57 }
58
59 @Override
60 public void setParent(ITmfFilterTreeNode parent) {
61
62 }
63
64 @Override
65 public ITmfFilterTreeNode replaceChild(int index, ITmfFilterTreeNode node) {
66 return null;
67 }
68
69 @Override
70 public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) {
71 return null;
72 }
73
74 @Override
75 public ITmfFilterTreeNode remove() {
76 return null;
77 }
78
79 @Override
80 public boolean hasChildren() {
81 return false;
82 }
83
84 @Override
85 public List<String> getValidChildren() {
86 return Collections.EMPTY_LIST;
87 }
88
89 @Override
90 public ITmfFilterTreeNode getParent() {
91 return null;
92 }
93
94 @Override
95 public String getNodeName() {
96 return "YES";
97 }
98
99 @Override
100 public int getChildrenCount() {
101 return 0;
102 }
103
104 @Override
105 public ITmfFilterTreeNode[] getChildren() {
106 return null;
107 }
108
109 @Override
110 public ITmfFilterTreeNode getChild(int index) {
111 return null;
112 }
113
114 @Override
115 public int addChild(ITmfFilterTreeNode node) {
116 return 0;
117 }
118
119 @Override
120 public ITmfFilterTreeNode clone() {
121 return null;
122 }
123
124 @Override
125 public String toString(boolean explicit) {
126 return toString();
127 }
128
129 @Override
130 public String toString() {
131 return getNodeName();
132 }
133 }
134
135 private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
136
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>";
142
143 private static final String PROJECT_NAME = "TestForFiltering";
144
145 /** The Log4j logger instance. */
146 private static final Logger fLogger = Logger.getRootLogger();
147 private static SWTWorkbenchBot fBot;
148
149 private static String makeEvent(int ts, int val) {
150 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
151 }
152
153 private static File fFileLocation;
154
155 /**
156 * Initialization, creates a temp trace
157 *
158 * @throws IOException
159 * should not happen
160 */
161 @BeforeClass
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();
170
171 SWTBotUtils.closeView("welcome", fBot);
172
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));
181 }
182 braf.writeBytes(TRACE_END);
183 }
184 }
185
186 /**
187 * Open a trace in an editor
188 */
189 @Before
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);
196 }
197
198 /**
199 * Delete the file
200 */
201 @AfterClass
202 public static void cleanUp() {
203 fLogger.removeAllAppenders();
204 fFileLocation.delete();
205 }
206
207 /**
208 * Close the editor
209 */
210 @After
211 public void tearDown() {
212 fBot.closeAllEditors();
213 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
214 }
215
216 /**
217 * Test color by making all events yellow
218 */
219 @Test
220 public void testYellow() {
221 SWTBotView viewBot = fBot.viewById(ColorsView.ID);
222 viewBot.setFocus();
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() {
244 @Override
245 public void run() {
246 cs[0] = new ColorSetting(foreground, background, foreground, new PassAll());
247 ColorSettingsManager.setColorSettings(cs);
248 }
249 });
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>() {
256 @Override
257 public RGB run() {
258 return tableItem.widget.getForeground().getRGB();
259 }
260 });
261 RGB bgc = UIThreadRunnable.syncExec(new Result<RGB>() {
262 @Override
263 public RGB run() {
264 return tableItem.widget.getBackground().getRGB();
265 }
266 });
267 assertEquals("Fg", foreground, fgc);
268 assertEquals("Bg", background, bgc);
269 // reset color settings
270 UIThreadRunnable.syncExec(new VoidResult() {
271 @Override
272 public void run() {
273 ColorSettingsManager.setColorSettings(new ColorSetting[0]);
274 }
275 });
276 }
277 }
This page took 0.039144 seconds and 5 git commands to generate.