summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorLorenzo Bettini2012-08-31 10:55:56 (EDT)
committer Mickael Istria2012-08-31 13:21:50 (EDT)
commit020cfc1c7fa680ecf2e05d7ee6865569276105df (patch)
tree6bdca4c77265e731a4e77100cbea24490389ec90
parent83d4ef7a1f33739fac747b73dcfef35c1eb12e26 (diff)
downloadorg.eclipse.swtbot-020cfc1c7fa680ecf2e05d7ee6865569276105df.zip
org.eclipse.swtbot-020cfc1c7fa680ecf2e05d7ee6865569276105df.tar.gz
org.eclipse.swtbot-020cfc1c7fa680ecf2e05d7ee6865569276105df.tar.bz2
BUG 384621: SWTBotTreeItem.contextMenu does not work anymore in e4refs/changes/83/8083/1
https://bugs.eclipse.org/bugs/show_bug.cgi?id=384621 Signed-off-by: Mickael Istria <mistria@redhat.com>
-rw-r--r--org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/AllTests.java2
-rw-r--r--org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseProjectTest.java126
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/finders/ContextMenuHelper.java94
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java17
4 files changed, 237 insertions, 2 deletions
diff --git a/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/AllTests.java b/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/AllTests.java
index 6a19215..7c823c9 100644
--- a/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/AllTests.java
+++ b/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/AllTests.java
@@ -13,6 +13,7 @@ package org.eclipse.swtbot.eclipse.finder;
import org.eclipse.swtbot.eclipse.finder.exceptions.QuickFixNotFoundExceptionTest;
import org.eclipse.swtbot.eclipse.finder.finders.CommandFinderTest;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditorTest;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseProjectTest;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotMultiPageEditorTest;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotViewTest;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJUnit4Suite;
@@ -28,6 +29,7 @@ import org.junit.runners.Suite.SuiteClasses;
QuickFixNotFoundExceptionTest.class,
CommandFinderTest.class,
SWTBotEclipseEditorTest.class,
+ SWTBotEclipseProjectTest.class,
SWTBotViewTest.class,
SWTBotMultiPageEditorTest.class })
public class AllTests {
diff --git a/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseProjectTest.java b/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseProjectTest.java
new file mode 100644
index 0000000..7acba00
--- /dev/null
+++ b/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseProjectTest.java
@@ -0,0 +1,126 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Lorenzo Bettini.
+ * 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:
+ * Lorenzo Bettini - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swtbot.eclipse.finder.widgets;
+
+import java.util.List;
+
+import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.eclipse.finder.widgets.helpers.NewJavaClass;
+import org.eclipse.swtbot.eclipse.finder.widgets.helpers.NewJavaProject;
+import org.eclipse.swtbot.eclipse.finder.widgets.helpers.PackageExplorerView;
+import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * @author Lorenzo Bettini &lt;bettini [at] dsi [dot] unifi [dot] it&gt;
+ * @version $Id$
+ */
+@RunWith(SWTBotJunit4ClassRunner.class)
+public class SWTBotEclipseProjectTest {
+
+ private static final String PROJECT_NAME = "FooBarProject";
+ private static final String PACKAGE_NAME = "org.eclipse.swtbot.eclipse.test";
+ private static final String CLASS_NAME = "HelloWorld";
+ private static final String CLASS_FILE_NAME = CLASS_NAME + ".java";
+
+ private NewJavaClass javaClass = new NewJavaClass();
+ private NewJavaProject javaProject = new NewJavaProject();
+ private PackageExplorerView packageExplorerView = new PackageExplorerView();
+ private static SWTWorkbenchBot bot = new SWTWorkbenchBot();
+
+ @BeforeClass
+ public static void beforeClass() {
+ closeWelcomePage();
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ javaProject.createProject(PROJECT_NAME);
+ javaClass.createClass(PACKAGE_NAME, CLASS_NAME);
+ }
+
+ private static void closeWelcomePage() {
+ try {
+ System.setProperty("org.eclipse.swtbot.search.timeout", "0");
+ bot.viewByTitle("Welcome").close();
+ } catch (WidgetNotFoundException e) {
+ // do nothing
+ }finally{
+ System.setProperty("org.eclipse.swtbot.search.timeout", "5000");
+ }
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ saveAndCloseAllEditors();
+ packageExplorerView.deleteProject(PROJECT_NAME);
+ }
+
+ /**
+ * @throws WidgetNotFoundException
+ */
+ private void saveAndCloseAllEditors() {
+ List<? extends SWTBotEditor> editors = bot.editors();
+ for (SWTBotEditor editor : editors) {
+ editor.saveAndClose();
+ }
+ }
+
+ @Test
+ public void canRefreshProject() {
+ packageExplorerTree().expandNode(PROJECT_NAME, "src")
+ .contextMenu("Refresh").click();
+ }
+
+ @Test
+ public void canAccessOpen() {
+ javaClassFileTreeItem().contextMenu("Open").click();
+ }
+
+ @Test
+ public void canAccessContextMenuWithSubmenus() {
+ // this fails in e4 with current implementation of
+ // SWTBotTreeItem.contextMenu
+ // since "Open With" has submenus
+ javaClassFileTreeItem().contextMenu("Open With");
+ }
+
+ @Test
+ public void canAccessContextMenuCopyQualifiedName() {
+ javaClassFileTreeItem().contextMenu("Copy Qualified Name");
+ }
+
+ @Test
+ public void canAccessContextMenuSubmenu() {
+ // this fails in e4 with current implementation of
+ // SWTBotTreeItem.contextMenu
+ // since "Open With" has submenus
+ SWTBotMenu openWithMenu = javaClassFileTreeItem().contextMenu("Open With");
+ openWithMenu.menu("Text Editor").click();
+ }
+
+ private SWTBotTreeItem javaClassFileTreeItem() {
+ return packageExplorerTree().expandNode(PROJECT_NAME, "src",
+ PACKAGE_NAME, CLASS_FILE_NAME);
+ }
+
+ private SWTBotTree packageExplorerTree() {
+ return bot.viewByTitle("Package Explorer").bot().tree();
+ }
+}
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/finders/ContextMenuHelper.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/finders/ContextMenuHelper.java
new file mode 100644
index 0000000..cb543c3
--- /dev/null
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/finders/ContextMenuHelper.java
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Lorenzo Bettini 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:
+ * Stefan Seelmann (initial)
+ * Stefan Schaefer (extension)
+ * Lorenzo Bettini (extracted method to get the MenuItem)
+ *******************************************************************************/
+package org.eclipse.swtbot.swt.finder.finders;
+
+import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.instanceOf;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
+import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.results.WidgetResult;
+import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
+import org.hamcrest.Matcher;
+
+/**
+ * This helper is a workaround for a bug in SWTBot, where the bot can't find a
+ * dynamically created context menu, see also
+ * http://www.eclipse.org/forums/index.php/t/11863/
+ *
+ * @author Stefan Seelmann (initial)
+ * @author Stefan Schaefer (extension)
+ * @author Lorenzo Bettini (extracted method to get the MenuItem)
+ */
+public class ContextMenuHelper {
+
+ public static MenuItem contextMenu(
+ final AbstractSWTBot<? extends Control> bot, final String... texts) {
+ return UIThreadRunnable.syncExec(new WidgetResult<MenuItem>() {
+ public MenuItem run() {
+ MenuItem menuItem = null;
+ Control control = bot.widget;
+
+ // MenuDetectEvent added by Stefan Schaefer
+ Event event = new Event();
+ control.notifyListeners(SWT.MenuDetect, event);
+ if (!event.doit) {
+ return null;
+ }
+
+ Menu menu = control.getMenu();
+ for (String text : texts) {
+ @SuppressWarnings("unchecked")
+ Matcher<?> matcher = allOf(instanceOf(MenuItem.class),
+ withMnemonic(text));
+ menuItem = show(menu, matcher);
+ if (menuItem != null) {
+ menu = menuItem.getMenu();
+ } else {
+ hide(menu);
+ throw new WidgetNotFoundException(
+ "Could not find menu: " + text); //$NON-NLS-1$
+ }
+ }
+
+ return menuItem;
+ }
+ });
+ }
+
+ private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
+ if (menu != null) {
+ menu.notifyListeners(SWT.Show, new Event());
+ MenuItem[] items = menu.getItems();
+ for (final MenuItem menuItem : items) {
+ if (matcher.matches(menuItem)) {
+ return menuItem;
+ }
+ }
+ menu.notifyListeners(SWT.Hide, new Event());
+ }
+ return null;
+ }
+
+ private static void hide(final Menu menu) {
+ menu.notifyListeners(SWT.Hide, new Event());
+ if (menu.getParentMenu() != null) {
+ hide(menu.getParentMenu());
+ }
+ }
+}
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java
index eed22a8..f018357 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java
@@ -24,6 +24,7 @@ import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.finders.ContextMenuHelper;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.ArrayResult;
import org.eclipse.swtbot.swt.finder.results.BoolResult;
@@ -545,12 +546,24 @@ public class SWTBotTreeItem extends AbstractSWTBot<TreeItem> {
@Override
public SWTBotMenu contextMenu(String text) {
- new SWTBotTree(tree).waitForEnabled();
+ SWTBotTree swtBotTree = new SWTBotTree(tree);
+ swtBotTree.waitForEnabled();
select();
notifyTree(SWT.MouseDown, createMouseEvent(0, 0, 3, 0, 1));
notifyTree(SWT.MouseUp, createMouseEvent(0, 0, 3, 0, 1));
notifyTree(SWT.MenuDetect);
- return super.contextMenu(tree, text);
+ SWTBotMenu contextMenu = null;
+ try {
+ contextMenu = super.contextMenu(tree, text);
+ } catch (WidgetNotFoundException e) {
+ // in e4, if the context menu contains submenus it appears
+ // as disposed after detection, so we use the ContextMenuHelper
+ if (e.getMessage().contains("was disposed")) {
+ return new SWTBotMenu(ContextMenuHelper.contextMenu(swtBotTree, text));
+ } else
+ throw e;
+ }
+ return contextMenu;
}
/**