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);
}
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);
}
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));
}
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());
}
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();
}
}
Aggregations