/
TraceTypePreferencePageTest.java
345 lines (308 loc) · 13.6 KB
/
TraceTypePreferencePageTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*******************************************************************************
* Copyright (c) 2017, 2018 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.tmf.ui.swtbot.tests.tracetype.preferences;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCCombo;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
import org.eclipse.tracecompass.tmf.ui.views.filter.FilterView;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* SWTBot test for the trace type preference page
*
* @author Jean-Christian Kouame
*
*/
@RunWith(SWTBotJunit4ClassRunner.class)
public class TraceTypePreferencePageTest {
private static final @NonNull String FILTER =
"<ROOT>"
+ "<FILTER name=\"test\">"
+ "<TRACETYPE name=\"Custom XML : testxmlextension\" type=\"custom.xml.trace:Custom XML:testxmlextension\">"
+ "<MATCHES eventaspect=\"Message\" not=\"false\" regex=\"msg\" tracetypeid=\"custom.xml.trace:Custom XML:testxmlextension\"/>"
+ "</TRACETYPE>"
+ "</FILTER>"
+ "</ROOT>";
private static final @NonNull String CHECK_SELECTED = "Check selected";
private static final @NonNull String CHECK_ALL = "Check all";
private static final @NonNull String UNCHECK_SELECTED = "Uncheck selected";
private static final @NonNull String UNCHECK_ALL = "Uncheck all";
private static final String TRACE_PATH = "testfiles/syslog_collapse";
/** LTTng kernel trace type */
protected static final String KERNEL_TRACE_TYPE = "org.eclipse.linuxtools.lttng2.kernel.tracetype";
/** LTTng kernel perspective */
protected static final String KERNEL_PERSPECTIVE_ID = "org.eclipse.linuxtools.lttng2.kernel.ui.perspective";
/** Default project name */
protected static final String TRACE_PROJECT_NAME = "test";
private static SWTWorkbenchBot fBot;
/**
* Before Class
*/
@BeforeClass
public static void beforeClass() {
SWTBotUtils.initialize();
/* set up for swtbot */
SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
fBot = new SWTWorkbenchBot();
/* Create the trace project */
SWTBotUtils.createProject(TRACE_PROJECT_NAME);
/* Finish waiting for eclipse to load */
WaitUtils.waitForJobs();
/* set up test trace */
setUpTrace();
}
private static void setUpTrace() {
URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TRACE_PATH), null);
URI uri;
try {
uri = FileLocator.toFileURL(location).toURI();
File testFile = new File(uri);
SWTBotUtils.openTrace(TRACE_PROJECT_NAME, testFile.getAbsolutePath(), KERNEL_TRACE_TYPE);
assertNotNull(testFile);
assumeTrue(testFile.exists());
} catch (URISyntaxException | IOException e) {
fail("Failed to open the trace");
}
}
/**
* Before method to reset the preference values
*/
@Before
public void before() {
setTraceTypePreferences(CHECK_ALL);
}
/**
* After Class
*/
@AfterClass
public static void afterClass() {
setTraceTypePreferences(CHECK_ALL);
SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
fBot.closeAllEditors();
}
/**
* Test the filter
*/
@Test
public void testPreferencePage() {
SWTBot bot = openTraceTypePreferences().bot();
SWTBotTree treeBot = bot.tree(1);
//get default count
bot.button(CHECK_ALL).click();
int defaultCount = SWTBotUtils.getTreeCheckedItemCount(treeBot);
int defaultVisibleRows = treeBot.visibleRowCount();
// test "uncheck all button"
bot.button(UNCHECK_ALL).click();
int checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals(0, checked);
// test check all
bot.button(CHECK_ALL).click();
checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals(CHECK_ALL, defaultCount, checked);
// test check selected
treeBot.getTreeItem("Custom XML").select();
bot.button(UNCHECK_ALL).click();
bot.button(CHECK_SELECTED).click();
checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals(CHECK_SELECTED, 2, checked);
// test uncheck selected
bot.button(CHECK_ALL).click();
bot.button(UNCHECK_SELECTED).click();
checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals(UNCHECK_SELECTED, defaultCount - 2, checked);
// test filter
bot.button(UNCHECK_ALL).click();
checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals(0, checked);
bot.text(1).setText("Custom XML");
SWTBotUtils.waitUntil(tree -> tree.visibleRowCount() == 2, treeBot, "Visible row count: Default expected 2, but actual value is " + treeBot.visibleRowCount());
assertEquals("Filtered no checked", 0, checked);
bot.button(CHECK_ALL).click();
checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals("Filtered check all", 2, checked);
bot.text(1).setText("");
SWTBotUtils.waitUntil(tree -> tree.visibleRowCount() == defaultVisibleRows + 1, treeBot, "Visible row count: Default expected " + defaultCount +", but actual value is " + treeBot.visibleRowCount());
bot.button(CHECK_ALL).click();
checked = SWTBotUtils.getTreeCheckedItemCount(treeBot);
assertEquals("Filtered removed all check", defaultCount, checked);
bot.button("Apply").click();
SWTBotUtils.pressOKishButtonInPreferences(fBot);
}
/**
* Test the select trace type menu
*/
@Test
public void testSelectTraceType() {
List<String> menuItems = getSelectTraceTypeMenuItems();
List<String> categories = Arrays.asList(
"Common Trace Format",
"Custom Text",
"Custom XML",
"Test trace",
"",
"Manage Custom Parsers...");
assertEquals("Test all categories enabled", categories, menuItems);
//Change the preference values and test the 'Select Trace Type...' options
setTraceTypePreferences(UNCHECK_ALL, "Custom Text");
menuItems = getSelectTraceTypeMenuItems();
categories = Arrays.asList(
"Custom Text",
"Test trace",
"",
"Manage Custom Parsers...");
assertEquals("Test 1 category enabled", categories, menuItems);
setTraceTypePreferences(CHECK_ALL);
}
/**
* Test the import trace's trace type combo
*/
@Test
public void testImportTraceTypeOptions() {
int defaultCount = TmfTraceType.getAvailableTraceTypes().length;
setTraceTypePreferences(UNCHECK_ALL, "Custom Text", "testtxtextension");
String[] traceTypeComboItems = getTraceTypeComboItems();
String[] availableTraceTypes = new String[] {
"<Automatic Detection>",
"Custom Text : testtxtextension"
};
assertArrayEquals("Test one trace type enabled", availableTraceTypes, traceTypeComboItems);
setTraceTypePreferences(CHECK_ALL);
traceTypeComboItems = getTraceTypeComboItems();
assertEquals("Test all trace type enabled", defaultCount + 1, traceTypeComboItems.length);
}
/**
* Test enabled/disabled trace types behavior in the filters view
*/
@Test
public void testFiltersView() {
int defaultCount = TmfTraceType.getAvailableTraceTypes().length;
SWTBotUtils.openView(FilterView.ID);
SWTBotView viewBot = fBot.viewById(FilterView.ID);
importFilter(viewBot);
SWTBot bot = viewBot.bot();
SWTBotTree tree = bot.tree(0);
SWTBotTreeItem item = tree.expandNode("FILTER test", "WITH TRACETYPE Custom XML : testxmlextension");
item.select();
SWTBotCCombo comboBox = bot.ccomboBox(0);
assertEquals("Combo: number of trace types", defaultCount, comboBox.itemCount());
// Change the preference value for testxmlextension
setTraceTypePreferences(UNCHECK_ALL, "Custom XML", "testxmlextension");
// Change node to refresh the tree
item = tree.expandNode("FILTER test");
item.select();
item = tree.expandNode("FILTER test", "WITH TRACETYPE Custom XML : testxmlextension");
item.select();
comboBox = bot.ccomboBox(0);
assertEquals("Combo: number of trace types", 1, comboBox.itemCount());
setTraceTypePreferences(UNCHECK_ALL, "Custom Text", "testtxtextension");
// Change node to refresh the tree
item = tree.expandNode("FILTER test");
item.select();
item = tree.expandNode("FILTER test", "WITH TRACETYPE Custom XML : testxmlextension");
item.select();
comboBox = bot.ccomboBox(0);
assertEquals("Combo: number of trace types", 2, comboBox.itemCount());
}
private static void importFilter(SWTBotView viewBot) {
try {
File tempFile = File.createTempFile("test", ".xml");
FileWriter fw = new FileWriter(tempFile);
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(FILTER);
}
TmfFileDialogFactory.setOverrideFiles(tempFile.getAbsolutePath());
} catch (IOException e) {
fail("Failed to add a filter");
}
viewBot.toolbarButton("Import filters").click();
SWTBotUtils.waitUntil(tree -> tree.rowCount() > 0, viewBot.bot().tree(0), "Failed to import test filter");
}
private static void setTraceTypePreferences(@NonNull String button, @NonNull String... pathToCheck) {
SWTBot bot = openTraceTypePreferences().bot();
SWTBotTree treeBot = bot.tree(1);
if (!button.isEmpty()) {
bot.button(button).click();
}
if (pathToCheck.length > 0) {
SWTBotTreeItem treeItem = treeBot.expandNode(pathToCheck);
assertNotNull("Tree item not null", treeItem);
treeItem.select();
bot.button(CHECK_SELECTED).click();
}
bot.button("Apply").click();
SWTBotUtils.pressOKishButtonInPreferences(fBot);
}
private static List<String> getSelectTraceTypeMenuItems() {
SWTBotTreeItem tracesFolder = SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME);
tracesFolder.expand();
SWTBotTreeItem trace = tracesFolder.getNode("syslog_collapse");
trace.select();
List<String> menuItems = trace.contextMenu().menu("Select Trace Type...").menuItems();
return menuItems;
}
private static SWTBotShell openTraceTypePreferences() {
SWTBotShell preferencesShell = SWTBotUtils.openPreferences(fBot);
SWTBot bot = preferencesShell.bot();
SWTBotTree tree = bot.tree(0);
SWTBotTreeItem treeNode = tree.getTreeItem("Tracing");
treeNode.select();
treeNode.expand();
bot.waitUntil(ConditionHelpers.isTreeChildNodeAvailable("Trace Types", treeNode));
treeNode = treeNode.getNode("Trace Types");
treeNode.select();
return preferencesShell;
}
private static String[] getTraceTypeComboItems() {
SWTBotTreeItem tracesFolder = SWTBotUtils.selectTracesFolder(fBot, TRACE_PROJECT_NAME);
SWTBotMenu menu = tracesFolder.contextMenu().menu("Import...");
menu.click();
SWTBot bot = fBot.shell("Trace Import").activate().bot();
SWTBotCombo combo = bot.comboBox(2);
String[] items = combo.items();
bot.button("Cancel").click();
return items;
}
}