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
.views
.filter
.FilterView
;
44 import org
.junit
.After
;
45 import org
.junit
.AfterClass
;
46 import org
.junit
.Before
;
47 import org
.junit
.BeforeClass
;
48 import org
.junit
.Test
;
49 import org
.junit
.runner
.RunWith
;
52 * Test for filter views in trace compass
54 @RunWith(SWTBotJunit4ClassRunner
.class)
55 public class FilterViewerTest
{
58 private static final String COMPARE
= "COMPARE";
59 private static final String CONTAINS
= "CONTAINS";
60 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
61 private static final String TRACETYPE
= "Test trace : XML Trace Stub";
62 private static final String AND
= "AND";
63 private static final String WITH_TRACETYPE
= "WITH TRACETYPE " + TRACETYPE
;
64 private static final String FILTER_TEST
= "FILTER ";
65 private static final String TIMESTAMP
= "Timestamp";
66 private static final String CONTENTS
= "Contents";
68 private static final String TRACE_START
= "<trace>";
69 private static final String EVENT_BEGIN
= "<event timestamp=\"";
70 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
71 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
72 private static final String TRACE_END
= "</trace>";
74 private static final String PROJECT_NAME
= "TestForFiltering";
76 /** The Log4j logger instance. */
77 private static final Logger fLogger
= Logger
.getRootLogger();
78 private static final String OR
= "OR";
79 private static SWTWorkbenchBot fBot
;
81 private static String
makeEvent(int ts
, int val
) {
82 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
85 private static File fFileLocation
;
88 * Initialization, creates a temp trace
94 public static void init() throws IOException
{
95 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
96 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, "GMT-05:00");
97 TmfTimestampFormat
.updateDefaultFormats();
99 SWTBotUtils
.initialize();
100 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
101 /* set up for swtbot */
102 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
103 fLogger
.removeAllAppenders();
104 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
105 fBot
= new SWTWorkbenchBot();
107 SWTBotUtils
.closeView("welcome", fBot
);
109 SWTBotUtils
.switchToTracingPerspective();
110 /* finish waiting for eclipse to load */
111 SWTBotUtils
.waitForJobs();
112 fFileLocation
= File
.createTempFile("sample", ".xml");
113 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
114 braf
.writeBytes(TRACE_START
);
115 for (int i
= 0; i
< 100; i
++) {
116 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
118 braf
.writeBytes(TRACE_END
);
123 * Open a trace in an editor
126 public void beforeTest() {
127 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
128 SWTBotUtils
.createProject(PROJECT_NAME
);
129 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(bot
, PROJECT_NAME
);
130 assertNotNull(treeItem
);
131 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
132 SWTBotUtils
.openView(FilterView
.ID
);
139 public static void cleanUp() {
140 fFileLocation
.delete();
141 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
142 fLogger
.removeAllAppenders();
143 SWTBotUtils
.closeViewById(FilterView
.ID
, fBot
);
145 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
146 defaultPreferences
.put(ITmfTimePreferencesConstants
.TIME_ZONE
, TmfTimePreferences
.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants
.TIME_ZONE
));
147 TmfTimestampFormat
.updateDefaultFormats();
154 public void tearDown() {
155 fBot
.closeAllEditors();
159 * Return all timestamps ending with 100... for reasons
162 public void testTimestampFilter() {
163 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
165 SWTBot filterBot
= viewBot
.bot();
166 SWTBotTree treeBot
= filterBot
.tree();
168 viewBot
.toolbarButton("Add new filter").click();
169 treeBot
.getTreeItem("FILTER <name>").select();
170 SWTBotText textBot
= filterBot
.text();
172 String filterName
= "timestamp";
173 textBot
.setText(filterName
);
174 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
175 filterNodeBot
.click();
176 filterNodeBot
.contextMenu("TRACETYPE").click();
177 filterNodeBot
.expand();
178 SWTBotCombo comboBot
= filterBot
.comboBox();
179 comboBot
.setSelection(TRACETYPE
);
180 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
182 // --------------------------------------------------------------------
184 // --------------------------------------------------------------------
186 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(AND
).click();
188 // --------------------------------------------------------------------
189 // add CONTAINS "100"
190 // --------------------------------------------------------------------
192 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).contextMenu(CONTAINS
).click();
193 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).expand();
194 comboBot
= filterBot
.comboBox(1); // aspect
195 comboBot
.setSelection(TIMESTAMP
);
196 textBot
= filterBot
.text();
198 textBot
.setText("100");
199 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).getNode("Timestamp CONTAINS \"100\"").select();
200 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).select();
202 viewBot
.toolbarButton("Save filters").click();
204 String ret
= applyFilter(fBot
, filterName
);
205 assertEquals("10/100", ret
);
209 * Return all timestamps ending with 100... for reasons
212 public void testTimestampEqualsOr() {
213 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
215 SWTBot filterBot
= viewBot
.bot();
216 SWTBotTree treeBot
= filterBot
.tree();
218 viewBot
.toolbarButton("Add new filter").click();
219 treeBot
.getTreeItem("FILTER <name>").select();
220 SWTBotText textBot
= filterBot
.text();
222 String filterName
= "matchAndEquals";
223 textBot
.setText(filterName
);
224 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
225 filterNodeBot
.click();
226 filterNodeBot
.contextMenu("TRACETYPE").click();
227 filterNodeBot
.expand();
228 SWTBotCombo comboBot
= filterBot
.comboBox();
229 comboBot
.setSelection(TRACETYPE
);
230 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
232 // --------------------------------------------------------------------
234 // --------------------------------------------------------------------
236 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(OR
).click();
238 // --------------------------------------------------------------------
239 // add EQUALS "19...300"
240 // --------------------------------------------------------------------
242 SWTBotTreeItem orNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(OR
);
243 orNode
.contextMenu("EQUALS").click();
245 orNode
.getNode(0).select();
246 comboBot
= filterBot
.comboBox(1); // aspect
247 comboBot
.setSelection(TIMESTAMP
);
248 textBot
= filterBot
.text();
250 textBot
.setText("19:00:00.000 000 300");
252 // --------------------------------------------------------------------
254 // --------------------------------------------------------------------
255 orNode
.contextMenu("MATCHES").click();
257 orNode
.getNode(1).select();
258 comboBot
= filterBot
.comboBox(1); // aspect
259 comboBot
.setSelection(CONTENTS
);
260 textBot
= filterBot
.text(0); // field
262 textBot
.setText("field");
263 textBot
= filterBot
.text(1); // value
265 textBot
.setText("1");
267 viewBot
.toolbarButton("Save filters").click();
269 String ret
= applyFilter(fBot
, filterName
);
270 assertEquals("26/100", ret
);
271 // filterNodeBot.contextMenu().menu("Delete").click();
275 * test compare field >= 2
278 public void testField01() {
279 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
281 SWTBot filterBot
= viewBot
.bot();
282 SWTBotTree treeBot
= filterBot
.tree();
284 viewBot
.toolbarButton("Add new filter").click();
285 treeBot
.getTreeItem("FILTER <name>").select();
286 SWTBotText textBot
= filterBot
.text();
288 String filterName
= "field";
289 textBot
.setText(filterName
);
290 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
291 filterNodeBot
.click();
292 filterNodeBot
.contextMenu("TRACETYPE").click();
293 filterNodeBot
.expand();
294 SWTBotCombo comboBot
= filterBot
.comboBox();
295 comboBot
.setSelection(TRACETYPE
);
296 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
298 // --------------------------------------------------------------------
300 // --------------------------------------------------------------------
302 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(COMPARE
).click();
303 SWTBotTreeItem contentNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode("<select aspect> " + "=" + " <value>");
304 contentNode
.expand();
305 comboBot
= filterBot
.comboBox(1); // aspect
306 comboBot
.setSelection(CONTENTS
);
307 textBot
= filterBot
.text(0); // field
309 textBot
.setText(filterName
);
311 textBot
= filterBot
.text(1); // value
313 textBot
.setText("1.5");
314 filterBot
.radio(">").click();
316 // --------------------------------------------------------------------
318 // --------------------------------------------------------------------
319 viewBot
.toolbarButton("Save filters").click();
321 String ret
= applyFilter(fBot
, filterName
);
322 // filterNodeBot.contextMenu().menu("Delete").click();
323 assertEquals("50/100", ret
);
326 private static String
applyFilter(SWTWorkbenchBot bot
, final String filterName
) {
327 SWTBotUtils
.waitForJobs();
328 final SWTBotTable eventsTable
= SWTBotUtils
.activeEventsEditor(bot
).bot().table();
329 SWTBotTableItem tableItem
= eventsTable
.getTableItem(2);
330 tableItem
.contextMenu(filterName
).click();
331 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(eventsTable
, "/100", 1, 1));
332 return eventsTable
.cell(1, 1);