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
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
24 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
25 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
26 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
27 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotCombo
;
28 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
29 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTableItem
;
30 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotText
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTree
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.ConditionHelpers
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
.FilterView
;
37 import org
.junit
.After
;
38 import org
.junit
.AfterClass
;
39 import org
.junit
.Before
;
40 import org
.junit
.BeforeClass
;
41 import org
.junit
.Test
;
44 * Test for filter views in trace compass
46 public class FilterViewerTest
{
49 private static final String COMPARE
= "COMPARE";
50 private static final String CONTAINS
= "CONTAINS";
51 private static final String XMLSTUB_ID
= "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
52 private static final String TRACETYPE
= "Test trace : XML Trace Stub";
53 private static final String AND
= "AND";
54 private static final String WITH_TRACETYPE
= "WITH TRACETYPE " + TRACETYPE
;
55 private static final String FILTER_TEST
= "FILTER ";
56 private static final String TIMESTAMP
= "Timestamp";
57 private static final String CONTENTS
= "Contents";
59 private static final String TRACE_START
= "<trace>";
60 private static final String EVENT_BEGIN
= "<event timestamp=\"";
61 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
62 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
63 private static final String TRACE_END
= "</trace>";
65 private static final String PROJECT_NAME
= "TestForFiltering";
67 /** The Log4j logger instance. */
68 private static final Logger fLogger
= Logger
.getRootLogger();
69 private static final String OR
= "OR";
70 private static SWTWorkbenchBot fBot
;
72 private static String
makeEvent(int ts
, int val
) {
73 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
76 private static File fFileLocation
;
79 * Initialization, creates a temp trace
85 public static void init() throws IOException
{
86 SWTBotUtils
.failIfUIThread();
87 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
88 /* set up for swtbot */
89 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
90 fLogger
.removeAllAppenders();
91 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
92 fBot
= new SWTWorkbenchBot();
94 SWTBotUtils
.closeView("welcome", fBot
);
96 SWTBotUtils
.switchToTracingPerspective();
97 /* finish waiting for eclipse to load */
98 SWTBotUtils
.waitForJobs();
99 fFileLocation
= File
.createTempFile("sample", ".xml");
100 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
101 braf
.writeBytes(TRACE_START
);
102 for (int i
= 0; i
< 100; i
++) {
103 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
105 braf
.writeBytes(TRACE_END
);
110 * Open a trace in an editor
113 public void beforeTest() {
114 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
115 SWTBotUtils
.createProject(PROJECT_NAME
);
116 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(bot
, PROJECT_NAME
);
117 assertNotNull(treeItem
);
118 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
119 SWTBotUtils
.openView(FilterView
.ID
);
126 public static void cleanUp() {
127 fFileLocation
.delete();
128 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
129 fLogger
.removeAllAppenders();
130 SWTBotUtils
.closeViewById(FilterView
.ID
, fBot
);
137 public void tearDown() {
138 fBot
.closeAllEditors();
142 * Return all timestamps ending with 100... for reasons
145 public void testTimestampFilter() {
146 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
148 SWTBot filterBot
= viewBot
.bot();
149 SWTBotTree treeBot
= filterBot
.tree();
151 viewBot
.toolbarButton("Add new filter").click();
152 treeBot
.getTreeItem("FILTER <name>").select();
153 SWTBotText textBot
= filterBot
.text();
155 String filterName
= "timestamp";
156 textBot
.setText(filterName
);
157 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
158 filterNodeBot
.click();
159 filterNodeBot
.contextMenu("TRACETYPE").click();
160 filterNodeBot
.expand();
161 SWTBotCombo comboBot
= filterBot
.comboBox();
162 comboBot
.setSelection(TRACETYPE
);
163 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
165 // --------------------------------------------------------------------
167 // --------------------------------------------------------------------
169 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(AND
).click();
171 // --------------------------------------------------------------------
172 // add CONTAINS "100"
173 // --------------------------------------------------------------------
175 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).contextMenu(CONTAINS
).click();
176 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).expand();
177 comboBot
= filterBot
.comboBox(1); // aspect
178 comboBot
.setSelection(TIMESTAMP
);
179 textBot
= filterBot
.text();
181 textBot
.setText("100");
182 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).getNode("Timestamp CONTAINS \"100\"").select();
183 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).select();
185 viewBot
.toolbarButton("Save filters").click();
187 String ret
= applyFilter(fBot
, filterName
);
188 assertEquals("10/100", ret
);
192 * Return all timestamps ending with 100... for reasons
195 public void testTimestampEqualsOr() {
196 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
198 SWTBot filterBot
= viewBot
.bot();
199 SWTBotTree treeBot
= filterBot
.tree();
201 viewBot
.toolbarButton("Add new filter").click();
202 treeBot
.getTreeItem("FILTER <name>").select();
203 SWTBotText textBot
= filterBot
.text();
205 String filterName
= "matchAndEquals";
206 textBot
.setText(filterName
);
207 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
208 filterNodeBot
.click();
209 filterNodeBot
.contextMenu("TRACETYPE").click();
210 filterNodeBot
.expand();
211 SWTBotCombo comboBot
= filterBot
.comboBox();
212 comboBot
.setSelection(TRACETYPE
);
213 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
215 // --------------------------------------------------------------------
217 // --------------------------------------------------------------------
219 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(OR
).click();
221 // --------------------------------------------------------------------
222 // add EQUALS "19...300"
223 // --------------------------------------------------------------------
225 SWTBotTreeItem orNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(OR
);
226 orNode
.contextMenu("EQUALS").click();
228 orNode
.getNode(0).select();
229 comboBot
= filterBot
.comboBox(1); // aspect
230 comboBot
.setSelection(TIMESTAMP
);
231 textBot
= filterBot
.text();
233 textBot
.setText("19:00:00.000 000 300");
235 // --------------------------------------------------------------------
237 // --------------------------------------------------------------------
238 orNode
.contextMenu("MATCHES").click();
240 orNode
.getNode(1).select();
241 comboBot
= filterBot
.comboBox(1); // aspect
242 comboBot
.setSelection(CONTENTS
);
243 textBot
= filterBot
.text(0); // field
245 textBot
.setText("field");
246 textBot
= filterBot
.text(1); // value
248 textBot
.setText("1");
250 viewBot
.toolbarButton("Save filters").click();
252 String ret
= applyFilter(fBot
, filterName
);
253 assertEquals("26/100", ret
);
257 * test compare field >= 2
260 public void testField01() {
261 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
263 SWTBot filterBot
= viewBot
.bot();
264 SWTBotTree treeBot
= filterBot
.tree();
266 viewBot
.toolbarButton("Add new filter").click();
267 treeBot
.getTreeItem("FILTER <name>").select();
268 SWTBotText textBot
= filterBot
.text();
270 String filterName
= "field";
271 textBot
.setText(filterName
);
272 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
273 filterNodeBot
.click();
274 filterNodeBot
.contextMenu("TRACETYPE").click();
275 filterNodeBot
.expand();
276 SWTBotCombo comboBot
= filterBot
.comboBox();
277 comboBot
.setSelection(TRACETYPE
);
278 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
280 // --------------------------------------------------------------------
282 // --------------------------------------------------------------------
284 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(COMPARE
).click();
285 SWTBotTreeItem contentNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode("<select aspect> " + "=" + " <value>");
286 contentNode
.expand();
287 comboBot
= filterBot
.comboBox(1); // aspect
288 comboBot
.setSelection(CONTENTS
);
289 textBot
= filterBot
.text(0); // field
291 textBot
.setText(filterName
);
293 textBot
= filterBot
.text(1); // value
295 textBot
.setText("1.5");
296 filterBot
.radio(">").click();
298 // --------------------------------------------------------------------
300 // --------------------------------------------------------------------
301 viewBot
.toolbarButton("Save filters").click();
303 String ret
= applyFilter(fBot
, filterName
);
304 assertEquals("50/100", ret
);
307 private static String
applyFilter(SWTWorkbenchBot bot
, final String filterName
) {
308 SWTBotUtils
.waitForJobs();
309 final SWTBotTable eventsTable
= SWTBotUtils
.activeEventsEditor(bot
).bot().table();
310 SWTBotTableItem tableItem
= eventsTable
.getTableItem(2);
311 tableItem
.contextMenu(filterName
).click();
312 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(eventsTable
, "/100", 1, 1));
313 return eventsTable
.cell(1, 1);