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
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
55 import org
.eclipse
.ui
.PlatformUI
;
56 import org
.junit
.After
;
57 import org
.junit
.AfterClass
;
58 import org
.junit
.Before
;
59 import org
.junit
.BeforeClass
;
60 import org
.junit
.Test
;
61 import org
.junit
.runner
.RunWith
;
63 import com
.google
.common
.collect
.Multiset
;
66 * SWTBot test for testing highlighting
68 @RunWith(SWTBotJunit4ClassRunner
.class)
69 public class FilterColorEditorTest
{
71 private static final int TIMESTAMP_COLUMN
= 1;
72 private static final int SOURCE_COLUMN
= 2;
73 private static final int MESSAGE_COLUMN
= 6;
74 private static final RGB GREEN
= new RGB(0, 255, 0);
75 private static final String HIGHLIGHT_COLOR_DEFINITION_ID
= "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
76 private static final String TRACE_PROJECT_NAME
= "test";
77 private static final String COLUMN_TRACE
= "syslog_collapse";
78 private static final String COLUMN_TRACE_PATH
= "testfiles/" + COLUMN_TRACE
;
79 private static final String COLUMN_TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
81 private static File fTestFile
= null;
83 private static SWTWorkbenchBot fBot
;
85 /** The Log4j logger instance. */
86 private static final Logger fLogger
= Logger
.getRootLogger();
87 private SWTBotTable fTableBot
;
88 private static final int ROW
= 8;
89 /** Expected color values */
90 private RGB fForeground
;
91 private RGB fBackground
;
92 private static RGB fHighlight
;
93 private static RGB EXPECTED_GREEN
;
99 public static void init() {
100 SWTBotUtils
.initialize();
102 /* set up test trace */
103 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH
), null);
106 uri
= FileLocator
.toFileURL(location
).toURI();
107 fTestFile
= new File(uri
);
108 } catch (URISyntaxException
| IOException e
) {
109 fail(e
.getMessage());
112 assumeTrue(fTestFile
.exists());
114 /* Set up for swtbot */
115 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
116 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
118 fLogger
.removeAllAppenders();
119 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
120 fBot
= new SWTWorkbenchBot();
122 /* Close welcome view */
123 SWTBotUtils
.closeView("Welcome", fBot
);
125 /* Switch perspectives */
126 SWTBotUtils
.switchToTracingPerspective();
128 /* Finish waiting for eclipse to load */
129 WaitUtils
.waitForJobs();
131 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
132 fHighlight
= ImageHelper
.adjustExpectedColor(colorRegistry
.get(HIGHLIGHT_COLOR_DEFINITION_ID
).getRGB());
133 EXPECTED_GREEN
= ImageHelper
.adjustExpectedColor(GREEN
);
137 * Test class tear down method.
140 public static void tearDown() {
141 fLogger
.removeAllAppenders();
148 public void setup() {
149 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
151 // Open the actual trace
152 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), COLUMN_TRACE_TYPE
);
153 SWTBotEditor editorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
155 fTableBot
= editorBot
.bot().table();
156 fBackground
= fTableBot
.backgroundColor().getRGB();
157 fForeground
= fTableBot
.foregroundColor().getRGB();
159 SWTBotUtils
.maximizeTable(fTableBot
);
166 public void cleanup() {
167 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
168 WaitUtils
.waitForJobs();
172 * Test basic highlight
175 public void testHighlight() {
176 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
178 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
179 Multiset
<RGB
> colorBefore
= before
.getHistogram();
180 // Select source column and enter regex
181 fTableBot
.click(0, SOURCE_COLUMN
);
182 fBot
.text().typeText("HostF\n", 100);
183 // make sure selected row is not matching row
184 fTableBot
.select(ROW
- 1);
185 Multiset
<RGB
> colorAfter
= waitForNewImage(cellBounds
, before
).getHistogram();
187 assertTrue(colorBefore
.contains(fBackground
));
188 assertTrue(colorBefore
.contains(fForeground
));
189 assertFalse(colorBefore
.contains(fHighlight
));
191 assertTrue(colorAfter
.contains(fBackground
));
192 assertTrue(colorAfter
.contains(fForeground
));
193 assertTrue(colorAfter
.contains(fHighlight
));
196 * Check that some background became highlighted.
198 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
199 assertTrue(colorAfter
.count(fHighlight
) > colorBefore
.count(fHighlight
));
203 * Test highlighting multiple elements in a message
206 public void testMultiHighlightMessage() {
207 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, MESSAGE_COLUMN
);
209 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
210 // enter regex in message column
211 fTableBot
.click(0, MESSAGE_COLUMN
);
212 fBot
.text().typeText("e\n", 100);
213 // make sure matching item is not selected
214 fTableBot
.select(ROW
- 1);
215 ImageHelper after
= waitForNewImage(cellBounds
, before
);
217 Multiset
<RGB
> colorBefore
= before
.getHistogram();
218 Multiset
<RGB
> colorAfter
= after
.getHistogram();
220 assertTrue(colorBefore
.contains(fBackground
));
221 assertTrue(colorBefore
.contains(fForeground
));
222 assertFalse(colorBefore
.contains(fHighlight
));
224 assertTrue(colorAfter
.contains(fBackground
));
225 assertTrue(colorAfter
.contains(fForeground
));
226 assertTrue(colorAfter
.contains(fHighlight
));
230 List
<Point
> intervals
= new ArrayList
<>();
231 List
<RGB
> pixelRow
= after
.getPixelRow(2);
232 for (int i
= 1; i
< pixelRow
.size(); i
++) {
233 RGB prevPixel
= pixelRow
.get(i
- 1);
234 RGB pixel
= pixelRow
.get(i
);
235 if (prevPixel
.equals(fBackground
) && pixel
.equals(fHighlight
)) {
238 if (prevPixel
.equals(fHighlight
) && pixel
.equals(fBackground
)) {
243 intervals
.add(new Point(start
, end
));
246 assertEquals(2, intervals
.size());
250 * Switch to filter and back
253 public void testSwitchToFilter() {
254 Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
255 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
257 // enter regex in Timestamp column
258 fTableBot
.click(0, TIMESTAMP_COLUMN
);
259 fBot
.text().typeText("00\n", 100);
260 // make sure matching column is not selected
261 fTableBot
.select(ROW
- 1);
262 ImageHelper afterSearch
= waitForNewImage(cellBounds
, before
);
264 // click Add as Filter
265 fTableBot
.click(0, 0);
266 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "<srch>", 0, TIMESTAMP_COLUMN
));
267 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "22/22", 1, TIMESTAMP_COLUMN
));
268 // the bounds have changed after applying the filter
269 cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
270 ImageHelper afterFilter
= ImageHelper
.grabImage(cellBounds
);
272 // press DEL to clear highlighting
273 fTableBot
.pressShortcut(Keystrokes
.DELETE
);
274 ImageHelper afterClear
= waitForNewImage(cellBounds
, afterFilter
);
276 List
<RGB
> beforeLine
= before
.getPixelRow(2);
277 List
<RGB
> afterSearchLine
= afterSearch
.getPixelRow(2);
278 List
<RGB
> afterFilterLine
= afterFilter
.getPixelRow(2);
279 List
<RGB
> afterClearLine
= afterClear
.getPixelRow(2);
281 assertEquals(beforeLine
.size(), afterSearchLine
.size());
282 assertEquals(beforeLine
.size(), afterFilterLine
.size());
283 assertEquals(beforeLine
.size(), afterClearLine
.size());
284 for (int i
= 0; i
< beforeLine
.size(); i
++) {
285 RGB beforePixel
= beforeLine
.get(i
);
286 RGB afterSearchPixel
= afterSearchLine
.get(i
);
287 RGB afterFilterPixel
= afterFilterLine
.get(i
);
288 RGB afterClearPixel
= afterClearLine
.get(i
);
290 assertEquals(afterSearchPixel
, afterFilterPixel
);
291 assertEquals(beforePixel
, afterClearPixel
);
292 if (!afterSearchPixel
.equals(fHighlight
)) {
293 assertEquals(beforePixel
, afterSearchPixel
);
295 assertNotEquals(fHighlight
, beforePixel
);
299 assertEquals(afterSearchLine
, afterFilterLine
);
300 assertEquals(beforeLine
, afterClearLine
);
301 assertNotEquals(afterSearchLine
, beforeLine
);
305 * Test highlight color preference
308 public void testPreference() {
309 // change the highlight color preference
310 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
311 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, GREEN
);
313 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
315 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
316 Multiset
<RGB
> colorBefore
= before
.getHistogram();
317 // Select source column and enter regex
318 fTableBot
.click(0, SOURCE_COLUMN
);
319 fBot
.text().typeText("HostF\n", 100);
320 // make sure selected row is not matching row
321 fTableBot
.select(ROW
- 1);
322 Multiset
<RGB
> colorAfter
= waitForNewImage(cellBounds
, before
).getHistogram();
324 assertTrue(colorBefore
.contains(fBackground
));
325 assertTrue(colorBefore
.contains(fForeground
));
326 assertFalse(colorBefore
.contains(fHighlight
));
327 assertFalse(colorBefore
.contains(EXPECTED_GREEN
));
329 assertTrue(colorAfter
.contains(fBackground
));
330 assertTrue(colorAfter
.contains(fForeground
));
331 assertFalse(colorAfter
.contains(fHighlight
));
332 assertTrue(colorAfter
.contains(EXPECTED_GREEN
));
335 * Check that some background became green.
337 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
338 assertTrue(colorAfter
.count(EXPECTED_GREEN
) > colorBefore
.count(EXPECTED_GREEN
));
340 // reset the highlight color preference
341 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, fHighlight
);
345 * Test the header bar
348 public void testHeaderBar() {
349 // Add search filter on Timestamp column
350 fTableBot
.click(0, TIMESTAMP_COLUMN
);
351 fBot
.text().typeText("2");
352 fBot
.text().pressShortcut(Keystrokes
.CR
);
353 // Add search filter on Message column and Add as Filter
354 fTableBot
.click(0, MESSAGE_COLUMN
);
355 fBot
.text().typeText("F");
356 fBot
.text().pressShortcut(Keystrokes
.CTRL
, Keystrokes
.CR
);
357 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "2/22", 1, 1));
358 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "Message F", 3, MESSAGE_COLUMN
));
359 fBot
.clabel("Timestamp matches \"2\"");
360 fBot
.clabel("Message matches \"F\"");
361 waitForHighlightState(3, TIMESTAMP_COLUMN
, true);
362 waitForHighlightState(3, MESSAGE_COLUMN
, true);
363 // Clear all filter highlighting
364 fTableBot
.pressShortcut(Keystrokes
.DELETE
);
365 waitForHighlightState(3, TIMESTAMP_COLUMN
, false);
366 waitForHighlightState(3, MESSAGE_COLUMN
, false);
367 // Click filter label to set the filter highlighting on Message filter
368 SWTBotCLabel filterCLabel
= fBot
.clabel("Message matches \"F\"");
369 SWTBotCanvas filterCanvas
= new SWTBotCanvas(filterCLabel
.widget
);
370 filterCanvas
.click();
371 waitForHighlightState(3, TIMESTAMP_COLUMN
, false);
372 waitForHighlightState(3, MESSAGE_COLUMN
, true);
373 // Click filter icon to remove the Message filter
374 Rectangle imageBounds
= filterCLabel
.image().getBounds();
375 filterCanvas
.click(filterCLabel
.widget
.getLeftMargin() + imageBounds
.width
/ 2, filterCLabel
.widget
.getTopMargin() + imageBounds
.height
/ 2);
376 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "5/22", 1, 1));
379 private void waitForHighlightState(int row
, int column
, boolean highlight
) {
380 fBot
.waitUntil(new DefaultCondition() {
382 public boolean test() throws Exception
{
383 Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, row
, column
);
384 ImageHelper imageHelper
= ImageHelper
.grabImage(cellBounds
);
385 return imageHelper
.getHistogram().contains(fHighlight
) == highlight
;
389 public String
getFailureMessage() {
390 return String
.format("Cell (%d, %d) did not have highlight state: %s", row
, column
, Boolean
.toString(highlight
));
395 private static ImageHelper
waitForNewImage(Rectangle bounds
, ImageHelper currentImage
) {
396 ImageHelper
[] newImage
= new ImageHelper
[1];
397 fBot
.waitUntil(new DefaultCondition() {
399 public boolean test() throws Exception
{
400 return UIThreadRunnable
.syncExec(new Result
<Boolean
> () {
402 public Boolean
run() {
403 newImage
[0] = ImageHelper
.grabImage(bounds
);
404 return !newImage
[0].equals(currentImage
);
410 public String
getFailureMessage() {
411 return "Image at bounds " + bounds
+ " did not change";