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