Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Tasse2015-05-28 22:04:27 +0000
committerPatrick Tasse2015-05-28 22:43:44 +0000
commit1dee35099a09c1a749c99b04669bf1bc4234ed4a (patch)
treeff876deaf857119ecb6296a89a0b01e7aa97d0cf
parentb1ba72397dc62f796e94fa1d96c7b2eb3a8e795f (diff)
downloadorg.eclipse.swtbot-1dee35099a09c1a749c99b04669bf1bc4234ed4a.tar.gz
org.eclipse.swtbot-1dee35099a09c1a749c99b04669bf1bc4234ed4a.tar.xz
org.eclipse.swtbot-1dee35099a09c1a749c99b04669bf1bc4234ed4a.zip
Bug 451126: Fix click() behavior on SWTBotMenu radio menu item
Also fixes Bug 397649. When clicking on a radio menu item, its selected state should not be toggled. Instead it should be selected, and any radio menu item in the same menu group should be unselected. The radio menu group is delimited by separator menu items. Change-Id: Ia3f329786f8a83fc06bc8653e230e7976c94fa72 Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
-rw-r--r--org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotPopupMenuTest.java29
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/utils/internal/SiblingFinder.java15
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotMenu.java73
3 files changed, 108 insertions, 9 deletions
diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotPopupMenuTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotPopupMenuTest.java
index 1d0e8dbc..bf6c4eac 100644
--- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotPopupMenuTest.java
+++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotPopupMenuTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2015 Ketan Padegaonkar 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
@@ -7,6 +7,7 @@
*
* Contributors:
* Ketan Padegaonkar - initial API and implementation
+ * Patrick Tasse - Fix radio menu item click behavior (Bug 451126 & Bug 397649)
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;
@@ -16,6 +17,7 @@ import static org.eclipse.swtbot.swt.finder.SWTBotTestCase.assertTextContains;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.test.AbstractControlExampleTest;
@@ -63,6 +65,31 @@ public class SWTBotPopupMenuTest extends AbstractControlExampleTest {
assertTextContains("Selection [13]: SelectionEvent{MenuItem {Push} time=", bot.textInGroup("Listeners").widget);
}
+ @Test
+ public void clicksCheckMenuItem() throws Exception {
+ SWTBotMenu item = popupShell.contextMenu("Check");
+ boolean checked = item.isChecked();
+ item.click();
+ assertTrue(checked != item.isChecked());
+ item.click();
+ assertTrue(checked == item.isChecked());
+ }
+
+ @Test
+ public void clicksRadioMenuItem() throws Exception {
+ SWTBotMenu item1 = popupShell.contextMenu("Radio1");
+ SWTBotMenu item2 = popupShell.contextMenu("Radio2");
+ item1.click();
+ assertTrue(item1.isChecked());
+ assertTrue(!item2.isChecked());
+ item1.click();
+ assertTrue(item1.isChecked());
+ assertTrue(!item2.isChecked());
+ item2.click();
+ assertTrue(!item1.isChecked());
+ assertTrue(item2.isChecked());
+ }
+
@Before
public void setUp() throws Exception {
bot = new SWTBot();
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/utils/internal/SiblingFinder.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/utils/internal/SiblingFinder.java
index 2fed88ba..f032ea7a 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/utils/internal/SiblingFinder.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/utils/internal/SiblingFinder.java
@@ -7,13 +7,14 @@
*
* Contributors:
* Ketan Padegaonkar - initial API and implementation
- * Patrick Tasse - support tool item
+ * Patrick Tasse - support tool item and menu item
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.utils.internal;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
@@ -53,6 +54,8 @@ public final class SiblingFinder implements ArrayResult<Widget> {
siblings = ((TabItem) w).getParent().getItems();
else if (isToolItem(w))
siblings = ((ToolItem) w).getParent().getItems();
+ else if (isMenuItem(w))
+ siblings = ((MenuItem) w).getParent().getItems();
return siblings;
}
@@ -98,4 +101,14 @@ public final class SiblingFinder implements ArrayResult<Widget> {
private boolean isToolItem(Widget w) {
return w instanceof ToolItem;
}
+
+ /**
+ * Gets if this is a menu item widget.
+ *
+ * @param w The widget.
+ * @return <code>true</code> if it is a menu item. Otherwise <code>false</code>.
+ */
+ private boolean isMenuItem(Widget w) {
+ return w instanceof MenuItem;
+ }
}
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotMenu.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotMenu.java
index 6d2bb270..006815d3 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotMenu.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotMenu.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2015 Ketan Padegaonkar 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
@@ -7,6 +7,7 @@
*
* Contributors:
* Ketan Padegaonkar - initial API and implementation
+ * Patrick Tasse - Fix radio menu item click behavior (Bug 451126 & Bug 397649)
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;
@@ -24,6 +25,7 @@ import org.eclipse.swtbot.swt.finder.results.BoolResult;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
+import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.hamcrest.Matcher;
import org.hamcrest.SelfDescribing;
@@ -56,26 +58,83 @@ public class SWTBotMenu extends AbstractSWTBot<MenuItem> {
public SWTBotMenu click() {
log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
waitForEnabled();
- toggleSelection();
+ if (SWTUtils.hasStyle(widget, SWT.CHECK)) {
+ toggleCheckSelection();
+ } else if (SWTUtils.hasStyle(widget, SWT.RADIO)) {
+ setRadioSelection();
+ }
notify(SWT.Selection);
log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
return this;
}
/**
- * Toggle the selection of the checkbox if applicable.
+ * Toggle the selection of the check menu item.
*/
- private void toggleSelection() {
+ private void toggleCheckSelection() {
syncExec(new VoidResult() {
public void run() {
- if (hasStyle(widget, SWT.CHECK) || hasStyle(widget, SWT.RADIO))
- widget.setSelection(!widget.getSelection());
+ widget.setSelection(!widget.getSelection());
}
});
}
/**
- * Gets the menu matching the given name.
+ * Set the selection of the radio menu item and clear the selection of any
+ * other radio menu item in the same group.
+ */
+ private void setRadioSelection() {
+ final SWTBotMenu otherSelectedRadioItem = otherSelectedRadioItem();
+ if (otherSelectedRadioItem != null) {
+ otherSelectedRadioItem.notify(SWT.Deactivate);
+ asyncExec(new VoidResult() {
+ public void run() {
+ otherSelectedRadioItem.widget.setSelection(false);
+ }
+ });
+ otherSelectedRadioItem.notify(SWT.Selection);
+ }
+ syncExec(new VoidResult() {
+ public void run() {
+ widget.setSelection(true);
+ }
+ });
+ }
+
+ private SWTBotMenu otherSelectedRadioItem() {
+ MenuItem other = syncExec(new WidgetResult<MenuItem>() {
+ public MenuItem run() {
+ if (hasStyle(widget.getParent(), SWT.NO_RADIO_GROUP))
+ return null;
+ Widget[] siblings = SWTUtils.siblings(widget);
+ boolean ownGroup = false;
+ MenuItem selected = null;
+ for (Widget sibling : siblings) {
+ if (sibling == widget) {
+ ownGroup = true;
+ } else if (((sibling instanceof MenuItem) && hasStyle(sibling, SWT.RADIO))) {
+ if (((MenuItem) sibling).getSelection()) {
+ selected = (MenuItem) sibling;
+ }
+ } else if ((sibling instanceof MenuItem) && hasStyle(sibling, SWT.SEPARATOR)) {
+ ownGroup = false;
+ selected = null;
+ }
+ if (ownGroup && selected != null) {
+ return selected;
+ }
+ }
+ return null;
+ }
+ });
+
+ if (other != null)
+ return new SWTBotMenu(other);
+ return null;
+ }
+
+ /**
+ * Gets the menu item matching the given name.
*
* @param menuName the name of the menu item that is to be found
* @return the first menu that matches the menuName

Back to the top