Skip to content

Commit

Permalink
REDDEE-1835 Implement a dialog for filtering Java classes
Browse files Browse the repository at this point in the history
Signed-off-by: Lukáš Valach <lvalach@redhat.com>
  • Loading branch information
luvalach authored and rawagner committed Mar 19, 2018
1 parent fc79792 commit 9a18c81
Show file tree
Hide file tree
Showing 3 changed files with 340 additions and 0 deletions.
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat, Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.eclipse.jdt.ui.dialogs;

import org.eclipse.reddeer.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
import org.eclipse.reddeer.jface.window.AbstractWindow;
import org.eclipse.reddeer.jface.window.Openable;
import org.eclipse.reddeer.swt.api.Shell;
import org.eclipse.reddeer.workbench.workbenchmenu.WorkbenchMenuOpenable;

/**
* Represents {@value #DIALOG_TITLE} dialog.
*
* @see org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog
*
* @author lvalach
*
*/
public class OpenTypeSelectionDialog extends FilteredItemsSelectionDialog {

public static final String DIALOG_TITLE = "Open Type";
public static final String[] MENU_PATH = new String[] { "Navigate", "Open Type..." };

/**
* Instantiates new OpenTypeSelectionDialog. Implementations are responsible for
* making sure given shell is OpenTypeSelectionDialog.
*
* @param shell
* instance of OpenTypeSelectionDialog
*/
public OpenTypeSelectionDialog(Shell shell) {
super(shell);
}

/**
* Instantiates new OpenTypeSelectionDialog. An {@value #DIALOG_TITLE} shell
* will be connected with this instance. If there is no such shell, the shell
* may be assigned additionally (see {@link AbstractWindow#activate()}).
*/
public OpenTypeSelectionDialog() {
super();
isOpen();
}

/**
* {@inheritDoc}
*/
@Override
public Openable getDefaultOpenAction() {
return new WorkbenchMenuOpenable(DIALOG_TITLE, MENU_PATH);
}
}
@@ -0,0 +1,135 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat, Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.eclipse.ui.dialogs;

import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.core.matcher.WithMnemonicTextMatcher;
import org.eclipse.reddeer.jface.window.AbstractWindow;
import org.eclipse.reddeer.jface.window.Openable;
import org.eclipse.reddeer.swt.api.Shell;
import org.eclipse.reddeer.swt.api.Table;
import org.eclipse.reddeer.swt.impl.button.CancelButton;
import org.eclipse.reddeer.swt.impl.button.OkButton;
import org.eclipse.reddeer.swt.impl.table.DefaultTable;
import org.eclipse.reddeer.swt.impl.text.DefaultText;
import org.eclipse.reddeer.workbench.core.condition.JobIsRunning;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.hamcrest.Matcher;

