Search in sources :

Example 36 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableViewPageFactoryNGTest method callSelectedOnlyModeMultipleCalls.

@Test
public void callSelectedOnlyModeMultipleCalls() {
    doNothing().when(tableViewPageFactory).restoreSelectionFromGraph(any(Graph.class), any(TableViewState.class), any(Map.class));
    final ObservableList<String> row1 = FXCollections.observableArrayList(List.of("row1Column1", "row1Column2"));
    final ObservableList<String> row2 = FXCollections.observableArrayList(List.of("row2Column1", "row2Column2"));
    final ObservableList<String> row3 = FXCollections.observableArrayList(List.of("row3Column1", "row3Column2"));
    final int maxRowsPerPage = 2;
    final int pageIndex = 0;
    final ObservableList<TableColumn<ObservableList<String>, String>> sortOrder = FXCollections.observableArrayList();
    doReturn(sortOrder).when(tableView).getSortOrder();
    final TableViewState tableViewState = new TableViewState();
    tableViewState.setSelectedOnly(true);
    when(tableViewTopComponent.getCurrentState()).thenReturn(tableViewState);
    // This is called after the page set up so just mocking it so that it is realistic
    when(tableView.getItems()).thenReturn(FXCollections.observableList(List.of(row1, row2)));
    // =============RUN ONE=================
    tableViewPageFactory.update(List.of(row1, row2, row3), maxRowsPerPage);
    // Set the selected only mode selection to rows 1 and 2
    selectedOnlySelectedRows.add(row1);
    selectedOnlySelectedRows.add(row2);
    assertEquals(tableView, tableViewPageFactory.call(pageIndex));
    // In the first call this gets cleared
    assertTrue(selectedOnlySelectedRows.isEmpty());
    // =============RUN TWO=================
    // Re-populate the selected only selected rows set as it was cleared in run 1
    selectedOnlySelectedRows.add(row1);
    selectedOnlySelectedRows.add(row2);
    assertEquals(tableView, tableViewPageFactory.call(pageIndex));
    // The second run has the same new row list so it does not get cleared
    assertEquals(2, selectedOnlySelectedRows.size());
    // =============RUN THREE=================
    // Change the new row list
    tableViewPageFactory.update(List.of(row1, row2), maxRowsPerPage);
    assertEquals(tableView, tableViewPageFactory.call(pageIndex));
    // The third run now has a different new row list so it is cleared again
    assertTrue(selectedOnlySelectedRows.isEmpty());
    // ======================================
    verify(tableView, times(3)).setItems(FXCollections.observableList(List.of(row1, row2)));
    assertTrue(sortOrder.isEmpty());
    // This selection update only happens in the second run as it is called with
    // the same new row list and that avoids it being cleared
    verify(selectionModel, times(1)).selectIndices(0, 0, 1);
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) Map(java.util.Map) TableColumn(javafx.scene.control.TableColumn) Test(org.testng.annotations.Test)

Example 37 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableViewStateIoProviderNGTest method writeObject.

