1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
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
;
23 import java
.io
.IOException
;
25 import java
.net
.URISyntaxException
;
27 import java
.util
.ArrayList
;
28 import java
.util
.List
;
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
;
62 import com
.google
.common
.collect
.Multiset
;
65 * SWTBot test for testing highlighting
67 @RunWith(SWTBotJunit4ClassRunner
.class)
68 public class FilterColorEditorTest
{
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";
80 private static File fTestFile
= null;
82 private static SWTWorkbenchBot fBot
;
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
;
98 public static void init() {
99 SWTBotUtils
.initialize();
101 /* set up test trace */
102 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH
), null);
105 uri
= FileLocator
.toFileURL(location
).toURI();
106 fTestFile
= new File(uri
);
107 } catch (URISyntaxException
| IOException e
) {
108 fail(e
.getMessage());
111 assumeTrue(fTestFile
.exists());
113 /* Set up for swtbot */
114 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
115 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
117 fLogger
.removeAllAppenders();
118 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
119 fBot
= new SWTWorkbenchBot();
121 /* Close welcome view */
122 SWTBotUtils
.closeView("Welcome", fBot
);
124 /* Switch perspectives */
125 SWTBotUtils
.switchToTracingPerspective();
127 /* Finish waiting for eclipse to load */
128 SWTBotUtils
.waitForJobs();
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
);
136 * Test class tear down method.
139 public static void tearDown() {
140 fLogger
.removeAllAppenders();
147 public void setup() {
148 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
150 // Open the actual trace
151 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), COLUMN_TRACE_TYPE
);
152 SWTBotEditor editorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
154 fTableBot
= editorBot
.bot().table();
155 fBackground
= fTableBot
.backgroundColor().getRGB();
156 fForeground
= fTableBot
.foregroundColor().getRGB();
158 SWTBotUtils
.maximizeTable(fTableBot
);
165 public void cleanup() {
166 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
167 SWTBotUtils
.waitForJobs();
171 * Test basic highlight
174 public void testHighlight() {
175 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
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();
186 assertTrue(colorBefore
.contains(fBackground
));
187 assertTrue(colorBefore
.contains(fForeground
));
188 assertFalse(colorBefore
.contains(fHighlight
));
190 assertTrue(colorAfter
.contains(fBackground
));
191 assertTrue(colorAfter
.contains(fForeground
));
192 assertTrue(colorAfter
.contains(fHighlight
));
195 * Check that some background became highlighted.
197 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
198 assertTrue(colorAfter
.count(fHighlight
) > colorBefore
.count(fHighlight
));
202 * Test highlighting multiple elements in a message
205 public void testMultiHighlightMessage() {
206 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, MESSAGE_COLUMN
);
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
);
216 Multiset
<RGB
> colorBefore
= before
.getHistogram();
217 Multiset
<RGB
> colorAfter
= after
.getHistogram();
219 assertTrue(colorBefore
.contains(fBackground
));
220 assertTrue(colorBefore
.contains(fForeground
));
221 assertFalse(colorBefore
.contains(fHighlight
));
223 assertTrue(colorAfter
.contains(fBackground
));
224 assertTrue(colorAfter
.contains(fForeground
));
225 assertTrue(colorAfter
.contains(fHighlight
));
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
)) {
237 if (prevPixel
.equals(fHighlight
) && pixel
.equals(fBackground
)) {
242 intervals
.add(new Point(start
, end
));
245 assertEquals(2, intervals
.size());
249 * Switch to filter and back
252 public void testSwitchToFilter() {
253 Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
254 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
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
);
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
);
271 // press DEL to clear highlighting
272 fTableBot
.pressShortcut(Keystrokes
.DELETE
);
273 ImageHelper afterClear
= waitForNewImage(cellBounds
, afterFilter
);
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);
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
);
289 assertEquals(afterSearchPixel
, afterFilterPixel
);
290 assertEquals(beforePixel
, afterClearPixel
);
291 if (!afterSearchPixel
.equals(fHighlight
)) {
292 assertEquals(beforePixel
, afterSearchPixel
);
294 assertNotEquals(fHighlight
, beforePixel
);
298 assertEquals(afterSearchLine
, afterFilterLine
);
299 assertEquals(beforeLine
, afterClearLine
);
300 assertNotEquals(afterSearchLine
, beforeLine
);
304 * Test highlight color preference
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
);
312 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
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();
323 assertTrue(colorBefore
.contains(fBackground
));
324 assertTrue(colorBefore
.contains(fForeground
));
325 assertFalse(colorBefore
.contains(fHighlight
));
326 assertFalse(colorBefore
.contains(EXPECTED_GREEN
));
328 assertTrue(colorAfter
.contains(fBackground
));
329 assertTrue(colorAfter
.contains(fForeground
));
330 assertFalse(colorAfter
.contains(fHighlight
));
331 assertTrue(colorAfter
.contains(EXPECTED_GREEN
));
334 * Check that some background became green.
336 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
337 assertTrue(colorAfter
.count(EXPECTED_GREEN
) > colorBefore
.count(EXPECTED_GREEN
));
339 // reset the highlight color preference
340 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, fHighlight
);
344 * Test the header bar
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));
378 private void waitForHighlightState(int row
, int column
, boolean highlight
) {
379 fBot
.waitUntil(new DefaultCondition() {
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
;
388 public String
getFailureMessage() {
389 return String
.format("Cell (%d, %d) did not have highlight state: %s", row
, column
, Boolean
.toString(highlight
));
394 private static ImageHelper
waitForNewImage(Rectangle bounds
, ImageHelper currentImage
) {
395 ImageHelper
[] newImage
= new ImageHelper
[1];
396 fBot
.waitUntil(new DefaultCondition() {
398 public boolean test() throws Exception
{
399 return UIThreadRunnable
.syncExec(new Result
<Boolean
> () {
401 public Boolean
run() {
402 newImage
[0] = ImageHelper
.grabImage(bounds
);
403 return !newImage
[0].equals(currentImage
);
409 public String
getFailureMessage() {
410 return "Image at bounds " + bounds
+ " did not change";