From f1a81b5d42c385790a1fc68ad7d390acfac8a486 Mon Sep 17 00:00:00 2001 From: mparvu Date: Fri, 22 Feb 2019 16:54:34 +0200 Subject: [PATCH 1/2] Rewritten `Tree Node Popup Menu Item Should Be Enabled/Disabled` keywords --- .../keyword/tree/TreeNodePopupKeywords.java | 42 +++---------- .../swing/tree/ITreePopupMenuItemFinder.java | 23 ------- .../swing/tree/TreePopupMenuItemFinder.java | 63 ------------------- .../tree/TreeNodePopupKeywordsSpec.java | 46 +++++--------- .../tree/TreePopupMenuItemFinderSpec.java | 54 ---------------- 5 files changed, 23 insertions(+), 205 deletions(-) delete mode 100644 src/main/java/org/robotframework/swing/tree/ITreePopupMenuItemFinder.java delete mode 100644 src/main/java/org/robotframework/swing/tree/TreePopupMenuItemFinder.java delete mode 100644 src/test/java/org/robotframework/swing/tree/TreePopupMenuItemFinderSpec.java diff --git a/src/main/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywords.java b/src/main/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywords.java index d353558f..c28f0dc7 100644 --- a/src/main/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywords.java +++ b/src/main/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywords.java @@ -16,23 +16,13 @@ package org.robotframework.swing.keyword.tree; -import java.awt.Component; -import java.awt.event.KeyEvent; - -import javax.swing.JMenuItem; - import org.junit.Assert; - import org.netbeans.jemmy.operators.JPopupMenuOperator; import org.robotframework.javalib.annotation.ArgumentNames; import org.robotframework.javalib.annotation.RobotKeyword; import org.robotframework.javalib.annotation.RobotKeywords; import org.robotframework.swing.comparator.EqualsStringComparator; -import org.robotframework.swing.tree.ITreePopupMenuItemFinder; -import org.robotframework.swing.tree.TreePopupMenuItemFinder; import org.robotframework.swing.tree.TreeSupport; - -import abbot.tester.ComponentTester; import org.robotframework.swing.util.SwingWaiter; @RobotKeywords @@ -77,8 +67,11 @@ public void selectFromPopupMenuOnSelectedTreeNodes(String identifier, String men + "| `Tree Node Popup Menu Item Should Be Enabled` | 0 | 1 | New Folder |\n") @ArgumentNames({"identifier", "nodeIdentifier", "menuPath"}) public void treeNodePopupMenuItemShouldBeEnabled(String identifier, String nodeIdentifier, String menuPath) { - boolean menuItemIsEnabled = menuItemIsEnabled(identifier, nodeIdentifier, menuPath); + JPopupMenuOperator popupOperator = treeOperator(identifier).createPopupOperator(nodeIdentifier); + popupOperator.setComparator(new EqualsStringComparator()); + boolean menuItemIsEnabled = popupOperator.showMenuItem(menuPath).isEnabled(); Assert.assertTrue("Menu item '" + menuPath + "' was disabled", menuItemIsEnabled); + popupOperator.setVisible(false); } @RobotKeyword("Fails if given popup menu item is enabled.\n\n" @@ -87,29 +80,10 @@ public void treeNodePopupMenuItemShouldBeEnabled(String identifier, String nodeI + "| `Tree Node Popup Menu Item Should Be Disabled` | 0 | 1 | New Folder |\n") @ArgumentNames({"identifier", "nodeIdentifier", "menuPath"}) public void treeNodePopupMenuItemShouldBeDisabled(String identifier, String nodeIdentifier, String menuPath) { - boolean menuItemIsEnabled = menuItemIsEnabled(identifier, nodeIdentifier, menuPath); + JPopupMenuOperator popupOperator = treeOperator(identifier).createPopupOperator(nodeIdentifier); + popupOperator.setComparator(new EqualsStringComparator()); + boolean menuItemIsEnabled = popupOperator.showMenuItem(menuPath).isEnabled(); Assert.assertFalse("Menu item '" + menuPath + "' was enabled", menuItemIsEnabled); + popupOperator.setVisible(false); } - - private boolean menuItemIsEnabled(String identifier, String nodeIdentifier, String menuPath) { - try { - return createPopupMenuItem(identifier, nodeIdentifier, menuPath).isEnabled(); - } finally { - closePopup(); - } - } - - private void closePopup() { - new ComponentTester().actionKeyStroke(KeyEvent.VK_ESCAPE); - } - - private JMenuItem createPopupMenuItem(String identifier, String nodeIdentifier, String menuPath) { - Component source = treeOperator(identifier).getSource(); - return createPopupMenuItemFinder(source).findMenu(nodeIdentifier, menuPath); - } - - ITreePopupMenuItemFinder createPopupMenuItemFinder(Component source) { - return new TreePopupMenuItemFinder(source); - } - } diff --git a/src/main/java/org/robotframework/swing/tree/ITreePopupMenuItemFinder.java b/src/main/java/org/robotframework/swing/tree/ITreePopupMenuItemFinder.java deleted file mode 100644 index c6f896aa..00000000 --- a/src/main/java/org/robotframework/swing/tree/ITreePopupMenuItemFinder.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2008-2011 Nokia Siemens Networks Oyj - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.robotframework.swing.tree; - -import javax.swing.JMenuItem; - -public interface ITreePopupMenuItemFinder { - JMenuItem findMenu(String nodeIdentifier, String menuPath); -} diff --git a/src/main/java/org/robotframework/swing/tree/TreePopupMenuItemFinder.java b/src/main/java/org/robotframework/swing/tree/TreePopupMenuItemFinder.java deleted file mode 100644 index a4f742c7..00000000 --- a/src/main/java/org/robotframework/swing/tree/TreePopupMenuItemFinder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2008-2011 Nokia Siemens Networks Oyj - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.robotframework.swing.tree; - -import java.awt.Component; - -import javax.swing.JMenuItem; - -import org.robotframework.swing.arguments.IdentifierHandler; - -import abbot.finder.BasicFinder; -import abbot.finder.matchers.JMenuItemMatcher; -import abbot.tester.ComponentTester; -import abbot.tester.JComponentTester; -import abbot.tester.JTreeLocation; -import abbot.util.AWT; - -public class TreePopupMenuItemFinder implements ITreePopupMenuItemFinder { - private BasicFinder basicFinder = new BasicFinder(); - private ComponentTester componentTester = new JComponentTester(); - private IdentifierHandler treeLocationFactory = new JTreeLocationFactory(); - private final Component tree; - - public TreePopupMenuItemFinder(Component tree) { - this.tree = tree; - AWT.POPUP_TIMEOUT = 250000; - } - - public JMenuItem findMenu(String nodeIdentifier, String menuPath) { - return findMenu(treeLocationFactory.parseArgument(nodeIdentifier), menuPath); - } - - private JMenuItem findMenu(JTreeLocation treeLocation, String menuPath) { - popupMenuOnContainer(treeLocation); - return findMenu(menuPath); - } - - private void popupMenuOnContainer(JTreeLocation nodeLocation) { - componentTester.actionShowPopupMenu(tree, nodeLocation); - } - - private JMenuItem findMenu(String menuPath) { - try { - return (JMenuItem) basicFinder.find(new JMenuItemMatcher(menuPath)); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/test/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywordsSpec.java b/src/test/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywordsSpec.java index ebeedbaa..3be2f460 100644 --- a/src/test/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywordsSpec.java +++ b/src/test/java/org/robotframework/swing/keyword/tree/TreeNodePopupKeywordsSpec.java @@ -1,21 +1,15 @@ package org.robotframework.swing.keyword.tree; -import java.awt.Component; - -import javax.swing.JMenuItem; -import javax.swing.JTree; - import jdave.Block; import jdave.junit4.JDaveRunner; - import org.jmock.Expectations; import org.junit.runner.RunWith; +import org.netbeans.jemmy.operators.JMenuItemOperator; import org.netbeans.jemmy.operators.JPopupMenuOperator; import org.robotframework.jdave.contract.FieldIsNotNullContract; import org.robotframework.jdave.contract.RobotKeywordContract; import org.robotframework.jdave.contract.RobotKeywordsContract; import org.robotframework.swing.comparator.EqualsStringComparator; -import org.robotframework.swing.tree.ITreePopupMenuItemFinder; @RunWith(JDaveRunner.class) @@ -23,6 +17,7 @@ public class TreeNodePopupKeywordsSpec extends TreeSpecification { - public class Any { - private JTree tree = dummy(JTree.class); - private JMenuItem menuItem = dummy(JMenuItem.class); - - public TreePopupMenuItemFinder create() { - return new TreePopupMenuItemFinder(tree); - } - - public void findsPopupMenuItemWithTreeRowIndexAndMenuPath() throws Exception { - injectMockTreeLocationFactory(); - - final BasicFinder basicFinder = injectMockToContext(BasicFinder.class); - - checking(new Expectations() {{ - one(basicFinder).find(with(any(JMenuItemMatcher.class))); - will(returnValue(menuItem)); - }}); - - specify(context.findMenu("some|node", "some|menu"), must.equal(menuItem)); - } - - private void injectMockTreeLocationFactory() { - final IdentifierHandler treeLocationFactory = injectMockToContext("treeLocationFactory", IdentifierHandler.class); - final JTreeLocation treeLocation = mock(JTreeLocation.class); - final ComponentTester componentTester = injectMockToContext("componentTester", ComponentTester.class); - - checking(new Expectations() {{ - one(treeLocationFactory).parseArgument("some|node"); - will(returnValue(treeLocation)); - one(componentTester).actionShowPopupMenu(tree, treeLocation); - }}); - } - } -} From 23cd3dd4fbdfb163c9ac931a752e80a14e012c8a Mon Sep 17 00:00:00 2001 From: mparvu Date: Mon, 25 Feb 2019 16:47:20 +0200 Subject: [PATCH 2/2] Added acceptance test --- .../java/org/robotframework/swing/testapp/TestMenuBar.java | 1 + .../java/org/robotframework/swing/testapp/TestTree.java | 1 + src/test/resources/robot-tests/tree.robot | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/src/main/java/org/robotframework/swing/testapp/TestMenuBar.java b/src/main/java/org/robotframework/swing/testapp/TestMenuBar.java index bc57bf76..07bf9bc6 100644 --- a/src/main/java/org/robotframework/swing/testapp/TestMenuBar.java +++ b/src/main/java/org/robotframework/swing/testapp/TestMenuBar.java @@ -58,6 +58,7 @@ public void actionPerformed(ActionEvent e) { }); add(new JCheckBoxMenuItem("Test menu checkbox")); + add(new TestMenuItem("Menu Item").setDisabled()); }}); add(new TestMenu("Test Menu2") {{ add(new TestMenuItem("placeholder item 1") {}); diff --git a/src/main/java/org/robotframework/swing/testapp/TestTree.java b/src/main/java/org/robotframework/swing/testapp/TestTree.java index 7c190996..555c659b 100644 --- a/src/main/java/org/robotframework/swing/testapp/TestTree.java +++ b/src/main/java/org/robotframework/swing/testapp/TestTree.java @@ -200,6 +200,7 @@ public MyPopup() { }}); add(new JMenuItem("Enabled menuitem")); }}); + add(new JMenuItem("Menu Item")); setOpaque(true); setLightWeightPopupEnabled(true); diff --git a/src/test/resources/robot-tests/tree.robot b/src/test/resources/robot-tests/tree.robot index 21c687bf..a186f29c 100644 --- a/src/test/resources/robot-tests/tree.robot +++ b/src/test/resources/robot-tests/tree.robot @@ -315,6 +315,13 @@ Tree Keywords Fail If Context Is Not Correct keywordShouldFailBecauseContextIsIllegal getTreeNodeLabel ${treeName} 0 [Teardown] selectMainWindow +Tree Item With Same Name As Menu Item Enablement Status + [Tags] display-required + menuItemShouldBeDisabled Test Menu|Menu Item + selectMainWindow + treeNodePopupMenuItemShouldBeEnabled ${treeName} ${rootNode} Menu Item + popupMenuShouldNotBeDisplayed + *** Keywords *** keywordShouldFailBecauseContextIsIllegal [Arguments] ${keyword} @{arguments}