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