Skip to content

Commit

Permalink
Fixes Eclipse wizard dialogs should handle open perspective shell #1565
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Kopriva <jkopriva@redhat.com>
  • Loading branch information
jkopriva authored and odockal committed Aug 28, 2018
1 parent f02c747 commit b95372e
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 9 deletions.
Expand Up @@ -11,37 +11,102 @@
*******************************************************************************/
package org.eclipse.reddeer.eclipse.selectionwizard;

import org.eclipse.reddeer.common.exception.RedDeerException;
import org.eclipse.reddeer.common.properties.RedDeerProperties;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.direct.preferences.PreferencesUtil;
import org.eclipse.reddeer.eclipse.ui.dialogs.NewWizard;
import org.eclipse.reddeer.jface.window.Openable;
import org.eclipse.reddeer.swt.condition.ShellIsAvailable;
import org.eclipse.reddeer.swt.impl.button.FinishButton;
import org.eclipse.reddeer.swt.impl.button.NoButton;
import org.eclipse.reddeer.swt.impl.button.PushButton;
import org.eclipse.reddeer.workbench.core.condition.JobIsRunning;

/**
* Represents wizard which can be found in new wizard dialog (menu 'File -&gt; New -&gt; Other...').
* Represents wizard which can be found in new wizard dialog (menu 'File -&gt;
* New -&gt; Other...').
*
* @author rawagner
* @contributor jkopriva@redhat.com
*
*/
public abstract class NewMenuWizard extends AbstractSelectionWizardDialog{
public abstract class NewMenuWizard extends AbstractSelectionWizardDialog {

/**
* Constructs new Wizard that can be found in NewWizard.
* @param shellText new wizard text when next is clicked (ie 'New File')
*
* @param shellText new wizard text when next is clicked (ie 'New File')
* @param wizardCategory new wizard category (ie 'General')
* @param wizardName new wizard name (ie 'File')
* @param wizardName new wizard name (ie 'File')
*/
public NewMenuWizard(String shellText, String wizardCategory, String wizardName) {
super(shellText, wizardCategory, wizardName);
}

/**
* Constructs new Wizard that can be found in NewWizard.
* @param shellText new wizard text when next is clicked (ie 'New File')
*
* @param shellText new wizard text when next is clicked (ie 'New File')
* @param wizardPath wizard path in new wizard (ie 'General, File')
*/
public NewMenuWizard(String shellText, String[] wizardPath) {
super(shellText,wizardPath);
super(shellText, wizardPath);
}

@Override
protected Openable getOpenAction() {
return new SelectionWizardOpenable(new NewWizard(), wizardPath, matcher);
}

/**
* Click the finish button in wizard dialog. This method should be used with
* NewMenuWizard with associated perspective. If user wants to use this method
* with NewMenuWizard without associated perspective, he should use finish() or
* finish(TimePeriod).
*
* @param openPerspective open perspective related to the new project
*/
public void finish(boolean openPerspective) {
finish(TimePeriod.LONG, openPerspective);
}

/**
* Click the finish button in wizard dialog. This method should be used with
* NewMenuWizard with associated perspective. If user wants to use this method
* with NewMenuWizard without associated perspective, he should use finish() or
* finish(TimePeriod).
*
* @param timePeriod timeout for running job after clicking on Finish
* button
* @param openPerspective open perspective related to the new project
*/
public void finish(TimePeriod timePeriod, boolean openPerspective) {
String openAssociatedPerspective = PreferencesUtil.getOpenAssociatedPerspective();
checkShell();
log.info("Finish wizard");

new FinishButton(this).click();

try {
new WaitWhile(new JobIsRunning(), timePeriod);
} catch (NoClassDefFoundError e) {
// do nothing, reddeer.workbench plugin is not available
}

if (!openAssociatedPerspective.equals("prompt")) {
throw new RedDeerException("Property " + RedDeerProperties.OPEN_ASSOCIATED_PERSPECTIVE + " is set to '"
+ openAssociatedPerspective + "' but it should be set to 'prompt'");
}

new WaitUntil(new ShellIsAvailable("Open Associated Perspective?"));
if (openPerspective) {
new PushButton("Open Perspective").click();
} else {
new NoButton().click();
}
}

}
@@ -0,0 +1,115 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.eclipse.test.selectionwizard;

import static org.junit.Assert.assertTrue;

import org.eclipse.reddeer.common.exception.RedDeerException;
import org.eclipse.reddeer.common.util.Display;
import org.eclipse.reddeer.common.util.ResultRunnable;
import org.eclipse.reddeer.direct.preferences.PreferencesUtil;
import org.eclipse.reddeer.eclipse.jdt.ui.packageview.PackageExplorerPart;
import org.eclipse.reddeer.eclipse.jst.servlet.ui.project.facet.WebProjectFirstPage;
import org.eclipse.reddeer.eclipse.jst.servlet.ui.project.facet.WebProjectWizard;
import org.eclipse.reddeer.eclipse.ui.perspectives.AbstractPerspective;
import org.eclipse.reddeer.eclipse.ui.perspectives.JavaPerspective;
import org.eclipse.reddeer.junit.runner.RedDeerSuite;
import org.eclipse.reddeer.workbench.handler.WorkbenchShellHandler;
import org.eclipse.ui.PlatformUI;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* A test class for NewMenuWizardDialog for opening perspectives when creating
* new projects.
*
* @author jkopriva@redhat.com
*
*/
@RunWith(RedDeerSuite.class)
public class NewMenuWizardTest {
private PackageExplorerPart packageExplorer;
private String projectName = "webProject";

@BeforeClass
public static void setUpPreferences() {
PreferencesUtil.setOpenAssociatedPerspective("prompt");
}

@Before
public void prepareWorkspace() {
// open package explorer
packageExplorer = new PackageExplorerPart();
packageExplorer.open();

// open Java Perspective - could be done with requirement, but we need to set up
// perspective before every test
AbstractPerspective perspective = new JavaPerspective();
perspective.open();
}

@After
public void deleteProject() {
WorkbenchShellHandler.getInstance().closeAllNonWorbenchShells();
// Need to reopen packageExplorer, otherwise it fails
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=473278
packageExplorer.close();
packageExplorer.open();
packageExplorer.deleteAllProjects(true);
}

@AfterClass
public static void cleanUpPreferences() {
PreferencesUtil.setOpenAssociatedPerspective("never");
}

@Test
public void openPerspective() {
createProjectAndOpenPerspective(projectName, true);
assertTrue(checkPerspective().equals("Java EE"));
}

@Test
public void doNotOpenPerspective() {
createProjectAndOpenPerspective(projectName, false);
assertTrue(checkPerspective().equals("Java"));
}

@Test(expected = RedDeerException.class)
public void testThrowException() {
PreferencesUtil.setOpenAssociatedPerspective("never");
createProjectAndOpenPerspective(projectName, false);
}

private void createProjectAndOpenPerspective(String projectName, boolean openPerspective) {
WebProjectWizard ww = new WebProjectWizard();
ww.open();
WebProjectFirstPage fp = new WebProjectFirstPage(ww);
fp.setProjectName(projectName);
ww.finish(openPerspective);
}

private String checkPerspective() {
String activePerspectiveLabel = Display.syncExec(new ResultRunnable<String>() {
@Override
public String run() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getPerspective().getLabel();
}
});
return activePerspectiveLabel;
}

}

0 comments on commit b95372e

Please sign in to comment.