b79ba51b2757192df754b462ff75df15c0152c66
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / FilterViewerTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.io.File;
18 import java.io.IOException;
19
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;
42
43 /**
44 * Test for filter views in trace compass
45 */
46 public class FilterViewerTest {
47
48
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";
58
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>";
64
65 private static final String PROJECT_NAME = "TestForFiltering";
66
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;
71
72 private static String makeEvent(int ts, int val) {
73 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
74 }
75
76 private static File fFileLocation;
77
78 /**
79 * Initialization, creates a temp trace
80 *
81 * @throws IOException
82 * should not happen
83 */
84 @BeforeClass
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();
93
94 SWTBotUtils.closeView("welcome", fBot);
95
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));
104 }
105 braf.writeBytes(TRACE_END);
106 }
107 }
108
109 /**
110 * Open a trace in an editor
111 */
112 @Before
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);
120 }
121
122 /**
123 * Delete the file
124 */
125 @AfterClass
126 public static void cleanUp() {
127 fFileLocation.delete();
128 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
129 fLogger.removeAllAppenders();
130 SWTBotUtils.closeViewById(FilterView.ID, fBot);
131 }
132
133 /**
134 * Close the editor
135 */
136 @After
137 public void tearDown() {
138 fBot.closeAllEditors();
139 }
140
141 /**
142 * Return all timestamps ending with 100... for reasons
143 */
144 @Test
145 public void testTimestampFilter() {
146 SWTBotView viewBot = fBot.viewById(FilterView.ID);
147 viewBot.setFocus();
148 SWTBot filterBot = viewBot.bot();
149 SWTBotTree treeBot = filterBot.tree();
150
151 viewBot.toolbarButton("Add new filter").click();
152 treeBot.getTreeItem("FILTER <name>").select();
153 SWTBotText textBot = filterBot.text();
154 textBot.setFocus();
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();
164
165 // --------------------------------------------------------------------
166 // add AND
167 // --------------------------------------------------------------------
168
169 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(AND).click();
170
171 // --------------------------------------------------------------------
172 // add CONTAINS "100"
173 // --------------------------------------------------------------------
174
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();
180 textBot.setFocus();
181 textBot.setText("100");
182 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).getNode("Timestamp CONTAINS \"100\"").select();
183 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).select();
184
185 viewBot.toolbarButton("Save filters").click();
186
187 String ret = applyFilter(fBot, filterName);
188 assertEquals("10/100", ret);
189 }
190
191 /**
192 * Return all timestamps ending with 100... for reasons
193 */
194 @Test
195 public void testTimestampEqualsOr() {
196 SWTBotView viewBot = fBot.viewById(FilterView.ID);
197 viewBot.setFocus();
198 SWTBot filterBot = viewBot.bot();
199 SWTBotTree treeBot = filterBot.tree();
200
201 viewBot.toolbarButton("Add new filter").click();
202 treeBot.getTreeItem("FILTER <name>").select();
203 SWTBotText textBot = filterBot.text();
204 textBot.setFocus();
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();
214
215 // --------------------------------------------------------------------
216 // add OR
217 // --------------------------------------------------------------------
218
219 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(OR).click();
220
221 // --------------------------------------------------------------------
222 // add EQUALS "19...300"
223 // --------------------------------------------------------------------
224
225 SWTBotTreeItem orNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode(OR);
226 orNode.contextMenu("EQUALS").click();
227 orNode.expand();
228 orNode.getNode(0).select();
229 comboBot = filterBot.comboBox(1); // aspect
230 comboBot.setSelection(TIMESTAMP);
231 textBot = filterBot.text();
232 textBot.setFocus();
233 textBot.setText("19:00:00.000 000 300");
234
235 // --------------------------------------------------------------------
236 // add MATCHES "1"
237 // --------------------------------------------------------------------
238 orNode.contextMenu("MATCHES").click();
239 orNode.expand();
240 orNode.getNode(1).select();
241 comboBot = filterBot.comboBox(1); // aspect
242 comboBot.setSelection(CONTENTS);
243 textBot = filterBot.text(0); // field
244 textBot.setFocus();
245 textBot.setText("field");
246 textBot = filterBot.text(1); // value
247 textBot.setFocus();
248 textBot.setText("1");
249
250 viewBot.toolbarButton("Save filters").click();
251
252 String ret = applyFilter(fBot, filterName);
253 assertEquals("26/100", ret);
254 }
255
256 /**
257 * test compare field >= 2
258 */
259 @Test
260 public void testField01() {
261 SWTBotView viewBot = fBot.viewById(FilterView.ID);
262 viewBot.setFocus();
263 SWTBot filterBot = viewBot.bot();
264 SWTBotTree treeBot = filterBot.tree();
265
266 viewBot.toolbarButton("Add new filter").click();
267 treeBot.getTreeItem("FILTER <name>").select();
268 SWTBotText textBot = filterBot.text();
269 textBot.setFocus();
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();
279
280 // --------------------------------------------------------------------
281 // add Compare > 1.5
282 // --------------------------------------------------------------------
283
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
290 textBot.setFocus();
291 textBot.setText(filterName);
292
293 textBot = filterBot.text(1); // value
294 textBot.setFocus();
295 textBot.setText("1.5");
296 filterBot.radio(">").click();
297
298 // --------------------------------------------------------------------
299 // apply
300 // --------------------------------------------------------------------
301 viewBot.toolbarButton("Save filters").click();
302
303 String ret = applyFilter(fBot, filterName);
304 assertEquals("50/100", ret);
305 }
306
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);
314 }
315 }
This page took 0.038299 seconds and 4 git commands to generate.