Commit | Line | Data |
---|---|---|
34067e9a PT |
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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events; | |
14 | ||
15 | import static org.junit.Assert.assertArrayEquals; | |
16 | import static org.junit.Assert.fail; | |
17 | import static org.junit.Assume.assumeTrue; | |
18 | ||
19 | import java.io.File; | |
20 | import java.io.IOException; | |
21 | import java.net.URI; | |
22 | import java.net.URISyntaxException; | |
23 | import java.net.URL; | |
24 | import java.util.ArrayList; | |
25 | import java.util.List; | |
26 | ||
27 | import org.apache.log4j.ConsoleAppender; | |
28 | import org.apache.log4j.Logger; | |
29 | import org.apache.log4j.SimpleLayout; | |
30 | import org.eclipse.core.runtime.FileLocator; | |
31 | import org.eclipse.core.runtime.Path; | |
32 | import org.eclipse.swt.widgets.Table; | |
33 | import org.eclipse.swt.widgets.TableColumn; | |
34 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
35 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; | |
36 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
37 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; | |
38 | import org.eclipse.swtbot.swt.finder.results.ArrayResult; | |
39 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
40 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; | |
41 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableColumn; | |
42 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; | |
43 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
f0beeb4a | 44 | import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils; |
34067e9a PT |
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 | * SWTBot test for event table column header menu. | |
54 | */ | |
55 | @RunWith(SWTBotJunit4ClassRunner.class) | |
56 | public class ColumnHeaderMenuTest { | |
57 | ||
58 | private static final String TRACE_PROJECT_NAME = "test"; | |
59 | private static final String COLUMN_TRACE = "syslog_collapse"; | |
60 | private static final String COLUMN_TRACE_PATH = "testfiles/" + COLUMN_TRACE; | |
61 | private static final String COLUMN_TRACE_TYPE = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog"; | |
62 | ||
63 | private static File fTestFile = null; | |
64 | ||
65 | private static SWTWorkbenchBot fBot; | |
66 | private SWTBotEditor fEditorBot; | |
67 | ||
68 | /** The Log4j logger instance. */ | |
69 | private static final Logger fLogger = Logger.getRootLogger(); | |
70 | ||
71 | /** | |
72 | * Test Class setup | |
73 | */ | |
74 | @BeforeClass | |
75 | public static void beforeClass() { | |
5785ab49 | 76 | SWTBotUtils.initialize(); |
34067e9a PT |
77 | |
78 | /* set up test trace */ | |
79 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(COLUMN_TRACE_PATH), null); | |
80 | URI uri; | |
81 | try { | |
82 | uri = FileLocator.toFileURL(location).toURI(); | |
83 | fTestFile = new File(uri); | |
84 | } catch (URISyntaxException | IOException e) { | |
85 | fail(e.getMessage()); | |
86 | } | |
87 | ||
88 | assumeTrue(fTestFile.exists()); | |
89 | ||
90 | /* Set up for swtbot */ | |
91 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ | |
92 | fLogger.removeAllAppenders(); | |
93 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); | |
94 | fBot = new SWTWorkbenchBot(); | |
95 | ||
96 | /* Close welcome view */ | |
97 | SWTBotUtils.closeView("Welcome", fBot); | |
98 | ||
99 | /* Switch perspectives */ | |
100 | SWTBotUtils.switchToTracingPerspective(); | |
101 | ||
102 | /* Finish waiting for eclipse to load */ | |
f0beeb4a | 103 | WaitUtils.waitForJobs(); |
34067e9a PT |
104 | |
105 | SWTBotUtils.createProject(TRACE_PROJECT_NAME); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Test class tear down method. | |
110 | */ | |
111 | @AfterClass | |
112 | public static void afterClass() { | |
113 | SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot); | |
114 | fLogger.removeAllAppenders(); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Before Test | |
119 | */ | |
120 | @Before | |
121 | public void before() { | |
122 | SWTBotUtils.openTrace(TRACE_PROJECT_NAME, fTestFile.getAbsolutePath(), COLUMN_TRACE_TYPE); | |
123 | fEditorBot = SWTBotUtils.activateEditor(fBot, fTestFile.getName()); | |
124 | } | |
125 | ||
126 | /** | |
127 | * After Test | |
128 | */ | |
129 | @After | |
130 | public void after() { | |
131 | fBot.closeAllEditors(); | |
1dfcd42b | 132 | SWTBotUtils.closeSecondaryShells(fBot); |
34067e9a PT |
133 | } |
134 | ||
135 | /** | |
136 | * Test the check menu items to toggle column visibility | |
137 | */ | |
138 | @Test | |
139 | public void testToggleColumns() { | |
140 | final SWTBotTable tableBot = fEditorBot.bot().table(); | |
141 | SWTBotTableColumn headerBot = tableBot.header(""); | |
142 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line", "Message" }); | |
143 | ||
144 | headerBot.contextMenu("Message").click(); | |
145 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line" }); | |
146 | ||
147 | headerBot.contextMenu("Line").click(); | |
148 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File" }); | |
149 | ||
150 | headerBot.contextMenu("File").click(); | |
151 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger" }); | |
152 | ||
153 | headerBot.contextMenu("Logger").click(); | |
154 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host" }); | |
155 | ||
156 | headerBot.contextMenu("Host").click(); | |
157 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp" }); | |
158 | ||
159 | headerBot.contextMenu("Timestamp").click(); | |
d056d9a9 | 160 | assertVisibleColumns(tableBot.widget, new String[] {}); |
34067e9a PT |
161 | |
162 | headerBot.contextMenu("Message").click(); | |
163 | assertVisibleColumns(tableBot.widget, new String[] { "Message" }); | |
164 | ||
165 | headerBot.contextMenu("Timestamp").click(); | |
166 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Message" }); | |
167 | ||
168 | headerBot.contextMenu("Line").click(); | |
169 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Line", "Message" }); | |
170 | ||
171 | headerBot.contextMenu("Host").click(); | |
172 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Line", "Message" }); | |
173 | ||
174 | headerBot.contextMenu("File").click(); | |
175 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "File", "Line", "Message" }); | |
176 | ||
177 | headerBot.contextMenu("Logger").click(); | |
178 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line", "Message" }); | |
179 | } | |
180 | ||
d056d9a9 MK |
181 | /** |
182 | * Test the Show All menu item | |
183 | */ | |
184 | @Test | |
185 | public void testPersistHiding() { | |
186 | SWTBotTable tableBot = fEditorBot.bot().table(); | |
187 | SWTBotTableColumn headerBot = tableBot.header(""); | |
188 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line", "Message" }); | |
189 | ||
190 | headerBot.contextMenu("Timestamp").click(); | |
191 | headerBot.contextMenu("Host").click(); | |
192 | headerBot.contextMenu("Logger").click(); | |
193 | headerBot.contextMenu("File").click(); | |
194 | headerBot.contextMenu("Message").click(); | |
195 | assertVisibleColumns(tableBot.widget, new String[] { "Line" }); | |
196 | after(); | |
197 | ||
198 | before(); | |
199 | tableBot = fEditorBot.bot().table(); | |
200 | assertVisibleColumns(tableBot.widget, new String[] { "Line" }); | |
201 | headerBot = tableBot.header(""); | |
202 | headerBot.contextMenu("Show All").click(); | |
203 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line", "Message" }); | |
204 | } | |
205 | ||
34067e9a PT |
206 | /** |
207 | * Test the Show All menu item | |
208 | */ | |
209 | @Test | |
210 | public void testShowAll() { | |
211 | final SWTBotTable tableBot = fEditorBot.bot().table(); | |
212 | SWTBotTableColumn headerBot = tableBot.header(""); | |
213 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line", "Message" }); | |
214 | ||
215 | headerBot.contextMenu("Timestamp").click(); | |
216 | headerBot.contextMenu("Host").click(); | |
217 | headerBot.contextMenu("Logger").click(); | |
218 | headerBot.contextMenu("File").click(); | |
219 | headerBot.contextMenu("Line").click(); | |
220 | headerBot.contextMenu("Message").click(); | |
d056d9a9 | 221 | assertVisibleColumns(tableBot.widget, new String[] {}); |
34067e9a | 222 | |
e33ad2c1 | 223 | headerBot.contextMenu("Show All").click(); |
34067e9a PT |
224 | assertVisibleColumns(tableBot.widget, new String[] { "Timestamp", "Host", "Logger", "File", "Line", "Message" }); |
225 | } | |
226 | ||
227 | private static void assertVisibleColumns(final Table table, String[] expected) { | |
228 | String[] actual = UIThreadRunnable.syncExec(new ArrayResult<String>() { | |
229 | @Override | |
230 | public String[] run() { | |
231 | List<String> visible = new ArrayList<>(); | |
232 | for (int i : table.getColumnOrder()) { | |
233 | TableColumn column = table.getColumns()[i]; | |
234 | if (column.getResizable() && column.getWidth() > 0) { | |
235 | visible.add(column.getText()); | |
236 | } | |
237 | } | |
238 | return visible.toArray(new String[0]); | |
239 | } | |
240 | }); | |
241 | assertArrayEquals(expected, actual); | |
242 | } | |
243 | } |