Commit | Line | Data |
---|---|---|
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 | *******************************************************************************/ | |
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 | ||
d87a9633 MK |
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 | } | |
e883975e PT |
123 | |
124 | @Override | |
125 | public String toString(boolean explicit) { | |
126 | return toString(); | |
127 | } | |
128 | ||
129 | @Override | |
130 | public String toString() { | |
131 | return getNodeName(); | |
132 | } | |
d87a9633 MK |
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 { | |
5785ab49 | 163 | SWTBotUtils.initialize(); |
d87a9633 MK |
164 | Thread.currentThread().setName("SWTBot Thread"); // for the debugger |
165 | /* set up for swtbot */ | |
166 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ | |
f10c30a0 | 167 | fLogger.removeAllAppenders(); |
d87a9633 MK |
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() { | |
f10c30a0 | 203 | fLogger.removeAllAppenders(); |
d87a9633 MK |
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); | |
e4d15418 | 214 | SWTBotUtils.closeViewById(ColorsView.ID, fBot); |
d87a9633 MK |
215 | } |
216 | ||
217 | /** | |
218 | * Test color by making all events yellow | |
219 | */ | |
220 | @Test | |
221 | public void testYellow() { | |
222 | SWTBotView viewBot = fBot.viewById(ColorsView.ID); | |
223 | viewBot.setFocus(); | |
224 | final String insert = "Insert new color setting"; | |
225 | final String increasePriority = "Increase priority"; | |
226 | final String decreasePriority = "Decrease priority"; | |
227 | final String delete = "Delete color setting"; | |
228 | viewBot.toolbarButton(insert).click(); | |
229 | viewBot.toolbarButton(insert).click(); | |
230 | viewBot.toolbarButton(insert).click(); | |
231 | viewBot.toolbarButton(insert).click(); | |
232 | viewBot.toolbarButton(increasePriority).click(); | |
233 | viewBot.toolbarButton(decreasePriority).click(); | |
234 | viewBot.toolbarButton(delete).click(); | |
235 | viewBot.bot().label(0).setFocus(); | |
236 | viewBot.toolbarButton(delete).click(); | |
237 | viewBot.bot().label(0).setFocus(); | |
238 | viewBot.toolbarButton(delete).click(); | |
239 | final RGB foreground = new RGB(0, 0, 0); | |
240 | final RGB background = new RGB(255, 255, 0); | |
c776a34f | 241 | // Simulate the side effects of picking a color because we cannot |
d87a9633 MK |
242 | // control native Color picker dialog in SWTBot. |
243 | final ColorSetting[] cs = new ColorSetting[1]; | |
244 | UIThreadRunnable.syncExec(new VoidResult() { | |
245 | @Override | |
246 | public void run() { | |
247 | cs[0] = new ColorSetting(foreground, background, foreground, new PassAll()); | |
248 | ColorSettingsManager.setColorSettings(cs); | |
249 | } | |
250 | }); | |
a345ad25 | 251 | final SWTBotTable eventsEditor = SWTBotUtils.activeEventsEditor(fBot).bot().table(); |
c776a34f MK |
252 | // should fix race condition of loading the trace |
253 | SWTBotUtils.waitForJobs(); | |
d87a9633 MK |
254 | eventsEditor.select(2); |
255 | final SWTBotTableItem tableItem = eventsEditor.getTableItem(2); | |
256 | RGB fgc = UIThreadRunnable.syncExec(new Result<RGB>() { | |
257 | @Override | |
258 | public RGB run() { | |
259 | return tableItem.widget.getForeground().getRGB(); | |
260 | } | |
261 | }); | |
262 | RGB bgc = UIThreadRunnable.syncExec(new Result<RGB>() { | |
263 | @Override | |
264 | public RGB run() { | |
265 | return tableItem.widget.getBackground().getRGB(); | |
266 | } | |
267 | }); | |
268 | assertEquals("Fg", foreground, fgc); | |
269 | assertEquals("Bg", background, bgc); | |
c776a34f MK |
270 | // reset color settings |
271 | UIThreadRunnable.syncExec(new VoidResult() { | |
272 | @Override | |
273 | public void run() { | |
274 | ColorSettingsManager.setColorSettings(new ColorSetting[0]); | |
275 | } | |
276 | }); | |
d87a9633 MK |
277 | } |
278 | } |