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