From bbdb3d6d92a6f044fd0e2f068332111b33452216 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Wed, 1 Apr 2015 00:53:56 -0400 Subject: [PATCH] tmf: SWTBot tests for importing archives from directories and archives Change-Id: I47b16520e3c91f3cf21af2c91fdcfeedd8dec1e6 Signed-off-by: Marc-Andre Laperle Reviewed-on: https://git.eclipse.org/r/44966 Reviewed-by: Hudson CI --- .../tests/StandardImportAndReadSmokeTest.java | 273 ++++++++++++++---- .../swtbot/tests/shared/ConditionHelpers.java | 22 ++ .../ui/swtbot/tests/shared/SWTBotUtils.java | 83 +++++- 3 files changed, 317 insertions(+), 61 deletions(-) diff --git a/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/StandardImportAndReadSmokeTest.java b/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/StandardImportAndReadSmokeTest.java index 4bc0fbf609..0a9e28c364 100644 --- a/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/StandardImportAndReadSmokeTest.java +++ b/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/StandardImportAndReadSmokeTest.java @@ -8,6 +8,7 @@ * * Contributors: * Bernd Hufmann - Initial API and implementation + * Marc-Andre Laperle - Added tests for extracting archives during import *******************************************************************************/ package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests; @@ -18,15 +19,20 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.URIUtil; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.widgets.Shell; @@ -37,12 +43,16 @@ import org.eclipse.swtbot.swt.finder.waits.Conditions; import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo; import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem; import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard; import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizardPage; 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.TmfProjectElement; import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry; @@ -66,91 +76,186 @@ import org.junit.runner.RunWith; public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTest { private static final String TRACE_FOLDER_PARENT_PATH = fTrace.getPath() + File.separator + ".." + File.separator + ".." + File.separator; - private static final String TRACE_ARCHIVE_PATH = TRACE_FOLDER_PARENT_PATH + "synctraces.tar.gz"; + private static final String ARCHIVE_FILE_NAME = "synctraces.tar.gz"; + private static final String TRACE_ARCHIVE_PATH = TRACE_FOLDER_PARENT_PATH + ARCHIVE_FILE_NAME; + private static final String TRACE_FOLDER_PARENT_NAME = "traces"; private static final String TRACE_PROJECT_NAME = "Tracing"; + private static final String ARCHIVE_ROOT_ELEMENT_NAME = "/"; + private static final String GENERATED_ARCHIVE_NAME = "testtraces.zip"; + private static final String URI_SEPARATOR = "/"; + private static final String URI_FILE_SCHEME = "file:"; + private static final String URI_JAR_FILE_SCHEME = "jar:file:"; + private static final boolean IS_WIN32 = System.getProperty("os.name").startsWith("Windows"); //$NON-NLS-1$//$NON-NLS-2$ + private static final String URI_DEVICE_SEPARATOR = IS_WIN32 ? URI_SEPARATOR : ""; + /** * Test import from directory + * + * @throws Exception + * on error */ @Test - public void testImportFromDirectory() { + public void testImportFromDirectory() throws Exception { testImport(0, false, false); } /** * Test import from directory, create links + * + * @throws Exception + * on error */ @Test - public void testImportFromDirectoryLinks() { + public void testImportFromDirectoryLinks() throws Exception { testImport(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE, false, false); } /** * Test import from directory, preserve folder structure + * + * @throws Exception + * on error */ @Test - public void testImportFromDirectoryPreserveFolder() { + public void testImportFromDirectoryPreserveFolder() throws Exception { testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, false); } /** * Test import from directory, create links, preserve folder structure + * + * @throws Exception + * on error */ @Test - public void testImportFromDirectoryLinksPreserveFolder() { + public void testImportFromDirectoryLinksPreserveFolder() throws Exception { int options = ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE; testImport(options, false, false); } /** * Test import from directory, overwrite all + * + * @throws Exception + * on error */ @Test - public void testImportFromDirectoryOverwrite() { + public void testImportFromDirectoryOverwrite() throws Exception { testImport(0, false, false); testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, false); } /** * Test import from archive + * + * @throws Exception + * on error */ @Test - public void testImportFromArchive() { + public void testImportFromArchive() throws Exception { testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, true); } /** * Test import from directory, preserve folder structure + * @throws Exception on error */ @Test - public void testImportFromArchivePreserveFolder() { + public void testImportFromArchivePreserveFolder() throws Exception { testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, true); } /** * Test import from directory, overwrite all + * + * @throws Exception + * on error */ @Test - public void testImportFromArchiveOverwrite() { + public void testImportFromArchiveOverwrite() throws Exception { testImport(0, false, true); testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, true); } - private void testImport(int options, boolean testViews, boolean fromArchive) { - createProject(); + /** + * Test import from directory containing archives + * + * @throws Exception + * on error + */ + @Test + public void testExtractArchivesFromDirectory() throws Exception { + testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false); + } + + /** + * Test import from directory containing archives, create links + * @throws Exception on error + */ + @Test + public void testExtractArchivesFromDirectoryLinks() throws Exception { + testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false); + } + + /** + * Test import from directory containing archives, create links, preserve folder structure + * @throws Exception on error + */ + @Test + public void testExtractArchivesFromDirectoryLinksPreserveStruture() throws Exception { + testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false); + } - importOpenWizard(); + /** + * Test import from archive containing archives + * + * @throws Exception + * on error + */ + @Test + public void testExtractArchivesFromArchive() throws Exception { + testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, true); + } + + /** + * Test import from archive containing archives, preserve folder structure + * + * @throws Exception + * on error + */ + @Test + public void testExtractArchivesFromArchivePreserveFolder() throws Exception { + testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true); + } + + private void testImport(int options, boolean testViews, boolean fromArchive) throws Exception { + createProject(); + String expectedSourceLocation = null; + openImportWizard(); if (fromArchive) { - importAddArchive(); + 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; + selectImportFromArchive(TRACE_ARCHIVE_PATH); + selectFolder(ARCHIVE_ROOT_ELEMENT_NAME); + SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace); + assertFalse(checkBox.isEnabled()); } else { - importAddDirectory(); + String sourcePath = TRACE_FOLDER_PARENT_PATH + File.separator + TRACE_FOLDER + File.separator + TRACE_NAME; + expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(sourcePath).getCanonicalPath()) + URI_SEPARATOR; + selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH); + selectFolder(new String [] {TRACE_FOLDER_PARENT_NAME, TRACE_FOLDER }); } setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); importFinish(); - checkOptions(options); - TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, getProjectName(), getTraceElementPath(options)); + IPath expectedElementPath = new Path(TRACE_NAME); + if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) { + expectedElementPath = new Path(TRACE_FOLDER).append(expectedElementPath); + } + + checkOptions(options, expectedSourceLocation, expectedElementPath); + TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, getProjectName(), expectedElementPath); if (testViews) { testViews(tmfEd); } @@ -160,13 +265,84 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe SWTBotUtils.deleteProject(getProjectName(), fBot); } + private void testImportAndExtractArchives(int options, boolean fromArchive) throws Exception { + createProject(); + + String expectedSourceLocation; + IPath expectedElementPath; + if (fromArchive) { + String testArchivePath = createArchive(); + openImportWizard(); + selectImportFromArchive(testArchivePath); + selectFile(ARCHIVE_FILE_NAME, ARCHIVE_ROOT_ELEMENT_NAME, TRACE_PROJECT_NAME, TRACE_FOLDER_PARENT_NAME); + + expectedSourceLocation = URI_JAR_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(testArchivePath).getCanonicalPath()) + "!" + URI_SEPARATOR + TRACE_PROJECT_NAME + URI_SEPARATOR + TRACE_FOLDER_PARENT_NAME + URI_SEPARATOR + ARCHIVE_FILE_NAME + + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR; + expectedElementPath = new Path(TRACE_PROJECT_NAME).append(TRACE_FOLDER_PARENT_NAME).append(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME); + } else { + openImportWizard(); + selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH); + selectFile(ARCHIVE_FILE_NAME, TRACE_FOLDER_PARENT_NAME); + expectedElementPath = new Path(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME); + expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath()) + URI_SEPARATOR + ARCHIVE_FILE_NAME + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR; + } + + if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) == 0) { + expectedElementPath = new Path(TRACE_NAME); + } + + setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); + importFinish(); + // Archives should never be imported as links + int expectedOptions = options & ~ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE; + checkOptions(expectedOptions, expectedSourceLocation, expectedElementPath); + + TmfEventsEditor editor = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath); + testViews(editor); + + SWTBotUtils.deleteProject(getProjectName(), fBot); + } + + /** + * Create a temporary archive containing a nested archive. For example, + * testtraces.zip/synctraces.tar.gz can be used to test a nested archive. + */ + private String createArchive() throws URISyntaxException, CoreException, IOException { + + // Link to the test traces folder. We use a link so that we can safely + // delete the entire project when we are done. + IProject project = getProjectResource(); + String canonicalPath = new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath(); + IFolder folder = project.getFolder(TRACE_FOLDER_PARENT_NAME); + folder.createLink(new Path(canonicalPath), IResource.REPLACE, null); + + SWTBotTreeItem traceFilesProject = SWTBotUtils.selectProject(fBot, TRACE_PROJECT_NAME); + traceFilesProject.contextMenu("Export...").click(); + + fBot.waitUntil(Conditions.shellIsActive("Export")); + SWTBotShell activeShell = fBot.activeShell(); + SWTBotTree exportWizardsTree = fBot.tree(); + SWTBotTreeItem treeItem = SWTBotUtils.getTreeItem(fBot, exportWizardsTree, "General", "Archive File"); + treeItem.select(); + fBot.button("Next >").click(); + fBot.button("&Deselect All").click(); + selectFile(ARCHIVE_FILE_NAME, TRACE_PROJECT_NAME, TRACE_FOLDER_PARENT_NAME); + + String workspacePath = URIUtil.toFile(URIUtil.fromString(System.getProperty("osgi.instance.area"))).getAbsolutePath(); + final String archiveDestinationPath = workspacePath + File.separator + TRACE_PROJECT_NAME + File.separator + GENERATED_ARCHIVE_NAME; + fBot.comboBox().setText(archiveDestinationPath); + fBot.button("&Finish").click(); + fBot.waitUntil(Conditions.shellCloses(activeShell)); + return archiveDestinationPath; + } + private void testViews(TmfEventsEditor editor) { testHistogramView(getViewPart("Histogram"), editor); testPropertyView(getViewPart("Properties")); testStatisticsView(getViewPart("Statistics")); } - private static void importOpenWizard() { + private static void openImportWizard() { fWizard = new ImportTraceWizard(); UIThreadRunnable.asyncExec(new VoidResult() { @@ -188,50 +364,48 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard)); } - private static void importAddDirectory() { + private static void selectImportFromDirectory(String directoryPath) { SWTBotRadio button = fBot.radio("Select roo&t directory:"); button.click(); SWTBotCombo sourceCombo = fBot.comboBox(); - File traceFolderParent = new File(TRACE_FOLDER_PARENT_PATH); + File traceFolderParent = new File(directoryPath); sourceCombo.setText(traceFolderParent.getAbsolutePath()); SWTBotText text = fBot.text(); text.setFocus(); - - fBot.activeShell(); - SWTBotTree tree = fBot.tree(); - fBot.waitUntil(Conditions.widgetIsEnabled(tree)); - - final String traceFolderParentName = new Path(traceFolderParent.getAbsolutePath()).lastSegment(); - fBot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(traceFolderParentName, tree)); - final SWTBotTreeItem folderParentNode = tree.getTreeItem(traceFolderParentName); - folderParentNode.expand(); - - fBot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(TRACE_FOLDER, folderParentNode)); - final SWTBotTreeItem folderNode = folderParentNode.getNode(TRACE_FOLDER); - folderNode.check(); } - private static void importAddArchive() { + private static void selectImportFromArchive(String archivePath) { SWTBotRadio button = fBot.radio("Select &archive file:"); button.click(); SWTBotCombo sourceCombo = fBot.comboBox(1); - sourceCombo.setText(new File(TRACE_ARCHIVE_PATH).getAbsolutePath()); + sourceCombo.setText(new File(archivePath).getAbsolutePath()); SWTBotText text = fBot.text(); text.setFocus(); + } + private static void selectFolder(String... treePath) { SWTBotTree tree = fBot.tree(); fBot.waitUntil(Conditions.widgetIsEnabled(tree)); - final SWTBotTreeItem genericCtfTreeItem = tree.getTreeItem("/"); - fBot.waitUntil(Conditions.widgetIsEnabled(genericCtfTreeItem)); - genericCtfTreeItem.check(); + SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, tree, treePath); + folderNode.check(); + } - SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace); - assertFalse(checkBox.isEnabled()); + private static void selectFile(String fileName, String... folderTreePath) { + SWTBotTree folderTree = fBot.tree(); + fBot.waitUntil(Conditions.widgetIsEnabled(folderTree)); + SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, folderTree, folderTreePath); + folderNode.select(); + + SWTBotTable fileTable = fBot.table(); + fBot.waitUntil(Conditions.widgetIsEnabled(fileTable)); + fBot.waitUntil(ConditionHelpers.isTableItemAvailable(fileName, fileTable)); + SWTBotTableItem tableItem = fileTable.getTableItem(fileName); + tableItem.check(); } private static void setOptions(int optionFlags, String traceTypeName) { @@ -273,8 +447,8 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe } } - private static void checkOptions(int optionFlags) { - IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(TRACE_PROJECT_NAME); + private void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath) throws CoreException { + IProject project = getProjectResource(); assertTrue(project.exists()); TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true); assertNotNull(tmfProject); @@ -295,21 +469,20 @@ public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTe assertEquals((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0, traceResource.isLinked()); // i.e. /Tracing/Traces - IPath expectedPath = Path.ROOT.append(new Path(TRACE_PROJECT_NAME)).append(TmfTracesFolder.TRACES_FOLDER_NAME); - expectedPath = expectedPath.append(getTraceElementPath(optionFlags)); + IPath expectedPath = Path.ROOT.append(new Path(TRACE_PROJECT_NAME)).append(TmfTracesFolder.TRACES_FOLDER_NAME).append(expectedElementPath); assertEquals(expectedPath, traceResource.getFullPath()); - } - private static IPath getTraceElementPath(int optionFlags) { - IPath traceElementPath = new Path(""); - if ((optionFlags & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) { - traceElementPath = traceElementPath.append(TRACE_FOLDER); - } - return traceElementPath.append(TRACE_NAME); + String sourceLocation = traceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION); + assertNotNull(sourceLocation); + assertEquals(expectedSourceLocation, sourceLocation); } @Override protected String getProjectName() { return TRACE_PROJECT_NAME; } + + private IProject getProjectResource() { + return ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName()); + } } diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java index a0eb9b186d..5923be38e7 100644 --- a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ConditionHelpers.java @@ -87,6 +87,28 @@ public final class ConditionHelpers { }; } + /** + * Is a table item available + * + * @param name + * the name of the item + * @param table + * the parent table + * @return true or false, it should swallow all exceptions + */ + public static ICondition isTableItemAvailable(final String name, final SWTBotTable table) { + return new SWTBotTestCondition() { + @Override + public boolean test() throws Exception { + try { + return table.containsItem(name); + } catch (Exception e) { + } + return false; + } + }; + } + /** * Is the treeItem's node available * diff --git a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java index 7454d19a48..a3e34dde34 100644 --- a/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java +++ b/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java @@ -37,6 +37,7 @@ import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory; import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; +import org.eclipse.swtbot.swt.finder.SWTBot; import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; import org.eclipse.swtbot.swt.finder.results.Result; import org.eclipse.swtbot.swt.finder.results.VoidResult; @@ -120,14 +121,16 @@ public final class SWTBotUtils { } /** - * Deletes a tracing project + * Deletes a project * * @param projectName * the name of the tracing project + * @param deleteResources + * whether or not to deleted resources under the project * @param bot * the workbench bot */ - public static void deleteProject(String projectName, SWTWorkbenchBot bot) { + public static void deleteProject(final String projectName, boolean deleteResources, SWTWorkbenchBot bot) { // Wait for any analysis to complete because it might create // supplementary files SWTBotUtils.waitForJobs(); @@ -146,10 +149,12 @@ public final class SWTBotUtils { SWTBotMenu contextMenu = treeItem.contextMenu("Delete"); contextMenu.click(); - bot.shell("Delete Resources").setFocus(); - final SWTBotCheckBox checkBox = bot.checkBox(); - bot.waitUntil(Conditions.widgetIsEnabled(checkBox)); - checkBox.click(); + if (deleteResources) { + bot.shell("Delete Resources").setFocus(); + final SWTBotCheckBox checkBox = bot.checkBox(); + bot.waitUntil(Conditions.widgetIsEnabled(checkBox)); + checkBox.click(); + } final SWTBotButton okButton = bot.button("OK"); bot.waitUntil(Conditions.widgetIsEnabled(okButton)); @@ -158,6 +163,18 @@ public final class SWTBotUtils { SWTBotUtils.waitForJobs(); } + /** + * Deletes a project and its resources + * + * @param projectName + * the name of the tracing project + * @param bot + * the workbench bot + */ + public static void deleteProject(String projectName, SWTWorkbenchBot bot) { + deleteProject(projectName, true, bot); + } + /** * Focus on the main window * @@ -372,17 +389,30 @@ public final class SWTBotUtils { * @return a {@link SWTBotTreeItem} of the "Traces" folder */ public static SWTBotTreeItem selectTracesFolder(SWTWorkbenchBot bot, String projectName) { - SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer"); - projectExplorerBot.show(); - SWTBotTree tree = projectExplorerBot.bot().tree(); - bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(projectName, tree)); - SWTBotTreeItem projectTreeItem = tree.getTreeItem(projectName); + SWTBotTreeItem projectTreeItem = selectProject(bot, projectName); projectTreeItem.select(); SWTBotTreeItem tracesFolderItem = getTraceProjectItem(bot, projectTreeItem, TmfTracesFolder.TRACES_FOLDER_NAME); tracesFolderItem.select(); return tracesFolderItem; } + /** + * Select the project in Project Explorer + * + * @param bot + * a given workbench bot + * @param projectName + * the name of the project (it needs to exist or else it would time out) + * @return a {@link SWTBotTreeItem} of the project + */ + public static SWTBotTreeItem selectProject(SWTWorkbenchBot bot, String projectName) { + SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer"); + projectExplorerBot.show(); + SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName); + treeItem.select(); + return treeItem; + } + /** * Open a view by id. * @@ -445,4 +475,35 @@ public final class SWTBotUtils { } }); } + + /** + * Get the tree item from a tree at the specified location + * + * @param bot + * the SWTBot + * @param tree + * the tree to find the tree item in + * @param nodeNames + * the path to the tree item, in the form of node names (from + * parent to child). + * @return the tree item + */ + public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTree tree, String... nodeNames) { + if (nodeNames.length == 0) { + return null; + } + + bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(nodeNames[0], tree)); + SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]); + for (int i = 1; i < nodeNames.length; i++) { + currentNode.expand(); + + String nodeName = nodeNames[i]; + bot.waitUntil(ConditionHelpers.IsTreeChildNodeAvailable(nodeName, currentNode)); + SWTBotTreeItem newNode = currentNode.getNode(nodeName); + currentNode = newNode; + } + + return currentNode; + } } -- 2.34.1