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
.SWTBotTable
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ImageHelper
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
52 import org
.eclipse
.ui
.PlatformUI
;
53 import org
.junit
.After
;
54 import org
.junit
.AfterClass
;
55 import org
.junit
.Before
;
56 import org
.junit
.BeforeClass
;
57 import org
.junit
.Test
;
58 import org
.junit
.runner
.RunWith
;
60 import com
.google
.common
.collect
.Multiset
;
63 * SWTBot test for testing highlighting
65 @RunWith(SWTBotJunit4ClassRunner
.class)
66 public class FilterColorEditorTest
{
68 private static final int TIMESTAMP_COLUMN
= 1;
69 private static final int SOURCE_COLUMN
= 2;
70 private static final int MESSAGE_COLUMN
= 6;
71 private static final RGB GREEN
= new RGB(0, 255, 0);
72 private static final String HIGHLIGHT_COLOR_DEFINITION_ID
= "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
73 private static final String TRACE_PROJECT_NAME
= "test";
74 private static final String COLUMN_TRACE
= "syslog_collapse";
75 private static final String COLUMN_TRACE_PATH
= "testfiles/" + COLUMN_TRACE
;
76 private static final String COLUMN_TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
78 private static File fTestFile
= null;
80 private static SWTWorkbenchBot fBot
;
82 /** The Log4j logger instance. */
83 private static final Logger fLogger
= Logger
.getRootLogger();
84 private SWTBotTable fTableBot
;
85 private static final int ROW
= 8;
86 /** Expected color values */
87 private RGB fForeground
;
88 private RGB fBackground
;
89 private static RGB fHighlight
;
90 private static RGB EXPECTED_GREEN
;
96 public static void init() {
97 SWTBotUtils
.initialize();
99 /* set up test trace */
100 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH
), null);
103 uri
= FileLocator
.toFileURL(location
).toURI();
104 fTestFile
= new File(uri
);
105 } catch (URISyntaxException
| IOException e
) {
106 fail(e
.getMessage());
109 assumeTrue(fTestFile
.exists());
111 /* Set up for swtbot */
112 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
113 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
115 fLogger
.removeAllAppenders();
116 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
117 fBot
= new SWTWorkbenchBot();
119 /* Close welcome view */
120 SWTBotUtils
.closeView("Welcome", fBot
);
122 /* Switch perspectives */
123 SWTBotUtils
.switchToTracingPerspective();
125 /* Finish waiting for eclipse to load */
126 SWTBotUtils
.waitForJobs();
128 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
129 fHighlight
= ImageHelper
.adjustExpectedColor(colorRegistry
.get(HIGHLIGHT_COLOR_DEFINITION_ID
).getRGB());
130 EXPECTED_GREEN
= ImageHelper
.adjustExpectedColor(GREEN
);
134 * Test class tear down method.
137 public static void tearDown() {
138 fLogger
.removeAllAppenders();
145 public void setup() {
146 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
148 // Open the actual trace
149 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), COLUMN_TRACE_TYPE
);
150 SWTBotEditor editorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
152 fTableBot
= editorBot
.bot().table();
153 fBackground
= fTableBot
.backgroundColor().getRGB();
154 fForeground
= fTableBot
.foregroundColor().getRGB();
156 SWTBotUtils
.maximizeTable(fTableBot
);
163 public void cleanup() {
164 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
165 SWTBotUtils
.waitForJobs();
169 * Test basic highlight
172 public void testHighlight() {
173 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
175 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
176 Multiset
<RGB
> colorBefore
= before
.getHistogram();
177 // Select source column and enter regex
178 fTableBot
.click(0, SOURCE_COLUMN
);
179 fBot
.text().typeText("HostF\n", 100);
180 // make sure selected row is not matching row
181 fTableBot
.select(ROW
- 1);
182 Multiset
<RGB
> colorAfter
= waitForNewImage(cellBounds
, before
).getHistogram();
184 assertTrue(colorBefore
.contains(fBackground
));
185 assertTrue(colorBefore
.contains(fForeground
));
186 assertFalse(colorBefore
.contains(fHighlight
));
188 assertTrue(colorAfter
.contains(fBackground
));
189 assertTrue(colorAfter
.contains(fForeground
));
190 assertTrue(colorAfter
.contains(fHighlight
));
193 * Check that some background became highlighted.
195 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
196 assertTrue(colorAfter
.count(fHighlight
) > colorBefore
.count(fHighlight
));
200 * Test highlighting multiple elements in a message
203 public void testMultiHighlightMessage() {
204 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, MESSAGE_COLUMN
);
206 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
207 // enter regex in message column
208 fTableBot
.click(0, MESSAGE_COLUMN
);
209 fBot
.text().typeText("e\n", 100);
210 // make sure matching item is not selected
211 fTableBot
.select(ROW
- 1);
212 ImageHelper after
= waitForNewImage(cellBounds
, before
);
214 Multiset
<RGB
> colorBefore
= before
.getHistogram();
215 Multiset
<RGB
> colorAfter
= after
.getHistogram();
217 assertTrue(colorBefore
.contains(fBackground
));
218 assertTrue(colorBefore
.contains(fForeground
));
219 assertFalse(colorBefore
.contains(fHighlight
));
221 assertTrue(colorAfter
.contains(fBackground
));
222 assertTrue(colorAfter
.contains(fForeground
));
223 assertTrue(colorAfter
.contains(fHighlight
));
227 List
<Point
> intervals
= new ArrayList
<>();
228 List
<RGB
> pixelRow
= after
.getPixelRow(2);
229 for (int i
= 1; i
< pixelRow
.size(); i
++) {
230 RGB prevPixel
= pixelRow
.get(i
- 1);
231 RGB pixel
= pixelRow
.get(i
);
232 if (prevPixel
.equals(fBackground
) && pixel
.equals(fHighlight
)) {
235 if (prevPixel
.equals(fHighlight
) && pixel
.equals(fBackground
)) {
240 intervals
.add(new Point(start
, end
));
243 assertEquals(2, intervals
.size());
247 * Switch to filter and back
250 public void testSwitchToFilter() {
251 Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
252 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
254 // enter regex in Timestamp column
255 fTableBot
.click(0, TIMESTAMP_COLUMN
);
256 fBot
.text().typeText("00\n", 100);
257 // make sure matching column is not selected
258 fTableBot
.select(ROW
- 1);
259 ImageHelper afterSearch
= waitForNewImage(cellBounds
, before
);
261 // click Add as Filter
262 fTableBot
.click(0, 0);
263 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "<srch>", 0, TIMESTAMP_COLUMN
));
264 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "22/22", 1, TIMESTAMP_COLUMN
));
265 // the bounds have changed after applying the filter
266 cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
267 ImageHelper afterFilter
= ImageHelper
.grabImage(cellBounds
);
269 // press DEL to clear highlighting
270 fTableBot
.pressShortcut(Keystrokes
.DELETE
);
271 ImageHelper afterClear
= waitForNewImage(cellBounds
, afterFilter
);
273 List
<RGB
> beforeLine
= before
.getPixelRow(2);
274 List
<RGB
> afterSearchLine
= afterSearch
.getPixelRow(2);
275 List
<RGB
> afterFilterLine
= afterFilter
.getPixelRow(2);
276 List
<RGB
> afterClearLine
= afterClear
.getPixelRow(2);
278 assertEquals(beforeLine
.size(), afterSearchLine
.size());
279 assertEquals(beforeLine
.size(), afterFilterLine
.size());
280 assertEquals(beforeLine
.size(), afterClearLine
.size());
281 for (int i
= 0; i
< beforeLine
.size(); i
++) {
282 RGB beforePixel
= beforeLine
.get(i
);
283 RGB afterSearchPixel
= afterSearchLine
.get(i
);
284 RGB afterFilterPixel
= afterFilterLine
.get(i
);
285 RGB afterClearPixel
= afterClearLine
.get(i
);
287 assertEquals(afterSearchPixel
, afterFilterPixel
);
288 assertEquals(beforePixel
, afterClearPixel
);
289 if (!afterSearchPixel
.equals(fHighlight
)) {
290 assertEquals(beforePixel
, afterSearchPixel
);
292 assertNotEquals(fHighlight
, beforePixel
);
296 assertEquals(afterSearchLine
, afterFilterLine
);
297 assertEquals(beforeLine
, afterClearLine
);
298 assertNotEquals(afterSearchLine
, beforeLine
);
302 * Test highlight color preference
305 public void testPreference() {
306 // change the highlight color preference
307 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
308 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, GREEN
);
310 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
312 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
313 Multiset
<RGB
> colorBefore
= before
.getHistogram();
314 // Select source column and enter regex
315 fTableBot
.click(0, SOURCE_COLUMN
);
316 fBot
.text().typeText("HostF\n", 100);
317 // make sure selected row is not matching row
318 fTableBot
.select(ROW
- 1);
319 Multiset
<RGB
> colorAfter
= waitForNewImage(cellBounds
, before
).getHistogram();
321 assertTrue(colorBefore
.contains(fBackground
));
322 assertTrue(colorBefore
.contains(fForeground
));
323 assertFalse(colorBefore
.contains(fHighlight
));
324 assertFalse(colorBefore
.contains(EXPECTED_GREEN
));
326 assertTrue(colorAfter
.contains(fBackground
));
327 assertTrue(colorAfter
.contains(fForeground
));
328 assertFalse(colorAfter
.contains(fHighlight
));
329 assertTrue(colorAfter
.contains(EXPECTED_GREEN
));
332 * Check that some background became green.
334 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
335 assertTrue(colorAfter
.count(EXPECTED_GREEN
) > colorBefore
.count(EXPECTED_GREEN
));
337 // reset the highlight color preference
338 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, fHighlight
);
341 private static ImageHelper
waitForNewImage(Rectangle bounds
, ImageHelper currentImage
) {
342 ImageHelper
[] newImage
= new ImageHelper
[1];
343 fBot
.waitUntil(new DefaultCondition() {
345 public boolean test() throws Exception
{
346 return UIThreadRunnable
.syncExec(new Result
<Boolean
> () {
348 public Boolean
run() {
349 newImage
[0] = ImageHelper
.grabImage(bounds
);
350 return !newImage
[0].equals(currentImage
);
356 public String
getFailureMessage() {
357 return "Image at bounds " + bounds
+ " did not change";