| 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 | |
| 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.junit.SWTBotJunit4ClassRunner; |
| 42 | import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes; |
| 43 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
| 44 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; |
| 45 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; |
| 46 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
| 47 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper; |
| 48 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 49 | import org.eclipse.ui.PlatformUI; |
| 50 | import org.junit.After; |
| 51 | import org.junit.AfterClass; |
| 52 | import org.junit.Before; |
| 53 | import org.junit.BeforeClass; |
| 54 | import org.junit.Test; |
| 55 | import org.junit.runner.RunWith; |
| 56 | |
| 57 | import com.google.common.collect.Multiset; |
| 58 | |
| 59 | /** |
| 60 | * SWTBot test for testing highlighting |
| 61 | */ |
| 62 | @RunWith(SWTBotJunit4ClassRunner.class) |
| 63 | public 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; |
| 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$ |
| 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; |
| 83 | /** Expected color values */ |
| 84 | private RGB fForeground; |
| 85 | private RGB fBackground; |
| 86 | private static RGB fHighlight; |
| 87 | private static RGB EXPECTED_GREEN; |
| 88 | |
| 89 | /** |
| 90 | * Test Class setup |
| 91 | */ |
| 92 | @BeforeClass |
| 93 | public static void init() { |
| 94 | SWTBotUtils.initialize(); |
| 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(); |
| 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); |
| 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)); |
| 182 | assertFalse(colorBefore.contains(fHighlight)); |
| 183 | |
| 184 | assertTrue(colorAfter.contains(fBackground)); |
| 185 | assertTrue(colorAfter.contains(fForeground)); |
| 186 | assertTrue(colorAfter.contains(fHighlight)); |
| 187 | |
| 188 | /* |
| 189 | * Check that some background became highlighted. |
| 190 | */ |
| 191 | assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground)); |
| 192 | assertTrue(colorAfter.count(fHighlight) > colorBefore.count(fHighlight)); |
| 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)); |
| 215 | assertFalse(colorBefore.contains(fHighlight)); |
| 216 | |
| 217 | assertTrue(colorAfter.contains(fBackground)); |
| 218 | assertTrue(colorAfter.contains(fForeground)); |
| 219 | assertTrue(colorAfter.contains(fHighlight)); |
| 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); |
| 228 | if (prevPixel.equals(fBackground) && pixel.equals(fHighlight)) { |
| 229 | start = i; |
| 230 | } |
| 231 | if (prevPixel.equals(fHighlight) && pixel.equals(fBackground)) { |
| 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() { |
| 247 | Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN); |
| 248 | ImageHelper before = ImageHelper.grabImage(cellBounds); |
| 249 | // enter regex in Timestamp column |
| 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); |
| 254 | ImageHelper afterSearch = ImageHelper.grabImage(cellBounds); |
| 255 | // click Add as Filter |
| 256 | fTableBot.click(0, 0); |
| 257 | fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "<srch>", 0, TIMESTAMP_COLUMN)); |
| 258 | //TODO: We need a better way to make sure that the table is done updating |
| 259 | SWTBotUtils.delay(2000); |
| 260 | // the bounds have changed after applying the filter |
| 261 | cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN); |
| 262 | ImageHelper afterFilter = ImageHelper.grabImage(cellBounds); |
| 263 | // press DEL to clear highlighting |
| 264 | fTableBot.pressShortcut(Keystrokes.DELETE); |
| 265 | ImageHelper afterClear = ImageHelper.grabImage(cellBounds); |
| 266 | |
| 267 | List<RGB> beforeLine = before.getPixelRow(2); |
| 268 | List<RGB> afterSearchLine = afterSearch.getPixelRow(2); |
| 269 | List<RGB> afterFilterLine = afterFilter.getPixelRow(2); |
| 270 | List<RGB> afterClearLine = afterClear.getPixelRow(2); |
| 271 | |
| 272 | assertEquals(beforeLine.size(), afterSearchLine.size()); |
| 273 | assertEquals(beforeLine.size(), afterFilterLine.size()); |
| 274 | assertEquals(beforeLine.size(), afterClearLine.size()); |
| 275 | for (int i = 0; i < beforeLine.size(); i++) { |
| 276 | RGB beforePixel = beforeLine.get(i); |
| 277 | RGB afterSearchPixel = afterSearchLine.get(i); |
| 278 | RGB afterFilterPixel = afterFilterLine.get(i); |
| 279 | RGB afterClearPixel = afterClearLine.get(i); |
| 280 | |
| 281 | assertEquals(afterSearchPixel, afterFilterPixel); |
| 282 | assertEquals(beforePixel, afterClearPixel); |
| 283 | if (!afterSearchPixel.equals(fHighlight)) { |
| 284 | assertEquals(beforePixel, afterSearchPixel); |
| 285 | } else { |
| 286 | assertNotEquals(fHighlight, beforePixel); |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | assertEquals(afterSearchLine, afterFilterLine); |
| 291 | assertEquals(beforeLine, afterClearLine); |
| 292 | assertNotEquals(afterSearchLine, beforeLine); |
| 293 | } |
| 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)); |
| 317 | assertFalse(colorBefore.contains(EXPECTED_GREEN)); |
| 318 | |
| 319 | assertTrue(colorAfter.contains(fBackground)); |
| 320 | assertTrue(colorAfter.contains(fForeground)); |
| 321 | assertFalse(colorAfter.contains(fHighlight)); |
| 322 | assertTrue(colorAfter.contains(EXPECTED_GREEN)); |
| 323 | |
| 324 | /* |
| 325 | * Check that some background became green. |
| 326 | */ |
| 327 | assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground)); |
| 328 | assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN)); |
| 329 | |
| 330 | // reset the highlight color preference |
| 331 | colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight); |
| 332 | } |
| 333 | } |