Search in sources :

Example 11 with ICondition

use of org.eclipse.swtbot.swt.finder.waits.ICondition in project egit by eclipse.

the class BranchAndResetActionTest method testCheckoutWithNonDeleted.

@Test
public void testCheckoutWithNonDeleted() throws Exception {
    // we need to check if this file system has problems to
    // delete a file with an open FileInputStrem
    IFile test = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1).getFolder(FOLDER).getFile("temp.txt");
    test.create(new ByteArrayInputStream(new byte[0]), false, null);
    File testFile = new File(test.getLocation().toString());
    assertTrue(testFile.exists());
    FileInputStream fis = new FileInputStream(testFile);
    try {
        FileUtils.delete(testFile);
        return;
    } catch (IOException e) {
    // the test makes sense only if deletion of
    // a file with open stream fails
    } finally {
        fis.close();
        if (testFile.exists())
            FileUtils.delete(testFile);
    }
    final Image folderImage = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
    final Image projectImage = PlatformUI.getWorkbench().getSharedImages().getImage(SharedImages.IMG_OBJ_PROJECT);
    // checkout stable
    checkoutAndVerify(new String[] { LOCAL_BRANCHES, "stable" });
    // add a file
    IFile toBeDeleted = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1).getFolder(FOLDER).getFile("ToBeDeleted");
    toBeDeleted.create(new ByteArrayInputStream(new byte[0]), false, null);
    ArrayList<IFile> untracked = new ArrayList<IFile>();
    untracked.add(toBeDeleted);
    // commit to stable
    CommitOperation op = new CommitOperation(new IFile[] { toBeDeleted }, untracked, TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER, "Add to stable");
    op.execute(null);
    InputStream is = toBeDeleted.getContents();
    try {
        checkout(new String[] { LOCAL_BRANCHES, "master" });
        final SWTBotShell showUndeleted = bot.shell(UIText.NonDeletedFilesDialog_NonDeletedFilesTitle);
        // repo relative path
        assertEquals("ToBeDeleted", showUndeleted.bot().tree().getAllItems()[0].getItems()[0].getItems()[0].getText());
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                assertSame(folderImage, showUndeleted.bot().tree().getAllItems()[0].widget.getImage());
            }
        });
        SWTBotToolbarDropDownButton pathButton = showUndeleted.bot().toolbarDropDownButton();
        pathButton.menuItem(UIText.NonDeletedFilesTree_FileSystemPathsButton).click();
        // see http://www.eclipse.org/forums/index.php/t/159133/ why we need this
        pathButton.pressShortcut(KeyStroke.getInstance("ESC"));
        // fs path
        IPath path = new Path(lookupRepository(repositoryFile).getWorkTree().getPath()).append(PROJ1).append(FOLDER).append("ToBeDeleted");
        SWTBotTreeItem[] items = showUndeleted.bot().tree().getAllItems();
        for (int i = 0; i < path.segmentCount(); i++) {
            boolean found = false;
            String segment = path.segment(i);
            for (SWTBotTreeItem item : items) if (item.getText().equals(segment)) {
                found = true;
                items = item.getItems();
            }
            assertTrue(found);
        }
        pathButton.menuItem(UIText.NonDeletedFilesTree_ResourcePathsButton).click();
        // see http://www.eclipse.org/forums/index.php/t/159133/ why we need this
        pathButton.pressShortcut(KeyStroke.getInstance("ESC"));
        // resource path
        assertEquals("ToBeDeleted", showUndeleted.bot().tree().getAllItems()[0].getItems()[0].getItems()[0].getText());
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                assertSame(projectImage, showUndeleted.bot().tree().getAllItems()[0].widget.getImage());
            }
        });
        ICondition treeEmpty = new ICondition() {

            @Override
            public boolean test() throws Exception {
                return showUndeleted.bot().tree().getAllItems().length == 0;
            }

            @Override
            public void init(SWTBot actBot) {
            // nothing
            }

            @Override
            public String getFailureMessage() {
                return "Not deleted";
            }
        };
        showUndeleted.bot().button(UIText.NonDeletedFilesDialog_RetryDeleteButton).click();
        try {
            showUndeleted.bot().waitUntil(treeEmpty, 1000, 100);
            fail("Should have failed");
        } catch (TimeoutException e) {
        // expected
        }
        is.close();
        showUndeleted.bot().button(UIText.NonDeletedFilesDialog_RetryDeleteButton).click();
        showUndeleted.bot().waitUntil(treeEmpty, 1000, 100);
        showUndeleted.close();
    } finally {
        is.close();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) SWTBot(org.eclipse.swtbot.swt.finder.SWTBot) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SWTBotToolbarDropDownButton(org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarDropDownButton) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CommitOperation(org.eclipse.egit.core.op.CommitOperation) SWTBotTreeItem(org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem) SWTBotShell(org.eclipse.swtbot.swt.finder.widgets.SWTBotShell) IFile(org.eclipse.core.resources.IFile) File(java.io.File) ICondition(org.eclipse.swtbot.swt.finder.waits.ICondition) TimeoutException(org.eclipse.swtbot.swt.finder.widgets.TimeoutException) Test(org.junit.Test)

Aggregations

ICondition (org.eclipse.swtbot.swt.finder.waits.ICondition)11 SWTBot (org.eclipse.swtbot.swt.finder.SWTBot)10 SWTBotTreeItem (org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem)8 SWTBotShell (org.eclipse.swtbot.swt.finder.widgets.SWTBotShell)6 ArrayList (java.util.ArrayList)2 TimeoutException (org.eclipse.swtbot.swt.finder.widgets.TimeoutException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 CommitOperation (org.eclipse.egit.core.op.CommitOperation)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 Image (org.eclipse.swt.graphics.Image)1 Display (org.eclipse.swt.widgets.Display)1 Shell (org.eclipse.swt.widgets.Shell)1