tmf: Add a more reliable way to get the active editor with SWTBot
[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
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>";
62
63
64 private static final String PROJECT_NAME = "TestForFiltering";
65
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;
70
71 private static String makeEvent(int ts, int val) {
72 return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n";
73 }
74
75 private static File fFileLocation;
76
77 /**
78 * Initialization, creates a temp trace
79 *
80 * @throws IOException
81 * should not happen
82 */
83 @BeforeClass
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();
92
93 SWTBotUtils.closeView("welcome", fBot);
94
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));
103 }
104 braf.writeBytes(TRACE_END);
105 }
106 }
107
108 /**
109 * Open a trace in an editor
110 */
111 @Before
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);
119 }
120
121 /**
122 * Delete the file
123 */
124 @AfterClass
125 public static void cleanUp() {
126 fFileLocation.delete();
127 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
128 fLogger.removeAllAppenders();
129 SWTBotUtils.closeViewById(FilterView.ID, fBot);
130 }
131
132 /**
133 * Close the editor
134 */
135 @After
136 public void tearDown() {
137 fBot.closeAllEditors();
138 }
139
140 /**
141 * Return all timestamps ending with 100... for reasons
142 */
143 @Test
144 public void testTimestampFilter() {
145 SWTBotView viewBot = fBot.viewById(FilterView.ID);
146 viewBot.setFocus();
147 SWTBot filterBot = viewBot.bot();
148 SWTBotTree treeBot = filterBot.tree();
149
150 viewBot.toolbarButton("Add new filter").click();
151 treeBot.getTreeItem("FILTER <name>").select();
152 SWTBotText textBot = filterBot.text();
153 textBot.setFocus();
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();
163
164 // --------------------------------------------------------------------
165 // add AND
166 // --------------------------------------------------------------------
167
168 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(AND).click();
169
170 // --------------------------------------------------------------------
171 // add CONTAINS "100"
172 // --------------------------------------------------------------------
173
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();
179 textBot.setFocus();
180 textBot.setText("100");
181 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).getNode("Timestamp CONTAINS \"100\"").select();
182 filterNodeBot.getNode(WITH_TRACETYPE).getNode(AND).select();
183
184 viewBot.toolbarButton("Save filters").click();
185
186 String ret = applyFilter(fBot, filterName);
187 assertEquals("10/100", ret);
188 }
189
190 /**
191 * Return all timestamps ending with 100... for reasons
192 */
193 @Test
194 public void testTimestampEqualsOr() {
195 SWTBotView viewBot = fBot.viewById(FilterView.ID);
196 viewBot.setFocus();
197 SWTBot filterBot = viewBot.bot();
198 SWTBotTree treeBot = filterBot.tree();
199
200 viewBot.toolbarButton("Add new filter").click();
201 treeBot.getTreeItem("FILTER <name>").select();
202 SWTBotText textBot = filterBot.text();
203 textBot.setFocus();
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();
213
214 // --------------------------------------------------------------------
215 // add OR
216 // --------------------------------------------------------------------
217
218 filterNodeBot.getNode(WITH_TRACETYPE).contextMenu(OR).click();
219
220 // --------------------------------------------------------------------
221 // add EQUALS "19...300"
222 // --------------------------------------------------------------------
223
224 SWTBotTreeItem orNode = filterNodeBot.getNode(WITH_TRACETYPE).getNode(OR);
225 orNode.contextMenu("EQUALS").click();
226 orNode.expand();
227 orNode.getNode(0).select();
228 comboBot = filterBot.comboBox();
229 //timestamp
230 comboBot.setSelection(comboBot.itemCount() - 3);
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 // contents
242 comboBot = filterBot.comboBox();
243 comboBot.setSelection(comboBot.itemCount() - 1);
244 textBot = filterBot.text(0);
245 textBot.setFocus();
246 textBot.setText("field");
247 textBot = filterBot.text(1);
248 textBot.setFocus();
249 textBot.setText("1");
250
251 viewBot.toolbarButton("Save filters").click();
252
253 String ret = applyFilter(fBot, filterName);
254 assertEquals("26/100", ret);
255 }
256
257 /**
258 * test compare field >= 2
259 */
260 @Test
261 public void testField01() {
262 SWTBotView viewBot = fBot.viewById(FilterView.ID);
263 viewBot.setFocus();
264 SWTBot filterBot = viewBot.bot();
265 SWTBotTree treeBot = filterBot.tree();
266
267 viewBot.toolbarButton("Add new filter").click();
268 treeBot.getTreeItem("FILTER <name>").select();
269 SWTBotText textBot = filterBot.text();
270 textBot.setFocus();
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();
280
281 // --------------------------------------------------------------------
282 // add Compare > 1.5
283 // --------------------------------------------------------------------
284
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);
291 textBot.setFocus();
292 textBot.setText(filterName);
293
294 textBot = filterBot.text(1);
295 textBot.setFocus();
296 textBot.setText("1.5");
297 filterBot.radio(2).click();
298
299 // --------------------------------------------------------------------
300 // apply
301 // --------------------------------------------------------------------
302 viewBot.toolbarButton("Save filters").click();
303
304 String ret = applyFilter(fBot, filterName);
305 assertEquals("50/100", ret);
306 }
307
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);
315 }
316 }
This page took 0.038363 seconds and 5 git commands to generate.