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