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 ";
57 private static final String TRACE_START
= "<trace>";
58 private static final String EVENT_BEGIN
= "<event timestamp=\"";
59 private static final String EVENT_MIDDLE
= " \" name=\"event\"><field name=\"field\" value=\"";
60 private static final String EVENT_END
= "\" type=\"int\" />" + "</event>";
61 private static final String TRACE_END
= "</trace>";
64 private static final String PROJECT_NAME
= "TestForFiltering";
66 /** The Log4j logger instance. */
67 private static final Logger fLogger
= Logger
.getRootLogger();
68 private static final String OR
= "OR";
69 private static SWTWorkbenchBot fBot
;
71 private static String
makeEvent(int ts
, int val
) {
72 return EVENT_BEGIN
+ Integer
.toString(ts
) + EVENT_MIDDLE
+ Integer
.toString(val
) + EVENT_END
+ "\n";
75 private static File fFileLocation
;
78 * Initialization, creates a temp trace
84 public static void init() throws IOException
{
85 SWTBotUtils
.failIfUIThread();
86 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
87 /* set up for swtbot */
88 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
89 fLogger
.removeAllAppenders();
90 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
91 fBot
= new SWTWorkbenchBot();
93 SWTBotUtils
.closeView("welcome", fBot
);
95 SWTBotUtils
.switchToTracingPerspective();
96 /* finish waiting for eclipse to load */
97 SWTBotUtils
.waitForJobs();
98 fFileLocation
= File
.createTempFile("sample", ".xml");
99 try (BufferedRandomAccessFile braf
= new BufferedRandomAccessFile(fFileLocation
, "rw")) {
100 braf
.writeBytes(TRACE_START
);
101 for (int i
= 0; i
< 100; i
++) {
102 braf
.writeBytes(makeEvent(i
* 100, i
% 4));
104 braf
.writeBytes(TRACE_END
);
109 * Open a trace in an editor
112 public void beforeTest() {
113 SWTWorkbenchBot bot
= new SWTWorkbenchBot();
114 SWTBotUtils
.createProject(PROJECT_NAME
);
115 SWTBotTreeItem treeItem
= SWTBotUtils
.selectTracesFolder(bot
, PROJECT_NAME
);
116 assertNotNull(treeItem
);
117 SWTBotUtils
.openTrace(PROJECT_NAME
, fFileLocation
.getAbsolutePath(), XMLSTUB_ID
);
118 SWTBotUtils
.openView(FilterView
.ID
);
125 public static void cleanUp() {
126 fFileLocation
.delete();
127 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
128 fLogger
.removeAllAppenders();
129 SWTBotUtils
.closeViewById(FilterView
.ID
, fBot
);
136 public void tearDown() {
137 fBot
.closeAllEditors();
141 * Return all timestamps ending with 100... for reasons
144 public void testTimestampFilter() {
145 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
147 SWTBot filterBot
= viewBot
.bot();
148 SWTBotTree treeBot
= filterBot
.tree();
150 viewBot
.toolbarButton("Add new filter").click();
151 treeBot
.getTreeItem("FILTER <name>").select();
152 SWTBotText textBot
= filterBot
.text();
154 String filterName
= "timestamp";
155 textBot
.setText(filterName
);
156 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
157 filterNodeBot
.click();
158 filterNodeBot
.contextMenu("TRACETYPE").click();
159 filterNodeBot
.expand();
160 SWTBotCombo comboBot
= filterBot
.comboBox();
161 comboBot
.setSelection(TRACETYPE
);
162 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
164 // --------------------------------------------------------------------
166 // --------------------------------------------------------------------
168 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(AND
).click();
170 // --------------------------------------------------------------------
171 // add CONTAINS "100"
172 // --------------------------------------------------------------------
174 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).contextMenu(CONTAINS
).click();
175 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).expand();
176 comboBot
= filterBot
.comboBox();
177 comboBot
.setSelection(comboBot
.itemCount() - 3);
178 textBot
= filterBot
.text();
180 textBot
.setText("100");
181 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).getNode("Timestamp CONTAINS \"100\"").select();
182 filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(AND
).select();
184 viewBot
.toolbarButton("Save filters").click();
186 String ret
= applyFilter(fBot
, filterName
);
187 assertEquals("10/100", ret
);
191 * Return all timestamps ending with 100... for reasons
194 public void testTimestampEqualsOr() {
195 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
197 SWTBot filterBot
= viewBot
.bot();
198 SWTBotTree treeBot
= filterBot
.tree();
200 viewBot
.toolbarButton("Add new filter").click();
201 treeBot
.getTreeItem("FILTER <name>").select();
202 SWTBotText textBot
= filterBot
.text();
204 String filterName
= "matchAndEquals";
205 textBot
.setText(filterName
);
206 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
207 filterNodeBot
.click();
208 filterNodeBot
.contextMenu("TRACETYPE").click();
209 filterNodeBot
.expand();
210 SWTBotCombo comboBot
= filterBot
.comboBox();
211 comboBot
.setSelection(TRACETYPE
);
212 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
214 // --------------------------------------------------------------------
216 // --------------------------------------------------------------------
218 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(OR
).click();
220 // --------------------------------------------------------------------
221 // add EQUALS "19...300"
222 // --------------------------------------------------------------------
224 SWTBotTreeItem orNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode(OR
);
225 orNode
.contextMenu("EQUALS").click();
227 orNode
.getNode(0).select();
228 comboBot
= filterBot
.comboBox();
230 comboBot
.setSelection(comboBot
.itemCount() - 3);
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();
242 comboBot
= filterBot
.comboBox();
243 comboBot
.setSelection(comboBot
.itemCount() - 1);
244 textBot
= filterBot
.text(0);
246 textBot
.setText("field");
247 textBot
= filterBot
.text(1);
249 textBot
.setText("1");
251 viewBot
.toolbarButton("Save filters").click();
253 String ret
= applyFilter(fBot
, filterName
);
254 assertEquals("26/100", ret
);
258 * test compare field >= 2
261 public void testField01() {
262 SWTBotView viewBot
= fBot
.viewById(FilterView
.ID
);
264 SWTBot filterBot
= viewBot
.bot();
265 SWTBotTree treeBot
= filterBot
.tree();
267 viewBot
.toolbarButton("Add new filter").click();
268 treeBot
.getTreeItem("FILTER <name>").select();
269 SWTBotText textBot
= filterBot
.text();
271 String filterName
= "field";
272 textBot
.setText(filterName
);
273 SWTBotTreeItem filterNodeBot
= treeBot
.getTreeItem(FILTER_TEST
+ filterName
);
274 filterNodeBot
.click();
275 filterNodeBot
.contextMenu("TRACETYPE").click();
276 filterNodeBot
.expand();
277 SWTBotCombo comboBot
= filterBot
.comboBox();
278 comboBot
.setSelection(TRACETYPE
);
279 filterNodeBot
.getNode(WITH_TRACETYPE
).expand();
281 // --------------------------------------------------------------------
283 // --------------------------------------------------------------------
285 filterNodeBot
.getNode(WITH_TRACETYPE
).contextMenu(COMPARE
).click();
286 SWTBotTreeItem contentNode
= filterNodeBot
.getNode(WITH_TRACETYPE
).getNode("<select aspect> " + "=" + " <value>");
287 contentNode
.expand();
288 comboBot
= filterBot
.comboBox();
289 comboBot
.setSelection(comboBot
.itemCount() - 1);
290 textBot
= filterBot
.text(0);
292 textBot
.setText(filterName
);
294 textBot
= filterBot
.text(1);
296 textBot
.setText("1.5");
297 filterBot
.radio(2).click();
299 // --------------------------------------------------------------------
301 // --------------------------------------------------------------------
302 viewBot
.toolbarButton("Save filters").click();
304 String ret
= applyFilter(fBot
, filterName
);
305 assertEquals("50/100", ret
);
308 private static String
applyFilter(SWTWorkbenchBot bot
, final String filterName
) {
309 SWTBotUtils
.waitForJobs();
310 final SWTBotTable eventsEditor
= SWTBotUtils
.activeEventsEditor(bot
).bot().table();
311 SWTBotTableItem tableItem
= eventsEditor
.getTableItem(2);
312 tableItem
.contextMenu(filterName
).click();
313 fBot
.waitUntil(ConditionHelpers
.isTableCellFilled(eventsEditor
, "/100", 1, 1));
314 return eventsEditor
.cell(1, 1);