tmf.ui.swtbot: add filterview tests
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / FilterViewerTest.java
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 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.io.File;
18 import java.io.IOException;
19
20 import org.apache.log4j.ConsoleAppender;
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.SimpleLayout;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Listener;
26 import org.eclipse.swt.widgets.MenuItem;
27 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29 import org.eclipse.swtbot.swt.finder.SWTBot;
30 import org.eclipse.swtbot.swt.finder.finders.ContextMenuFinder;
31 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
32 import org.eclipse.swtbot.swt.finder.results.VoidResult;
33 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
34 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
35 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
36 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
37 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
38 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
39 import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
40 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
41 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
42 import org.eclipse.tracecompass.tmf.ui.views.filter.FilterView;
43 import org.hamcrest.BaseMatcher;
44 import org.hamcrest.Description;
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50
51 /**
52 * Test for filter views in trace compass
53 */
54 public class FilterViewerTest {
55
56
57 private static final String COMPARE = "COMPARE";
58 private static final String CONTAINS = "CONTAINS";
59 private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
60 private static final String TRACETYPE = "Test trace : XML Trace Stub";
61 private static final String AND = "AND";
62 private static final String WITH_TRACETYPE = "WITH TRACETYPE " + TRACETYPE;
63 private static final String FILTER_TEST = "FILTER ";
64
65 private static final String TRACE_START = "<trace>";
66 private static final String EVENT_BEGIN = "<event timestamp=\"";
67 private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\"";
68 private static final String EVENT_END = "\" type=\"int\" />" + "</event>";
69 private static final String TRACE_END = "</trace>";
70
71
72 private static final String PROJECT_NAME = "TestForFiltering";
73
74 /** The Log4j logger instance. */
75 private static final Logger fLogger = Logger.getRootLogger();
76 private static final String OR = "OR";
77 private static SWTWorkbenchBot fBot;
78
79 private static String makeEvent(int ts, int val) {
80 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
81 }
82
83 private static File fFileLocation;
84
85 /**
86 * Initialization, creates a temp trace
87 *
88 * @throws IOException
89 * should not happen
90 */
91 @BeforeClass
92 public static void init() throws IOException {
93 SWTBotUtils.failIfUIThread();
94 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
95 /* set up for swtbot */
96 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
97 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
98 fBot = new SWTWorkbenchBot();
99
100 SWTBotUtils.closeView("welcome", fBot);
101
102 SWTBotUtils.switchToTracingPerspective();
103 /* finish waiting for eclipse to load */
104 SWTBotUtils.waitForJobs();
105 fFileLocation = File.createTempFile("sample", ".xml");
106 try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fFileLocation, "rw")) {
107 braf.writeBytes(TRACE_START);
108 for (int i = 0; i < 100; i++) {
109 braf.writeBytes(makeEvent(i * 100, i % 4));
110 }
111 braf.writeBytes(TRACE_END);
112 }
113 }
114
115 /**
116 * Open a trace in an editor
117 */
118 @Before
119 public void beforeTest() {
120 SWTWorkbenchBot bot = new SWTWorkbenchBot();
121 SWTBotUtils.createProject(PROJECT_NAME);
122 SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(bot, PROJECT_NAME);
123 assertNotNull(treeItem);
124 SWTBotUtils.openTrace(PROJECT_NAME, fFileLocation.getAbsolutePath(), XMLSTUB_ID);
125 SWTBotUtils.openView(FilterView.ID);
126 }
127
128 /**
129 * Delete the file
130 */
131 @AfterClass
132 public static void cleanUp() {
133 fFileLocation.delete();
134 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
135 }
136
137 /**
138 * Close the editor
139 */
140 @After
141 public void tearDown() {
142 fBot.closeAllEditors();
143 }
144
145 /**
146 * Return all timestamps ending with 100... for reasons
147 */
148 @Test
149 public void testTimestampFilter() {
150 SWTBotView viewBot = fBot.viewById(FilterView.ID);
151 viewBot.setFocus();
152 SWTBot filterBot = viewBot.bot();
153 SWTBotTree treeBot = filterBot.tree();
154
155 viewBot.toolbarButton("Add new filter").click();
156 treeBot.getTreeItem("FILTER <name>").select();
157 SWTBotText textBot = filterBot.text();
158 textBot.setFocus();
159 String filterName = "timestamp";
160 textBot.setText(filterName);
161 SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName);
162 filterNodeBot.click();
163 filterNodeBot.contextMenu("TRACETYPE").click();
164 filterNodeBot.expand();
165 SWTBotCombo comboBot = filterBot.comboBox();
166 comboBot.setSelection(TRACETYPE);
167 filterNodeBot.getNode(WITH_TRACETYPE).expand();
168
169 // --------------------------------------------------------------------
170 // add AND
171 // --------------------------------------------------------------------
172
173 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(AND).click();
174
175 // --------------------------------------------------------------------
176 // add CONTAINS "100"
177 // --------------------------------------------------------------------
178
179 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).contextMenu(CONTAINS).click();
180 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).expand();
181 comboBot = filterBot.comboBox();
182 comboBot.setSelection(comboBot.itemCount() - 3);
183 textBot = filterBot.text();
184 textBot.setFocus();
185 textBot.setText("100");
186 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).getNode("Timestamp CONTAINS \"100\"").select();
187 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).select();
188
189 viewBot.toolbarButton("Save filters").click();
190
191 String ret = applyFilter(fBot, filterName);
192 assertEquals("10/100", ret);
193 }
194
195 /**
196 * Return all timestamps ending with 100... for reasons
197 */
198 @Test
199 public void testTimestampEqualsOr() {
200 SWTBotView viewBot = fBot.viewById(FilterView.ID);
201 viewBot.setFocus();
202 SWTBot filterBot = viewBot.bot();
203 SWTBotTree treeBot = filterBot.tree();
204
205 viewBot.toolbarButton("Add new filter").click();
206 treeBot.getTreeItem("FILTER <name>").select();
207 SWTBotText textBot = filterBot.text();
208 textBot.setFocus();
209 String filterName = "matchAndEquals";
210 textBot.setText(filterName);
211 SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName);
212 filterNodeBot.click();
213 filterNodeBot.contextMenu("TRACETYPE").click();
214 filterNodeBot.expand();
215 SWTBotCombo comboBot = filterBot.comboBox();
216 comboBot.setSelection(TRACETYPE);
217 filterNodeBot.getNode(WITH_TRACETYPE).expand();
218
219 // --------------------------------------------------------------------
220 // add OR
221 // --------------------------------------------------------------------
222
223 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(OR).click();
224
225 // --------------------------------------------------------------------
226 // add EQUALS "19...300"
227 // --------------------------------------------------------------------
228
229 SWTBotTreeItem orNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode(OR);
230 orNode.contextMenu("EQUALS").click();
231 orNode.expand();
232 orNode.getNode(0).select();
233 comboBot = filterBot.comboBox();
234 //timestamp
235 comboBot.setSelection(comboBot.itemCount() - 3);
236 textBot = filterBot.text();
237 textBot.setFocus();
238 textBot.setText("19:00:00.000 000 300");
239
240 // --------------------------------------------------------------------
241 // add MATCHES "1"
242 // --------------------------------------------------------------------
243 orNode.contextMenu("MATCHES").click();
244 orNode.expand();
245 orNode.getNode(1).select();
246 // contents
247 comboBot = filterBot.comboBox();
248 comboBot.setSelection(comboBot.itemCount() - 1);
249 textBot = filterBot.text(0);
250 textBot.setFocus();
251 textBot.setText("field");
252 textBot = filterBot.text(1);
253 textBot.setFocus();
254 textBot.setText("1");
255
256 viewBot.toolbarButton("Save filters").click();
257
258 String ret = applyFilter(fBot, filterName);
259 assertEquals("26/100", ret);
260 }
261
262 /**
263 * test compare field >= 2
264 */
265 @Test
266 public void testField01() {
267 SWTBotView viewBot = fBot.viewById(FilterView.ID);
268 viewBot.setFocus();
269 SWTBot filterBot = viewBot.bot();
270 SWTBotTree treeBot = filterBot.tree();
271
272 viewBot.toolbarButton("Add new filter").click();
273 treeBot.getTreeItem("FILTER <name>").select();
274 SWTBotText textBot = filterBot.text();
275 textBot.setFocus();
276 String filterName = "field";
277 textBot.setText(filterName);
278 SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName);
279 filterNodeBot.click();
280 filterNodeBot.contextMenu("TRACETYPE").click();
281 filterNodeBot.expand();
282 SWTBotCombo comboBot = filterBot.comboBox();
283 comboBot.setSelection(TRACETYPE);
284 filterNodeBot.getNode(WITH_TRACETYPE).expand();
285
286 // --------------------------------------------------------------------
287 // add Compare > 1.5
288 // --------------------------------------------------------------------
289
290 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(COMPARE).click();
291 SWTBotTreeItem contentNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode("<select aspect> " + "=" + " <value>");
292 contentNode.expand();
293 comboBot = filterBot.comboBox();
294 comboBot.setSelection(comboBot.itemCount() - 1);
295 textBot = filterBot.text(0);
296 textBot.setFocus();
297 textBot.setText(filterName);
298
299 textBot = filterBot.text(1);
300 textBot.setFocus();
301 textBot.setText("1.5");
302 filterBot.radio(2).click();
303
304 // --------------------------------------------------------------------
305 // apply
306 // --------------------------------------------------------------------
307 viewBot.toolbarButton("Save filters").click();
308
309 String ret = applyFilter(fBot, filterName);
310 assertEquals("50/100", ret);
311 }
312
313 private static String applyFilter(SWTWorkbenchBot bot, final String filterName) {
314 SWTBotUtils.waitForJobs();
315 final SWTBotTable eventsEditor = bot.activeEditor().bot().table();
316 eventsEditor.select(2);
317 UIThreadRunnable.syncExec(new VoidResult() {
318
319 @Override
320 public void run() {
321 ContextMenuFinder cmf = new ContextMenuFinder(eventsEditor.widget);
322 ContextMenuListeners matcher = new ContextMenuListeners(filterName);
323 cmf.findMenus(matcher);
324 }
325 });
326 fBot.waitUntil(ConditionHelpers.isTableCellFilled(eventsEditor, "/100", 1, 1));
327 return eventsEditor.cell(1, 1);
328 }
329
330 /**
331 * FIXME: This is a big hack until SWTBot supports context menus better (bug 458975)
332 */
333 private static final class ContextMenuListeners extends BaseMatcher<MenuItem> {
334 private final String filterName;
335
336 private boolean found = false;
337
338 private ContextMenuListeners(String filterName) {
339 this.filterName = filterName;
340 }
341
342 @Override
343 public void describeTo(Description description) {
344 }
345
346 @Override
347 public boolean matches(Object item) {
348 if (item instanceof MenuItem) {
349 MenuItem menuItem = (MenuItem) item;
350 if (menuItem.getText().equals(filterName)) {
351 for (Listener listener : menuItem.getListeners(SWT.Selection)) {
352 if (!found) {
353 Event event = new Event();
354 event.type = SWT.Selection;
355 event.widget = menuItem;
356 event.button = 1;
357 listener.handleEvent(event);
358 found = true;
359 }
360 return true;
361 }
362 }
363 }
364 return false;
365 }
366 }
367 }
This page took 0.064535 seconds and 5 git commands to generate.