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
.core
.runtime
.preferences
.IEclipsePreferences
;
24 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
25 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
26 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
27 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
28 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
29 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCombo
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTableItem
;
33 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotText
;
34 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
35 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimePreferencesConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimePreferences
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestampFormat
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
.FilterView
;
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 import org
.junit
.runner
.RunWith
;
53 * Test for filter views in trace compass
55 @RunWith(SWTBotJunit4ClassRunner
.class)
56 public class FilterViewerTest
{
59 private static final String COMPARE
= "COMPARE";
60 private static final String CONTAINS
= "CONTAINS";
61 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
62 private static final String TRACETYPE
= "Test trace : XML Trace Stub";
63 private static final String AND
= "AND";
64 private static final String WITH_TRACETYPE
= "WITH TRACETYPE " + TRACETYPE
;
65 private static final String FILTER_TEST
= "FILTER ";
66 private static final String TIMESTAMP
= "Timestamp";
67 private static final String CONTENTS
= "Contents";
69 private static final String TRACE_START
= "<trace>";
70 private static final String EVENT_BEGIN
= "<event timestamp=\"";
71 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
72 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
73 private static final String TRACE_END
= "</trace>";
75 private static final String PROJECT_NAME
= "TestForFiltering";
77 /** The Log4j logger instance. */
78 private static final Logger fLogger
= Logger
.getRootLogger();
79 private static final String OR
= "OR";
80 private static SWTWorkbenchBot fBot
;
82 private static String
makeEvent(int ts
, int val
) {
83 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
86 private static File fFileLocation
;
89 * Initialization, creates a temp trace
95 public static void init() throws IOException
{
96 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
97 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, "GMT-05:00");
98 TmfTimestampFormat
.updateDefaultFormats();
100 SWTBotUtils
.initialize();
101 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
102 /* set up for swtbot */
103 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
104 fLogger
.removeAllAppenders();
105 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
106 fBot
= new SWTWorkbenchBot();
108 SWTBotUtils
.closeView("welcome", fBot
);
110 SWTBotUtils
.switchToTracingPerspective();
111 /* finish waiting for eclipse to load */
112 WaitUtils
.waitForJobs();
113 fFileLocation
= File
.createTempFile("sample", ".xml");
114 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
115 braf
.writeBytes(TRACE_START
);
116 for (int i
= 0; i
< 100; i
++) {
117 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
119 braf
.writeBytes(TRACE_END
);
124 * Open a trace in an editor
127 public void beforeTest() {
128 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
129 SWTBotUtils
.createProject(PROJECT_NAME
);
130 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(bot
, PROJECT_NAME
);
131 assertNotNull(treeItem
);
132 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
133 SWTBotUtils
.openView(FilterView
.ID
);
140 public static void cleanUp() {
141 fFileLocation
.delete();
142 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
143 fLogger
.removeAllAppenders();
144 SWTBotUtils
.closeViewById(FilterView
.ID
, fBot
);
146 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
147 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, TmfTimePreferences
.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants
.TIME_ZONE
));
148 TmfTimestampFormat
.updateDefaultFormats();
155 public void tearDown() {
156 fBot
.closeAllEditors();
160 * Return all timestamps ending with 100... for reasons
163 public void testTimestampFilter() {
164 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
166 SWTBot filterBot
= viewBot
.bot();
167 SWTBotTree treeBot
= filterBot
.tree();
169 viewBot
.toolbarButton("Add new filter").click();
170 treeBot
.getTreeItem("FILTER <name>").select();
171 SWTBotText textBot
= filterBot
.text();
173 String filterName
= "timestamp";
174 textBot
.setText(filterName
);
175 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
176 filterNodeBot
.click();
177 filterNodeBot
.contextMenu("TRACETYPE").click();
178 filterNodeBot
.expand();
179 SWTBotCombo comboBot
= filterBot
.comboBox();
180 comboBot
.setSelection(TRACETYPE
);
181 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
183 // --------------------------------------------------------------------
185 // --------------------------------------------------------------------
187 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(AND
).click();
189 // --------------------------------------------------------------------
190 // add CONTAINS "100"
191 // --------------------------------------------------------------------
193 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).contextMenu(CONTAINS
).click();
194 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).expand();
195 comboBot
= filterBot
.comboBox(1); // aspect
196 comboBot
.setSelection(TIMESTAMP
);
197 textBot
= filterBot
.text();
199 textBot
.setText("100");
200 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).getNode("Timestamp CONTAINS \"100\"").select();
201 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).select();
203 viewBot
.toolbarButton("Save filters").click();
205 String ret
= applyFilter(fBot
, filterName
);
206 assertEquals("10/100", ret
);
210 * Return all timestamps ending with 100... for reasons
213 public void testTimestampEqualsOr() {
214 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
216 SWTBot filterBot
= viewBot
.bot();
217 SWTBotTree treeBot
= filterBot
.tree();
219 viewBot
.toolbarButton("Add new filter").click();
220 treeBot
.getTreeItem("FILTER <name>").select();
221 SWTBotText textBot
= filterBot
.text();
223 String filterName
= "matchAndEquals";
224 textBot
.setText(filterName
);
225 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
226 filterNodeBot
.click();
227 filterNodeBot
.contextMenu("TRACETYPE").click();
228 filterNodeBot
.expand();
229 SWTBotCombo comboBot
= filterBot
.comboBox();
230 comboBot
.setSelection(TRACETYPE
);
231 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
233 // --------------------------------------------------------------------
235 // --------------------------------------------------------------------
237 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(OR
).click();
239 // --------------------------------------------------------------------
240 // add EQUALS "19...300"
241 // --------------------------------------------------------------------
243 SWTBotTreeItem orNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(OR
);
244 orNode
.contextMenu("EQUALS").click();
246 orNode
.getNode(0).select();
247 comboBot
= filterBot
.comboBox(1); // aspect
248 comboBot
.setSelection(TIMESTAMP
);
249 textBot
= filterBot
.text();
251 textBot
.setText("19:00:00.000 000 300");
253 // --------------------------------------------------------------------
255 // --------------------------------------------------------------------
256 orNode
.contextMenu("MATCHES").click();
258 orNode
.getNode(1).select();
259 comboBot
= filterBot
.comboBox(1); // aspect
260 comboBot
.setSelection(CONTENTS
);
261 textBot
= filterBot
.text(0); // field
263 textBot
.setText("field");
264 textBot
= filterBot
.text(1); // value
266 textBot
.setText("1");
268 viewBot
.toolbarButton("Save filters").click();
270 String ret
= applyFilter(fBot
, filterName
);
271 assertEquals("26/100", ret
);
272 // filterNodeBot.contextMenu().menu("Delete").click();
276 * test compare field >= 2
279 public void testField01() {
280 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
282 SWTBot filterBot
= viewBot
.bot();
283 SWTBotTree treeBot
= filterBot
.tree();
285 viewBot
.toolbarButton("Add new filter").click();
286 treeBot
.getTreeItem("FILTER <name>").select();
287 SWTBotText textBot
= filterBot
.text();
289 String filterName
= "field";
290 textBot
.setText(filterName
);
291 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
292 filterNodeBot
.click();
293 filterNodeBot
.contextMenu("TRACETYPE").click();
294 filterNodeBot
.expand();
295 SWTBotCombo comboBot
= filterBot
.comboBox();
296 comboBot
.setSelection(TRACETYPE
);
297 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
299 // --------------------------------------------------------------------
301 // --------------------------------------------------------------------
303 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(COMPARE
).click();
304 SWTBotTreeItem contentNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode("<select aspect> " + "=" + " <value>");
305 contentNode
.expand();
306 comboBot
= filterBot
.comboBox(1); // aspect
307 comboBot
.setSelection(CONTENTS
);
308 textBot
= filterBot
.text(0); // field
310 textBot
.setText(filterName
);
312 textBot
= filterBot
.text(1); // value
314 textBot
.setText("1.5");
315 filterBot
.radio(">").click();
317 // --------------------------------------------------------------------
319 // --------------------------------------------------------------------
320 viewBot
.toolbarButton("Save filters").click();
322 String ret
= applyFilter(fBot
, filterName
);
323 // filterNodeBot.contextMenu().menu("Delete").click();
324 assertEquals("50/100", ret
);
327 private static String
applyFilter(SWTWorkbenchBot bot
, final String filterName
) {
328 WaitUtils
.waitForJobs();
329 final SWTBotTable eventsTable
= SWTBotUtils
.activeEventsEditor(bot
).bot().table();
330 SWTBotTableItem tableItem
= eventsTable
.getTableItem(2);
331 tableItem
.contextMenu(filterName
).click();
332 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(eventsTable
, "/100", 1, 1));
333 return eventsTable
.cell(1, 1);