/**
* Represends {@link org.eclipse.ui.dialogs.FilteredItemsSelectionDialog}.
*
* @author lvalach
*
*/
public class FilteredItemsSelectionDialog extends AbstractWindow {

/**
* Implementations are responsible for making sure given shell is
* FilteredItemsSelectionDialog.
*
* @param shell
* instance of FilteredItemsSelectionDialog
*/
public FilteredItemsSelectionDialog(Shell shell) {
super(shell);
}

/**
* Finds FilteredItemsSelectionDialog matching given matchers.
*
* @param matchers
* to match FilteredItemsSelectionDialog
*/
public FilteredItemsSelectionDialog(Matcher<?>... matchers) {
super(matchers);
}

/**
* Shell won't be assigned at the moment of construction but may be assigned
* additionally (see {@link AbstractWindow#activate()}).
*/
public FilteredItemsSelectionDialog() {
super();
}

/**
* {@inheritDoc}
*/
@Override
public Openable getDefaultOpenAction() {
return null;
}

/**
* Click 'OK' button.
*/
public void ok() {
new OkButton(this).click();
}

/**
* Click 'Cancel' button.
*/
public void cancel() {
new CancelButton(this).click();
}

/**
* Get filter expression.
*
* @return text from search box
*/
public void getFilterText() {
new DefaultText(this).getText();
}

/**
* Set filter expression.
*/
@SuppressWarnings("restriction")
public void setFilterText(String text) {
new DefaultText(this).setText(text);
new WaitWhile(new JobIsRunning(
new WithMnemonicTextMatcher(WorkbenchMessages.FilteredItemsSelectionDialog_jobLabel), false), false);
}

/**
* Get results table.
*
* @return table containing results of the search
*/
public Table getResultsTable() {
return new DefaultTable(this);
}

/**
* Selects items in results table.
*
* @param items
* texts of items to select
* @see {@link Table#select(String...)}
*/
public void selectItem(String... items) {
new DefaultTable(this).select(items);
}

/**
* Selects items in results table.
*
* @param indices
* row indices to select
* @see {@link Table#select(int...)}
*/
public void selectItem(int... indices) {
new DefaultTable(this).select(indices);
}
}
@@ -0,0 +1,145 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat, Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.eclipse.test.jdt.ui.dialogs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.eclipse.reddeer.common.exception.RedDeerException;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.eclipse.jdt.ui.dialogs.OpenTypeSelectionDialog;
import org.eclipse.reddeer.eclipse.jdt.ui.wizards.JavaProjectWizard;
import org.eclipse.reddeer.eclipse.jdt.ui.wizards.NewClassCreationWizard;
import org.eclipse.reddeer.eclipse.jdt.ui.wizards.NewClassWizardPage;
import org.eclipse.reddeer.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageOne;
import org.eclipse.reddeer.eclipse.ui.navigator.resources.ProjectExplorer;
import org.eclipse.reddeer.junit.runner.RedDeerSuite;
import org.eclipse.reddeer.swt.api.TableItem;
import org.eclipse.reddeer.swt.condition.ShellIsActive;
import org.eclipse.reddeer.swt.impl.menu.ShellMenuItem;
import org.eclipse.reddeer.swt.impl.shell.DefaultShell;
import org.eclipse.reddeer.workbench.condition.EditorWithTitleIsActive;
import org.eclipse.reddeer.workbench.impl.editor.TextEditor;
import org.eclipse.reddeer.workbench.impl.shell.WorkbenchShell;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class OpenTypeSelectionDialogTest {

private static final String TEST_PROJECT_NAME = "OpenTypeSelectionDialogTest";
private static final String TEST_CLASS_NAME = "FindMe";

@Test
public void openTest() {
OpenTypeSelectionDialog dialog = new OpenTypeSelectionDialog();
dialog.open();

assertTrue("Shell 'Open Type' is not available",
new ShellIsActive(OpenTypeSelectionDialog.DIALOG_TITLE).test());

dialog.cancel();

assertFalse("Shell 'Open Type' should be closed",
new ShellIsActive(OpenTypeSelectionDialog.DIALOG_TITLE).test());
}

@Test
public void constructorTakingShellTest() {
new ShellMenuItem(new WorkbenchShell(), "Navigate", "Open Type...").select();
DefaultShell shell = new DefaultShell(OpenTypeSelectionDialog.DIALOG_TITLE);
OpenTypeSelectionDialog dialog = new OpenTypeSelectionDialog(shell);

assertNotNull(dialog.getShell());

dialog.cancel();

assertFalse("Shell 'Open Type' should be closed",
new ShellIsActive(OpenTypeSelectionDialog.DIALOG_TITLE).test());
}

@Test
public void nonParametericConstructorTest() {
new ShellMenuItem(new WorkbenchShell(), "Navigate", "Open Type...").select();
OpenTypeSelectionDialog dialog = new OpenTypeSelectionDialog();

assertNotNull(dialog.getShell());

dialog.cancel();

assertFalse("Shell 'Open Type' should be closed",
new ShellIsActive(OpenTypeSelectionDialog.DIALOG_TITLE).test());
}

@Test
public void findAndOpenClass() {
OpenTypeSelectionDialog dialog = new OpenTypeSelectionDialog();
dialog.open();

dialog.setFilterText(TEST_CLASS_NAME);
List<TableItem> items = dialog.getResultsTable().getItems();
assertEquals("There should be one item in 'Open Type' dialog.", 1, items.size());
assertTrue("The 'Open Type' dialog should find '" + TEST_CLASS_NAME + "' class.",
items.get(0).getText().contains(TEST_CLASS_NAME));

dialog.ok();

assertFalse("Shell 'Open Type' should be closed",
new ShellIsActive(OpenTypeSelectionDialog.DIALOG_TITLE).test());
try {
new TextEditor(TEST_CLASS_NAME + ".java");
} catch (RedDeerException e) {
fail("Editor " + TEST_CLASS_NAME + ".java is unavailable.");
}
}

@BeforeClass
public static void prepareWorkspace() {
createJavaProject(TEST_PROJECT_NAME);
createJavaClass(TEST_CLASS_NAME);
new WaitUntil(new EditorWithTitleIsActive(TEST_CLASS_NAME + ".java"));
new TextEditor(TEST_CLASS_NAME + ".java").close();
}

@AfterClass
public static void cleanWorkspace() {
ProjectExplorer pe = new ProjectExplorer();
pe.deleteAllProjects();
}

private static void createJavaProject(String name) {
JavaProjectWizard javaProject = new JavaProjectWizard();
javaProject.open();

NewJavaProjectWizardPageOne javaWizardPage = new NewJavaProjectWizardPageOne(javaProject);
javaWizardPage.setProjectName(name);

javaProject.finish();
}

private static void createJavaClass(String name) {
NewClassCreationWizard javaClassDialog = new NewClassCreationWizard();
javaClassDialog.open();

NewClassWizardPage wizardPage = new NewClassWizardPage(javaClassDialog);
wizardPage.setName(name);
wizardPage.setPackage("test");

javaClassDialog.finish();
}
}

0 comments on commit 9a18c81

Please sign in to comment.