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
.ConditionHelpers
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ImageHelper
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
48 import org
.eclipse
.ui
.PlatformUI
;
49 import org
.junit
.After
;
50 import org
.junit
.AfterClass
;
51 import org
.junit
.Before
;
52 import org
.junit
.BeforeClass
;
53 import org
.junit
.Test
;
54 import org
.junit
.runner
.RunWith
;
56 import com
.google
.common
.collect
.Multiset
;
59 * SWTBot test for testing highlighting
61 @RunWith(SWTBotJunit4ClassRunner
.class)
62 public class FilterColorEditorTest
{
64 private static final int TIMESTAMP_COLUMN
= 1;
65 private static final int SOURCE_COLUMN
= 2;
66 private static final int MESSAGE_COLUMN
= 6;
67 private static final RGB GREEN
= new RGB(0, 255, 0);
68 private static final String HIGHLIGHT_COLOR_DEFINITION_ID
= "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
69 private static final String TRACE_PROJECT_NAME
= "test";
70 private static final String COLUMN_TRACE
= "syslog_collapse";
71 private static final String COLUMN_TRACE_PATH
= "testfiles/" + COLUMN_TRACE
;
72 private static final String COLUMN_TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
74 private static File fTestFile
= null;
76 private static SWTWorkbenchBot fBot
;
78 /** The Log4j logger instance. */
79 private static final Logger fLogger
= Logger
.getRootLogger();
80 private SWTBotTable fTableBot
;
81 private static final int ROW
= 8;
82 /** Expected color values */
83 private RGB fForeground
;
84 private RGB fBackground
;
85 private static RGB fHighlight
;
86 private static RGB EXPECTED_GREEN
;
92 public static void init() {
93 SWTBotUtils
.initialize();
95 /* set up test trace */
96 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH
), null);
99 uri
= FileLocator
.toFileURL(location
).toURI();
100 fTestFile
= new File(uri
);
101 } catch (URISyntaxException
| IOException e
) {
102 fail(e
.getMessage());
105 assumeTrue(fTestFile
.exists());
107 /* Set up for swtbot */
108 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
109 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
111 fLogger
.removeAllAppenders();
112 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
113 fBot
= new SWTWorkbenchBot();
115 /* Close welcome view */
116 SWTBotUtils
.closeView("Welcome", fBot
);
118 /* Switch perspectives */
119 SWTBotUtils
.switchToTracingPerspective();
121 /* Finish waiting for eclipse to load */
122 SWTBotUtils
.waitForJobs();
124 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
125 fHighlight
= ImageHelper
.adjustExpectedColor(colorRegistry
.get(HIGHLIGHT_COLOR_DEFINITION_ID
).getRGB());
126 EXPECTED_GREEN
= ImageHelper
.adjustExpectedColor(GREEN
);
130 * Test class tear down method.
133 public static void tearDown() {
134 fLogger
.removeAllAppenders();
141 public void setup() {
142 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
144 // Open the actual trace
145 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, fTestFile
.getAbsolutePath(), COLUMN_TRACE_TYPE
);
146 SWTBotEditor editorBot
= SWTBotUtils
.activateEditor(fBot
, fTestFile
.getName());
148 fTableBot
= editorBot
.bot().table();
149 fBackground
= fTableBot
.backgroundColor().getRGB();
150 fForeground
= fTableBot
.foregroundColor().getRGB();
152 SWTBotUtils
.maximizeTable(fTableBot
);
159 public void cleanup() {
160 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
161 SWTBotUtils
.waitForJobs();
165 * Test basic highlight
168 public void testHighlight() {
169 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
171 Multiset
<RGB
> colorBefore
= ImageHelper
.grabImage(cellBounds
).getHistogram();
172 // Select source column and enter regex
173 fTableBot
.click(0, SOURCE_COLUMN
);
174 fBot
.text().typeText("HostF\n", 100);
175 // make sure selected row is not matching row
176 fTableBot
.select(ROW
- 1);
177 Multiset
<RGB
> colorAfter
= ImageHelper
.grabImage(cellBounds
).getHistogram();
179 assertTrue(colorBefore
.contains(fBackground
));
180 assertTrue(colorBefore
.contains(fForeground
));
181 assertFalse(colorBefore
.contains(fHighlight
));
183 assertTrue(colorAfter
.contains(fBackground
));
184 assertTrue(colorAfter
.contains(fForeground
));
185 assertTrue(colorAfter
.contains(fHighlight
));
188 * Check that some background became highlighted.
190 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
191 assertTrue(colorAfter
.count(fHighlight
) > colorBefore
.count(fHighlight
));
195 * Test highlighting multiple elements in a message
198 public void testMultiHighlightMessage() {
199 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, MESSAGE_COLUMN
);
201 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
202 // enter regex in message column
203 fTableBot
.click(0, MESSAGE_COLUMN
);
204 fBot
.text().typeText("e\n", 100);
205 // make sure matching item is not selected
206 fTableBot
.select(ROW
- 1);
207 ImageHelper after
= ImageHelper
.grabImage(cellBounds
);
209 Multiset
<RGB
> colorBefore
= before
.getHistogram();
210 Multiset
<RGB
> colorAfter
= after
.getHistogram();
212 assertTrue(colorBefore
.contains(fBackground
));
213 assertTrue(colorBefore
.contains(fForeground
));
214 assertFalse(colorBefore
.contains(fHighlight
));
216 assertTrue(colorAfter
.contains(fBackground
));
217 assertTrue(colorAfter
.contains(fForeground
));
218 assertTrue(colorAfter
.contains(fHighlight
));
222 List
<Point
> intervals
= new ArrayList
<>();
223 List
<RGB
> pixelRow
= after
.getPixelRow(2);
224 for (int i
= 1; i
< pixelRow
.size(); i
++) {
225 RGB prevPixel
= pixelRow
.get(i
- 1);
226 RGB pixel
= pixelRow
.get(i
);
227 if (prevPixel
.equals(fBackground
) && pixel
.equals(fHighlight
)) {
230 if (prevPixel
.equals(fHighlight
) && pixel
.equals(fBackground
)) {
235 intervals
.add(new Point(start
, end
));
238 assertEquals(2, intervals
.size());
242 * Switch to filter and back
245 public void testSwitchToFilter() {
246 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, TIMESTAMP_COLUMN
);
247 ImageHelper before
= ImageHelper
.grabImage(cellBounds
);
248 // enter regex in message column
249 fTableBot
.click(0, TIMESTAMP_COLUMN
);
250 fBot
.text().typeText("00\n", 100);
251 // make sure matching column is not selected
252 fTableBot
.select(ROW
- 1);
253 ImageHelper after
= ImageHelper
.grabImage(cellBounds
);
254 // toggle filter mode
255 fTableBot
.click(0, 0);
256 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(fTableBot
, "<filter>", 0, 1));
257 //TODO: We need a better way to make sure that the table is done updating
258 SWTBotUtils
.delay(2000);
259 ImageHelper afterFilter
= ImageHelper
.grabImage(cellBounds
);
261 List
<RGB
> beforeLine
= before
.getPixelRow(2);
262 List
<RGB
> afterLine
= after
.getPixelRow(2);
263 List
<RGB
> afterFilterLine
= afterFilter
.getPixelRow(2);
265 assertEquals(beforeLine
.size(), afterLine
.size());
266 assertEquals(beforeLine
.size(), afterFilterLine
.size());
267 for (int i
= 0; i
< beforeLine
.size(); i
++) {
268 RGB afterFilterPixel
= afterFilterLine
.get(i
);
269 RGB beforePixel
= beforeLine
.get(i
);
270 RGB afterPixel
= afterLine
.get(i
);
272 assertEquals(beforePixel
, afterFilterPixel
);
273 if (!afterPixel
.equals(fHighlight
)) {
274 assertEquals(beforePixel
, afterPixel
);
276 assertNotEquals(fHighlight
, beforePixel
);
280 assertEquals(beforeLine
, afterFilterLine
);
281 assertNotEquals(afterLine
, beforeLine
);
285 * Test highlight color preference
288 public void testPreference() {
289 // change the highlight color preference
290 ColorRegistry colorRegistry
= PlatformUI
.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
291 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, GREEN
);
293 final Rectangle cellBounds
= SWTBotUtils
.getCellBounds(fTableBot
.widget
, ROW
, SOURCE_COLUMN
);
295 Multiset
<RGB
> colorBefore
= ImageHelper
.grabImage(cellBounds
).getHistogram();
296 // Select source column and enter regex
297 fTableBot
.click(0, SOURCE_COLUMN
);
298 fBot
.text().typeText("HostF\n", 100);
299 // make sure selected row is not matching row
300 fTableBot
.select(ROW
- 1);
301 Multiset
<RGB
> colorAfter
= ImageHelper
.grabImage(cellBounds
).getHistogram();
303 assertTrue(colorBefore
.contains(fBackground
));
304 assertTrue(colorBefore
.contains(fForeground
));
305 assertFalse(colorBefore
.contains(fHighlight
));
306 assertFalse(colorBefore
.contains(EXPECTED_GREEN
));
308 assertTrue(colorAfter
.contains(fBackground
));
309 assertTrue(colorAfter
.contains(fForeground
));
310 assertFalse(colorAfter
.contains(fHighlight
));
311 assertTrue(colorAfter
.contains(EXPECTED_GREEN
));
314 * Check that some background became green.
316 assertTrue(colorAfter
.count(fBackground
) < colorBefore
.count(fBackground
));
317 assertTrue(colorAfter
.count(EXPECTED_GREEN
) > colorBefore
.count(EXPECTED_GREEN
));
319 // reset the highlight color preference
320 colorRegistry
.put(HIGHLIGHT_COLOR_DEFINITION_ID
, fHighlight
);