Search in sources :

Example 1 with TableViewPreferencesIoProvider

use of au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider in project constellation by constellation-app.

the class PreferenceMenuNGTest method verifySavePreferencesAction.

/**
 * Verifies that when the save userTablePreferences button is pressed then
 * it will call out to
 * {@link TableViewPreferencesIoProvider#savePreferences(GraphElementType, TableView, int)}.
 * If certain values are not set, then it will not save the preferences and
 * just return.
 * <p/>
 * There is a lot of mock setup for this so the code tries to re-use as much
 * of that as possible which is why its a little weird.
 *
 * @param savePreferencesMenu the save userTablePreferences menu
 * @param isTableViewColumnsEmpty true if when
 * {@link TableView#getColumns()} is called it should return an empty list,
 * false otherwise
 * @param isActivGraphNull true if when
 * {@link GraphManager#getActiveGraph()} is called it should return null,
 * false otherwise
 * @param expectSavePrefsCalled true if it is expected that
 * {@link TableViewPreferencesIoProvider#savePreferences(GraphElementType, TableView, int)}
 * should have been called, false otherwise
 */
private void verifySavePreferencesAction(final MenuItem savePreferencesMenu, final boolean isTableViewColumnsEmpty, final boolean isActivGraphNull, final boolean expectSavePrefsCalled) {
    try (final MockedStatic<TableViewPreferencesIoProvider> tablePrefsIOUtilsMockedStatic = Mockito.mockStatic(TableViewPreferencesIoProvider.class);
        final MockedStatic<GraphManager> graphManagerMockedStatic = Mockito.mockStatic(GraphManager.class)) {
        final TableView<ObservableList<String>> tableView = mock(TableView.class);
        when(table.getTableView()).thenReturn(tableView);
        final TableViewState tableViewState = new TableViewState();
        tableViewState.setElementType(GraphElementType.VERTEX);
        when(tableViewTopComponent.getCurrentState()).thenReturn(tableViewState);
        final UserTablePreferences userTablePreferences = new UserTablePreferences();
        userTablePreferences.setMaxRowsPerPage(42);
        when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
        final GraphManager graphManager = mock(GraphManager.class);
        graphManagerMockedStatic.when(GraphManager::getDefault).thenReturn(graphManager);
        final Graph graph = mock(Graph.class);
        final ActionEvent actionEvent = mock(ActionEvent.class);
        if (isTableViewColumnsEmpty) {
            when(tableView.getColumns()).thenReturn(FXCollections.observableArrayList());
        } else {
            when(tableView.getColumns()).thenReturn(FXCollections.observableArrayList(mock(TableColumn.class)));
        }
        if (isActivGraphNull) {
            when(graphManager.getActiveGraph()).thenReturn(null);
        } else {
            when(graphManager.getActiveGraph()).thenReturn(graph);
        }
        savePreferencesMenu.getOnAction().handle(actionEvent);
        if (expectSavePrefsCalled) {
            tablePrefsIOUtilsMockedStatic.verify(() -> TableViewPreferencesIoProvider.savePreferences(GraphElementType.VERTEX, tableView, 42));
        } else {
            tablePrefsIOUtilsMockedStatic.verifyNoInteractions();
        }
        verify(actionEvent).consume();
    }
}
Also used : GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) Graph(au.gov.asd.tac.constellation.graph.Graph) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ObservableList(javafx.collections.ObservableList) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) ActionEvent(javafx.event.ActionEvent) TableViewPreferencesIoProvider(au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider)

Example 2 with TableViewPreferencesIoProvider

use of au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider in project constellation by constellation-app.

the class PreferenceMenuNGTest method loadPreferences.

