Commit | Line | Data |
---|---|---|
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 | ||
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.swt.graphics.Point; | |
36 | import org.eclipse.swt.graphics.RGB; | |
37 | import org.eclipse.swt.graphics.Rectangle; | |
38 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
39 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; | |
40 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; | |
41 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
42 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; | |
43 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; | |
44 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper; | |
45 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
46 | import org.junit.After; | |
47 | import org.junit.AfterClass; | |
48 | import org.junit.Before; | |
49 | import org.junit.BeforeClass; | |
50 | import org.junit.Test; | |
51 | import org.junit.runner.RunWith; | |
52 | ||
328e5fe4 MK |
53 | import com.google.common.collect.Multiset; |
54 | ||
55 | /** | |
56 | * SWTBot test for testing highlighting | |
57 | */ | |
58 | @RunWith(SWTBotJunit4ClassRunner.class) | |
59 | public class FilterColorEditorTest { | |
60 | ||
61 | private static final int TIMESTAMP_COLUMN = 1; | |
62 | private static final int SOURCE_COLUMN = 2; | |
63 | private static final int MESSAGE_COLUMN = 6; | |
64 | private static final RGB YELLOW = new RGB(255, 255, 0); | |
65 | private static final String TRACE_PROJECT_NAME = "test"; | |
66 | private static final String COLUMN_TRACE = "syslog_collapse"; | |
67 | private static final String COLUMN_TRACE_PATH = "testfiles/" + COLUMN_TRACE; | |
68 | private static final String COLUMN_TRACE_TYPE = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog"; | |
69 | ||
70 | private static File fTestFile = null; | |
71 | ||
72 | private static SWTWorkbenchBot fBot; | |
73 | ||
74 | /** The Log4j logger instance. */ | |
75 | private static final Logger fLogger = Logger.getRootLogger(); | |
76 | private SWTBotTable fTableBot; | |
77 | private static final int ROW = 8; | |
78 | private RGB fForeground; | |
79 | private RGB fBackground; | |
80 | ||
81 | /** | |
82 | * Test Class setup | |
83 | */ | |
84 | @BeforeClass | |
85 | public static void init() { | |
86 | SWTBotUtils.failIfUIThread(); | |
87 | ||
88 | /* set up test trace */ | |
89 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null); | |
90 | URI uri; | |
91 | try { | |
92 | uri = FileLocator.toFileURL(location).toURI(); | |
93 | fTestFile = new File(uri); | |
94 | } catch (URISyntaxException | IOException e) { | |
95 | fail(e.getMessage()); | |
96 | } | |
97 | ||
98 | assumeTrue(fTestFile.exists()); | |
99 | ||
100 | /* Set up for swtbot */ | |
101 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ | |
102 | SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US"; | |
103 | ||
104 | fLogger.removeAllAppenders(); | |
105 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); | |
106 | fBot = new SWTWorkbenchBot(); | |
107 | ||
108 | /* Close welcome view */ | |
109 | SWTBotUtils.closeView("Welcome", fBot); | |
110 | ||
111 | /* Switch perspectives */ | |
112 | SWTBotUtils.switchToTracingPerspective(); | |
113 | ||
114 | /* Finish waiting for eclipse to load */ | |
115 | SWTBotUtils.waitForJobs(); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Test class tear down method. | |
120 | */ | |
121 | @AfterClass | |
122 | public static void tearDown() { | |
123 | fLogger.removeAllAppenders(); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Bring up the table | |
128 | */ | |
129 | @Before | |
130 | public void setup() { | |
131 | SWTBotUtils.createProject(TRACE_PROJECT_NAME); | |
132 | ||
133 | // Open the actual trace | |
134 | SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE); | |
135 | SWTBotEditor editorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName()); | |
136 | ||
137 | fTableBot = editorBot.bot().table(); | |
138 | fBackground = fTableBot.backgroundColor().getRGB(); | |
139 | fForeground = fTableBot.foregroundColor().getRGB(); | |
140 | ||
141 | SWTBotUtils.maximizeTable(fTableBot); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Remove the project | |
146 | */ | |
147 | @After | |
148 | public void cleanup() { | |
149 | SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot); | |
150 | SWTBotUtils.waitForJobs(); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Test basic highlight | |
155 | */ | |
156 | @Test | |
157 | public void testHighlight() { | |
158 | final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, SOURCE_COLUMN); | |
159 | ||
160 | Multiset<RGB> colorBefore = ImageHelper.grabImage(cellBounds).getHistogram(); | |
161 | // Select source column and enter regex | |
162 | fTableBot.click(0, SOURCE_COLUMN); | |
163 | fBot.text().typeText("HostF\n", 100); | |
164 | // make sure selected row is not matching row | |
165 | fTableBot.select(ROW - 1); | |
166 | Multiset<RGB> colorAfter = ImageHelper.grabImage(cellBounds).getHistogram(); | |
167 | ||
168 | assertTrue(colorBefore.contains(fBackground)); | |
169 | assertTrue(colorBefore.contains(fForeground)); | |
170 | assertFalse(colorBefore.contains(YELLOW)); | |
171 | ||
172 | assertTrue(colorAfter.contains(fBackground)); | |
173 | assertTrue(colorAfter.contains(fForeground)); | |
174 | assertTrue(colorAfter.contains(YELLOW)); | |
3dadb8ca | 175 | |
328e5fe4 | 176 | /* |
3dadb8ca | 177 | * Check that some background became yellow. |
328e5fe4 | 178 | */ |
3dadb8ca PT |
179 | assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground)); |
180 | assertTrue(colorAfter.count(YELLOW) > colorBefore.count(YELLOW)); | |
328e5fe4 MK |
181 | } |
182 | ||
183 | /** | |
184 | * Test highlighting multiple elements in a message | |
185 | */ | |
186 | @Test | |
187 | public void testMultiHighlightMessage() { | |
188 | final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, MESSAGE_COLUMN); | |
189 | ||
190 | ImageHelper before = ImageHelper.grabImage(cellBounds); | |
191 | // enter regex in message column | |
192 | fTableBot.click(0, MESSAGE_COLUMN); | |
193 | fBot.text().typeText("e\n", 100); | |
194 | // make sure matching item is not selected | |
195 | fTableBot.select(ROW - 1); | |
196 | ImageHelper after = ImageHelper.grabImage(cellBounds); | |
197 | ||
198 | Multiset<RGB> colorBefore = before.getHistogram(); | |
199 | Multiset<RGB> colorAfter = after.getHistogram(); | |
200 | ||
201 | assertTrue(colorBefore.contains(fBackground)); | |
202 | assertTrue(colorBefore.contains(fForeground)); | |
203 | assertFalse(colorBefore.contains(YELLOW)); | |
204 | ||
205 | assertTrue(colorAfter.contains(fBackground)); | |
206 | assertTrue(colorAfter.contains(fForeground)); | |
207 | assertTrue(colorAfter.contains(YELLOW)); | |
208 | ||
209 | int start = -1; | |
210 | int end; | |
211 | List<Point> intervals = new ArrayList<>(); | |
212 | List<RGB> pixelRow = after.getPixelRow(2); | |
213 | for (int i = 1; i < pixelRow.size(); i++) { | |
214 | RGB prevPixel = pixelRow.get(i - 1); | |
215 | RGB pixel = pixelRow.get(i); | |
216 | if (prevPixel.equals(fBackground) && pixel.equals(YELLOW)) { | |
217 | start = i; | |
218 | } | |
219 | if (prevPixel.equals(YELLOW) && pixel.equals(fBackground)) { | |
220 | end = i; | |
221 | if (start == -1) { | |
222 | fail(); | |
223 | } | |
224 | intervals.add(new Point(start, end)); | |
225 | } | |
226 | } | |
227 | assertEquals(2, intervals.size()); | |
228 | } | |
229 | ||
230 | /** | |
231 | * Switch to filter and back | |
232 | */ | |
233 | @Test | |
234 | public void testSwitchToFilter() { | |
235 | final Rectangle cellBounds = SWTBotUtils.getCellBounds(fTableBot.widget, ROW, TIMESTAMP_COLUMN); | |
236 | ImageHelper before = ImageHelper.grabImage(cellBounds); | |
237 | // enter regex in message column | |
238 | fTableBot.click(0, TIMESTAMP_COLUMN); | |
239 | fBot.text().typeText("00\n", 100); | |
240 | // make sure matching column is not selected | |
241 | fTableBot.select(ROW - 1); | |
242 | ImageHelper after = ImageHelper.grabImage(cellBounds); | |
243 | // toggle filter mode | |
244 | fTableBot.click(0, 0); | |
245 | ImageHelper afterFilter = ImageHelper.grabImage(cellBounds); | |
328e5fe4 MK |
246 | |
247 | List<RGB> beforeLine = before.getPixelRow(2); | |
248 | List<RGB> afterLine = after.getPixelRow(2); | |
249 | List<RGB> afterFilterLine = afterFilter.getPixelRow(2); | |
250 | ||
251 | assertEquals(beforeLine.size(), afterLine.size()); | |
252 | assertEquals(beforeLine.size(), afterFilterLine.size()); | |
253 | for (int i = 0; i < beforeLine.size(); i++) { | |
254 | RGB afterFilterPixel = afterFilterLine.get(i); | |
255 | RGB beforePixel = beforeLine.get(i); | |
256 | RGB afterPixel = afterLine.get(i); | |
257 | ||
258 | assertEquals(beforePixel, afterFilterPixel); | |
259 | if (!afterPixel.equals(YELLOW)) { | |
260 | assertEquals(beforePixel, afterPixel); | |
261 | } else { | |
262 | assertNotEquals(YELLOW, beforePixel); | |
263 | } | |
264 | ||
265 | } | |
266 | assertEquals(beforeLine, afterFilterLine); | |
267 | assertNotEquals(afterLine, beforeLine); | |
268 | } | |
269 | } |