package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests;
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.URIUtil;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages;
import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
testImport(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE, false, false);
}
+ /**
+ * Test import from directory, create experiment
+ *
+ * @throws Exception
+ * on error
+ */
+ @Test
+ public void testImportWithExperiment() throws Exception {
+ testImport(ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, false);
+ }
+
+ /**
+ * Test import from directory, create experiment (validate experiment name)
+ *
+ * @throws Exception
+ * on error
+ */
+ @Test
+ public void testImportWithExperimentValidation() throws Exception {
+ testImport(ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, false, false);
+ }
+
/**
* Test import from directory, preserve folder structure
*
testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, true);
}
+ /**
+ * Test import from archive, create Experiment
+ *
+ * @throws Exception
+ * on error
+ */
+ @Test
+ public void testImportFromArchiveWithExperiment() throws Exception {
+ testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE | ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT, false, true);
+ }
+
/**
* Test import from directory, preserve folder structure
* @throws Exception on error
}
private void testImport(int options, boolean testViews, boolean fromArchive) throws Exception {
+ testImport(options, testViews, fromArchive, true);
+ }
+
+ private void testImport(int options, boolean testViews, boolean fromArchive, boolean defaultExperiment) throws Exception {
String expectedSourceLocation = null;
+
+ @NonNull String experimentName;
+ if (fromArchive) {
+ experimentName = checkNotNull(new Path(ARCHIVE_FILE_NAME).lastSegment());
+ } else {
+ experimentName = checkNotNull(new Path(TRACE_FOLDER_PARENT_PATH).lastSegment());
+ }
+
+ if (!defaultExperiment) {
+ SWTBotUtils.createExperiment(fBot, TRACE_PROJECT_NAME, experimentName);
+ }
+
openImportWizard();
if (fromArchive) {
expectedSourceLocation = URI_JAR_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(TRACE_ARCHIVE_PATH).getCanonicalPath()) + "!" + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR;
}
setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT);
+
+ if (!defaultExperiment) {
+ experimentName = verifyExperimentNameHandling(experimentName);
+ }
+ checkFinishButton(true);
+
importFinish();
IPath expectedElementPath = new Path(TRACE_NAME);
expectedElementPath = new Path(TRACE_FOLDER).append(expectedElementPath);
}
- checkOptions(options, expectedSourceLocation, expectedElementPath);
+ checkOptions(options, expectedSourceLocation, expectedElementPath, experimentName);
TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath);
if (testViews) {
testViews(tmfEd);
fBot.closeAllEditors();
+ SWTBotUtils.clearExperimentFolder(fBot, TRACE_PROJECT_NAME);
SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
}
testViews(editor);
}
+ SWTBotUtils.clearExperimentFolder(fBot, TRACE_PROJECT_NAME);
+
SWTBotUtils.clearTracesFolder(fBot, TRACE_PROJECT_NAME);
if (testArchivePath != null) {
Files.delete(Paths.get(testArchivePath));
checkBox.deselect();
}
+ checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateExperiment);
+ if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) {
+ checkBox.select();
+ } else {
+ checkBox.deselect();
+ }
+
SWTBotCombo comboBox = fBot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType);
if (traceTypeName != null && !traceTypeName.isEmpty()) {
comboBox.setSelection(traceTypeName);
}
private static void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath) throws CoreException {
+ checkOptions(optionFlags, expectedSourceLocation, expectedElementPath, null);
+ }
+
+ private static void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath, String experimentName) throws CoreException {
IProject project = getProjectResource();
assertTrue(project.exists());
TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
String sourceLocation = traceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION);
assertNotNull(sourceLocation);
assertEquals(expectedSourceLocation, sourceLocation);
+
+ TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
+ assertNotNull(expFolder);
+ if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_EXPERIMENT) != 0) {
+ if (experimentName != null) {
+ TmfExperimentElement expElement = expFolder.getExperiment(experimentName);
+ assertNotNull(expElement);
+ assertEquals(2, expElement.getTraces().size());
+ }
+ } else {
+ assertTrue(expFolder.getExperiments().size() == 0);
+ }
}
private static IProject getProjectResource() {
return ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME);
}
+
+ private @NonNull String verifyExperimentNameHandling(String aExperimentName) {
+ String experimentName = aExperimentName;
+
+ // experiment already exists
+ checkFinishButton(false);
+
+ SWTBotText expText = fBot.textInGroup("Options");
+
+ // Invalid experiment name (only whitespaces)
+ expText.setText(String.valueOf(' '));
+ checkFinishButton(false);
+
+ // Invalid experiment name
+ expText.setText(String.valueOf('/'));
+ checkFinishButton(false);
+
+ // Set valid experiment name
+ experimentName += '_';
+ expText.setText(experimentName);
+ return experimentName;
+ }
+
}
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.bindings.keys.IKeyLookup;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
+import org.eclipse.tracecompass.internal.tmf.ui.project.operations.NewExperimentOperation;
import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
*
* @author Matthew Khouzam
*/
+@SuppressWarnings("restriction")
public final class SWTBotUtils {
private static final String WINDOW_MENU = "Window";
deleteProject(projectName, true, bot);
}
+ /**
+ * Creates an experiment
+ *
+ * @param bot
+ * a given workbench bot
+ * @param projectName
+ * the name of the project, creates the project if needed
+ * @param expName
+ * the experiment name
+ */
+ public static void createExperiment(SWTWorkbenchBot bot, String projectName, final @NonNull String expName) {
+ IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+ TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true);
+ TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
+ assertNotNull(expFolder);
+ NewExperimentOperation operation = new NewExperimentOperation(expFolder, expName);
+ operation.run(new NullProgressMonitor());
+
+ bot.waitUntil(new DefaultCondition() {
+ @Override
+ public boolean test() throws Exception {
+ TmfExperimentElement experiment = expFolder.getExperiment(expName);
+ return experiment != null;
+ }
+
+ @Override
+ public String getFailureMessage() {
+ return "Experiment (" + expName + ") couldn't be created";
+ }
+ });
+ }
+
+
/**
* Focus on the main window
*
return "Traces Folder not empty (" + fTraceNb + ")";
}
});
+ }
+
+ /**
+ * Clear the experiment folder
+ *
+ * @param bot
+ * a given workbench bot
+ * @param projectName
+ * the name of the project (needs to exist)
+ */
+ public static void clearExperimentFolder(SWTWorkbenchBot bot, String projectName) {
+ IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+ TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, false);
+ TmfExperimentFolder expFolder = tmfProject.getExperimentsFolder();
+ expFolder.getExperiments().forEach(experiment -> {
+ IResource resource = experiment.getResource();
+ try {
+ // Close the experiment if open
+ experiment.closeEditors();
+
+ IPath path = resource.getLocation();
+ if (path != null) {
+ // Delete supplementary files
+ experiment.deleteSupplementaryFolder();
+ }
+ // Finally, delete the experiment
+ resource.delete(true, null);
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+ });
+
+ bot.waitUntil(new DefaultCondition() {
+ private int fExperimentNb = 0;
+ @Override
+ public boolean test() throws Exception {
+ List<TmfExperimentElement> experiments = expFolder.getExperiments();
+ fExperimentNb = experiments.size();
+ return fExperimentNb == 0;
+ }
+
+ @Override
+ public String getFailureMessage() {
+ return "Experiment Folder not empty (" + fExperimentNb + ")";
+ }
+ });
}
/**