tmf: Add waitUntil / condition to tmf.ui.tests
[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;
fd7909d2
PT
47import org.eclipse.swtbot.swt.finder.widgets.SWTBotCLabel;
48import org.eclipse.swtbot.swt.finder.widgets.SWTBotCanvas;
328e5fe4
MK
49import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
50import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
7431c59e 51import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
328e5fe4
MK
52import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper;
53import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
f0beeb4a 54import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
2592b7f6 55import org.eclipse.ui.PlatformUI;
328e5fe4
MK
56import org.junit.After;
57import org.junit.AfterClass;
58import org.junit.Before;
59import org.junit.BeforeClass;
60import org.junit.Test;
61import org.junit.runner.RunWith;
62
328e5fe4
MK
63import com.google.common.collect.Multiset;
64
65/**
66 * SWTBot test for testing highlighting
67 */
68@RunWith(SWTBotJunit4ClassRunner.class)
69public class FilterColorEditorTest {
70
71 private static final int TIMESTAMP_COLUMN = 1;
72 private static final int SOURCE_COLUMN = 2;
73 private static final int MESSAGE_COLUMN = 6;
2592b7f6
PT
74 private static final RGB GREEN = new RGB(0, 255, 0);
75 private static final String HIGHLIGHT_COLOR_DEFINITION_ID = "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
328e5fe4
MK
76 private static final String TRACE_PROJECT_NAME = "test";
77 private static final String COLUMN_TRACE = "syslog_collapse";
78 private static final String COLUMN_TRACE_PATH = "testfiles/" + COLUMN_TRACE;
79 private static final String COLUMN_TRACE_TYPE = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
80
81 private static File fTestFile = null;
82
83 private static SWTWorkbenchBot fBot;
84
85 /** The Log4j logger instance. */
86 private static final Logger fLogger = Logger.getRootLogger();
87 private SWTBotTable fTableBot;
88 private static final int ROW = 8;
7431c59e 89 /** Expected color values */
328e5fe4
MK
90 private RGB fForeground;
91 private RGB fBackground;
7431c59e
MAL
92 private static RGB fHighlight;
93 private static RGB EXPECTED_GREEN;
328e5fe4
MK
94
95 /**
96 * Test Class setup
97 */
98 @BeforeClass
99 public static void init() {
5785ab49 100 SWTBotUtils.initialize();
328e5fe4
MK
101
102 /* set up test trace */
103 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null);
104 URI uri;
105 try {
106 uri = FileLocator.toFileURL(location).toURI();
107 fTestFile = new File(uri);
108 } catch (URISyntaxException | IOException e) {
109 fail(e.getMessage());
110 }
111
112 assumeTrue(fTestFile.exists());
113
114 /* Set up for swtbot */
115 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
116 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
117
118 fLogger.removeAllAppenders();
119 fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
120 fBot = new SWTWorkbenchBot();
121
122 /* Close welcome view */
123 SWTBotUtils.closeView("Welcome", fBot);
124
125 /* Switch perspectives */
126 SWTBotUtils.switchToTracingPerspective();
127
128 /* Finish waiting for eclipse to load */
f0beeb4a 129 WaitUtils.waitForJobs();
7431c59e
MAL
130
131 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
132 fHighlight = ImageHelper.adjustExpectedColor(colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID).getRGB());
133 EXPECTED_GREEN = ImageHelper.adjustExpectedColor(GREEN);
328e5fe4
MK
134 }
135
136 /**
137 * Test class tear down method.
138 */
139 @AfterClass
140 public static void tearDown() {
141 fLogger.removeAllAppenders();
142 }
143
144 /**
145 * Bring up the table
146 */
147 @Before
148 public void setup() {
149 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
150
151 // Open the actual trace
152 SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE);
153 SWTBotEditor editorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName());
154
155 fTableBot = editorBot.bot().table();
156 fBackground = fTableBot.backgroundColor().getRGB();
157 fForeground = fTableBot.foregroundColor().getRGB();
158
159 SWTBotUtils.maximizeTable(fTableBot);
160 }
161
162 /**
163 * Remove the project
164 */
165 @After
166 public void cleanup() {
167 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
f0beeb4a 168 WaitUtils.waitForJobs();
328e5fe4
MK
169 }
170
171 /**
172 * Test basic highlight
173 */
174 @Test
175 public void testHighlight() {
176 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
177
7d11641e
PT
178 ImageHelper before = ImageHelper.grabImage(cellBounds);
179 Multiset<RGB> colorBefore = before.getHistogram();
328e5fe4
MK
180 // Select source column and enter regex
181 fTableBot.click(0, SOURCE_COLUMN);
182 fBot.text().typeText("HostF\n", 100);
183 // make sure selected row is not matching row
184 fTableBot.select(ROW - 1);
7d11641e 185 Multiset<RGB> colorAfter = waitForNewImage(cellBounds, before).getHistogram();
328e5fe4
MK
186
187 assertTrue(colorBefore.contains(fBackground));
188 assertTrue(colorBefore.contains(fForeground));
2592b7f6 189 assertFalse(colorBefore.contains(fHighlight));
328e5fe4
MK
190
191 assertTrue(colorAfter.contains(fBackground));
192 assertTrue(colorAfter.contains(fForeground));
2592b7f6 193 assertTrue(colorAfter.contains(fHighlight));
3dadb8ca 194
328e5fe4 195 /*
2592b7f6 196 * Check that some background became highlighted.
328e5fe4 197 */
3dadb8ca 198 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
2592b7f6 199 assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight));
328e5fe4
MK
200 }
201
202 /**
203 * Test highlighting multiple elements in a message
204 */
205 @Test
206 public void testMultiHighlightMessage() {
207 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, MESSAGE_COLUMN);
208
209 ImageHelper before = ImageHelper.grabImage(cellBounds);
210 // enter regex in message column
211 fTableBot.click(0, MESSAGE_COLUMN);
212 fBot.text().typeText("e\n", 100);
213 // make sure matching item is not selected
214 fTableBot.select(ROW - 1);
7d11641e 215 ImageHelper after = waitForNewImage(cellBounds, before);
328e5fe4
MK
216
217 Multiset<RGB> colorBefore = before.getHistogram();
218 Multiset<RGB> colorAfter = after.getHistogram();
219
220 assertTrue(colorBefore.contains(fBackground));
221 assertTrue(colorBefore.contains(fForeground));
2592b7f6 222 assertFalse(colorBefore.contains(fHighlight));
328e5fe4
MK
223
224 assertTrue(colorAfter.contains(fBackground));
225 assertTrue(colorAfter.contains(fForeground));
2592b7f6 226 assertTrue(colorAfter.contains(fHighlight));
328e5fe4
MK
227
228 int start = -1;
229 int end;
230 List<Point> intervals = new ArrayList<>();
231 List<RGB> pixelRow = after.getPixelRow(2);
232 for (int i = 1; i < pixelRow.size(); i++) {
233 RGB prevPixel = pixelRow.get(i - 1);
234 RGB pixel = pixelRow.get(i);
2592b7f6 235 if (prevPixel.equals(fBackground) && pixel.equals(fHighlight)) {
328e5fe4
MK
236 start = i;
237 }
2592b7f6 238 if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) {
328e5fe4
MK
239 end = i;
240 if (start == -1) {
241 fail();
242 }
243 intervals.add(new Point(start, end));
244 }
245 }
246 assertEquals(2, intervals.size());
247 }
248
249 /**
250 * Switch to filter and back
251 */
252 @Test
253 public void testSwitchToFilter() {
0a08e17d 254 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
328e5fe4 255 ImageHelper before = ImageHelper.grabImage(cellBounds);
7d11641e 256
0a08e17d 257 // enter regex in Timestamp column
328e5fe4
MK
258 fTableBot.click(0, TIMESTAMP_COLUMN);
259 fBot.text().typeText("00\n", 100);
260 // make sure matching column is not selected
261 fTableBot.select(ROW - 1);
7d11641e
PT
262 ImageHelper afterSearch = waitForNewImage(cellBounds, before);
263
0a08e17d 264 // click Add as Filter
328e5fe4 265 fTableBot.click(0, 0);
0a08e17d 266 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "<srch>", 0, TIMESTAMP_COLUMN));
7d11641e 267 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "22/22", 1, TIMESTAMP_COLUMN));
0a08e17d
PT
268 // the bounds have changed after applying the filter
269 cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN);
328e5fe4 270 ImageHelper afterFilter = ImageHelper.grabImage(cellBounds);
7d11641e 271
0a08e17d
PT
272 // press DEL to clear highlighting
273 fTableBot.pressShortcut(Keystrokes.DELETE);
7d11641e 274 ImageHelper afterClear = waitForNewImage(cellBounds, afterFilter);
328e5fe4
MK
275
276 List<RGB> beforeLine = before.getPixelRow(2);
0a08e17d 277 List<RGB> afterSearchLine = afterSearch.getPixelRow(2);
328e5fe4 278 List<RGB> afterFilterLine = afterFilter.getPixelRow(2);
0a08e17d 279 List<RGB> afterClearLine = afterClear.getPixelRow(2);
328e5fe4 280
0a08e17d 281 assertEquals(beforeLine.size(), afterSearchLine.size());
328e5fe4 282 assertEquals(beforeLine.size(), afterFilterLine.size());
0a08e17d 283 assertEquals(beforeLine.size(), afterClearLine.size());
328e5fe4 284 for (int i = 0; i < beforeLine.size(); i++) {
328e5fe4 285 RGB beforePixel = beforeLine.get(i);
0a08e17d
PT
286 RGB afterSearchPixel = afterSearchLine.get(i);
287 RGB afterFilterPixel = afterFilterLine.get(i);
288 RGB afterClearPixel = afterClearLine.get(i);
328e5fe4 289
0a08e17d
PT
290 assertEquals(afterSearchPixel, afterFilterPixel);
291 assertEquals(beforePixel, afterClearPixel);
292 if (!afterSearchPixel.equals(fHighlight)) {
293 assertEquals(beforePixel, afterSearchPixel);
328e5fe4 294 } else {
2592b7f6 295 assertNotEquals(fHighlight, beforePixel);
328e5fe4
MK
296 }
297
298 }
0a08e17d
PT
299 assertEquals(afterSearchLine, afterFilterLine);
300 assertEquals(beforeLine, afterClearLine);
301 assertNotEquals(afterSearchLine, beforeLine);
328e5fe4 302 }
2592b7f6
PT
303
304 /**
305 * Test highlight color preference
306 */
307 @Test
308 public void testPreference() {
309 // change the highlight color preference
310 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
311 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, GREEN);
312
313 final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN);
314
7d11641e
PT
315 ImageHelper before = ImageHelper.grabImage(cellBounds);
316 Multiset<RGB> colorBefore = before.getHistogram();
2592b7f6
PT
317 // Select source column and enter regex
318 fTableBot.click(0, SOURCE_COLUMN);
319 fBot.text().typeText("HostF\n", 100);
320 // make sure selected row is not matching row
321 fTableBot.select(ROW - 1);
7d11641e 322 Multiset<RGB> colorAfter = waitForNewImage(cellBounds, before).getHistogram();
2592b7f6
PT
323
324 assertTrue(colorBefore.contains(fBackground));
325 assertTrue(colorBefore.contains(fForeground));
326 assertFalse(colorBefore.contains(fHighlight));
7431c59e 327 assertFalse(colorBefore.contains(EXPECTED_GREEN));
2592b7f6
PT
328
329 assertTrue(colorAfter.contains(fBackground));
330 assertTrue(colorAfter.contains(fForeground));
331 assertFalse(colorAfter.contains(fHighlight));
7431c59e 332 assertTrue(colorAfter.contains(EXPECTED_GREEN));
2592b7f6
PT
333
334 /*
335 * Check that some background became green.
336 */
337 assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground));
7431c59e 338 assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN));
2592b7f6
PT
339
340 // reset the highlight color preference
341 colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight);
342 }
7d11641e 343
fd7909d2
PT
344 /**
345 * Test the header bar
346 */
347 @Test
348 public void testHeaderBar() {
349 // Add search filter on Timestamp column
350 fTableBot.click(0, TIMESTAMP_COLUMN);
351 fBot.text().typeText("2");
352 fBot.text().pressShortcut(Keystrokes.CR);
353 // Add search filter on Message column and Add as Filter
354 fTableBot.click(0, MESSAGE_COLUMN);
355 fBot.text().typeText("F");
356 fBot.text().pressShortcut(Keystrokes.CTRL, Keystrokes.CR);
357 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "2/22", 1, 1));
358 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "Message F", 3, MESSAGE_COLUMN));
359 fBot.clabel("Timestamp matches \"2\"");
360 fBot.clabel("Message matches \"F\"");
361 waitForHighlightState(3, TIMESTAMP_COLUMN, true);
362 waitForHighlightState(3, MESSAGE_COLUMN, true);
363 // Clear all filter highlighting
364 fTableBot.pressShortcut(Keystrokes.DELETE);
365 waitForHighlightState(3, TIMESTAMP_COLUMN, false);
366 waitForHighlightState(3, MESSAGE_COLUMN, false);
367 // Click filter label to set the filter highlighting on Message filter
368 SWTBotCLabel filterCLabel = fBot.clabel("Message matches \"F\"");
369 SWTBotCanvas filterCanvas = new SWTBotCanvas(filterCLabel.widget);
370 filterCanvas.click();
371 waitForHighlightState(3, TIMESTAMP_COLUMN, false);
372 waitForHighlightState(3, MESSAGE_COLUMN, true);
373 // Click filter icon to remove the Message filter
374 Rectangle imageBounds = filterCLabel.image().getBounds();
375 filterCanvas.click(filterCLabel.widget.getLeftMargin() + imageBounds.width / 2, filterCLabel.widget.getTopMargin() + imageBounds.height / 2);
376 fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "5/22", 1, 1));
377 }
378
379 private void waitForHighlightState(int row, int column, boolean highlight) {
380 fBot.waitUntil(new DefaultCondition() {
381 @Override
382 public boolean test() throws Exception {
383 Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, row, column);
384 ImageHelper imageHelper = ImageHelper.grabImage(cellBounds);
385 return imageHelper.getHistogram().contains(fHighlight) == highlight;
386 }
387
388 @Override
389 public String getFailureMessage() {
390 return String.format("Cell (%d, %d) did not have highlight state: %s", row, column, Boolean.toString(highlight));
391 }
392 });
393 }
394
7d11641e
PT
395 private static ImageHelper waitForNewImage(Rectangle bounds, ImageHelper currentImage) {
396 ImageHelper[] newImage = new ImageHelper[1];
397 fBot.waitUntil(new DefaultCondition() {
398 @Override
399 public boolean test() throws Exception {
400 return UIThreadRunnable.syncExec(new Result<Boolean> () {
401 @Override
402 public Boolean run() {
403 newImage[0] = ImageHelper.grabImage(bounds);
404 return !newImage[0].equals(currentImage);
405 }
406 });
407 }
408
409 @Override
410 public String getFailureMessage() {
411 return "Image at bounds " + bounds + " did not change";
412 }
413 });
414 return newImage[0];
415 }
328e5fe4 416}
This page took 0.071224 seconds and 5 git commands to generate.