tmf: Add collapsible event table header bar with applied filter labels
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / FilterColorEditorTest.java
CommitLineData
328e5fe4
MK
1/*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertFalse;
17import static org.junit.Assert.assertNotEquals;
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.fail;
20import static org.junit.Assume.assumeTrue;
21
22import java.io.File;
23import java.io.IOException;
24import java.net.URI;
25import java.net.URISyntaxException;
26import java.net.URL;
27import java.util.ArrayList;
28import java.util.List;
29
30import org.apache.log4j.ConsoleAppender;
31import org.apache.log4j.Logger;
32import org.apache.log4j.SimpleLayout;
33import org.eclipse.core.runtime.FileLocator;
34import org.eclipse.core.runtime.Path;
2592b7f6 35import org.eclipse.jface.resource.ColorRegistry;
328e5fe4
MK
36import org.eclipse.swt.graphics.Point;
37import org.eclipse.swt.graphics.RGB;
38import org.eclipse.swt.graphics.Rectangle;
39import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
40import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
41import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
0a08e17d 42import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
328e5fe4
MK
43import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
44import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
45import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
7431c59e 46import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
328e5fe4
MK
47import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper;
48import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
2592b7f6 49import org.eclipse.ui.PlatformUI;
328e5fe4
MK
50import org.junit.After;
51import org.junit.AfterClass;
52import org.junit.Before;
53import org.junit.BeforeClass;
54import org.junit.Test;
55import org.junit.runner.RunWith;
56
328e5fe4
MK
57import com.google.common.collect.Multiset;
58
59/**
60 * SWTBot test for testing highlighting
61 */
62@RunWith(SWTBotJunit4ClassRunner.class)
63public class FilterColorEditorTest {
64
65 private static final int TIMESTAMP_COLUMN = 1;
66 private static final int SOURCE_COLUMN = 2;
67 private static final int MESSAGE_COLUMN = 6;
2592b7f6
PT
68 private static final RGB GREEN = new RGB(0, 255, 0);
69 private static final String HIGHLIGHT_COLOR_DEFINITION_ID = "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
328e5fe4
MK
70 private static final String TRACE_PROJECT_NAME = "test";
71 private static final String COLUMN_TRACE = "syslog_collapse";
72 private static final String COLUMN_TRACE_PATH = "testfiles/" + COLUMN_TRACE;
73 private static final String COLUMN_TRACE_TYPE = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
74
75 private static File fTestFile = null;
76
77 private static SWTWorkbenchBot fBot;
78
79 /** The Log4j logger instance. */
80 private static final Logger fLogger = Logger.getRootLogger();
81 private SWTBotTable fTableBot;
82 private static final int ROW = 8;
7431c59e 83 /** Expected color values */
328e5fe4
MK
84 private RGB fForeground;
85 private RGB fBackground;
7431c59e
MAL
86 private static RGB fHighlight;
87 private static RGB EXPECTED_GREEN;
328e5fe4
MK
88
89 /**
90 * Test Class setup
91 */
92 @BeforeClass
93 public static void init() {
5785ab49 94 SWTBotUtils.initialize();
328e5fe4
MK
95
96 /* set up test trace */
97 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null);
98 URI uri;
99 try {
100 uri = FileLocator.toFileURL(location).toURI();
101 fTestFile = new File(uri);
102 } catch (URISyntaxException | IOException e) {
103 fail(e.getMessage());
104 }
105
106 assumeTrue(fTestFile.exists());
107
108 /* Set up for swtbot */
109 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
110 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
111
112 fLogger.removeAllAppenders();
113 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
114 fBot = new SWTWorkbenchBot();
115
116 /* Close welcome view */
117 SWTBotUtils.closeView("Welcome", fBot);
118
119 /* Switch perspectives */
120 SWTBotUtils.switchToTracingPerspective();
121
122 /* Finish waiting for eclipse to load */
123 SWTBotUtils.waitForJobs();
7431c59e
MAL
124
125 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
126 fHighlight = ImageHelper.adjustExpectedColor(colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID).getRGB());
127 EXPECTED_GREEN = ImageHelper.adjustExpectedColor(GREEN);
328e5fe4
MK
128 }
129
130 /**
131 * Test class tear down method.
132 */
133 @AfterClass
134 public static void tearDown() {
135 fLogger.removeAllAppenders();
136 }
137
138 /**
139 * Bring up the table
140 */
141 @Before
142 public void setup() {
143 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
144
145 // Open the actual trace
146 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE);
147 SWTBotEditor editorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName());
148
149 fTableBot = editorBot.bot().table();
150 fBackground = fTableBot.backgroundColor().getRGB();
151 fForeground = fTableBot.foregroundColor().getRGB();
152
153 SWTBotUtils.maximizeTable(fTableBot);
154 }
155
156 /**
157 * Remove the project
158 */
159 @After
160 public void cleanup() {
161 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
162 SWTBotUtils.waitForJobs();
163 }
164
165 /**
166 * Test basic highlight
167 */
168 @Test
169 public void testHighlight() {
170 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
171
172 Multiset<RGB> colorBefore = ImageHelper.grabImage(cellBounds).getHistogram();
173 // Select source column and enter regex
174 fTableBot.click(0, SOURCE_COLUMN);
175 fBot.text().typeText("HostF\n", 100);
176 // make sure selected row is not matching row
177 fTableBot.select(ROW - 1);
178 Multiset<RGB> colorAfter = ImageHelper.grabImage(cellBounds).getHistogram();
179
180 assertTrue(colorBefore.contains(fBackground));
181 assertTrue(colorBefore.contains(fForeground));
2592b7f6 182 assertFalse(colorBefore.contains(fHighlight));
328e5fe4
MK
183
184 assertTrue(colorAfter.contains(fBackground));
185 assertTrue(colorAfter.contains(fForeground));
2592b7f6 186 assertTrue(colorAfter.contains(fHighlight));
3dadb8ca 187
328e5fe4 188 /*
2592b7f6 189 * Check that some background became highlighted.
328e5fe4 190 */
3dadb8ca 191 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
2592b7f6 192 assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight));
328e5fe4
MK
193 }
194
195 /**
196 * Test highlighting multiple elements in a message
197 */
198 @Test
199 public void testMultiHighlightMessage() {
200 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, MESSAGE_COLUMN);
201
202 ImageHelper before = ImageHelper.grabImage(cellBounds);
203 // enter regex in message column
204 fTableBot.click(0, MESSAGE_COLUMN);
205 fBot.text().typeText("e\n", 100);
206 // make sure matching item is not selected
207 fTableBot.select(ROW - 1);
208 ImageHelper after = ImageHelper.grabImage(cellBounds);
209
210 Multiset<RGB> colorBefore = before.getHistogram();
211 Multiset<RGB> colorAfter = after.getHistogram();
212
213 assertTrue(colorBefore.contains(fBackground));
214 assertTrue(colorBefore.contains(fForeground));
2592b7f6 215 assertFalse(colorBefore.contains(fHighlight));
328e5fe4
MK
216
217 assertTrue(colorAfter.contains(fBackground));
218 assertTrue(colorAfter.contains(fForeground));
2592b7f6 219 assertTrue(colorAfter.contains(fHighlight));
328e5fe4
MK
220
221 int start = -1;
222 int end;
223 List<Point> intervals = new ArrayList<>();
224 List<RGB> pixelRow = after.getPixelRow(2);
225 for (int i = 1; i < pixelRow.size(); i++) {
226 RGB prevPixel = pixelRow.get(i - 1);
227 RGB pixel = pixelRow.get(i);
2592b7f6 228 if (prevPixel.equals(fBackground) && pixel.equals(fHighlight)) {
328e5fe4
MK
229 start = i;
230 }
2592b7f6 231 if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) {
328e5fe4
MK
232 end = i;
233 if (start == -1) {
234 fail();
235 }
236 intervals.add(new Point(start, end));
237 }
238 }
239 assertEquals(2, intervals.size());
240 }
241
242 /**
243 * Switch to filter and back
244 */
245 @Test
246 public void testSwitchToFilter() {
0a08e17d 247 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
328e5fe4 248 ImageHelper before = ImageHelper.grabImage(cellBounds);
0a08e17d 249 // enter regex in Timestamp column
328e5fe4
MK
250 fTableBot.click(0, TIMESTAMP_COLUMN);
251 fBot.text().typeText("00\n", 100);
252 // make sure matching column is not selected
253 fTableBot.select(ROW - 1);
0a08e17d
PT
254 ImageHelper afterSearch = ImageHelper.grabImage(cellBounds);
255 // click Add as Filter
328e5fe4 256 fTableBot.click(0, 0);
0a08e17d 257 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "<srch>", 0, TIMESTAMP_COLUMN));
7431c59e
MAL
258 //TODO: We need a better way to make sure that the table is done updating
259 SWTBotUtils.delay(2000);
0a08e17d
PT
260 // the bounds have changed after applying the filter
261 cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
328e5fe4 262 ImageHelper afterFilter = ImageHelper.grabImage(cellBounds);
0a08e17d
PT
263 // press DEL to clear highlighting
264 fTableBot.pressShortcut(Keystrokes.DELETE);
265 ImageHelper afterClear = ImageHelper.grabImage(cellBounds);
328e5fe4
MK
266
267 List<RGB> beforeLine = before.getPixelRow(2);
0a08e17d 268 List<RGB> afterSearchLine = afterSearch.getPixelRow(2);
328e5fe4 269 List<RGB> afterFilterLine = afterFilter.getPixelRow(2);
0a08e17d 270 List<RGB> afterClearLine = afterClear.getPixelRow(2);
328e5fe4 271
0a08e17d 272 assertEquals(beforeLine.size(), afterSearchLine.size());
328e5fe4 273 assertEquals(beforeLine.size(), afterFilterLine.size());
0a08e17d 274 assertEquals(beforeLine.size(), afterClearLine.size());
328e5fe4 275 for (int i = 0; i < beforeLine.size(); i++) {
328e5fe4 276 RGB beforePixel = beforeLine.get(i);
0a08e17d
PT
277 RGB afterSearchPixel = afterSearchLine.get(i);
278 RGB afterFilterPixel = afterFilterLine.get(i);
279 RGB afterClearPixel = afterClearLine.get(i);
328e5fe4 280
0a08e17d
PT
281 assertEquals(afterSearchPixel, afterFilterPixel);
282 assertEquals(beforePixel, afterClearPixel);
283 if (!afterSearchPixel.equals(fHighlight)) {
284 assertEquals(beforePixel, afterSearchPixel);
328e5fe4 285 } else {
2592b7f6 286 assertNotEquals(fHighlight, beforePixel);
328e5fe4
MK
287 }
288
289 }
0a08e17d
PT
290 assertEquals(afterSearchLine, afterFilterLine);
291 assertEquals(beforeLine, afterClearLine);
292 assertNotEquals(afterSearchLine, beforeLine);
328e5fe4 293 }
2592b7f6
PT
294
295 /**
296 * Test highlight color preference
297 */
298 @Test
299 public void testPreference() {
300 // change the highlight color preference
301 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
302 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, GREEN);
303
304 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
305
306 Multiset<RGB> colorBefore = ImageHelper.grabImage(cellBounds).getHistogram();
307 // Select source column and enter regex
308 fTableBot.click(0, SOURCE_COLUMN);
309 fBot.text().typeText("HostF\n", 100);
310 // make sure selected row is not matching row
311 fTableBot.select(ROW - 1);
312 Multiset<RGB> colorAfter = ImageHelper.grabImage(cellBounds).getHistogram();
313
314 assertTrue(colorBefore.contains(fBackground));
315 assertTrue(colorBefore.contains(fForeground));
316 assertFalse(colorBefore.contains(fHighlight));
7431c59e 317 assertFalse(colorBefore.contains(EXPECTED_GREEN));
2592b7f6
PT
318
319 assertTrue(colorAfter.contains(fBackground));
320 assertTrue(colorAfter.contains(fForeground));
321 assertFalse(colorAfter.contains(fHighlight));
7431c59e 322 assertTrue(colorAfter.contains(EXPECTED_GREEN));
2592b7f6
PT
323
324 /*
325 * Check that some background became green.
326 */
327 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
7431c59e 328 assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN));
2592b7f6
PT
329
330 // reset the highlight color preference
331 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight);
332 }
328e5fe4 333}
This page took 0.05515 seconds and 5 git commands to generate.