tmf: Add waitUntil / condition to tmf.ui.tests
[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.tests.shared.WaitUtils;
44 import org.eclipse.tracecompass.tmf.ui.views.filter.FilterView;
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51
52 /**
53 * Test for filter views in trace compass
54 */
55 @RunWith(SWTBotJunit4ClassRunner.class)
56 public class FilterViewerTest {
57
58
59 private static final String COMPARE = "COMPARE";
60 private static final String CONTAINS = "CONTAINS";
61 private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub";
62 private static final String TRACETYPE = "Test trace : XML Trace Stub";
63 private static final String AND = "AND";
64 private static final String WITH_TRACETYPE = "WITH TRACETYPE " + TRACETYPE;
65 private static final String FILTER_TEST = "FILTER ";
66 private static final String TIMESTAMP = "Timestamp";
67 private static final String CONTENTS = "Contents";
68
69 private static final String TRACE_START = "<trace>";
70 private static final String EVENT_BEGIN = "<event timestamp=\"";
71 private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\"";
72 private static final String EVENT_END = "\" type=\"int\" />" + "</event>";
73 private static final String TRACE_END = "</trace>";
74
75 private static final String PROJECT_NAME = "TestForFiltering";
76
77 /** The Log4j logger instance. */
78 private static final Logger fLogger = Logger.getRootLogger();
79 private static final String OR = "OR";
80 private static SWTWorkbenchBot fBot;
81
82 private static String makeEvent(int ts, int val) {
83 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
84 }
85
86 private static File fFileLocation;
87
88 /**
89 * Initialization, creates a temp trace
90 *
91 * @throws IOException
92 * should not happen
93 */
94 @BeforeClass
95 public static void init() throws IOException {
96 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
97 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, "GMT-05:00");
98 TmfTimestampFormat.updateDefaultFormats();
99
100 SWTBotUtils.initialize();
101 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
102 /* set up for swtbot */
103 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
104 fLogger.removeAllAppenders();
105 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
106 fBot = new SWTWorkbenchBot();
107
108 SWTBotUtils.closeView("welcome", fBot);
109
110 SWTBotUtils.switchToTracingPerspective();
111 /* finish waiting for eclipse to load */
112 WaitUtils.waitForJobs();
113 fFileLocation = File.createTempFile("sample", ".xml");
114 try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fFileLocation, "rw")) {
115 braf.writeBytes(TRACE_START);
116 for (int i = 0; i < 100; i++) {
117 braf.writeBytes(makeEvent(i * 100, i % 4));
118 }
119 braf.writeBytes(TRACE_END);
120 }
121 }
122
123 /**
124 * Open a trace in an editor
125 */
126 @Before
127 public void beforeTest() {
128 SWTWorkbenchBot bot = new SWTWorkbenchBot();
129 SWTBotUtils.createProject(PROJECT_NAME);
130 SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(bot, PROJECT_NAME);
131 assertNotNull(treeItem);
132 SWTBotUtils.openTrace(PROJECT_NAME, fFileLocation.getAbsolutePath(), XMLSTUB_ID);
133 SWTBotUtils.openView(FilterView.ID);
134 }
135
136 /**
137 * Delete the file
138 */
139 @AfterClass
140 public static void cleanUp() {
141 fFileLocation.delete();
142 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
143 fLogger.removeAllAppenders();
144 SWTBotUtils.closeViewById(FilterView.ID, fBot);
145
146 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
147 defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, TmfTimePreferences.getDefaultPreferenceMap().get(ITmfTimePreferencesConstants.TIME_ZONE));
148 TmfTimestampFormat.updateDefaultFormats();
149 }
150
151 /**
152 * Close the editor
153 */
154 @After
155 public void tearDown() {
156 fBot.closeAllEditors();
157 }
158
159 /**
160 * Return all timestamps ending with 100... for reasons
161 */
162 @Test
163 public void testTimestampFilter() {
164 SWTBotView viewBot = fBot.viewById(FilterView.ID);
165 viewBot.setFocus();
166 SWTBot filterBot = viewBot.bot();
167 SWTBotTree treeBot = filterBot.tree();
168
169 viewBot.toolbarButton("Add new filter").click();
170 treeBot.getTreeItem("FILTER <name>").select();
171 SWTBotText textBot = filterBot.text();
172 textBot.setFocus();
173 String filterName = "timestamp";
174 textBot.setText(filterName);
175 SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName);
176 filterNodeBot.click();
177 filterNodeBot.contextMenu("TRACETYPE").click();
178 filterNodeBot.expand();
179 SWTBotCombo comboBot = filterBot.comboBox();
180 comboBot.setSelection(TRACETYPE);
181 filterNodeBot.getNode(WITH_TRACETYPE).expand();
182
183 // --------------------------------------------------------------------
184 // add AND
185 // --------------------------------------------------------------------
186
187 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(AND).click();
188
189 // --------------------------------------------------------------------
190 // add CONTAINS "100"
191 // --------------------------------------------------------------------
192
193 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).contextMenu(CONTAINS).click();
194 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).expand();
195 comboBot = filterBot.comboBox(1); // aspect
196 comboBot.setSelection(TIMESTAMP);
197 textBot = filterBot.text();
198 textBot.setFocus();
199 textBot.setText("100");
200 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).getNode("Timestamp CONTAINS \"100\"").select();
201 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).select();
202
203 viewBot.toolbarButton("Save filters").click();
204
205 String ret = applyFilter(fBot, filterName);
206 assertEquals("10/100", ret);
207 }
208
209 /**
210 * Return all timestamps ending with 100... for reasons
211 */
212 @Test
213 public void testTimestampEqualsOr() {
214 SWTBotView viewBot = fBot.viewById(FilterView.ID);
215 viewBot.setFocus();
216 SWTBot filterBot = viewBot.bot();
217 SWTBotTree treeBot = filterBot.tree();
218
219 viewBot.toolbarButton("Add new filter").click();
220 treeBot.getTreeItem("FILTER <name>").select();
221 SWTBotText textBot = filterBot.text();
222 textBot.setFocus();
223 String filterName = "matchAndEquals";
224 textBot.setText(filterName);
225 SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName);
226 filterNodeBot.click();
227 filterNodeBot.contextMenu("TRACETYPE").click();
228 filterNodeBot.expand();
229 SWTBotCombo comboBot = filterBot.comboBox();
230 comboBot.setSelection(TRACETYPE);
231 filterNodeBot.getNode(WITH_TRACETYPE).expand();
232
233 // --------------------------------------------------------------------
234 // add OR
235 // --------------------------------------------------------------------
236
237 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(OR).click();
238
239 // --------------------------------------------------------------------
240 // add EQUALS "19...300"
241 // --------------------------------------------------------------------
242
243 SWTBotTreeItem orNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode(OR);
244 orNode.contextMenu("EQUALS").click();
245 orNode.expand();
246 orNode.getNode(0).select();
247 comboBot = filterBot.comboBox(1); // aspect
248 comboBot.setSelection(TIMESTAMP);
249 textBot = filterBot.text();
250 textBot.setFocus();
251 textBot.setText("19:00:00.000 000 300");
252
253 // --------------------------------------------------------------------
254 // add MATCHES "1"
255 // --------------------------------------------------------------------
256 orNode.contextMenu("MATCHES").click();
257 orNode.expand();
258 orNode.getNode(1).select();
259 comboBot = filterBot.comboBox(1); // aspect
260 comboBot.setSelection(CONTENTS);
261 textBot = filterBot.text(0); // field
262 textBot.setFocus();
263 textBot.setText("field");
264 textBot = filterBot.text(1); // value
265 textBot.setFocus();
266 textBot.setText("1");
267
268 viewBot.toolbarButton("Save filters").click();
269
270 String ret = applyFilter(fBot, filterName);
271 assertEquals("26/100", ret);
272 // filterNodeBot.contextMenu().menu("Delete").click();
273 }
274
275 /**
276 * test compare field >= 2
277 */
278 @Test
279 public void testField01() {
280 SWTBotView viewBot = fBot.viewById(FilterView.ID);
281 viewBot.setFocus();
282 SWTBot filterBot = viewBot.bot();
283 SWTBotTree treeBot = filterBot.tree();
284
285 viewBot.toolbarButton("Add new filter").click();
286 treeBot.getTreeItem("FILTER <name>").select();
287 SWTBotText textBot = filterBot.text();
288 textBot.setFocus();
289 String filterName = "field";
290 textBot.setText(filterName);
291 SWTBotTreeItem filterNodeBot = treeBot.getTreeItem(FILTER_TEST + filterName);
292 filterNodeBot.click();
293 filterNodeBot.contextMenu("TRACETYPE").click();
294 filterNodeBot.expand();
295 SWTBotCombo comboBot = filterBot.comboBox();
296 comboBot.setSelection(TRACETYPE);
297 filterNodeBot.getNode(WITH_TRACETYPE).expand();
298
299 // --------------------------------------------------------------------
300 // add Compare > 1.5
301 // --------------------------------------------------------------------
302
303 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(COMPARE).click();
304 SWTBotTreeItem contentNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode("<select aspect> " + "=" + " <value>");
305 contentNode.expand();
306 comboBot = filterBot.comboBox(1); // aspect
307 comboBot.setSelection(CONTENTS);
308 textBot = filterBot.text(0); // field
309 textBot.setFocus();
310 textBot.setText(filterName);
311
312 textBot = filterBot.text(1); // value
313 textBot.setFocus();
314 textBot.setText("1.5");
315 filterBot.radio(">").click();
316
317 // --------------------------------------------------------------------
318 // apply
319 // --------------------------------------------------------------------
320 viewBot.toolbarButton("Save filters").click();
321
322 String ret = applyFilter(fBot, filterName);
323 // filterNodeBot.contextMenu().menu("Delete").click();
324 assertEquals("50/100", ret);
325 }
326
327 private static String applyFilter(SWTWorkbenchBot bot, final String filterName) {
328 WaitUtils.waitForJobs();
329 final SWTBotTable eventsTable = SWTBotUtils.activeEventsEditor(bot).bot().table();
330 SWTBotTableItem tableItem = eventsTable.getTableItem(2);
331 tableItem.contextMenu(filterName).click();
332 fBot.waitUntil(ConditionHelpers.isTableCellFilled(eventsTable, "/100", 1, 1));
333 return eventsTable.cell(1, 1);
334 }
335 }
This page took 0.038179 seconds and 5 git commands to generate.