2

Since MenuItem is not a Node, I'm not able to look it up. How do I test, if some MenuItem is disabled? I've tried to look it up as it was a node and it returned me something, which looks like this..

(toString representation of returned object):

(ContextMenuContent$MenuItemContainer[id=mnEditHrom, styleClass=menu-item])

But i can't cast MenuItem on that, it says "Node cannot be converted to MenuItem" and when I call isDisabled() function on what was returned, i get incorrect information.

Lets say I have MenuItem with "mnEdit" id, which is disabled. When i call

find("#mnEdit").isDisabled();

it returns false. Find method looks like this:

public <T extends Node> T find(String query) 
{
  return (T) lookup(query).queryAll().iterator().next();
} 

So again, how do I test whether some MenuItem is disabled or not in testFx?

Mono
  • 206
  • 1
  • 21

2 Answers2

2

You almost done in the original post. When you get the MenuItemContainer get the MenuItem firstly and finally call isDisable():

ContextMenuContent.MenuItemContainer actualMenuItemContainer = find("#mnEdit");

boolean actualResult = actualMenuItemContainer.getItem().isDisable();
Dmytro Maslenko
  • 2,247
  • 9
  • 16
0

I solved it by looking up MenuBar, identifying item I want to test by its Id and since I have now MenuItem obejct in hands, I can call isDisable() on it.

MenuTest.class

CommonTests common = new CommmonTests();  

@Test
public void disabledMenuItemTest() 
{
  common.disabledMenuItemTest("#mainMenu", "mnEditHrom", true);
}

CommonTests.class

TestUtils utils = new TestUtils();

public void disabledMenuItemTest(String menuBarSelector, String menuItemId, boolean expected)
{
  Boolean actual = utils.isMenuItemDisabled(menuBarSelector, menuItemId);
  if (actual != null)
    assertEquals("MenuItem "+menuItemId+" je enabled/disabled (expected = "+expected+").", expected, actual.booleanValue());
  else
    fail("MenuBar/MenuItem not found.");
}

TestUtils.class

public Boolean isMenuItemDisabled(String menuBarSelector, String menuItemId)
{
  ArrayList<MenuItem> list = getAllMenuItems(menuBarSelector);
  Boolean disabled = null;
  if(list != null)
  {
    for(MenuItem item : list)
    {
      if(item.getId() != null && item.getId().equals(menuItemId))
        return item.isDisable();
    }
  }
  return disabled;
}

private ArrayList<MenuItem> getAllMenuItems(String menuBarSelector)
{
  ArrayList<MenuItem> itemsList = new ArrayList<MenuItem>();
  MenuBar menuBar = (MenuBar) find(menuBarSelector);
  if(menuBar != null)
  {
    menuBar.getMenus().forEach(menu -> {
      menu.getItems().forEach(menuItem -> {
        itemsList.add(menuItem);
      });
    });
    return itemsList;
  }
  return null;
}
Mono
  • 206
  • 1
  • 21