70c9f71876da0d3b1cc1ab940ba402bcdb6d6f55
[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.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;
48
49 /**
50 * Test for filter views in trace compass
51 */
52 public class FilterViewerTest {
53
54
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";
64
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>";
70
71 private static final String PROJECT_NAME = "TestForFiltering";
72
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;
77
78 private static String makeEvent(int ts, int val) {
79 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
80 }
81
82 private static File fFileLocation;
83
84 /**
85 * Initialization, creates a temp trace
86 *
87 * @throws IOException
88 * should not happen
89 */
90 @BeforeClass
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();
95
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();
103
104 SWTBotUtils.closeView("welcome", fBot);
105
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));
114 }
115 braf.writeBytes(TRACE_END);
116 }
117 }
118
119 /**
120 * Open a trace in an editor
121 */
122 @Before
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);
130 }
131
132 /**
133 * Delete the file
134 */
135 @AfterClass
136 public static void cleanUp() {
137 fFileLocation.delete();
138 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
139 fLogger.removeAllAppenders();
140 SWTBotUtils.closeViewById(FilterView.ID, fBot);
141
142 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
143 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, TmfTimePreferences.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants.TIME_ZONE));
144 TmfTimestampFormat.updateDefaultFormats();
145 }
146
147 /**
148 * Close the editor
149 */
150 @After
151 public void tearDown() {
152 fBot.closeAllEditors();
153 }
154
155 /**
156 * Return all timestamps ending with 100... for reasons
157 */
158 @Test
159 public void testTimestampFilter() {
160 SWTBotView viewBot = fBot.viewById(FilterView.ID);
161 viewBot.setFocus();
162 SWTBot filterBot = viewBot.bot();
163 SWTBotTree treeBot = filterBot.tree();
164
165 viewBot.toolbarButton("Add new filter").click();
166 treeBot.getTreeItem("FILTER <name>").select();
167 SWTBotText textBot = filterBot.text();
168 textBot.setFocus();
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();
178
179 // --------------------------------------------------------------------
180 // add AND
181 // --------------------------------------------------------------------
182
183 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(AND).click();
184
185 // --------------------------------------------------------------------
186 // add CONTAINS "100"
187 // --------------------------------------------------------------------
188
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();
194 textBot.setFocus();
195 textBot.setText("100");
196 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).getNode("Timestamp CONTAINS \"100\"").select();
197 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).select();
198
199 viewBot.toolbarButton("Save filters").click();
200
201 String ret = applyFilter(fBot, filterName);
202 assertEquals("10/100", ret);
203 }
204
205 /**
206 * Return all timestamps ending with 100... for reasons
207 */
208 @Test
209 public void testTimestampEqualsOr() {
210 SWTBotView viewBot = fBot.viewById(FilterView.ID);
211 viewBot.setFocus();
212 SWTBot filterBot = viewBot.bot();
213 SWTBotTree treeBot = filterBot.tree();
214
215 viewBot.toolbarButton("Add new filter").click();
216 treeBot.getTreeItem("FILTER <name>").select();
217 SWTBotText textBot = filterBot.text();
218 textBot.setFocus();
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();
228
229 // --------------------------------------------------------------------
230 // add OR
231 // --------------------------------------------------------------------
232
233 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(OR).click();
234
235 // --------------------------------------------------------------------
236 // add EQUALS "19...300"
237 // --------------------------------------------------------------------
238
239 SWTBotTreeItem orNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode(OR);
240 orNode.contextMenu("EQUALS").click();
241 orNode.expand();
242 orNode.getNode(0).select();
243 comboBot = filterBot.comboBox(1); // aspect
244 comboBot.setSelection(TIMESTAMP);
245 textBot = filterBot.text();
246 textBot.setFocus();
247 textBot.setText("19:00:00.000 000 300");
248
249 // --------------------------------------------------------------------
250 // add MATCHES "1"
251 // --------------------------------------------------------------------
252 orNode.contextMenu("MATCHES").click();
253 orNode.expand();
254 orNode.getNode(1).select();
255 comboBot = filterBot.comboBox(1); // aspect
256 comboBot.setSelection(CONTENTS);
257 textBot = filterBot.text(0); // field
258 textBot.setFocus();
259 textBot.setText("field");
260 textBot = filterBot.text(1); // value
261 textBot.setFocus();
262 textBot.setText("1");
263
264 viewBot.toolbarButton("Save filters").click();
265
266 String ret = applyFilter(fBot, filterName);
267 assertEquals("26/100", ret);
268 // filterNodeBot.contextMenu().menu("Delete").click();
269 }
270
271 /**
272 * test compare field >= 2
273 */
274 @Test
275 public void testField01() {
276 SWTBotView viewBot = fBot.viewById(FilterView.ID);
277 viewBot.setFocus();
278 SWTBot filterBot = viewBot.bot();
279 SWTBotTree treeBot = filterBot.tree();
280
281 viewBot.toolbarButton("Add new filter").click();
282 treeBot.getTreeItem("FILTER <name>").select();
283 SWTBotText textBot = filterBot.text();
284 textBot.setFocus();
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();
294
295 // --------------------------------------------------------------------
296 // add Compare > 1.5
297 // --------------------------------------------------------------------
298
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
305 textBot.setFocus();
306 textBot.setText(filterName);
307
308 textBot = filterBot.text(1); // value
309 textBot.setFocus();
310 textBot.setText("1.5");
311 filterBot.radio(">").click();
312
313 // --------------------------------------------------------------------
314 // apply
315 // --------------------------------------------------------------------
316 viewBot.toolbarButton("Save filters").click();
317
318 String ret = applyFilter(fBot, filterName);
319 // filterNodeBot.contextMenu().menu("Delete").click();
320 assertEquals("50/100", ret);
321 }
322
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);
330 }
331 }
This page took 0.042717 seconds and 4 git commands to generate.