Search in sources :

Example 1 with DataAccessPaneState

use of au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessPaneState in project constellation by constellation-app.

the class DataAccessTabPaneNGTest method verifyNewTab.

/**
 * Verifies that creating a new tab adds it to the end of the existing tabs.
 * That appropriate menus are added to the new tab and that those menus and
 * all the button statuses are refreshed based on the initial state.
 *
 * @param tabHasEnabledPlugins true if the new tab will have enabled plugins, false otherwise
 * @param isExecuteButtonIsGo true if the execute button has the "Go" state set, false otherwise
 * @param isExecuteButtonDisabled true if the execute button is disabled, false otherwise
 * @param expectGraphDependentMenuItemsEnabled true if after the new tab is created, the graph
 *     dependent menu items should be enabled, false otherwise
 * @param expectPluginDependentMenuItemsEnabled true if after the new tab is created, the plugin
 *     dependent menu items should be enabled, false otherwise
 */
private void verifyNewTab(final boolean tabHasEnabledPlugins, final boolean isExecuteButtonIsGo, final boolean isExecuteButtonDisabled, final boolean expectGraphDependentMenuItemsEnabled, final boolean expectPluginDependentMenuItemsEnabled) {
    final QueryPhasePane queryPhasePane = mock(QueryPhasePane.class);
    final EventHandler<Event> onCloseEventHandler = mock(EventHandler.class);
    final MenuItem runMenuItem = mock(MenuItem.class);
    final MenuItem runFromHereMenuItem = mock(MenuItem.class);
    final MenuItem runToHereMenuItem = mock(MenuItem.class);
    final MenuItem deactivateAllPluginsMenuItem = mock(MenuItem.class);
    final ContextMenu contextMenu = mock(ContextMenu.class);
    // We don't need to test updateTabMenu here so it will just be verified
    // that it was called
    doNothing().when(dataAccessTabPane).updateTabMenu(any(Tab.class), anyBoolean(), anyBoolean());
    // Set up our fake tab pane
    final TabPane tabPane = mock(TabPane.class);
    final ObservableList<Tab> tabs = FXCollections.observableArrayList();
    when(dataAccessTabPane.getTabPane()).thenReturn(tabPane);
    when(tabPane.getTabs()).thenReturn(tabs);
    // Set up our fake button tool bar with an execute button
    final ButtonToolbar buttonToolbar = mock(ButtonToolbar.class);
    final Button executeButton = mock(Button.class);
    when(dataAccessPane.getButtonToolbar()).thenReturn(buttonToolbar);
    when(buttonToolbar.getExecuteButtonTop()).thenReturn(executeButton);
    final Label mockedLabel = mock(Label.class);
    final Label mockedDefaultCaptionLabel = mock(Label.class);
    try (// tab we intercept its creation and return a mock.
    final MockedConstruction<Tab> mockedTab = Mockito.mockConstruction(Tab.class, (tabMock, cntxt) -> {
        when(tabMock.getOnClosed()).thenReturn(onCloseEventHandler);
        when(tabMock.getGraphic()).thenReturn(mockedLabel);
        when(mockedLabel.getGraphic()).thenReturn(mockedDefaultCaptionLabel);
    });
        // Intercept the Label creation and insert our own mock
        final MockedConstruction<Label> mockedConstructionLabel = Mockito.mockConstruction(Label.class, (labelMock, cntxt) -> {
        });
        // that creation and insert our own mock
        final MockedConstruction<TabContextMenu> mockedTabContextMenu = Mockito.mockConstruction(TabContextMenu.class, (contextMenuMock, cntxt) -> {
            final List<Object> expectedArgs = new ArrayList<>();
            expectedArgs.add(dataAccessTabPane);
            expectedArgs.add(mockedTab.constructed().get(0));
            assertEquals(cntxt.arguments(), expectedArgs);
            when(contextMenuMock.getContextMenu()).thenReturn(contextMenu);
            when(contextMenuMock.getRunMenuItem()).thenReturn(runMenuItem);
            when(contextMenuMock.getRunFromHereMenuItem()).thenReturn(runFromHereMenuItem);
            when(contextMenuMock.getRunToHereMenuItem()).thenReturn(runToHereMenuItem);
            when(contextMenuMock.getDeactivateAllPluginsMenuItem()).thenReturn(deactivateAllPluginsMenuItem);
        });
        final MockedStatic<DataAccessPaneState> dapMockedStatic = Mockito.mockStatic(DataAccessPaneState.class);
        final MockedStatic<DataAccessTabPane> datpStateMockedStatic = Mockito.mockStatic(DataAccessTabPane.class)) {
        // Set up the variable flags that determine what values are passed into
        // updateTabMenu.
        // 
        // If the tab has enabled plugins, then the plugin menu items will be enabled
        // If the execute button is enabled, in the "Go" state and the tab has enabled
        // plugins, then the graph menu items will be enabled
        dapMockedStatic.when(DataAccessPaneState::isExecuteButtonIsGo).thenReturn(isExecuteButtonIsGo);
        datpStateMockedStatic.when(() -> DataAccessTabPane.tabHasEnabledPlugins(any(Tab.class))).thenReturn(tabHasEnabledPlugins);
        when(executeButton.isDisabled()).thenReturn(isExecuteButtonDisabled);
        dataAccessTabPane.newTab(queryPhasePane, newStepCaption);
        // Verify the tab and context menus were created
        assertEquals(mockedTab.constructed().size(), 1);
        assertEquals(mockedTabContextMenu.constructed().size(), 1);
        assertEquals(mockedConstructionLabel.constructed().size(), 2);
        // Verify that the context menu was created correctly
        final TabContextMenu newTabContextMenu = mockedTabContextMenu.constructed().get(0);
        verify(newTabContextMenu).init();
        verify(queryPhasePane).addGraphDependentMenuItems(runMenuItem, runFromHereMenuItem, runToHereMenuItem);
        verify(queryPhasePane).addPluginDependentMenuItems(deactivateAllPluginsMenuItem);
        // Verify that the tab was created correctly
        final Tab newTab = mockedTab.constructed().get(0);
        // Verify that the label was created correctly
        final Label newLabel = mockedConstructionLabel.constructed().get(0);
        verify(newTab).setContextMenu(contextMenu);
        verify(newTab).setClosable(true);
        final ArgumentCaptor<ScrollPane> scrollPaneCaptor = ArgumentCaptor.forClass(ScrollPane.class);
        verify(newTab).setContent(scrollPaneCaptor.capture());
        assertTrue(scrollPaneCaptor.getValue().isFitToWidth());
        assertEquals(scrollPaneCaptor.getValue().getContent(), queryPhasePane);
        assertEquals(scrollPaneCaptor.getValue().getStyle(), "-fx-background-color: black;");
        final ArgumentCaptor<Tooltip> tooltipCaptor = ArgumentCaptor.forClass(Tooltip.class);
        verify(newTab).setTooltip(tooltipCaptor.capture());
        assertEquals(tooltipCaptor.getValue().getText(), "Right click for more options");
        // Ensure the tab menus states were updated
        verify(dataAccessTabPane).updateTabMenu(newTab, expectGraphDependentMenuItemsEnabled, expectPluginDependentMenuItemsEnabled);
        // Verify that the tab pane has the correct tabs
        assertEquals(tabs, FXCollections.observableArrayList(newTab));
        // Verify the on close event for the new tab behaves as expected
        final ArgumentCaptor<EventHandler<Event>> onCloseCaptor = ArgumentCaptor.forClass(EventHandler.class);
        verify(newTab).setOnClosed(onCloseCaptor.capture());
        final Event event = mock(Event.class);
        onCloseCaptor.getValue().handle(event);
        verify(mockedDefaultCaptionLabel).setText("Step 1");
        verify(onCloseEventHandler).handle(event);
        // Verify the on Mouse Clicked event for the new Label behaves as expected
        final ArgumentCaptor<EventHandler<Event>> onClickCaptor = ArgumentCaptor.forClass(EventHandler.class);
        verify(newLabel).setOnMouseClicked(onClickCaptor.capture());
        final MouseEvent clickEvent = mock(MouseEvent.class);
        onClickCaptor.getValue().handle(clickEvent);
        verify(dataAccessTabPane).labelClickEvent(newTab, newLabel, clickEvent);
    }
}
Also used : QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) Label(javafx.scene.control.Label) ArrayList(java.util.ArrayList) EventHandler(javafx.event.EventHandler) ContextMenu(javafx.scene.control.ContextMenu) Button(javafx.scene.control.Button) DataAccessPaneState(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessPaneState) TabPane(javafx.scene.control.TabPane) MouseEvent(javafx.scene.input.MouseEvent) Tooltip(javafx.scene.control.Tooltip) MenuItem(javafx.scene.control.MenuItem) Tab(javafx.scene.control.Tab) ScrollPane(javafx.scene.control.ScrollPane) Event(javafx.event.Event) MouseEvent(javafx.scene.input.MouseEvent)

Aggregations

DataAccessPaneState (au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessPaneState)1 QueryPhasePane (au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane)1 ArrayList (java.util.ArrayList)1 Event (javafx.event.Event)1 EventHandler (javafx.event.EventHandler)1 Button (javafx.scene.control.Button)1 ContextMenu (javafx.scene.control.ContextMenu)1 Label (javafx.scene.control.Label)1 MenuItem (javafx.scene.control.MenuItem)1 ScrollPane (javafx.scene.control.ScrollPane)1 Tab (javafx.scene.control.Tab)1 TabPane (javafx.scene.control.TabPane)1 Tooltip (javafx.scene.control.Tooltip)1 MouseEvent (javafx.scene.input.MouseEvent)1