tmf: Wait until shell is active in swtbot test
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ui / swtbot / tests / TestCustomTxtWizard.java
1 /*******************************************************************************
2 * Copyright (c) 2014 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
13 package org.eclipse.linuxtools.tmf.ui.swtbot.tests;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.io.RandomAccessFile;
23
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.varia.NullAppender;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
30 import org.eclipse.swtbot.swt.finder.waits.Conditions;
31 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35
36 /**
37 * Custom text wizard tests
38 *
39 * Some reminders to help making tests (javadoc to keep formatting)
40 *
41 * Button reminder
42 *
43 * <pre>
44 * 0 Time Stamp Format Help
45 * 1 Remove line
46 * 2 Add next line
47 * 3 Add child line
48 * 4 Move up
49 * 5 Move down
50 * 6 Regular Expression Help
51 * 7 Remove group (group 1 toggle)
52 * 8 Remove group (group 2 toggle)
53 * 9 Add group (group 3 toggle ...)
54 * 10 Show parsing result
55 * 11 Preview Legend
56 * </pre>
57 *
58 * Combo box reminder
59 *
60 * <pre>
61 * 0 cardinality
62 * 1 event type (message, timestamp...)
63 * 2 how to handle the data (set, append...)
64 * repeat
65 * </pre>
66 *
67 * @author Matthew Khouzam
68 *
69 */
70 public class TestCustomTxtWizard {
71
72 private static final String MANAGE_CUSTOM_PARSERS_SHELL_TITLE = "Manage Custom Parsers";
73 private static final String PROJECT_NAME = "Test";
74 private static final String EXPECTED_TEST_DEFINITION = "<Definition name=\"Test\">\n" +
75 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
76 "<InputLine>\n" +
77 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
78 "<RegEx>\\s*(\\d\\d)\\s(.*\\S)</RegEx>\n" +
79 "<InputData action=\"0\" format=\"ss\" name=\"Time Stamp\"/>\n" +
80 "<InputData action=\"0\" name=\"Message\"/>\n" +
81 "</InputLine>\n" +
82 "<InputLine>\n" +
83 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
84 "<RegEx>([^0-9]*)</RegEx>\n" +
85 "<InputData action=\"2\" name=\"Message\"/>\n" +
86 "</InputLine>\n" +
87 "<OutputColumn name=\"Time Stamp\"/>\n" +
88 "<OutputColumn name=\"Message\"/>\n";
89
90 /** The Log4j logger instance. */
91 private static final Logger fLogger = Logger.getRootLogger();
92 private static SWTWorkbenchBot fBot;
93
94 /** Test Class setup */
95 @BeforeClass
96 public static void init() {
97 SWTBotUtil.failIfUIThread();
98 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
99 /* set up for swtbot */
100 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
101 fLogger.addAppender(new NullAppender());
102 fBot = new SWTWorkbenchBot();
103
104 SWTBotUtil.closeView("welcome", fBot);
105
106 SWTBotUtil.switchToTracingPerspective();
107 /* finish waiting for eclipse to load */
108 SWTBotUtil.waitForJobs();
109
110 }
111
112 /**
113 * Test to create a custom txt trace and compare the xml
114 *
115 * @throws IOException
116 * the xml file is not accessible, this is bad
117 * @throws FileNotFoundException
118 * the xml file wasn't written, this is bad
119 */
120 @Ignore
121 @Test
122 public void testNew() throws FileNotFoundException, IOException {
123 File xmlFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
124 SWTBotUtil.createProject(PROJECT_NAME);
125 SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer");
126 proejctExplorerBot.show();
127 SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
128 treeItem.select();
129 treeItem.expand();
130 SWTBotTreeItem treeNode = null;
131 for (String node : treeItem.getNodes()) {
132 if (node.startsWith("Trace")) {
133 treeNode = treeItem.getNode(node);
134 break;
135 }
136
137 }
138 assertNotNull(treeNode);
139 treeNode.contextMenu("Manage Custom Parsers...").click();
140 fBot.waitUntil(Conditions.shellIsActive(MANAGE_CUSTOM_PARSERS_SHELL_TITLE));
141 fBot.button("New...").click();
142
143 fBot.textWithLabel("Log type:").setText(PROJECT_NAME);
144 fBot.textWithLabel("Time Stamp format:").setText("ss");
145 fBot.comboBox(1).setSelection("Time Stamp");
146 fBot.textWithLabel("format:").setText("ss");
147 fBot.button(8).click();
148 fBot.button(2).click();
149 SWTBotTreeItem[] treeItems = fBot.tree().getAllItems();
150 SWTBotTreeItem eventLine[] = new SWTBotTreeItem[2];
151 treeItems = fBot.tree().getAllItems();
152 for (SWTBotTreeItem item : treeItems) {
153 if (item.getText().startsWith("Root Line 1")) {
154 eventLine[0] = item;
155 }
156 if (item.getText().startsWith("Root Line 2")) {
157 eventLine[1] = item;
158 }
159 }
160 assertNotNull(eventLine[0]);
161 assertNotNull(eventLine[1]);
162 fBot.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
163 eventLine[0].select();
164 SWTBotUtil.waitForJobs();
165 fBot.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
166 eventLine[1].select();
167 fBot.textWithLabel("Regular expression:").setText("([^0-9]*)");
168 fBot.button(7).click();
169 fBot.comboBox("Set").setSelection("Append with |");
170 fBot.button("Highlight All").click();
171 fBot.button("Next >").click();
172 fBot.button("Finish").click();
173 fBot.list().select(PROJECT_NAME);
174 String xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
175 assertEquals(EXPECTED_TEST_DEFINITION, xmlPart);
176 fBot.button("Delete").click();
177 fBot.button("Yes").click();
178 fBot.button("Close").click();
179 xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
180 assertEquals("", xmlPart);
181 }
182
183 /**
184 * Test to edit a custom txt trace and compare the xml
185 *
186 * @throws IOException
187 * the xml file is not accessible, this is bad
188 * @throws FileNotFoundException
189 * the xml file wasn't written, this is bad
190 */
191 @Ignore
192 @Test
193 public void testEdit() throws FileNotFoundException, IOException {
194 File xmlFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
195 try (FileWriter fw = new FileWriter(xmlFile)) {
196 String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
197 "<CustomTxtTraceDefinitionList>\n" +
198 "<Definition name=\"Demo trace\">\n" +
199 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
200 "<InputLine>\n" +
201 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
202 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
203 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
204 "<InputData action=\"0\" name=\"Message\"/>\n" +
205 "</InputLine>\n" +
206 "<OutputColumn name=\"Time Stamp\"/>\n" +
207 "<OutputColumn name=\"Message\"/>\n" +
208 "</Definition>\n" +
209 "<Definition name=\"dmesg\">\n" +
210 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
211 "<InputLine>\n" +
212 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
213 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
214 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
215 "<InputData action=\"0\" name=\"Message\"/>\n" +
216 "</InputLine>\n" +
217 "<OutputColumn name=\"Time Stamp\"/>\n" +
218 "<OutputColumn name=\"Message\"/>\n" +
219 "</Definition>\n" +
220 "</CustomTxtTraceDefinitionList>";
221 fw.write(xmlContent);
222 fw.flush();
223 }
224 SWTBotUtil.createProject(PROJECT_NAME);
225 SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer");
226 proejctExplorerBot.show();
227 SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
228 treeItem.select();
229 treeItem.expand();
230 SWTBotTreeItem treeNode = null;
231 for (String node : treeItem.getNodes()) {
232 if (node.startsWith("Trace")) {
233 treeNode = treeItem.getNode(node);
234 break;
235 }
236
237 }
238 assertNotNull(treeNode);
239 treeNode.contextMenu("Manage Custom Parsers...").click();
240 fBot.waitUntil(Conditions.shellIsActive(MANAGE_CUSTOM_PARSERS_SHELL_TITLE));
241 fBot.list().select(fBot.list().getItems()[0]);
242 fBot.button("Edit...").click();
243
244 fBot.textWithLabel("Log type:").setText(PROJECT_NAME);
245 fBot.textWithLabel("Time Stamp format:").setText("ss");
246 fBot.comboBox(1).setSelection("Time Stamp");
247 fBot.textWithLabel("format:").setText("ss");
248 fBot.button(2).click();
249 SWTBotTreeItem[] treeItems = fBot.tree().getAllItems();
250 SWTBotTreeItem eventLine[] = new SWTBotTreeItem[2];
251 for (SWTBotTreeItem item : treeItems) {
252 if (item.getText().startsWith("Root Line 1")) {
253 eventLine[0] = item;
254 }
255 if (item.getText().startsWith("Root Line 2")) {
256 eventLine[1] = item;
257 }
258 }
259 treeItems = fBot.tree().getAllItems();
260 assertNotNull(eventLine[0]);
261 assertNotNull(eventLine[1]);
262 fBot.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
263 eventLine[0].select();
264 SWTBotUtil.waitForJobs();
265 fBot.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
266 eventLine[1].select();
267 fBot.textWithLabel("Regular expression:").setText("([^0-9]*)");
268 fBot.button(7).click();
269 fBot.comboBox("Set").setSelection("Append with |");
270 fBot.button("Highlight All").click();
271 fBot.button("Next >").click();
272 fBot.button("Finish").click();
273 fBot.list().select(PROJECT_NAME);
274 String xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
275 assertEquals(EXPECTED_TEST_DEFINITION, xmlPart);
276 fBot.button("Delete").click();
277 fBot.button("Yes").click();
278 fBot.button("Close").click();
279 xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
280 assertEquals("", xmlPart);
281 }
282
283 private static String extractTestXml(File xmlFile, String definitionName) throws IOException, FileNotFoundException {
284 StringBuilder xmlPart = new StringBuilder();
285 boolean started = false;
286 try (RandomAccessFile raf = new RandomAccessFile(xmlFile, "r");) {
287 String s = raf.readLine();
288 while (s != null) {
289 if (s.equals("<Definition name=\"" + definitionName + "\">")) {
290 started = true;
291 }
292 if (started) {
293 if (s.equals("</Definition>")) {
294 break;
295 }
296 xmlPart.append(s);
297 xmlPart.append('\n');
298 }
299 s = raf.readLine();
300 }
301 }
302 return xmlPart.toString();
303 }
304 }
This page took 0.037272 seconds and 5 git commands to generate.