Class GridContextMenuTester

  • All Implemented Interfaces:
    com.vaadin.browserless.Clickable

    
    public class GridContextMenuTester<T extends GridContextMenu<Y>, Y>
    extends ComponentTester<T>
                        

    Tester for GridContextMenu components.

    A grid context menu is always about a row: the user right-clicks a row, and the events the menu fires report that row. open therefore takes the row to open the menu on, addressed by its zero-based index among the rows the user sees, the same way com.vaadin.flow.component.grid.GridTester addresses rows. The menu content is not part of the UI until the menu opens, so clickItem(...) and find only see the items of an open menu.
    Since:

    25.3

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final T component
    • Constructor Summary

      Constructors 
      Constructor Description
      GridContextMenuTester(T component) Wrap grid context menu for testing.
      GridContextMenuTester(T component, int row) Wrap grid context menu for testing, targeting the given row.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      void open() Opens the context menu on the row it currently targets, as if the user had asked for it in the browser.
      void open(int row) Opens the context menu on the given row, as if the user had asked for it in the browser.
      void open(int row, String columnKey) Opens the context menu on the given row and column, as if the user had asked for it in the browser.
      void close() Closes the context menu.
      void clickItem(String topLevelText, Array<String> nestedItemsText) Simulates a click on the item that matches the given text.
      void clickItem(int topLevelPosition, Array<int> nestedItemsPositions) Simulates a click on the item at the given position in the menu.
      boolean isItemChecked(String topLevelText, Array<String> nestedItemsText) Checks if the checkable menu item matching given text is checked.
      boolean isItemChecked(int topLevelPosition, Array<int> nestedItemsPositions) Checks if the checkable menu item at given position is checked.
      String getItemTooltipText(String topLevelText, Array<String> nestedItemsText) Gets the tooltip text of the menu item matching the given text.
      String getItemTooltipText(int topLevelPosition, Array<int> nestedItemsPositions) Gets the tooltip text of the menu item at the given position in the menu.
      List<String> getItemTexts() Gets the texts of the menu items, as the browser shows them.
      List<String> getItemTexts(String topLevelText, Array<String> nestedItemsText) Gets the texts of the items of the sub menu of the item matching the given text, as the browser shows them.
      <R extends Component> ComponentQuery<R> find(Class<R> componentType) Gets a to search for component of the given type nested inside the wrapped component.
      • Methods inherited from class com.vaadin.browserless.ComponentTester

        blur, ensureComponentIsUsable, focus, getComponent, isFocused, isUsable, setModal
      • Methods inherited from class com.vaadin.browserless.Clickable

        click, click, click, middleClick, middleClick, rightClick, rightClick
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GridContextMenuTester

        GridContextMenuTester(T component)

        Wrap grid context menu for testing.

        The menu targets no row, so it has to be opened with open.
        Parameters:
        component - target grid context menu
      • GridContextMenuTester

        GridContextMenuTester(T component, int row)

        Wrap grid context menu for testing, targeting the given row.

        The index is 0 based and counts the rows the user sees. Opening the menu with open opens it on that row.
        Parameters:
        component - target grid context menu
        row - row the menu is about
    • Method Detail

      • open

         void open()

        Opens the context menu on the row it currently targets, as if the user had asked for it in the browser.

        The target row is the one given to contextMenu, so this method is the counterpart of that entry point. Use open to open the menu on a row directly. It does not render any client-side overlay, it only simulates the server-side state changes that opening the menu produces: the menu is attached to the UI, so its items become findable and clickable, and a GridContextMenuOpenedEvent reporting the target row is fired.
      • open

         void open(int row)

        Opens the context menu on the given row, as if the user had asked for it in the browser.

        The index is 0 based and counts the rows the user sees. The row is reported by the events the menu fires, so GridContextMenuItemClickEvent.getItem() and GridContextMenuOpenedEvent.getItem() hold the item on that row.
        Parameters:
        row - row to open the menu on
      • open

         void open(int row, String columnKey)

        Opens the context menu on the given row and column, as if the user had asked for it in the browser.

        The column is reported by GridContextMenuOpenedEvent.getColumnId().
        Parameters:
        row - row to open the menu on, 0 based
        columnKey - key of the column to open the menu on, as set with Grid.Column.setKey(String)
      • close

         void close()

        Closes the context menu.

      • clickItem

         void clickItem(String topLevelText, Array<String> nestedItemsText)

        Simulates a click on the item that matches the given text.

        For a nested menu item, provide the text of each menu item in the hierarchy. The path to the menu item must reflect what is seen in the browser, meaning that hidden items are ignored. If there are multiple visible items at the same level with the same text, an IllegalStateException is thrown because the target is ambiguous. Disabled or invisible items cannot be clicked and will also cause an IllegalStateException. The menu has to be open, since its items are not part of the UI before that.
        Parameters:
        topLevelText - the text content of the top level menu item, not null.
        nestedItemsText - text content of the nested menu items
      • clickItem

         void clickItem(int topLevelPosition, Array<int> nestedItemsPositions)

        Simulates a click on the item at the given position in the menu.

        For a nested menu item, provide the position of each sub menu that should be navigated to reach the requested item. Positions are zero-based and refer only to items that are visible at each menu level, i.e. hidden items are ignored (the same way as in the browser). Disabled or invisible items cannot be clicked and will cause an IllegalStateException. The menu has to be open, since its items are not part of the UI before that.
        Parameters:
        topLevelPosition - the zero-based position of the item in the menu, as it will be seen in the browser.
        nestedItemsPositions - the zero-based position of the nested items, relative to the parent menu
      • isItemChecked

         boolean isItemChecked(String topLevelText, Array<String> nestedItemsText)

        Checks if the checkable menu item matching given text is checked.

        For a nested menu item, provide the text of each menu item in the hierarchy.
        Parameters:
        topLevelText - the text content of the top level menu item, not null.
        nestedItemsText - text content of the nested menu items
        Returns:

        true if the item at given path is checked, otherwise false.

      • isItemChecked

         boolean isItemChecked(int topLevelPosition, Array<int> nestedItemsPositions)

        Checks if the checkable menu item at given position is checked.

        For a nested menu item, provide the position of each sub menu that should be navigated to reach the requested item.
        Parameters:
        topLevelPosition - the zero-based position of the item in the menu, as it will be seen in the browser.
        nestedItemsPositions - the zero-based position of the nested items, relative to the parent menu
        Returns:

        true if the item at given position is checked, otherwise false.

      • getItemTooltipText

         String getItemTooltipText(String topLevelText, Array<String> nestedItemsText)

        Gets the tooltip text of the menu item matching the given text.

        For a nested menu item, provide the text of each menu item in the hierarchy.
        Parameters:
        topLevelText - the text content of the top level menu item, not null.
        nestedItemsText - text content of the nested menu items
        Returns:

        the tooltip text of the menu item at given path, or null if the item has no tooltip set.

      • getItemTooltipText

         String getItemTooltipText(int topLevelPosition, Array<int> nestedItemsPositions)

        Gets the tooltip text of the menu item at the given position in the menu.

        For a nested menu item, provide the position of each sub menu that should be navigated to reach the requested item.
        Parameters:
        topLevelPosition - the zero-based position of the item in the menu, as it will be seen in the browser.
        nestedItemsPositions - the zero-based position of the nested items, relative to the parent menu
        Returns:

        the tooltip text of the menu item at given position, or null if the item has no tooltip set.

      • getItemTexts

         List<String> getItemTexts()

        Gets the texts of the menu items, as the browser shows them.

        Hidden items are ignored, so the returned texts are aligned with the positions used by clickItem. A text can also be given to clickItem, as long as it identifies a single enabled item: a text that several visible items share is ambiguous, and a disabled item cannot be clicked. An item created from a component has no text of its own, and is reported as an empty string. Use find to reach such an item. The menu has to be open, since its items are not part of the UI before that. The items are the ones the menu offers for the row it was opened on, so a dynamic content handler has run by then.
        
        
        menu.addItem("Edit", event -> {
        });
        menu.addItem("Hidden", event -> {
        }).setVisible(false);
        menu.addItem("Share");
        
        tester.open(0);
        
        // ["Edit", "Share"]
        tester.getItemTexts();
        
        Returns:

        the texts of the visible top level menu items, in the order they are shown in

      • getItemTexts

         List<String> getItemTexts(String topLevelText, Array<String> nestedItemsText)

        Gets the texts of the items of the sub menu of the item matching the given text, as the browser shows them.

        For a nested sub menu, provide the text of each menu item in the hierarchy, the same way as in clickItem. Hidden items are ignored at every level, both when following the path and in the returned texts. The menu has to be open, since its items are not part of the UI before that.
        
        
        var subMenu = menu.addItem("Share").getSubMenu();
        subMenu.addItem("Copy link", event -> {
        });
        subMenu.addItem("Email", event -> {
        });
        
        tester.open(0);
        
        // ["Copy link", "Email"]
        tester.getItemTexts("Share");
        
        Parameters:
        topLevelText - the text content of the top level menu item, not null.
        nestedItemsText - text content of the nested menu items
        Returns:

        the texts of the visible items of the sub menu, in the order they are shown in

      • find

         <R extends Component> ComponentQuery<R> find(Class<R> componentType)

        Gets a to search for component of the given type nested inside the wrapped component.

        The query walks the server-side component tree. A component that another component renders per item, such as the component a ComponentRenderer} column renders for a grid row, is rendered into the column and not into the tree, and the content of an overlay, such as a context menu, is attached only while the overlay is open. Neither is reachable this way, and the lookup returns an empty result rather than failing, so reach those components through the owning component tester instead: GridTester.getCellComponent(row, column)} for grid cells, ContextMenuTester.open()} and then clickItem(...)} for a context menu, and GridTester.contextMenu(row).open()} for a grid context menu.

        Can be used to find components in the context menu. The menu content is only part of the UI while the menu is open, so components are returned in a detached state unless open has been called previously.

        // view:
        GridContextMenu<Person> menu = grid.addContextMenu();
        menu.addItem(new Checkbox("Show inactive"), event -> {
        });
        
        // test:
        GridContextMenuTester<GridContextMenu<Person>, Person> menuTester = test(
                view.menu);
        menuTester.open(0);
        Checkbox checkbox = menuTester.find(Checkbox.class).single();