@Test
public void loadPreferences() {
    try (final MockedStatic<TableViewPreferencesIoProvider> tablePrefIOUtilsMockedStatic = Mockito.mockStatic(TableViewPreferencesIoProvider.class)) {
        final TableViewState tableViewState = new TableViewState();
        tableViewState.setElementType(GraphElementType.VERTEX);
        final Graph graph = mock(Graph.class);
        when(tableViewTopComponent.getCurrentState()).thenReturn(tableViewState);
        when(tableViewTopComponent.getCurrentGraph()).thenReturn(graph);
        // These are the existing table userTablePreferences
        final UserTablePreferences currentTablePreferences = new UserTablePreferences();
        currentTablePreferences.setMaxRowsPerPage(42);
        // There are 4 columns in the table. Set them up and all required variables
        // that describe them
        when(activeTableReference.getUserTablePreferences()).thenReturn(currentTablePreferences);
        final TableView<ObservableList<String>> tableView = mock(TableView.class);
        when(table.getTableView()).thenReturn(tableView);
        final TableColumn<ObservableList<String>, String> column1 = mock(TableColumn.class);
        final TableColumn<ObservableList<String>, String> column2 = mock(TableColumn.class);
        final TableColumn<ObservableList<String>, String> column3 = mock(TableColumn.class);
        final TableColumn<ObservableList<String>, String> column4 = mock(TableColumn.class);
        when(column1.getText()).thenReturn("Column1");
        when(column2.getText()).thenReturn("Column2");
        when(column3.getText()).thenReturn("Column3");
        when(column4.getText()).thenReturn("Column4");
        when(tableView.getColumns()).thenReturn(FXCollections.observableList(List.of(column1, column2, column3, column4)));
        final Attribute attribute1 = mock(Attribute.class);
        final Attribute attribute2 = mock(Attribute.class);
        final Attribute attribute3 = mock(Attribute.class);
        final Attribute attribute4 = mock(Attribute.class);
        final CopyOnWriteArrayList<Column> columnIndex = new CopyOnWriteArrayList<>();
        columnIndex.add(new Column("type1", attribute1, column1));
        columnIndex.add(new Column("type2", attribute2, column2));
        columnIndex.add(new Column("type3", attribute3, column3));
        columnIndex.add(new Column("type4", attribute4, column4));
        when(table.getColumnIndex()).thenReturn(columnIndex);
        // The loaded userTablePreferences specifies 3 of the 4 columns in the table
        final UserTablePreferences loadedTablePreferences = new UserTablePreferences();
        loadedTablePreferences.setColumnOrder(List.of("Column1", "Column2", "Column4"));
        loadedTablePreferences.setSortByColumn(ImmutablePair.of("Column2", TableColumn.SortType.DESCENDING));
        loadedTablePreferences.setMaxRowsPerPage(150);
        when(activeTableReference.updateVisibleColumns(any(Graph.class), any(TableViewState.class), any(List.class), eq(UpdateMethod.REPLACE))).thenReturn(CompletableFuture.completedFuture(null));
        // Create page size option menu items, with one matching the pages size
        // in the loaded prefs above
        final ToggleGroup toggleGroup = mock(ToggleGroup.class);
        final RadioMenuItem pageSizeOption1 = mock(RadioMenuItem.class);
        final RadioMenuItem pageSizeOption2 = mock(RadioMenuItem.class);
        when(pageSizeOption1.getText()).thenReturn("100");
        when(pageSizeOption2.getText()).thenReturn("150");
        when(preferencesMenu.getPageSizeToggle()).thenReturn(toggleGroup);
        when(toggleGroup.getToggles()).thenReturn(FXCollections.observableList(List.of(pageSizeOption1, pageSizeOption2)));
        // Return the loaded userTablePreferences when the load call is made
        tablePrefIOUtilsMockedStatic.when(() -> TableViewPreferencesIoProvider.getPreferences(GraphElementType.VERTEX)).thenReturn(loadedTablePreferences);
        // Start the test
        preferencesMenu.loadPreferences();
        verify(column1).setVisible(true);
        verify(column2).setVisible(true);
        verify(column3, times(0)).setVisible(anyBoolean());
        verify(column4).setVisible(true);
        verify(activeTableReference).saveSortDetails("Column2", TableColumn.SortType.DESCENDING);
        verify(activeTableReference).updateVisibleColumns(same(graph), same(tableViewState), eq(List.of(Tuple.create("type1", attribute1), Tuple.create("type2", attribute2), Tuple.create("type4", attribute4))), eq(UpdateMethod.REPLACE));
        verify(pageSizeOption1, times(0)).setSelected(true);
        verify(pageSizeOption2, times(1)).setSelected(true);
        assertEquals(150, currentTablePreferences.getMaxRowsPerPage());
    }
}
Also used : Attribute(au.gov.asd.tac.constellation.graph.Attribute) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RadioMenuItem(javafx.scene.control.RadioMenuItem) Graph(au.gov.asd.tac.constellation.graph.Graph) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ObservableList(javafx.collections.ObservableList) Column(au.gov.asd.tac.constellation.views.tableview.api.Column) TableColumn(javafx.scene.control.TableColumn) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) ToggleGroup(javafx.scene.control.ToggleGroup) List(java.util.List) ObservableList(javafx.collections.ObservableList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TableViewPreferencesIoProvider(au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.testng.annotations.Test)

Example 3 with TableViewPreferencesIoProvider

use of au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider in project constellation by constellation-app.

the class PreferenceMenuNGTest method loadPreferencesColumnOrderIsEmpty.

@Test
public void loadPreferencesColumnOrderIsEmpty() {
    try (final MockedStatic<TableViewPreferencesIoProvider> tablePrefIOUtilsMockedStatic = Mockito.mockStatic(TableViewPreferencesIoProvider.class)) {
        final TableViewState tableViewState = new TableViewState();
        tableViewState.setElementType(GraphElementType.VERTEX);
        when(tableViewTopComponent.getCurrentState()).thenReturn(tableViewState);
        final UserTablePreferences currentTablePreferences = new UserTablePreferences();
        currentTablePreferences.setMaxRowsPerPage(42);
        when(activeTableReference.getUserTablePreferences()).thenReturn(currentTablePreferences);
        final UserTablePreferences loadedTablePreferences = new UserTablePreferences();
        loadedTablePreferences.setColumnOrder(Collections.emptyList());
        tablePrefIOUtilsMockedStatic.when(() -> TableViewPreferencesIoProvider.getPreferences(GraphElementType.VERTEX)).thenReturn(loadedTablePreferences);
        preferencesMenu.loadPreferences();
        verify(activeTableReference, times(0)).saveSortDetails(anyString(), any(TableColumn.SortType.class));
        verify(activeTableReference, times(0)).updateVisibleColumns(any(Graph.class), any(TableViewState.class), any(List.class), any(UpdateMethod.class));
    }
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) List(java.util.List) ObservableList(javafx.collections.ObservableList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) UpdateMethod(au.gov.asd.tac.constellation.views.tableview.api.UpdateMethod) TableViewPreferencesIoProvider(au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider) Test(org.testng.annotations.Test)

Aggregations

Graph (au.gov.asd.tac.constellation.graph.Graph)3 UserTablePreferences (au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences)3 TableViewPreferencesIoProvider (au.gov.asd.tac.constellation.views.tableview.io.TableViewPreferencesIoProvider)3 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)3 ObservableList (javafx.collections.ObservableList)3 List (java.util.List)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Test (org.testng.annotations.Test)2 Attribute (au.gov.asd.tac.constellation.graph.Attribute)1 GraphManager (au.gov.asd.tac.constellation.graph.manager.GraphManager)1 Column (au.gov.asd.tac.constellation.views.tableview.api.Column)1 UpdateMethod (au.gov.asd.tac.constellation.views.tableview.api.UpdateMethod)1 ActionEvent (javafx.event.ActionEvent)1 RadioMenuItem (javafx.scene.control.RadioMenuItem)1 TableColumn (javafx.scene.control.TableColumn)1 ToggleGroup (javafx.scene.control.ToggleGroup)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1