swtbot: Stabilize FilterColorEditorTest
[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 1/*******************************************************************************
7d11641e 2 * Copyright (c) 2015, 2016 Ericsson
328e5fe4
MK
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;
7d11641e 41import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
328e5fe4 42import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
0a08e17d 43import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
7d11641e 44import org.eclipse.swtbot.swt.finder.results.Result;
328e5fe4 45import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
7d11641e 46import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
328e5fe4
MK
47import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
48import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
7431c59e 49import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
328e5fe4
MK
50import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper;
51import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
2592b7f6 52import org.eclipse.ui.PlatformUI;
328e5fe4
MK
53import org.junit.After;
54import org.junit.AfterClass;
55import org.junit.Before;
56import org.junit.BeforeClass;
57import org.junit.Test;
58import org.junit.runner.RunWith;
59
328e5fe4
MK
60import com.google.common.collect.Multiset;
61
62/**
63 * SWTBot test for testing highlighting
64 */
65@RunWith(SWTBotJunit4ClassRunner.class)
66public 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;
2592b7f6
PT
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$
328e5fe4
MK
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;
7431c59e 86 /** Expected color values */
328e5fe4
MK
87 private RGB fForeground;
88 private RGB fBackground;
7431c59e
MAL
89 private static RGB fHighlight;
90 private static RGB EXPECTED_GREEN;
328e5fe4
MK
91
92 /**
93 * Test Class setup
94 */
95 @BeforeClass
96 public static void init() {
5785ab49 97 SWTBotUtils.initialize();
328e5fe4
MK
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();
7431c59e
MAL
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);
328e5fe4
MK
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
7d11641e
PT
175 ImageHelper before = ImageHelper.grabImage(cellBounds);
176 Multiset<RGB> colorBefore = before.getHistogram();
328e5fe4
MK
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);
7d11641e 182 Multiset<RGB> colorAfter = waitForNewImage(cellBounds, before).getHistogram();
328e5fe4
MK
183
184 assertTrue(colorBefore.contains(fBackground));
185 assertTrue(colorBefore.contains(fForeground));
2592b7f6 186 assertFalse(colorBefore.contains(fHighlight));
328e5fe4
MK
187
188 assertTrue(colorAfter.contains(fBackground));
189 assertTrue(colorAfter.contains(fForeground));
2592b7f6 190 assertTrue(colorAfter.contains(fHighlight));
3dadb8ca 191
328e5fe4 192 /*
2592b7f6 193 * Check that some background became highlighted.
328e5fe4 194 */
3dadb8ca 195 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
2592b7f6 196 assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight));
328e5fe4
MK
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);
7d11641e 212 ImageHelper after = waitForNewImage(cellBounds, before);
328e5fe4
MK
213
214 Multiset<RGB> colorBefore = before.getHistogram();
215 Multiset<RGB> colorAfter = after.getHistogram();
216
217 assertTrue(colorBefore.contains(fBackground));
218 assertTrue(colorBefore.contains(fForeground));
2592b7f6 219 assertFalse(colorBefore.contains(fHighlight));
328e5fe4
MK
220
221 assertTrue(colorAfter.contains(fBackground));
222 assertTrue(colorAfter.contains(fForeground));
2592b7f6 223 assertTrue(colorAfter.contains(fHighlight));
328e5fe4
MK
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);
2592b7f6 232 if (prevPixel.equals(fBackground) && pixel.equals(fHighlight)) {
328e5fe4
MK
233 start = i;
234 }
2592b7f6 235 if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) {
328e5fe4
MK
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() {
0a08e17d 251 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
328e5fe4 252 ImageHelper before = ImageHelper.grabImage(cellBounds);
7d11641e 253
0a08e17d 254 // enter regex in Timestamp column
328e5fe4
MK
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);
7d11641e
PT
259 ImageHelper afterSearch = waitForNewImage(cellBounds, before);
260
0a08e17d 261 // click Add as Filter
328e5fe4 262 fTableBot.click(0, 0);
0a08e17d 263 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "<srch>", 0, TIMESTAMP_COLUMN));
7d11641e 264 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "22/22", 1, TIMESTAMP_COLUMN));
0a08e17d
PT
265 // the bounds have changed after applying the filter
266 cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
328e5fe4 267 ImageHelper afterFilter = ImageHelper.grabImage(cellBounds);
7d11641e 268
0a08e17d
PT
269 // press DEL to clear highlighting
270 fTableBot.pressShortcut(Keystrokes.DELETE);
7d11641e 271 ImageHelper afterClear = waitForNewImage(cellBounds, afterFilter);
328e5fe4
MK
272
273 List<RGB> beforeLine = before.getPixelRow(2);
0a08e17d 274 List<RGB> afterSearchLine = afterSearch.getPixelRow(2);
328e5fe4 275 List<RGB> afterFilterLine = afterFilter.getPixelRow(2);
0a08e17d 276 List<RGB> afterClearLine = afterClear.getPixelRow(2);
328e5fe4 277
0a08e17d 278 assertEquals(beforeLine.size(), afterSearchLine.size());
328e5fe4 279 assertEquals(beforeLine.size(), afterFilterLine.size());
0a08e17d 280 assertEquals(beforeLine.size(), afterClearLine.size());
328e5fe4 281 for (int i = 0; i < beforeLine.size(); i++) {
328e5fe4 282 RGB beforePixel = beforeLine.get(i);
0a08e17d
PT
283 RGB afterSearchPixel = afterSearchLine.get(i);
284 RGB afterFilterPixel = afterFilterLine.get(i);
285 RGB afterClearPixel = afterClearLine.get(i);
328e5fe4 286
0a08e17d
PT
287 assertEquals(afterSearchPixel, afterFilterPixel);
288 assertEquals(beforePixel, afterClearPixel);
289 if (!afterSearchPixel.equals(fHighlight)) {
290 assertEquals(beforePixel, afterSearchPixel);
328e5fe4 291 } else {
2592b7f6 292 assertNotEquals(fHighlight, beforePixel);
328e5fe4
MK
293 }
294
295 }
0a08e17d
PT
296 assertEquals(afterSearchLine, afterFilterLine);
297 assertEquals(beforeLine, afterClearLine);
298 assertNotEquals(afterSearchLine, beforeLine);
328e5fe4 299 }
2592b7f6
PT
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
7d11641e
PT
312 ImageHelper before = ImageHelper.grabImage(cellBounds);
313 Multiset<RGB> colorBefore = before.getHistogram();
2592b7f6
PT
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);
7d11641e 319 Multiset<RGB> colorAfter = waitForNewImage(cellBounds, before).getHistogram();
2592b7f6
PT
320
321 assertTrue(colorBefore.contains(fBackground));
322 assertTrue(colorBefore.contains(fForeground));
323 assertFalse(colorBefore.contains(fHighlight));
7431c59e 324 assertFalse(colorBefore.contains(EXPECTED_GREEN));
2592b7f6
PT
325
326 assertTrue(colorAfter.contains(fBackground));
327 assertTrue(colorAfter.contains(fForeground));
328 assertFalse(colorAfter.contains(fHighlight));
7431c59e 329 assertTrue(colorAfter.contains(EXPECTED_GREEN));
2592b7f6
PT
330
331 /*
332 * Check that some background became green.
333 */
334 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
7431c59e 335 assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN));
2592b7f6
PT
336
337 // reset the highlight color preference
338 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight);
339 }
7d11641e
PT
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 }
328e5fe4 362}
This page took 0.063437 seconds and 5 git commands to generate.