tmf: Fix StandardImportGzipTraceTest failing when running from RunAllSWTBotTests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / wizards / StandardImportGzipTraceTest.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 * Matthew Khouzam - Initial API and implementation.
11 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.wizards;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.zip.GZIPOutputStream;
25
26 import org.apache.log4j.Logger;
27 import org.apache.log4j.varia.NullAppender;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.wizard.Wizard;
30 import org.eclipse.jface.wizard.WizardDialog;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
33 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
34 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
35 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
36 import org.eclipse.swtbot.swt.finder.results.VoidResult;
37 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
38 import org.eclipse.swtbot.swt.finder.waits.Conditions;
39 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
40 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
41 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo;
42 import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio;
43 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
44 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
45 import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
46 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
48 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
49 import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages;
50 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
51 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
52 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
53 import org.eclipse.ui.IPageLayout;
54 import org.eclipse.ui.IWorkbench;
55 import org.eclipse.ui.IWorkbenchWindow;
56 import org.eclipse.ui.PlatformUI;
57 import org.junit.After;
58 import org.junit.AfterClass;
59 import org.junit.Before;
60 import org.junit.BeforeClass;
61 import org.junit.Test;
62 import org.junit.runner.RunWith;
63
64 /**
65 *
66 * Import operation for gz traces
67 *
68 */
69 @RunWith(SWTBotJunit4ClassRunner.class)
70 public class StandardImportGzipTraceTest {
71 private static final String ROOT_FOLDER = "/";
72 private static final String PROJECT_NAME = "Tracing";
73 private static File fGzipTrace;
74 private Wizard fWizard;
75 private SWTWorkbenchBot fBot;
76
77 /** The Log4j logger instance. */
78 protected static final Logger fLogger = Logger.getRootLogger();
79
80 /**
81 * create a gzip file
82 */
83 @BeforeClass
84 public static void init() {
85 SWTBotUtils.failIfUIThread();
86 zipTrace();
87 fLogger.removeAllAppenders();
88 fLogger.addAppender(new NullAppender());
89 }
90
91 /**
92 * create the project
93 */
94 @Before
95 public void setup() {
96 createProject();
97 SWTBotPreferences.TIMEOUT = 20000;
98 }
99
100 /**
101 * cleanup
102 */
103 @AfterClass
104 public static void destroy() {
105 fLogger.removeAllAppenders();
106 }
107
108 /**
109 * Tear down the test
110 */
111 @After
112 public void tearDown() {
113 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
114 }
115
116 private void createProject() {
117 fBot = new SWTWorkbenchBot();
118 /* Close welcome view */
119 SWTBotUtils.closeView("Welcome", fBot);
120
121 SWTBotUtils.createProject(PROJECT_NAME);
122 }
123
124 /**
125 * Import a gzip trace
126 */
127 @Test
128 public void testGzipImport() {
129 final String traceType = "Test trace : TMF Tests";
130 final String tracesNode = "Traces [1]";
131
132 /*
133 * Actual importing
134 */
135 openImportWizard();
136 selectImportFromArchive(fGzipTrace.getAbsolutePath());
137 selectFolder(ROOT_FOLDER);
138 SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace);
139 assertFalse(checkBox.isEnabled());
140 SWTBotCombo comboBox = fBot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType);
141 comboBox.setSelection(traceType);
142 importFinish();
143 /*
144 * Remove .gz extension
145 */
146 assertNotNull(fGzipTrace);
147 String name = fGzipTrace.getName();
148 assertNotNull(name);
149 assertTrue(name.length() > 3);
150 String traceName = name.substring(0, name.length() - 3);
151 assertNotNull(traceName);
152 assertFalse(traceName.isEmpty());
153
154 /*
155 * Open trace
156 */
157 SWTBotView projectExplorer = fBot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
158 projectExplorer.setFocus();
159 final SWTBotTree tree = projectExplorer.bot().tree();
160 /*
161 * This appears to be problematic due to the length of the file name and
162 * the resolution in our CI.
163 */
164 tree.expandNode(PROJECT_NAME, true);
165 SWTBotTreeItem treeItem = tree.getTreeItem(PROJECT_NAME);
166 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(tracesNode, treeItem));
167 treeItem = treeItem.getNode(tracesNode);
168 fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(traceName, treeItem));
169 treeItem = treeItem.getNode(traceName);
170 treeItem.doubleClick();
171 SWTBotUtils.waitForJobs();
172 /*
173 * Check results
174 */
175 SWTBotTable editor = fBot.activeEditor().bot().table();
176 String c22 = editor.cell(2, 2);
177 String c10 = editor.cell(1, 0);
178 assertEquals("Type-1", c22);
179 assertEquals("", c10);
180 }
181
182 private static void zipTrace() {
183 try {
184 fGzipTrace = File.createTempFile("trace", ".gz");
185 byte[] buffer = new byte[1024];
186
187 try (GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(fGzipTrace));) {
188
189 try (FileInputStream in = new FileInputStream(TmfTestTrace.A_TEST_10K2.getFullPath());) {
190
191 int len;
192 while ((len = in.read(buffer)) > 0) {
193 gzos.write(buffer, 0, len);
194 }
195 }
196 gzos.finish();
197 }
198 } catch (IOException e) {
199 fail(e.getMessage());
200 }
201 }
202
203 private void selectImportFromArchive(String archivePath) {
204 SWTBotRadio button = fBot.radio("Select &archive file:");
205 button.click();
206
207 SWTBotCombo sourceCombo = fBot.comboBox(1);
208
209 sourceCombo.setText(new File(archivePath).getAbsolutePath());
210
211 SWTBotText text = fBot.text();
212 text.setFocus();
213 }
214
215 private void openImportWizard() {
216 fWizard = new ImportTraceWizard();
217
218 UIThreadRunnable.asyncExec(new VoidResult() {
219 @Override
220 public void run() {
221 final IWorkbench workbench = PlatformUI.getWorkbench();
222 // Fire the Import Trace Wizard
223 if (workbench != null) {
224 final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
225 Shell shell = activeWorkbenchWindow.getShell();
226 assertNotNull(shell);
227 ((ImportTraceWizard) fWizard).init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY);
228 WizardDialog dialog = new WizardDialog(shell, fWizard);
229 dialog.open();
230 }
231 }
232 });
233
234 fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard));
235 }
236
237 private void selectFolder(String... treePath) {
238 SWTBotTree tree = fBot.tree();
239 fBot.waitUntil(Conditions.widgetIsEnabled(tree));
240 SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, tree, treePath);
241 folderNode.check();
242 }
243
244 private void importFinish() {
245 SWTBotShell shell = fBot.activeShell();
246 final SWTBotButton finishButton = fBot.button("Finish");
247 finishButton.click();
248 fBot.waitUntil(Conditions.shellCloses(shell));
249 SWTBotUtils.waitForJobs();
250 }
251 }
This page took 0.060339 seconds and 5 git commands to generate.