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
.utils
.SWTBotPreferences
;
29 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCombo
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTableItem
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotText
;
33 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
34 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimePreferencesConstants
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimePreferences
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestampFormat
;
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
.junit
.After
;
44 import org
.junit
.AfterClass
;
45 import org
.junit
.Before
;
46 import org
.junit
.BeforeClass
;
47 import org
.junit
.Test
;
50 * Test for filter views in trace compass
52 public class FilterViewerTest
{
55 private static final String COMPARE
= "COMPARE";
56 private static final String CONTAINS
= "CONTAINS";
57 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
58 private static final String TRACETYPE
= "Test trace : XML Trace Stub";
59 private static final String AND
= "AND";
60 private static final String WITH_TRACETYPE
= "WITH TRACETYPE " + TRACETYPE
;
61 private static final String FILTER_TEST
= "FILTER ";
62 private static final String TIMESTAMP
= "Timestamp";
63 private static final String CONTENTS
= "Contents";
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>";
71 private static final String PROJECT_NAME
= "TestForFiltering";
73 /** The Log4j logger instance. */
74 private static final Logger fLogger
= Logger
.getRootLogger();
75 private static final String OR
= "OR";
76 private static SWTWorkbenchBot fBot
;
78 private static String
makeEvent(int ts
, int val
) {
79 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
82 private static File fFileLocation
;
85 * Initialization, creates a temp trace
91 public static void init() throws IOException
{
92 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
93 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, "GMT-05:00");
94 TmfTimestampFormat
.updateDefaultFormats();
96 SWTBotUtils
.initialize();
97 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
98 /* set up for swtbot */
99 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
100 fLogger
.removeAllAppenders();
101 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
102 fBot
= new SWTWorkbenchBot();
104 SWTBotUtils
.closeView("welcome", fBot
);
106 SWTBotUtils
.switchToTracingPerspective();
107 /* finish waiting for eclipse to load */
108 SWTBotUtils
.waitForJobs();
109 fFileLocation
= File
.createTempFile("sample", ".xml");
110 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
111 braf
.writeBytes(TRACE_START
);
112 for (int i
= 0; i
< 100; i
++) {
113 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
115 braf
.writeBytes(TRACE_END
);
120 * Open a trace in an editor
123 public void beforeTest() {
124 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
125 SWTBotUtils
.createProject(PROJECT_NAME
);
126 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(bot
, PROJECT_NAME
);
127 assertNotNull(treeItem
);
128 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
129 SWTBotUtils
.openView(FilterView
.ID
);
136 public static void cleanUp() {
137 fFileLocation
.delete();
138 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
139 fLogger
.removeAllAppenders();
140 SWTBotUtils
.closeViewById(FilterView
.ID
, fBot
);
142 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
143 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, TmfTimePreferences
.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants
.TIME_ZONE
));
144 TmfTimestampFormat
.updateDefaultFormats();
151 public void tearDown() {
152 fBot
.closeAllEditors();
156 * Return all timestamps ending with 100... for reasons
159 public void testTimestampFilter() {
160 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
162 SWTBot filterBot
= viewBot
.bot();
163 SWTBotTree treeBot
= filterBot
.tree();
165 viewBot
.toolbarButton("Add new filter").click();
166 treeBot
.getTreeItem("FILTER <name>").select();
167 SWTBotText textBot
= filterBot
.text();
169 String filterName
= "timestamp";
170 textBot
.setText(filterName
);
171 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
172 filterNodeBot
.click();
173 filterNodeBot
.contextMenu("TRACETYPE").click();
174 filterNodeBot
.expand();
175 SWTBotCombo comboBot
= filterBot
.comboBox();
176 comboBot
.setSelection(TRACETYPE
);
177 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
179 // --------------------------------------------------------------------
181 // --------------------------------------------------------------------
183 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(AND
).click();
185 // --------------------------------------------------------------------
186 // add CONTAINS "100"
187 // --------------------------------------------------------------------
189 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).contextMenu(CONTAINS
).click();
190 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).expand();
191 comboBot
= filterBot
.comboBox(1); // aspect
192 comboBot
.setSelection(TIMESTAMP
);
193 textBot
= filterBot
.text();
195 textBot
.setText("100");
196 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).getNode("Timestamp CONTAINS \"100\"").select();
197 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).select();
199 viewBot
.toolbarButton("Save filters").click();
201 String ret
= applyFilter(fBot
, filterName
);
202 assertEquals("10/100", ret
);
206 * Return all timestamps ending with 100... for reasons
209 public void testTimestampEqualsOr() {
210 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
212 SWTBot filterBot
= viewBot
.bot();
213 SWTBotTree treeBot
= filterBot
.tree();
215 viewBot
.toolbarButton("Add new filter").click();
216 treeBot
.getTreeItem("FILTER <name>").select();
217 SWTBotText textBot
= filterBot
.text();
219 String filterName
= "matchAndEquals";
220 textBot
.setText(filterName
);
221 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
222 filterNodeBot
.click();
223 filterNodeBot
.contextMenu("TRACETYPE").click();
224 filterNodeBot
.expand();
225 SWTBotCombo comboBot
= filterBot
.comboBox();
226 comboBot
.setSelection(TRACETYPE
);
227 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
229 // --------------------------------------------------------------------
231 // --------------------------------------------------------------------
233 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(OR
).click();
235 // --------------------------------------------------------------------
236 // add EQUALS "19...300"
237 // --------------------------------------------------------------------
239 SWTBotTreeItem orNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(OR
);
240 orNode
.contextMenu("EQUALS").click();
242 orNode
.getNode(0).select();
243 comboBot
= filterBot
.comboBox(1); // aspect
244 comboBot
.setSelection(TIMESTAMP
);
245 textBot
= filterBot
.text();
247 textBot
.setText("19:00:00.000 000 300");
249 // --------------------------------------------------------------------
251 // --------------------------------------------------------------------
252 orNode
.contextMenu("MATCHES").click();
254 orNode
.getNode(1).select();
255 comboBot
= filterBot
.comboBox(1); // aspect
256 comboBot
.setSelection(CONTENTS
);
257 textBot
= filterBot
.text(0); // field
259 textBot
.setText("field");
260 textBot
= filterBot
.text(1); // value
262 textBot
.setText("1");
264 viewBot
.toolbarButton("Save filters").click();
266 String ret
= applyFilter(fBot
, filterName
);
267 assertEquals("26/100", ret
);
268 // filterNodeBot.contextMenu().menu("Delete").click();
272 * test compare field >= 2
275 public void testField01() {
276 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
278 SWTBot filterBot
= viewBot
.bot();
279 SWTBotTree treeBot
= filterBot
.tree();
281 viewBot
.toolbarButton("Add new filter").click();
282 treeBot
.getTreeItem("FILTER <name>").select();
283 SWTBotText textBot
= filterBot
.text();
285 String filterName
= "field";
286 textBot
.setText(filterName
);
287 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
288 filterNodeBot
.click();
289 filterNodeBot
.contextMenu("TRACETYPE").click();
290 filterNodeBot
.expand();
291 SWTBotCombo comboBot
= filterBot
.comboBox();
292 comboBot
.setSelection(TRACETYPE
);
293 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
295 // --------------------------------------------------------------------
297 // --------------------------------------------------------------------
299 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(COMPARE
).click();
300 SWTBotTreeItem contentNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode("<select aspect> " + "=" + " <value>");
301 contentNode
.expand();
302 comboBot
= filterBot
.comboBox(1); // aspect
303 comboBot
.setSelection(CONTENTS
);
304 textBot
= filterBot
.text(0); // field
306 textBot
.setText(filterName
);
308 textBot
= filterBot
.text(1); // value
310 textBot
.setText("1.5");
311 filterBot
.radio(">").click();
313 // --------------------------------------------------------------------
315 // --------------------------------------------------------------------
316 viewBot
.toolbarButton("Save filters").click();
318 String ret
= applyFilter(fBot
, filterName
);
319 // filterNodeBot.contextMenu().menu("Delete").click();
320 assertEquals("50/100", ret
);
323 private static String
applyFilter(SWTWorkbenchBot bot
, final String filterName
) {
324 SWTBotUtils
.waitForJobs();
325 final SWTBotTable eventsTable
= SWTBotUtils
.activeEventsEditor(bot
).bot().table();
326 SWTBotTableItem tableItem
= eventsTable
.getTableItem(2);
327 tableItem
.contextMenu(filterName
).click();
328 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(eventsTable
, "/100", 1, 1));
329 return eventsTable
.cell(1, 1);