1 /*******************************************************************************
2 * Copyright (c) 2015 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
.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
.ImageHelper
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
47 import org
.eclipse
.ui
.PlatformUI
;
48 import org
.junit
.After
;
49 import org
.junit
.AfterClass
;
50 import org
.junit
.Before
;
51 import org
.junit
.BeforeClass
;
52 import org
.junit
.Test
;
53 import org
.junit
.runner
.RunWith
;
55 import com
.google
.common
.collect
.Multiset
;
58 * SWTBot test for testing highlighting
60 @RunWith(SWTBotJunit4ClassRunner
.class)
61 public class FilterColorEditorTest
{
63 private static final int TIMESTAMP_COLUMN
= 1;
64 private static final int SOURCE_COLUMN
= 2;
65 private static final int MESSAGE_COLUMN
= 6;
66 private static final RGB GREEN
= new RGB(0, 255, 0);
67 private static final String HIGHLIGHT_COLOR_DEFINITION_ID
= "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
68 private static final String TRACE_PROJECT_NAME
= "test";
69 private static final String COLUMN_TRACE
= "syslog_collapse";
70 private static final String COLUMN_TRACE_PATH
= "testfiles/" + COLUMN_TRACE
;
71 private static final String COLUMN_TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
73 private static File fTestFile
= null;
75 private static SWTWorkbenchBot fBot
;
77 /** The Log4j logger instance. */
78 private static final Logger fLogger
= Logger
.getRootLogger();
79 private SWTBotTable fTableBot
;
80 private static final int ROW
= 8;
81 private RGB fForeground
;
82 private RGB fBackground
;
83 private RGB fHighlight
;
89 public static void init() {
90 SWTBotUtils
.failIfUIThread();
92 /* set up test trace */
93 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH
), null);
96 uri
= FileLocator
.toFileURL(location
).toURI();
97 fTestFile
= new File(uri
);
98 } catch (URISyntaxException
| IOException e
) {
102 assumeTrue(fTestFile
.exists());
104 /* Set up for swtbot */
105 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
106 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
108 fLogger
.removeAllAppenders();
109 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
110 fBot
= new SWTWorkbenchBot();
112 /* Close welcome view */
113 SWTBotUtils
.closeView("Welcome", fBot
);
115 /* Switch perspectives */
116 SWTBotUtils
.switchToTracingPerspective();
118 /* Finish waiting for eclipse to load */
119 SWTBotUtils
.waitForJobs();
123 * Test class tear down method.
126 public static void tearDown() {
127 fLogger
.removeAllAppenders();
134 public void setup() {
135 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
137 // Open the actual trace
138 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), COLUMN_TRACE_TYPE
);
139 SWTBotEditor editorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
141 fTableBot
= editorBot
.bot().table();
142 fBackground
= fTableBot
.backgroundColor().getRGB();
143 fForeground
= fTableBot
.foregroundColor().getRGB();
144 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
145 fHighlight
= colorRegistry
.get(HIGHLIGHT_COLOR_DEFINITION_ID
).getRGB();
147 SWTBotUtils
.maximizeTable(fTableBot
);
154 public void cleanup() {
155 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
156 SWTBotUtils
.waitForJobs();
160 * Test basic highlight
163 public void testHighlight() {
164 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
166 Multiset
<RGB
> colorBefore
= ImageHelper
.grabImage(cellBounds
).getHistogram();
167 // Select source column and enter regex
168 fTableBot
.click(0, SOURCE_COLUMN
);
169 fBot
.text().typeText("HostF\n", 100);
170 // make sure selected row is not matching row
171 fTableBot
.select(ROW
- 1);
172 Multiset
<RGB
> colorAfter
= ImageHelper
.grabImage(cellBounds
).getHistogram();
174 assertTrue(colorBefore
.contains(fBackground
));
175 assertTrue(colorBefore
.contains(fForeground
));
176 assertFalse(colorBefore
.contains(fHighlight
));
178 assertTrue(colorAfter
.contains(fBackground
));
179 assertTrue(colorAfter
.contains(fForeground
));
180 assertTrue(colorAfter
.contains(fHighlight
));
183 * Check that some background became highlighted.
185 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
186 assertTrue(colorAfter
.count(fHighlight
) > colorBefore
.count(fHighlight
));
190 * Test highlighting multiple elements in a message
193 public void testMultiHighlightMessage() {
194 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, MESSAGE_COLUMN
);
196 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
197 // enter regex in message column
198 fTableBot
.click(0, MESSAGE_COLUMN
);
199 fBot
.text().typeText("e\n", 100);
200 // make sure matching item is not selected
201 fTableBot
.select(ROW
- 1);
202 ImageHelper after
= ImageHelper
.grabImage(cellBounds
);
204 Multiset
<RGB
> colorBefore
= before
.getHistogram();
205 Multiset
<RGB
> colorAfter
= after
.getHistogram();
207 assertTrue(colorBefore
.contains(fBackground
));
208 assertTrue(colorBefore
.contains(fForeground
));
209 assertFalse(colorBefore
.contains(fHighlight
));
211 assertTrue(colorAfter
.contains(fBackground
));
212 assertTrue(colorAfter
.contains(fForeground
));
213 assertTrue(colorAfter
.contains(fHighlight
));
217 List
<Point
> intervals
= new ArrayList
<>();
218 List
<RGB
> pixelRow
= after
.getPixelRow(2);
219 for (int i
= 1; i
< pixelRow
.size(); i
++) {
220 RGB prevPixel
= pixelRow
.get(i
- 1);
221 RGB pixel
= pixelRow
.get(i
);
222 if (prevPixel
.equals(fBackground
) && pixel
.equals(fHighlight
)) {
225 if (prevPixel
.equals(fHighlight
) && pixel
.equals(fBackground
)) {
230 intervals
.add(new Point(start
, end
));
233 assertEquals(2, intervals
.size());
237 * Switch to filter and back
240 public void testSwitchToFilter() {
241 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
242 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
243 // enter regex in message column
244 fTableBot
.click(0, TIMESTAMP_COLUMN
);
245 fBot
.text().typeText("00\n", 100);
246 // make sure matching column is not selected
247 fTableBot
.select(ROW
- 1);
248 ImageHelper after
= ImageHelper
.grabImage(cellBounds
);
249 // toggle filter mode
250 fTableBot
.click(0, 0);
251 ImageHelper afterFilter
= ImageHelper
.grabImage(cellBounds
);
253 List
<RGB
> beforeLine
= before
.getPixelRow(2);
254 List
<RGB
> afterLine
= after
.getPixelRow(2);
255 List
<RGB
> afterFilterLine
= afterFilter
.getPixelRow(2);
257 assertEquals(beforeLine
.size(), afterLine
.size());
258 assertEquals(beforeLine
.size(), afterFilterLine
.size());
259 for (int i
= 0; i
< beforeLine
.size(); i
++) {
260 RGB afterFilterPixel
= afterFilterLine
.get(i
);
261 RGB beforePixel
= beforeLine
.get(i
);
262 RGB afterPixel
= afterLine
.get(i
);
264 assertEquals(beforePixel
, afterFilterPixel
);
265 if (!afterPixel
.equals(fHighlight
)) {
266 assertEquals(beforePixel
, afterPixel
);
268 assertNotEquals(fHighlight
, beforePixel
);
272 assertEquals(beforeLine
, afterFilterLine
);
273 assertNotEquals(afterLine
, beforeLine
);
277 * Test highlight color preference
280 public void testPreference() {
281 // change the highlight color preference
282 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
283 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, GREEN
);
285 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
287 Multiset
<RGB
> colorBefore
= ImageHelper
.grabImage(cellBounds
).getHistogram();
288 // Select source column and enter regex
289 fTableBot
.click(0, SOURCE_COLUMN
);
290 fBot
.text().typeText("HostF\n", 100);
291 // make sure selected row is not matching row
292 fTableBot
.select(ROW
- 1);
293 Multiset
<RGB
> colorAfter
= ImageHelper
.grabImage(cellBounds
).getHistogram();
295 assertTrue(colorBefore
.contains(fBackground
));
296 assertTrue(colorBefore
.contains(fForeground
));
297 assertFalse(colorBefore
.contains(fHighlight
));
298 assertFalse(colorBefore
.contains(GREEN
));
300 assertTrue(colorAfter
.contains(fBackground
));
301 assertTrue(colorAfter
.contains(fForeground
));
302 assertFalse(colorAfter
.contains(fHighlight
));
303 assertTrue(colorAfter
.contains(GREEN
));
306 * Check that some background became green.
308 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
309 assertTrue(colorAfter
.count(GREEN
) > colorBefore
.count(GREEN
));
311 // reset the highlight color preference
312 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, fHighlight
);