1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
18 import java
.io
.IOException
;
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
;
52 * Test for filter views in trace compass
54 public class FilterViewerTest
{
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 ";
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>";
72 private static final String PROJECT_NAME
= "TestForFiltering";
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
;
79 private static String
makeEvent(int ts
, int val
) {
80 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
83 private static File fFileLocation
;
86 * Initialization, creates a temp trace
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();
100 SWTBotUtils
.closeView("welcome", fBot
);
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));
111 braf
.writeBytes(TRACE_END
);
116 * Open a trace in an editor
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
);
132 public static void cleanUp() {
133 fFileLocation
.delete();
134 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
141 public void tearDown() {
142 fBot
.closeAllEditors();
146 * Return all timestamps ending with 100... for reasons
149 public void testTimestampFilter() {
150 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
152 SWTBot filterBot
= viewBot
.bot();
153 SWTBotTree treeBot
= filterBot
.tree();
155 viewBot
.toolbarButton("Add new filter").click();
156 treeBot
.getTreeItem("FILTER <name>").select();
157 SWTBotText textBot
= filterBot
.text();
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();
169 // --------------------------------------------------------------------
171 // --------------------------------------------------------------------
173 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(AND
).click();
175 // --------------------------------------------------------------------
176 // add CONTAINS "100"
177 // --------------------------------------------------------------------
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();
185 textBot
.setText("100");
186 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).getNode("Timestamp CONTAINS \"100\"").select();
187 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).select();
189 viewBot
.toolbarButton("Save filters").click();
191 String ret
= applyFilter(fBot
, filterName
);
192 assertEquals("10/100", ret
);
196 * Return all timestamps ending with 100... for reasons
199 public void testTimestampEqualsOr() {
200 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
202 SWTBot filterBot
= viewBot
.bot();
203 SWTBotTree treeBot
= filterBot
.tree();
205 viewBot
.toolbarButton("Add new filter").click();
206 treeBot
.getTreeItem("FILTER <name>").select();
207 SWTBotText textBot
= filterBot
.text();
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();
219 // --------------------------------------------------------------------
221 // --------------------------------------------------------------------
223 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(OR
).click();
225 // --------------------------------------------------------------------
226 // add EQUALS "19...300"
227 // --------------------------------------------------------------------
229 SWTBotTreeItem orNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(OR
);
230 orNode
.contextMenu("EQUALS").click();
232 orNode
.getNode(0).select();
233 comboBot
= filterBot
.comboBox();
235 comboBot
.setSelection(comboBot
.itemCount() - 3);
236 textBot
= filterBot
.text();
238 textBot
.setText("19:00:00.000 000 300");
240 // --------------------------------------------------------------------
242 // --------------------------------------------------------------------
243 orNode
.contextMenu("MATCHES").click();
245 orNode
.getNode(1).select();
247 comboBot
= filterBot
.comboBox();
248 comboBot
.setSelection(comboBot
.itemCount() - 1);
249 textBot
= filterBot
.text(0);
251 textBot
.setText("field");
252 textBot
= filterBot
.text(1);
254 textBot
.setText("1");
256 viewBot
.toolbarButton("Save filters").click();
258 String ret
= applyFilter(fBot
, filterName
);
259 assertEquals("26/100", ret
);
263 * test compare field >= 2
266 public void testField01() {
267 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
269 SWTBot filterBot
= viewBot
.bot();
270 SWTBotTree treeBot
= filterBot
.tree();
272 viewBot
.toolbarButton("Add new filter").click();
273 treeBot
.getTreeItem("FILTER <name>").select();
274 SWTBotText textBot
= filterBot
.text();
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();
286 // --------------------------------------------------------------------
288 // --------------------------------------------------------------------
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);
297 textBot
.setText(filterName
);
299 textBot
= filterBot
.text(1);
301 textBot
.setText("1.5");
302 filterBot
.radio(2).click();
304 // --------------------------------------------------------------------
306 // --------------------------------------------------------------------
307 viewBot
.toolbarButton("Save filters").click();
309 String ret
= applyFilter(fBot
, filterName
);
310 assertEquals("50/100", ret
);
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() {
321 ContextMenuFinder cmf
= new ContextMenuFinder(eventsEditor
.widget
);
322 ContextMenuListeners matcher
= new ContextMenuListeners(filterName
);
323 cmf
.findMenus(matcher
);
326 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(eventsEditor
, "/100", 1, 1));
327 return eventsEditor
.cell(1, 1);
331 * FIXME: This is a big hack until SWTBot supports context menus better (bug 458975)
333 private static final class ContextMenuListeners
extends BaseMatcher
<MenuItem
> {
334 private final String filterName
;
336 private boolean found
= false;
338 private ContextMenuListeners(String filterName
) {
339 this.filterName
= filterName
;
343 public void describeTo(Description description
) {
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
)) {
353 Event event
= new Event();
354 event
.type
= SWT
.Selection
;
355 event
.widget
= menuItem
;
357 listener
.handleEvent(event
);