Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree node popup #122

Merged
merged 2 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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") {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public MyPopup() {
}});
add(new JMenuItem("Enabled menuitem"));
}});
add(new JMenuItem("Menu Item"));

setOpaque(true);
setLightWeightPopupEnabled(true);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
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)
public class TreeNodePopupKeywordsSpec extends TreeSpecification<TreeNodePopupKeywords> {
private String nodeIdentifier = "some|path";
private String menuPath = "some|menu";
private JPopupMenuOperator popupMenuOperator;
private JMenuItemOperator menuItemOperator;

public class Any {
public TreeNodePopupKeywords create() {
Expand Down Expand Up @@ -87,27 +82,25 @@ public void selectsFromTreeNodePopupMenuInSeparateThread() {
}

public class CheckingConditions {
private JMenuItem menuItem;
private String menuPath = "some|path";

public TreeNodePopupKeywords create() {
final ITreePopupMenuItemFinder menuFinder = createMockMenuFinder();
TreeNodePopupKeywords treeKeywords = populateWithMockOperatorFactory(new TreeNodePopupKeywords() {
ITreePopupMenuItemFinder createPopupMenuItemFinder(Component source) {
return menuFinder;
}
});

TreeNodePopupKeywords treeKeywords = populateWithMockOperatorFactory(new TreeNodePopupKeywords());
popupMenuOperator = mock(JPopupMenuOperator.class);
menuItemOperator = mock(JMenuItemOperator.class);
checking(new Expectations() {{
one(treeOperator).getSource(); will(returnValue(dummy(JTree.class)));
atLeast(1).of(popupMenuOperator).showMenuItem(menuPath); will(returnValue(menuItemOperator));
atLeast(1).of(popupMenuOperator).setComparator(with(any(EqualsStringComparator.class)));
one(treeOperator).createPopupOperator(nodeIdentifier);will(returnValue(popupMenuOperator));
}});

return treeKeywords;
}


public void treeNodePopupMenuItemShouldBeEnabledPassesIfMenuItemIsEnabled() throws Throwable {
checking(new Expectations() {{
one(menuItem).isEnabled(); will(returnValue(true));
one(menuItemOperator).isEnabled(); will(returnValue(true));
one(popupMenuOperator).setVisible(false);
}});

specify(new Block() {
Expand All @@ -119,7 +112,7 @@ public void run() throws Throwable {

public void treeNodePopupMenuItemShouldBeEnabledFailsIfMenuItemIsDisabled() throws Throwable {
checking(new Expectations() {{
one(menuItem).isEnabled(); will(returnValue(false));
one(menuItemOperator).isEnabled(); will(returnValue(false));
}});

specify(new Block() {
Expand All @@ -131,7 +124,8 @@ public void run() throws Throwable {

public void treeNodePopupMenuItemShouldBeDisabledPassesIfMenuItemIsDisabled() throws Throwable {
checking(new Expectations() {{
one(menuItem).isEnabled(); will(returnValue(false));
one(menuItemOperator).isEnabled(); will(returnValue(false));
one(popupMenuOperator).setVisible(false);
}});

specify(new Block() {
Expand All @@ -143,7 +137,7 @@ public void run() throws Throwable {

public void treeNodePopupMenuItemShouldBeDisabledFailsIfMenuItemIsEnabled() throws Throwable {
checking(new Expectations() {{
one(menuItem).isEnabled(); will(returnValue(true));
one(menuItemOperator).isEnabled(); will(returnValue(true));
}});

specify(new Block() {
Expand All @@ -152,15 +146,5 @@ public void run() throws Throwable {
}
}, must.raiseExactly(AssertionError.class, "Menu item '" + menuPath + "' was enabled"));
}

private ITreePopupMenuItemFinder createMockMenuFinder() {
final ITreePopupMenuItemFinder popupMenuItemFinder = mock(ITreePopupMenuItemFinder.class);
menuItem = mock(JMenuItem.class);
checking(new Expectations() {{
one(popupMenuItemFinder).findMenu(nodeIdentifier, menuPath);
will(returnValue(menuItem));
}});
return popupMenuItemFinder;
}
}
}

This file was deleted.

7 changes: 7 additions & 0 deletions src/test/resources/robot-tests/tree.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down