7233b9595680492df8d17950fac1a2c2fa81a030
[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, 2016 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.finders.UIThreadRunnable;
42 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
43 import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
44 import org.eclipse.swtbot.swt.finder.results.Result;
45 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
46 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCLabel;
48 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCanvas;
49 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
50 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
51 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
52 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper;
53 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
54 import org.eclipse.ui.PlatformUI;
55 import org.junit.After;
56 import org.junit.AfterClass;
57 import org.junit.Before;
58 import org.junit.BeforeClass;
59 import org.junit.Test;
60 import org.junit.runner.RunWith;
61
62 import com.google.common.collect.Multiset;
63
64 /**
65 * SWTBot test for testing highlighting
66 */
67 @RunWith(SWTBotJunit4ClassRunner.class)
68 public class FilterColorEditorTest {
69
70 private static final int TIMESTAMP_COLUMN = 1;
71 private static final int SOURCE_COLUMN = 2;
72 private static final int MESSAGE_COLUMN = 6;
73 private static final RGB GREEN = new RGB(0, 255, 0);
74 private static final String HIGHLIGHT_COLOR_DEFINITION_ID = "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
75 private static final String TRACE_PROJECT_NAME = "test";
76 private static final String COLUMN_TRACE = "syslog_collapse";
77 private static final String COLUMN_TRACE_PATH = "testfiles/" + COLUMN_TRACE;
78 private static final String COLUMN_TRACE_TYPE = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
79
80 private static File fTestFile = null;
81
82 private static SWTWorkbenchBot fBot;
83
84 /** The Log4j logger instance. */
85 private static final Logger fLogger = Logger.getRootLogger();
86 private SWTBotTable fTableBot;
87 private static final int ROW = 8;
88 /** Expected color values */
89 private RGB fForeground;
90 private RGB fBackground;
91 private static RGB fHighlight;
92 private static RGB EXPECTED_GREEN;
93
94 /**
95 * Test Class setup
96 */
97 @BeforeClass
98 public static void init() {
99 SWTBotUtils.initialize();
100
101 /* set up test trace */
102 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null);
103 URI uri;
104 try {
105 uri = FileLocator.toFileURL(location).toURI();
106 fTestFile = new File(uri);
107 } catch (URISyntaxException | IOException e) {
108 fail(e.getMessage());
109 }
110
111 assumeTrue(fTestFile.exists());
112
113 /* Set up for swtbot */
114 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
115 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
116
117 fLogger.removeAllAppenders();
118 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
119 fBot = new SWTWorkbenchBot();
120
121 /* Close welcome view */
122 SWTBotUtils.closeView("Welcome", fBot);
123
124 /* Switch perspectives */
125 SWTBotUtils.switchToTracingPerspective();
126
127 /* Finish waiting for eclipse to load */
128 SWTBotUtils.waitForJobs();
129
130 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
131 fHighlight = ImageHelper.adjustExpectedColor(colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID).getRGB());
132 EXPECTED_GREEN = ImageHelper.adjustExpectedColor(GREEN);
133 }
134
135 /**
136 * Test class tear down method.
137 */
138 @AfterClass
139 public static void tearDown() {
140 fLogger.removeAllAppenders();
141 }
142
143 /**
144 * Bring up the table
145 */
146 @Before
147 public void setup() {
148 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
149
150 // Open the actual trace
151 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE);
152 SWTBotEditor editorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName());
153
154 fTableBot = editorBot.bot().table();
155 fBackground = fTableBot.backgroundColor().getRGB();
156 fForeground = fTableBot.foregroundColor().getRGB();
157
158 SWTBotUtils.maximizeTable(fTableBot);
159 }
160
161 /**
162 * Remove the project
163 */
164 @After
165 public void cleanup() {
166 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
167 SWTBotUtils.waitForJobs();
168 }
169
170 /**
171 * Test basic highlight
172 */
173 @Test
174 public void testHighlight() {
175 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
176
177 ImageHelper before = ImageHelper.grabImage(cellBounds);
178 Multiset<RGB> colorBefore = before.getHistogram();
179 // Select source column and enter regex
180 fTableBot.click(0, SOURCE_COLUMN);
181 fBot.text().typeText("HostF\n", 100);
182 // make sure selected row is not matching row
183 fTableBot.select(ROW - 1);
184 Multiset<RGB> colorAfter = waitForNewImage(cellBounds, before).getHistogram();
185
186 assertTrue(colorBefore.contains(fBackground));
187 assertTrue(colorBefore.contains(fForeground));
188 assertFalse(colorBefore.contains(fHighlight));
189
190 assertTrue(colorAfter.contains(fBackground));
191 assertTrue(colorAfter.contains(fForeground));
192 assertTrue(colorAfter.contains(fHighlight));
193
194 /*
195 * Check that some background became highlighted.
196 */
197 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
198 assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight));
199 }
200
201 /**
202 * Test highlighting multiple elements in a message
203 */
204 @Test
205 public void testMultiHighlightMessage() {
206 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, MESSAGE_COLUMN);
207
208 ImageHelper before = ImageHelper.grabImage(cellBounds);
209 // enter regex in message column
210 fTableBot.click(0, MESSAGE_COLUMN);
211 fBot.text().typeText("e\n", 100);
212 // make sure matching item is not selected
213 fTableBot.select(ROW - 1);
214 ImageHelper after = waitForNewImage(cellBounds, before);
215
216 Multiset<RGB> colorBefore = before.getHistogram();
217 Multiset<RGB> colorAfter = after.getHistogram();
218
219 assertTrue(colorBefore.contains(fBackground));
220 assertTrue(colorBefore.contains(fForeground));
221 assertFalse(colorBefore.contains(fHighlight));
222
223 assertTrue(colorAfter.contains(fBackground));
224 assertTrue(colorAfter.contains(fForeground));
225 assertTrue(colorAfter.contains(fHighlight));
226
227 int start = -1;
228 int end;
229 List<Point> intervals = new ArrayList<>();
230 List<RGB> pixelRow = after.getPixelRow(2);
231 for (int i = 1; i < pixelRow.size(); i++) {
232 RGB prevPixel = pixelRow.get(i - 1);
233 RGB pixel = pixelRow.get(i);
234 if (prevPixel.equals(fBackground) && pixel.equals(fHighlight)) {
235 start = i;
236 }
237 if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) {
238 end = i;
239 if (start == -1) {
240 fail();
241 }
242 intervals.add(new Point(start, end));
243 }
244 }
245 assertEquals(2, intervals.size());
246 }
247
248 /**
249 * Switch to filter and back
250 */
251 @Test
252 public void testSwitchToFilter() {
253 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
254 ImageHelper before = ImageHelper.grabImage(cellBounds);
255
256 // enter regex in Timestamp column
257 fTableBot.click(0, TIMESTAMP_COLUMN);
258 fBot.text().typeText("00\n", 100);
259 // make sure matching column is not selected
260 fTableBot.select(ROW - 1);
261 ImageHelper afterSearch = waitForNewImage(cellBounds, before);
262
263 // click Add as Filter
264 fTableBot.click(0, 0);
265 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "<srch>", 0, TIMESTAMP_COLUMN));
266 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "22/22", 1, TIMESTAMP_COLUMN));
267 // the bounds have changed after applying the filter
268 cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
269 ImageHelper afterFilter = ImageHelper.grabImage(cellBounds);
270
271 // press DEL to clear highlighting
272 fTableBot.pressShortcut(Keystrokes.DELETE);
273 ImageHelper afterClear = waitForNewImage(cellBounds, afterFilter);
274
275 List<RGB> beforeLine = before.getPixelRow(2);
276 List<RGB> afterSearchLine = afterSearch.getPixelRow(2);
277 List<RGB> afterFilterLine = afterFilter.getPixelRow(2);
278 List<RGB> afterClearLine = afterClear.getPixelRow(2);
279
280 assertEquals(beforeLine.size(), afterSearchLine.size());
281 assertEquals(beforeLine.size(), afterFilterLine.size());
282 assertEquals(beforeLine.size(), afterClearLine.size());
283 for (int i = 0; i < beforeLine.size(); i++) {
284 RGB beforePixel = beforeLine.get(i);
285 RGB afterSearchPixel = afterSearchLine.get(i);
286 RGB afterFilterPixel = afterFilterLine.get(i);
287 RGB afterClearPixel = afterClearLine.get(i);
288
289 assertEquals(afterSearchPixel, afterFilterPixel);
290 assertEquals(beforePixel, afterClearPixel);
291 if (!afterSearchPixel.equals(fHighlight)) {
292 assertEquals(beforePixel, afterSearchPixel);
293 } else {
294 assertNotEquals(fHighlight, beforePixel);
295 }
296
297 }
298 assertEquals(afterSearchLine, afterFilterLine);
299 assertEquals(beforeLine, afterClearLine);
300 assertNotEquals(afterSearchLine, beforeLine);
301 }
302
303 /**
304 * Test highlight color preference
305 */
306 @Test
307 public void testPreference() {
308 // change the highlight color preference
309 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
310 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, GREEN);
311
312 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
313
314 ImageHelper before = ImageHelper.grabImage(cellBounds);
315 Multiset<RGB> colorBefore = before.getHistogram();
316 // Select source column and enter regex
317 fTableBot.click(0, SOURCE_COLUMN);
318 fBot.text().typeText("HostF\n", 100);
319 // make sure selected row is not matching row
320 fTableBot.select(ROW - 1);
321 Multiset<RGB> colorAfter = waitForNewImage(cellBounds, before).getHistogram();
322
323 assertTrue(colorBefore.contains(fBackground));
324 assertTrue(colorBefore.contains(fForeground));
325 assertFalse(colorBefore.contains(fHighlight));
326 assertFalse(colorBefore.contains(EXPECTED_GREEN));
327
328 assertTrue(colorAfter.contains(fBackground));
329 assertTrue(colorAfter.contains(fForeground));
330 assertFalse(colorAfter.contains(fHighlight));
331 assertTrue(colorAfter.contains(EXPECTED_GREEN));
332
333 /*
334 * Check that some background became green.
335 */
336 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
337 assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN));
338
339 // reset the highlight color preference
340 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight);
341 }
342
343 /**
344 * Test the header bar
345 */
346 @Test
347 public void testHeaderBar() {
348 // Add search filter on Timestamp column
349 fTableBot.click(0, TIMESTAMP_COLUMN);
350 fBot.text().typeText("2");
351 fBot.text().pressShortcut(Keystrokes.CR);
352 // Add search filter on Message column and Add as Filter
353 fTableBot.click(0, MESSAGE_COLUMN);
354 fBot.text().typeText("F");
355 fBot.text().pressShortcut(Keystrokes.CTRL, Keystrokes.CR);
356 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "2/22", 1, 1));
357 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "Message F", 3, MESSAGE_COLUMN));
358 fBot.clabel("Timestamp matches \"2\"");
359 fBot.clabel("Message matches \"F\"");
360 waitForHighlightState(3, TIMESTAMP_COLUMN, true);
361 waitForHighlightState(3, MESSAGE_COLUMN, true);
362 // Clear all filter highlighting
363 fTableBot.pressShortcut(Keystrokes.DELETE);
364 waitForHighlightState(3, TIMESTAMP_COLUMN, false);
365 waitForHighlightState(3, MESSAGE_COLUMN, false);
366 // Click filter label to set the filter highlighting on Message filter
367 SWTBotCLabel filterCLabel = fBot.clabel("Message matches \"F\"");
368 SWTBotCanvas filterCanvas = new SWTBotCanvas(filterCLabel.widget);
369 filterCanvas.click();
370 waitForHighlightState(3, TIMESTAMP_COLUMN, false);
371 waitForHighlightState(3, MESSAGE_COLUMN, true);
372 // Click filter icon to remove the Message filter
373 Rectangle imageBounds = filterCLabel.image().getBounds();
374 filterCanvas.click(filterCLabel.widget.getLeftMargin() + imageBounds.width / 2, filterCLabel.widget.getTopMargin() + imageBounds.height / 2);
375 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "5/22", 1, 1));
376 }
377
378 private void waitForHighlightState(int row, int column, boolean highlight) {
379 fBot.waitUntil(new DefaultCondition() {
380 @Override
381 public boolean test() throws Exception {
382 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, row, column);
383 ImageHelper imageHelper = ImageHelper.grabImage(cellBounds);
384 return imageHelper.getHistogram().contains(fHighlight) == highlight;
385 }
386
387 @Override
388 public String getFailureMessage() {
389 return String.format("Cell (%d, %d) did not have highlight state: %s", row, column, Boolean.toString(highlight));
390 }
391 });
392 }
393
394 private static ImageHelper waitForNewImage(Rectangle bounds, ImageHelper currentImage) {
395 ImageHelper[] newImage = new ImageHelper[1];
396 fBot.waitUntil(new DefaultCondition() {
397 @Override
398 public boolean test() throws Exception {
399 return UIThreadRunnable.syncExec(new Result<Boolean> () {
400 @Override
401 public Boolean run() {
402 newImage[0] = ImageHelper.grabImage(bounds);
403 return !newImage[0].equals(currentImage);
404 }
405 });
406 }
407
408 @Override
409 public String getFailureMessage() {
410 return "Image at bounds " + bounds + " did not change";
411 }
412 });
413 return newImage[0];
414 }
415 }
This page took 0.039249 seconds and 4 git commands to generate.