tmf: FilterColorEditorTest on Mac
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / FilterColorEditorTest.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
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotEquals;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20 import static org.junit.Assume.assumeTrue;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.log4j.ConsoleAppender;
31 import org.apache.log4j.Logger;
32 import org.apache.log4j.SimpleLayout;
33 import org.eclipse.core.runtime.FileLocator;
34 import org.eclipse.core.runtime.Path;
35 import org.eclipse.jface.resource.ColorRegistry;
36 import org.eclipse.swt.graphics.Point;
37 import org.eclipse.swt.graphics.RGB;
38 import org.eclipse.swt.graphics.Rectangle;
39 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
40 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
41 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
42 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
43 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
44 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
45 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
46 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper;
47 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
48 import org.eclipse.ui.PlatformUI;
49 import org.junit.After;
50 import org.junit.AfterClass;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55
56 import com.google.common.collect.Multiset;
57
58 /**
59 * SWTBot test for testing highlighting
60 */
61 @RunWith(SWTBotJunit4ClassRunner.class)
62 public class FilterColorEditorTest {
63
64 private static final int TIMESTAMP_COLUMN = 1;
65 private static final int SOURCE_COLUMN = 2;
66 private static final int MESSAGE_COLUMN = 6;
67 private static final RGB GREEN = new RGB(0, 255, 0);
68 private static final String HIGHLIGHT_COLOR_DEFINITION_ID = "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
69 private static final String TRACE_PROJECT_NAME = "test";
70 private static final String COLUMN_TRACE = "syslog_collapse";
71 private static final String COLUMN_TRACE_PATH = "testfiles/" + COLUMN_TRACE;
72 private static final String COLUMN_TRACE_TYPE = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
73
74 private static File fTestFile = null;
75
76 private static SWTWorkbenchBot fBot;
77
78 /** The Log4j logger instance. */
79 private static final Logger fLogger = Logger.getRootLogger();
80 private SWTBotTable fTableBot;
81 private static final int ROW = 8;
82 /** Expected color values */
83 private RGB fForeground;
84 private RGB fBackground;
85 private static RGB fHighlight;
86 private static RGB EXPECTED_GREEN;
87
88 /**
89 * Test Class setup
90 */
91 @BeforeClass
92 public static void init() {
93 SWTBotUtils.initialize();
94
95 /* set up test trace */
96 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null);
97 URI uri;
98 try {
99 uri = FileLocator.toFileURL(location).toURI();
100 fTestFile = new File(uri);
101 } catch (URISyntaxException | IOException e) {
102 fail(e.getMessage());
103 }
104
105 assumeTrue(fTestFile.exists());
106
107 /* Set up for swtbot */
108 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
109 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
110
111 fLogger.removeAllAppenders();
112 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
113 fBot = new SWTWorkbenchBot();
114
115 /* Close welcome view */
116 SWTBotUtils.closeView("Welcome", fBot);
117
118 /* Switch perspectives */
119 SWTBotUtils.switchToTracingPerspective();
120
121 /* Finish waiting for eclipse to load */
122 SWTBotUtils.waitForJobs();
123
124 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
125 fHighlight = ImageHelper.adjustExpectedColor(colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID).getRGB());
126 EXPECTED_GREEN = ImageHelper.adjustExpectedColor(GREEN);
127 }
128
129 /**
130 * Test class tear down method.
131 */
132 @AfterClass
133 public static void tearDown() {
134 fLogger.removeAllAppenders();
135 }
136
137 /**
138 * Bring up the table
139 */
140 @Before
141 public void setup() {
142 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
143
144 // Open the actual trace
145 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE);
146 SWTBotEditor editorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName());
147
148 fTableBot = editorBot.bot().table();
149 fBackground = fTableBot.backgroundColor().getRGB();
150 fForeground = fTableBot.foregroundColor().getRGB();
151
152 SWTBotUtils.maximizeTable(fTableBot);
153 }
154
155 /**
156 * Remove the project
157 */
158 @After
159 public void cleanup() {
160 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
161 SWTBotUtils.waitForJobs();
162 }
163
164 /**
165 * Test basic highlight
166 */
167 @Test
168 public void testHighlight() {
169 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
170
171 Multiset<RGB> colorBefore = ImageHelper.grabImage(cellBounds).getHistogram();
172 // Select source column and enter regex
173 fTableBot.click(0, SOURCE_COLUMN);
174 fBot.text().typeText("HostF\n", 100);
175 // make sure selected row is not matching row
176 fTableBot.select(ROW - 1);
177 Multiset<RGB> colorAfter = ImageHelper.grabImage(cellBounds).getHistogram();
178
179 assertTrue(colorBefore.contains(fBackground));
180 assertTrue(colorBefore.contains(fForeground));
181 assertFalse(colorBefore.contains(fHighlight));
182
183 assertTrue(colorAfter.contains(fBackground));
184 assertTrue(colorAfter.contains(fForeground));
185 assertTrue(colorAfter.contains(fHighlight));
186
187 /*
188 * Check that some background became highlighted.
189 */
190 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
191 assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight));
192 }
193
194 /**
195 * Test highlighting multiple elements in a message
196 */
197 @Test
198 public void testMultiHighlightMessage() {
199 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, MESSAGE_COLUMN);
200
201 ImageHelper before = ImageHelper.grabImage(cellBounds);
202 // enter regex in message column
203 fTableBot.click(0, MESSAGE_COLUMN);
204 fBot.text().typeText("e\n", 100);
205 // make sure matching item is not selected
206 fTableBot.select(ROW - 1);
207 ImageHelper after = ImageHelper.grabImage(cellBounds);
208
209 Multiset<RGB> colorBefore = before.getHistogram();
210 Multiset<RGB> colorAfter = after.getHistogram();
211
212 assertTrue(colorBefore.contains(fBackground));
213 assertTrue(colorBefore.contains(fForeground));
214 assertFalse(colorBefore.contains(fHighlight));
215
216 assertTrue(colorAfter.contains(fBackground));
217 assertTrue(colorAfter.contains(fForeground));
218 assertTrue(colorAfter.contains(fHighlight));
219
220 int start = -1;
221 int end;
222 List<Point> intervals = new ArrayList<>();
223 List<RGB> pixelRow = after.getPixelRow(2);
224 for (int i = 1; i < pixelRow.size(); i++) {
225 RGB prevPixel = pixelRow.get(i - 1);
226 RGB pixel = pixelRow.get(i);
227 if (prevPixel.equals(fBackground) && pixel.equals(fHighlight)) {
228 start = i;
229 }
230 if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) {
231 end = i;
232 if (start == -1) {
233 fail();
234 }
235 intervals.add(new Point(start, end));
236 }
237 }
238 assertEquals(2, intervals.size());
239 }
240
241 /**
242 * Switch to filter and back
243 */
244 @Test
245 public void testSwitchToFilter() {
246 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
247 ImageHelper before = ImageHelper.grabImage(cellBounds);
248 // enter regex in message column
249 fTableBot.click(0, TIMESTAMP_COLUMN);
250 fBot.text().typeText("00\n", 100);
251 // make sure matching column is not selected
252 fTableBot.select(ROW - 1);
253 ImageHelper after = ImageHelper.grabImage(cellBounds);
254 // toggle filter mode
255 fTableBot.click(0, 0);
256 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "<filter>", 0, 1));
257 //TODO: We need a better way to make sure that the table is done updating
258 SWTBotUtils.delay(2000);
259 ImageHelper afterFilter = ImageHelper.grabImage(cellBounds);
260
261 List<RGB> beforeLine = before.getPixelRow(2);
262 List<RGB> afterLine = after.getPixelRow(2);
263 List<RGB> afterFilterLine = afterFilter.getPixelRow(2);
264
265 assertEquals(beforeLine.size(), afterLine.size());
266 assertEquals(beforeLine.size(), afterFilterLine.size());
267 for (int i = 0; i < beforeLine.size(); i++) {
268 RGB afterFilterPixel = afterFilterLine.get(i);
269 RGB beforePixel = beforeLine.get(i);
270 RGB afterPixel = afterLine.get(i);
271
272 assertEquals(beforePixel, afterFilterPixel);
273 if (!afterPixel.equals(fHighlight)) {
274 assertEquals(beforePixel, afterPixel);
275 } else {
276 assertNotEquals(fHighlight, beforePixel);
277 }
278
279 }
280 assertEquals(beforeLine, afterFilterLine);
281 assertNotEquals(afterLine, beforeLine);
282 }
283
284 /**
285 * Test highlight color preference
286 */
287 @Test
288 public void testPreference() {
289 // change the highlight color preference
290 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
291 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, GREEN);
292
293 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
294
295 Multiset<RGB> colorBefore = ImageHelper.grabImage(cellBounds).getHistogram();
296 // Select source column and enter regex
297 fTableBot.click(0, SOURCE_COLUMN);
298 fBot.text().typeText("HostF\n", 100);
299 // make sure selected row is not matching row
300 fTableBot.select(ROW - 1);
301 Multiset<RGB> colorAfter = ImageHelper.grabImage(cellBounds).getHistogram();
302
303 assertTrue(colorBefore.contains(fBackground));
304 assertTrue(colorBefore.contains(fForeground));
305 assertFalse(colorBefore.contains(fHighlight));
306 assertFalse(colorBefore.contains(EXPECTED_GREEN));
307
308 assertTrue(colorAfter.contains(fBackground));
309 assertTrue(colorAfter.contains(fForeground));
310 assertFalse(colorAfter.contains(fHighlight));
311 assertTrue(colorAfter.contains(EXPECTED_GREEN));
312
313 /*
314 * Check that some background became green.
315 */
316 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
317 assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN));
318
319 // reset the highlight color preference
320 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight);
321 }
322 }
This page took 0.039243 seconds and 5 git commands to generate.