5ce5f348ee788ea65198af23e5cb86b1d492d1ff
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / FilterColorEditorTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 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
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
14
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;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.List;
29
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;
59
60 import com.google.common.collect.Multiset;
61
62 /**
63 * SWTBot test for testing highlighting
64 */
65 @RunWith(SWTBotJunit4ClassRunner.class)
66 public class FilterColorEditorTest {
67
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";
77
78 private static File fTestFile = null;
79
80 private static SWTWorkbenchBot fBot;
81
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;
91
92 /**
93 * Test Class setup
94 */
95 @BeforeClass
96 public static void init() {
97 SWTBotUtils.initialize();
98
99 /* set up test trace */
100 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null);
101 URI uri;
102 try {
103 uri = FileLocator.toFileURL(location).toURI();
104 fTestFile = new File(uri);
105 } catch (URISyntaxException | IOException e) {
106 fail(e.getMessage());
107 }
108
109 assumeTrue(fTestFile.exists());
110
111 /* Set up for swtbot */
112 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
113 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
114
115 fLogger.removeAllAppenders();
116 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
117 fBot = new SWTWorkbenchBot();
118
119 /* Close welcome view */
120 SWTBotUtils.closeView("Welcome", fBot);
121
122 /* Switch perspectives */
123 SWTBotUtils.switchToTracingPerspective();
124
125 /* Finish waiting for eclipse to load */
126 SWTBotUtils.waitForJobs();
127
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);
131 }
132
133 /**
134 * Test class tear down method.
135 */
136 @AfterClass
137 public static void tearDown() {
138 fLogger.removeAllAppenders();
139 }
140
141 /**
142 * Bring up the table
143 */
144 @Before
145 public void setup() {
146 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
147
148 // Open the actual trace
149 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE);
150 SWTBotEditor editorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName());
151
152 fTableBot = editorBot.bot().table();
153 fBackground = fTableBot.backgroundColor().getRGB();
154 fForeground = fTableBot.foregroundColor().getRGB();
155
156 SWTBotUtils.maximizeTable(fTableBot);
157 }
158
159 /**
160 * Remove the project
161 */
162 @After
163 public void cleanup() {
164 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
165 SWTBotUtils.waitForJobs();
166 }
167
168 /**
169 * Test basic highlight
170 */
171 @Test
172 public void testHighlight() {
173 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
174
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();
183
184 assertTrue(colorBefore.contains(fBackground));
185 assertTrue(colorBefore.contains(fForeground));
186 assertFalse(colorBefore.contains(fHighlight));
187
188 assertTrue(colorAfter.contains(fBackground));
189 assertTrue(colorAfter.contains(fForeground));
190 assertTrue(colorAfter.contains(fHighlight));
191
192 /*
193 * Check that some background became highlighted.
194 */
195 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
196 assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight));
197 }
198
199 /**
200 * Test highlighting multiple elements in a message
201 */
202 @Test
203 public void testMultiHighlightMessage() {
204 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, MESSAGE_COLUMN);
205
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);
213
214 Multiset<RGB> colorBefore = before.getHistogram();
215 Multiset<RGB> colorAfter = after.getHistogram();
216
217 assertTrue(colorBefore.contains(fBackground));
218 assertTrue(colorBefore.contains(fForeground));
219 assertFalse(colorBefore.contains(fHighlight));
220
221 assertTrue(colorAfter.contains(fBackground));
222 assertTrue(colorAfter.contains(fForeground));
223 assertTrue(colorAfter.contains(fHighlight));
224
225 int start = -1;
226 int end;
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)) {
233 start = i;
234 }
235 if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) {
236 end = i;
237 if (start == -1) {
238 fail();
239 }
240 intervals.add(new Point(start, end));
241 }
242 }
243 assertEquals(2, intervals.size());
244 }
245
246 /**
247 * Switch to filter and back
248 */
249 @Test
250 public void testSwitchToFilter() {
251 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
252 ImageHelper before = ImageHelper.grabImage(cellBounds);
253
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);
260
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);
268
269 // press DEL to clear highlighting
270 fTableBot.pressShortcut(Keystrokes.DELETE);
271 ImageHelper afterClear = waitForNewImage(cellBounds, afterFilter);
272
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);
277
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);
286
287 assertEquals(afterSearchPixel, afterFilterPixel);
288 assertEquals(beforePixel, afterClearPixel);
289 if (!afterSearchPixel.equals(fHighlight)) {
290 assertEquals(beforePixel, afterSearchPixel);
291 } else {
292 assertNotEquals(fHighlight, beforePixel);
293 }
294
295 }
296 assertEquals(afterSearchLine, afterFilterLine);
297 assertEquals(beforeLine, afterClearLine);
298 assertNotEquals(afterSearchLine, beforeLine);
299 }
300
301 /**
302 * Test highlight color preference
303 */
304 @Test
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);
309
310 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
311
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();
320
321 assertTrue(colorBefore.contains(fBackground));
322 assertTrue(colorBefore.contains(fForeground));
323 assertFalse(colorBefore.contains(fHighlight));
324 assertFalse(colorBefore.contains(EXPECTED_GREEN));
325
326 assertTrue(colorAfter.contains(fBackground));
327 assertTrue(colorAfter.contains(fForeground));
328 assertFalse(colorAfter.contains(fHighlight));
329 assertTrue(colorAfter.contains(EXPECTED_GREEN));
330
331 /*
332 * Check that some background became green.
333 */
334 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
335 assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN));
336
337 // reset the highlight color preference
338 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight);
339 }
340
341 private static ImageHelper waitForNewImage(Rectangle bounds, ImageHelper currentImage) {
342 ImageHelper[] newImage = new ImageHelper[1];
343 fBot.waitUntil(new DefaultCondition() {
344 @Override
345 public boolean test() throws Exception {
346 return UIThreadRunnable.syncExec(new Result<Boolean> () {
347 @Override
348 public Boolean run() {
349 newImage[0] = ImageHelper.grabImage(bounds);
350 return !newImage[0].equals(currentImage);
351 }
352 });
353 }
354
355 @Override
356 public String getFailureMessage() {
357 return "Image at bounds " + bounds + " did not change";
358 }
359 });
360 return newImage[0];
361 }
362 }
This page took 0.062997 seconds and 4 git commands to generate.