Commit | Line | Data |
---|---|---|
723a1d49 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 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 | ||
10 | package org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests; | |
11 | ||
12 | import static org.junit.Assert.assertEquals; | |
13 | import static org.junit.Assert.fail; | |
14 | ||
15 | import java.io.IOException; | |
16 | ||
17 | import org.eclipse.core.runtime.FileLocator; | |
18 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; | |
19 | import org.eclipse.core.runtime.preferences.InstanceScope; | |
20 | import org.eclipse.osgi.util.NLS; | |
21 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
22 | import org.eclipse.swtbot.swt.finder.SWTBot; | |
23 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
24 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; | |
25 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; | |
26 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton; | |
27 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; | |
28 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeColumn; | |
29 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
30 | import org.eclipse.tracecompass.internal.tmf.core.Activator; | |
31 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; | |
32 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimePreferencesConstants; | |
33 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; | |
34 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.SWTBotTestCondition; | |
35 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
36 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils; | |
37 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; | |
38 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; | |
39 | import org.junit.Before; | |
40 | import org.junit.Test; | |
41 | import org.junit.runner.RunWith; | |
42 | ||
43 | /** | |
44 | * SWTBot tests for column sorting in the Control Flow view. | |
45 | * | |
46 | * @author Bernd Hufmann | |
47 | */ | |
48 | @SuppressWarnings("restriction") | |
49 | @RunWith(SWTBotJunit4ClassRunner.class) | |
50 | public class ControlFlowViewSortingTest extends KernelTestBase { | |
51 | ||
52 | // ------------------------------------------------------------------------ | |
53 | // Constants | |
54 | // ------------------------------------------------------------------------ | |
55 | private static final String FILTER_ACTION = "Show View Filters"; | |
56 | private static final String FILTER_DIALOG_TITLE = "Filter"; | |
57 | private static final String UNCHECK_ALL = "Uncheck all"; | |
58 | private static final String CHECK_SUBTREE = "Check subtree"; | |
59 | private static final String OK_BUTTON = "OK"; | |
60 | ||
61 | private static final String PROCESS_COLUMN = "Process"; | |
62 | private static final int PROCESS_COLUMN_ID = 0; | |
63 | private static final String TID_COLUMN = "TID"; | |
64 | private static final int TID_COLUMN_ID = 1; | |
65 | private static final String PTID_COLUMN = "PTID"; | |
66 | private static final String BIRTH_COLUMN = "Birth time"; | |
67 | private static final int BIRTH_COLUMN_ID = 3; | |
68 | private static final String TRACE_COLUMN = "Trace"; | |
69 | ||
70 | private static final String SYSTEMD_PROCESS_NAME = "systemd"; | |
71 | private static final long SYSTEMD_BIRTHTIME = 1361214078967531336L; | |
72 | private static final String SYSTEMD_TID = "1"; | |
73 | ||
74 | private static final String KTHREAD_PROCESS_NAME = "kthreadd"; | |
75 | private static final long KTHREAD_BIRTHTIME = 1361214078967533536L; | |
76 | private static final String KTHREAD_TID = "2"; | |
77 | ||
78 | private static final String LTTNG_CONSUMER_PROCESS_NAME = "lttng-consumerd"; | |
79 | private static final long LTTNG_CONSUMER_BIRTHTIME = 1361214078963717040L; | |
80 | private static final String LTTNG_CONSUMER_TID = "4034"; | |
81 | ||
82 | // ------------------------------------------------------------------------ | |
83 | // Attributes | |
84 | // ------------------------------------------------------------------------ | |
85 | ||
86 | private SWTBotView fViewBot; | |
87 | ||
88 | // ------------------------------------------------------------------------ | |
89 | // Housekeeping | |
90 | // ------------------------------------------------------------------------ | |
91 | /** | |
92 | * Before Test | |
93 | */ | |
94 | @Override | |
95 | @Before | |
96 | public void before() { | |
97 | ||
98 | try { | |
99 | IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID); | |
100 | defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, "GMT-05:00"); | |
101 | TmfTimestampFormat.updateDefaultFormats(); | |
102 | ||
103 | String tracePath = FileLocator.toFileURL(CtfTestTrace.SYNC_DEST.getTraceURL()).getPath(); | |
104 | SWTBotUtils.openTrace(TRACE_PROJECT_NAME, tracePath, KERNEL_TRACE_TYPE); | |
105 | fViewBot = fBot.viewByTitle("Control Flow"); | |
106 | fViewBot.show(); | |
107 | fViewBot.setFocus(); | |
108 | } catch (IOException e) { | |
109 | fail(); | |
110 | } | |
111 | } | |
112 | ||
113 | @Override | |
114 | public void after() { | |
115 | IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID); | |
116 | defaultPreferences.put(ITmfTimePreferencesConstants.TIME_ZONE, "Local Time"); | |
117 | TmfTimestampFormat.updateDefaultFormats(); | |
118 | super.after(); | |
119 | } | |
120 | ||
121 | // ------------------------------------------------------------------------ | |
122 | // Test case(s) | |
123 | // ------------------------------------------------------------------------ | |
124 | /** | |
125 | * UI test of sorting of processes in CFV based on column selection. To verify that the sorting | |
126 | * was executed correctly, the test will use bot.waitUntil() the column content has the right | |
127 | * order. | |
128 | */ | |
129 | @Test | |
130 | public void testColumnSorting() { | |
131 | // Create a known state | |
132 | applyFilter(); | |
133 | final SWTBotTree tree = fViewBot.bot().tree(); | |
134 | SWTBotTreeItem item = SWTBotUtils.getTreeItem(fBot, tree, SYSTEMD_PROCESS_NAME); | |
135 | item.select(); | |
136 | ||
137 | testProcessSorting(tree); | |
138 | testTidSorting(tree); | |
139 | testPidSorting(tree); | |
140 | testBirthtimeSorting(tree); | |
141 | testTraceSorting(tree); | |
142 | } | |
143 | ||
144 | // ------------------------------------------------------------------------ | |
145 | // Helpers | |
146 | // ------------------------------------------------------------------------ | |
147 | private void applyFilter() { | |
148 | // Select only certain root nodes and their children but filter out rest | |
149 | SWTBotToolbarButton filterButton = fViewBot.toolbarButton(FILTER_ACTION); | |
150 | filterButton.click(); | |
151 | SWTBotShell shell = fBot.shell(FILTER_DIALOG_TITLE).activate(); | |
152 | ||
153 | SWTBot bot = shell.bot(); | |
154 | SWTBotTree treeBot = bot.tree(); | |
155 | bot.button(UNCHECK_ALL).click(); | |
156 | ||
157 | TreeCheckedCounter treeCheckCounter = new TreeCheckedCounter(treeBot); | |
158 | Integer checked = UIThreadRunnable.syncExec(treeCheckCounter); | |
159 | assertEquals("default", 0, checked.intValue()); | |
160 | ||
161 | // select root nodes and their children | |
d1a3f3fb BH |
162 | checkFilterTreeItems(bot, treeBot, SYSTEMD_PROCESS_NAME); |
163 | checkFilterTreeItems(bot, treeBot, KTHREAD_PROCESS_NAME); | |
164 | checkFilterTreeItems(bot, treeBot, LTTNG_CONSUMER_PROCESS_NAME); | |
723a1d49 BH |
165 | |
166 | bot.button(OK_BUTTON).click(); | |
167 | } | |
168 | ||
d1a3f3fb | 169 | private static void checkFilterTreeItems(SWTBot bot, SWTBotTree treeBot, String process) { |
723a1d49 BH |
170 | SWTBotTreeItem item = SWTBotUtils.getTreeItem(bot, treeBot, process); |
171 | item.select(); | |
172 | bot.button(CHECK_SUBTREE).click(); | |
173 | TreeCheckedCounter treeCheckCounter = new TreeCheckedCounter(treeBot); | |
d1a3f3fb | 174 | UIThreadRunnable.syncExec(treeCheckCounter); |
723a1d49 BH |
175 | } |
176 | ||
177 | private static void testProcessSorting(final SWTBotTree tree) { | |
178 | SWTBotTreeColumn column = tree.header(PROCESS_COLUMN); | |
179 | String[] expected = { KTHREAD_PROCESS_NAME, LTTNG_CONSUMER_PROCESS_NAME, SYSTEMD_PROCESS_NAME }; | |
180 | ||
181 | /* Sort direction Up */ | |
182 | SWTBotTestCondition condition = getSortCondition(PROCESS_COLUMN, PROCESS_COLUMN_ID, expected, tree, false); | |
183 | clickColumn(tree, column, condition); | |
184 | ||
185 | /* Sort direction Down */ | |
186 | condition = getSortCondition(PROCESS_COLUMN, 0, expected, tree, true); | |
187 | clickColumn(tree, column, condition); | |
188 | } | |
189 | ||
190 | private static void testTidSorting(final SWTBotTree tree) { | |
191 | String[] expected = { SYSTEMD_TID, KTHREAD_TID, LTTNG_CONSUMER_TID }; | |
192 | SWTBotTreeColumn column = tree.header(TID_COLUMN); | |
193 | /* Sort direction Up */ | |
194 | SWTBotTestCondition condition = getSortCondition(TID_COLUMN, TID_COLUMN_ID, expected, tree, false); | |
195 | clickColumn(tree, column, condition); | |
196 | ||
197 | /* Sort direction Down */ | |
198 | condition = getSortCondition(TID_COLUMN, TID_COLUMN_ID, expected, tree, true); | |
199 | clickColumn(tree, column, condition); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Note: In this test systemd and kthreadd have PTID 0 where | |
204 | * lttng-consumerd has PTID -1 that is unknown. Currently | |
205 | * in CFV PTID is only shown when it's greater than 0. | |
206 | */ | |
207 | private static void testPidSorting(final SWTBotTree tree) { | |
208 | SWTBotTreeColumn column = tree.header(PTID_COLUMN); | |
209 | String[] expected = { LTTNG_CONSUMER_PROCESS_NAME, SYSTEMD_PROCESS_NAME, KTHREAD_PROCESS_NAME }; | |
210 | /* Sort direction Up */ | |
211 | SWTBotTestCondition condition = getSortCondition(PTID_COLUMN, PROCESS_COLUMN_ID, expected, tree, false); | |
212 | clickColumn(tree, column, condition); | |
213 | ||
214 | /* Sort direction Down */ | |
215 | String[] expected2 = { SYSTEMD_PROCESS_NAME, KTHREAD_PROCESS_NAME, LTTNG_CONSUMER_PROCESS_NAME }; | |
216 | condition = getSortCondition(PTID_COLUMN, PROCESS_COLUMN_ID, expected2, tree, false); | |
217 | clickColumn(tree, column, condition); | |
218 | } | |
219 | ||
220 | private static void testBirthtimeSorting(final SWTBotTree tree) { | |
221 | SWTBotTreeColumn column = tree.header(BIRTH_COLUMN); | |
222 | String[] expected = { | |
223 | Utils.formatTime(LTTNG_CONSUMER_BIRTHTIME, TimeFormat.CALENDAR, Resolution.NANOSEC), | |
224 | Utils.formatTime(SYSTEMD_BIRTHTIME, TimeFormat.CALENDAR, Resolution.NANOSEC), | |
225 | Utils.formatTime(KTHREAD_BIRTHTIME, TimeFormat.CALENDAR, Resolution.NANOSEC) }; | |
226 | ||
227 | /* Sort direction Up */ | |
228 | SWTBotTestCondition condition = getSortCondition(BIRTH_COLUMN, BIRTH_COLUMN_ID, expected, tree, false); | |
229 | clickColumn(tree, column, condition); | |
230 | ||
231 | /* Sort direction Down */ | |
232 | condition = getSortCondition(BIRTH_COLUMN, BIRTH_COLUMN_ID, expected, tree, true); | |
233 | clickColumn(tree, column, condition); | |
234 | } | |
235 | ||
236 | /* | |
237 | * Note: In this test only one trace is visualized in CFV. | |
238 | * Sorting is done based on birth time (up direction). | |
239 | * The implementation in CFV won't change the order when changing | |
240 | * the direction. | |
241 | */ | |
242 | private static void testTraceSorting(final SWTBotTree tree) { | |
243 | SWTBotTreeColumn column = tree.header(TRACE_COLUMN); | |
244 | String[] expected = { LTTNG_CONSUMER_PROCESS_NAME, SYSTEMD_PROCESS_NAME, KTHREAD_PROCESS_NAME }; | |
245 | ||
246 | /* Sort direction Up */ | |
247 | SWTBotTestCondition condition = getSortCondition(TRACE_COLUMN, PROCESS_COLUMN_ID, expected, tree, false); | |
248 | clickColumn(tree, column, condition); | |
249 | ||
250 | /* Sort direction Down */ | |
251 | condition = getSortCondition(TRACE_COLUMN, PROCESS_COLUMN_ID, expected, tree, false); | |
252 | clickColumn(tree, column, condition); | |
253 | } | |
254 | ||
255 | private static void clickColumn(final SWTBotTree tree, SWTBotTreeColumn processColumn, SWTBotTestCondition condition) { | |
256 | processColumn.click(); | |
257 | fBot.waitUntil(condition); | |
258 | } | |
259 | ||
260 | private static SWTBotTestCondition getSortCondition(final String testCase, final int cell, final String[] expected, final SWTBotTree tree, final boolean reverse) { | |
261 | return new SWTBotTestCondition() { | |
262 | @Override | |
263 | public boolean test() throws Exception { | |
264 | SWTBotTreeItem[] items = tree.getAllItems(); | |
265 | if (reverse) { | |
266 | for (int i = expected.length - 1; i > 0; i--) { | |
267 | if (!expected[i].equals(items[expected.length - (i + 1)].cell(cell))) { | |
268 | return false; | |
269 | } | |
270 | } | |
271 | } else { | |
272 | for (int i = 0; i < expected.length; i++) { | |
273 | if (!expected[i].equals(items[i].cell(cell))) { | |
274 | return false; | |
275 | } | |
276 | } | |
277 | } | |
278 | return true; | |
279 | } | |
280 | @Override | |
281 | public String getFailureMessage() { | |
282 | return NLS.bind("Test Case: \'{0}\' failed!", testCase); | |
283 | } | |
284 | }; | |
285 | } | |
286 | ||
287 | } |