@Test
public void writeObject() throws IOException {
    final GraphAttribute transactionGraphAttr = mock(GraphAttribute.class);
    final GraphAttribute vertexGraphAttr = mock(GraphAttribute.class);
    when(transactionGraphAttr.getElementType()).thenReturn(GraphElementType.TRANSACTION);
    when(transactionGraphAttr.getName()).thenReturn("My Transaction");
    when(vertexGraphAttr.getElementType()).thenReturn(GraphElementType.VERTEX);
    when(vertexGraphAttr.getName()).thenReturn("My Vertex");
    final TableViewState state = new TableViewState();
    state.setSelectedOnly(true);
    state.setElementType(GraphElementType.VERTEX);
    state.setTransactionColumnAttributes(List.of(Tuple.create("transactionPrefix", transactionGraphAttr)));
    state.setVertexColumnAttributes(List.of(Tuple.create("vertexPrefix", vertexGraphAttr)));
    final Attribute attribute = mock(Attribute.class);
    when(attribute.getId()).thenReturn(ATTRIBUTE_ID);
    when(attribute.getName()).thenReturn("ATTR NAME");
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    when(graph.isDefaultValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(false);
    when(graph.getObjectValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(state);
    final JsonFactory factory = new JsonFactory();
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = factory.createGenerator(output);
    // The code is written with the assumption that it is called within a document
    // that has already started being written. Without starting the object in the test
    // the code would throw invalid json exceptions.
    jsonGenerator.writeStartObject();
    tableViewStateIoProvider.writeObject(attribute, ELEMENT_ID, jsonGenerator, graph, null, false);
    jsonGenerator.writeEndObject();
    jsonGenerator.flush();
    final ObjectMapper objectMapper = new ObjectMapper();
    final JsonNode expected = objectMapper.readTree(new FileInputStream(getClass().getResource("resources/tableViewStateWrite.json").getPath()));
    final JsonNode actual = objectMapper.readTree(new String(output.toByteArray(), StandardCharsets.UTF_8));
    assertEquals(actual, expected);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 38 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class TableViewStateIoProviderNGTest method readObject.

@Test
public void readObject() throws IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    final JsonNode jsonNode = objectMapper.readTree(new FileInputStream(getClass().getResource("resources/tableViewStateRead.json").getPath()));
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    when(graph.getAttribute(GraphElementType.TRANSACTION, "My Transaction")).thenReturn(1);
    when(graph.getAttribute(GraphElementType.VERTEX, "My Vertex")).thenReturn(2);
    tableViewStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, jsonNode, graph, null, null, null, null);
    // Capture the call on the graph setter, pulling out the state
    final ArgumentCaptor<TableViewState> captor = ArgumentCaptor.forClass(TableViewState.class);
    verify(graph, times(1)).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
    final TableViewState actual = captor.getValue();
    final TableViewState expected = new TableViewState();
    assertTrue(actual.isSelectedOnly());
    assertEquals(actual.getElementType(), GraphElementType.VERTEX);
    assertEquals(actual.getTransactionColumnAttributes().size(), 1);
    final Tuple<String, Attribute> transactionColumnAttr = actual.getTransactionColumnAttributes().get(0);
    assertEquals(transactionColumnAttr.getFirst(), "transactionPrefix");
    assertEquals(transactionColumnAttr.getSecond(), new GraphAttribute(graph, 1));
    assertEquals(actual.getVertexColumnAttributes().size(), 1);
    final Tuple<String, Attribute> vertexColumnAttr = actual.getVertexColumnAttributes().get(0);
    assertEquals(vertexColumnAttr.getFirst(), "vertexPrefix");
    assertEquals(vertexColumnAttr.getSecond(), new GraphAttribute(graph, 2));
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 39 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState in project constellation by constellation-app.

the class SelectedOnlySelectionListenerNGTest method onChangedCurrentStateSelectedOnlyModeFalse.

@Test
public void onChangedCurrentStateSelectedOnlyModeFalse() {
    final TableViewState currentState = new TableViewState();
    currentState.setSelectedOnly(false);
    when(tableViewTopComponent.getCurrentState()).thenReturn(currentState);
    selectedOnlySelectionListener.onChanged(null);
    assertTrue(selectedOnlySelectedRows.isEmpty());
}
Also used : TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) Test(org.testng.annotations.Test)

Example 40 with TableViewState

use of au.gov.asd.tac.constellation.views.tableview.state.TableViewState 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)

Aggregations

TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)65 Test (org.testng.annotations.Test)50 Graph (au.gov.asd.tac.constellation.graph.Graph)24 Platform (javafx.application.Platform)19 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)14 ObservableList (javafx.collections.ObservableList)14 Attribute (au.gov.asd.tac.constellation.graph.Attribute)13 UpdateStatePlugin (au.gov.asd.tac.constellation.views.tableview.plugins.UpdateStatePlugin)12 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)10 TableColumn (javafx.scene.control.TableColumn)10 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 TablePane (au.gov.asd.tac.constellation.views.tableview.panes.TablePane)7 ListChangeListener (javafx.collections.ListChangeListener)7 Tuple (au.gov.asd.tac.constellation.utilities.datastructure.Tuple)6 TableViewUtilities (au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities)6 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)5 Column (au.gov.asd.tac.constellation.views.tableview.api.Column)5 Table (au.gov.asd.tac.constellation.views.tableview.components.Table)5 ArrayList (java.util.ArrayList)5