Search in sources :

Example 1 with TableViewUtilities

use of au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities in project constellation by constellation-app.

the class TableNGTest method updateSelectionThreadInterrupted.

@Test
public void updateSelectionThreadInterrupted() {
    try (MockedStatic<TableViewUtilities> tableUtilsMockedStatic = Mockito.mockStatic(TableViewUtilities.class);
        final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        final TableViewState tableViewState = new TableViewState();
        tableViewState.setSelectedOnly(false);
        when(activeTableReference.getElementIdToRowIndex()).thenReturn(Map.of());
        tableUtilsMockedStatic.when(() -> TableViewUtilities.getSelectedIds(graph, tableViewState)).thenReturn(List.of());
        Thread.currentThread().interrupt();
        table.updateSelection(graph, tableViewState);
        assertTrue(Thread.currentThread().isInterrupted());
        // Clears the current threads interrupt status
        Thread.interrupted();
        platformMockedStatic.verify(() -> Platform.runLater(any(Runnable.class)), times(0));
    }
}
Also used : Platform(javafx.application.Platform) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) TableViewUtilities(au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities) Test(org.testng.annotations.Test)

Example 2 with TableViewUtilities

use of au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities in project constellation by constellation-app.

the class TableNGTest method updateSelectionNotInSelectedOnlyMode.

@Test
public void updateSelectionNotInSelectedOnlyMode() throws InterruptedException {
    final TableViewState tableViewState = new TableViewState();
    tableViewState.setSelectedOnly(false);
    tableViewState.setElementType(GraphElementType.VERTEX);
    final ChangeListener<ObservableList<String>> tableSelectionListener = mock(ChangeListener.class);
    final ListChangeListener selectedOnlySelectionListener = mock(ListChangeListener.class);
    doReturn(tableSelectionListener).when(table).getTableSelectionListener();
    doReturn(selectedOnlySelectionListener).when(table).getSelectedOnlySelectionListener();
    final ObservableList<String> vertex1 = FXCollections.observableList(List.of("Vertex1Attr1", "Vertex1Attr2"));
    final ObservableList<String> vertex2 = FXCollections.observableList(List.of("Vertex2Attr1", "Vertex2Attr2"));
    final ObservableList<String> vertex3 = FXCollections.observableList(List.of("Vertex3Attr1", "Vertex3Attr2"));
    when(activeTableReference.getElementIdToRowIndex()).thenReturn(Map.of(100, vertex1, 102, vertex2, 103, vertex3));
    final TableView<ObservableList<String>> tableView = mock(TableView.class);
    when(table.getTableView()).thenReturn(tableView);
    // Order is important here. Should match on vertex 1 and 2, so indicies 0 and 2.
    when(tableView.getItems()).thenReturn(FXCollections.observableList(List.of(vertex1, vertex3, vertex2)));
    final TableViewSelectionModel<ObservableList<String>> selectionModel = mock(TableViewSelectionModel.class);
    when(tableView.getSelectionModel()).thenReturn(selectionModel);
    final ObservableList<ObservableList<String>> selectedItems = mock(ObservableList.class);
    when(selectionModel.getSelectedItems()).thenReturn(selectedItems);
    final ReadOnlyObjectProperty<ObservableList<String>> selectedProperty = mock(ReadOnlyObjectProperty.class);
    when(table.getSelectedProperty()).thenReturn(selectedProperty);
    try (MockedStatic<TableViewUtilities> tableUtilsMockedStatic = Mockito.mockStatic(TableViewUtilities.class)) {
        tableUtilsMockedStatic.when(() -> TableViewUtilities.getSelectedIds(graph, tableViewState)).thenReturn(List.of(100, 102));
        table.updateSelection(graph, tableViewState);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    Platform.runLater(() -> latch.countDown());
    latch.await();
    verify(selectedProperty).removeListener(tableSelectionListener);
    verify(selectedItems).removeListener(selectedOnlySelectionListener);
    verify(selectionModel).clearSelection();
    verify(selectionModel).selectIndices(0, 0, 2);
    verify(selectedProperty).addListener(tableSelectionListener);
    verify(selectedItems).addListener(selectedOnlySelectionListener);
}
Also used : TableViewUtilities(au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities) CountDownLatch(java.util.concurrent.CountDownLatch) ListChangeListener(javafx.collections.ListChangeListener) ObservableList(javafx.collections.ObservableList) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) Test(org.testng.annotations.Test)

Example 3 with TableViewUtilities

use of au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities in project constellation by constellation-app.

the class TableNGTest method updateSelectionNotInSelectedOnlyModeAndNothingSelected.

@Test
public void updateSelectionNotInSelectedOnlyModeAndNothingSelected() throws InterruptedException {
    final TableViewState tableViewState = new TableViewState();
    tableViewState.setSelectedOnly(false);
    tableViewState.setElementType(GraphElementType.VERTEX);
    final ChangeListener<ObservableList<String>> tableSelectionListener = mock(ChangeListener.class);
    final ListChangeListener selectedOnlySelectionListener = mock(ListChangeListener.class);
    doReturn(tableSelectionListener).when(table).getTableSelectionListener();
    doReturn(selectedOnlySelectionListener).when(table).getSelectedOnlySelectionListener();
    final TableView<ObservableList<String>> tableView = mock(TableView.class);
    when(table.getTableView()).thenReturn(tableView);
    final TableViewSelectionModel<ObservableList<String>> selectionModel = mock(TableViewSelectionModel.class);
    when(tableView.getSelectionModel()).thenReturn(selectionModel);
    final ObservableList<ObservableList<String>> selectedItems = mock(ObservableList.class);
    when(selectionModel.getSelectedItems()).thenReturn(selectedItems);
    final ReadOnlyObjectProperty<ObservableList<String>> selectedProperty = mock(ReadOnlyObjectProperty.class);
    when(table.getSelectedProperty()).thenReturn(selectedProperty);
    try (MockedStatic<TableViewUtilities> tableUtilsMockedStatic = Mockito.mockStatic(TableViewUtilities.class)) {
        tableUtilsMockedStatic.when(() -> TableViewUtilities.getSelectedIds(graph, tableViewState)).thenReturn(List.of());
        table.updateSelection(graph, tableViewState);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    Platform.runLater(() -> latch.countDown());
    latch.await();
    verify(selectedProperty).removeListener(tableSelectionListener);
    verify(selectedItems).removeListener(selectedOnlySelectionListener);
    verify(selectionModel).clearSelection();
    verify(selectionModel, times(0)).selectIndices(anyInt(), any(int[].class));
    verify(selectedProperty).addListener(tableSelectionListener);
    verify(selectedItems).addListener(selectedOnlySelectionListener);
}
Also used : ObservableList(javafx.collections.ObservableList) TableViewState(au.gov.asd.tac.constellation.views.tableview.state.TableViewState) TableViewUtilities(au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities) CountDownLatch(java.util.concurrent.CountDownLatch) ListChangeListener(javafx.collections.ListChangeListener) Test(org.testng.annotations.Test)

Example 4 with TableViewUtilities

use of au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities in project constellation by constellation-app.

the class ExportToCsvFilePluginNGTest method exportCSV.

@Test
public void exportCSV() throws IOException, InterruptedException, PluginException {
    final TableView<ObservableList<String>> table = mock(TableView.class);
    final Pagination pagination = mock(Pagination.class);
    final PluginInteraction pluginInteraction = mock(PluginInteraction.class);
    File tmpFile = null;
    try (MockedStatic<TableViewUtilities> tableViewUtilsMockedStatic = Mockito.mockStatic(TableViewUtilities.class)) {
        tmpFile = File.createTempFile("constellationTest", ".csv");
        final String csv = "COLUMN_1,COLUMN_2\nrow1Column1,row1Column2\nrow2Column1,row2Column2\n";
        tableViewUtilsMockedStatic.when(() -> TableViewUtilities.getTableData(table, pagination, true, true)).thenReturn(csv);
        final ExportToCsvFilePlugin plugin = new ExportToCsvFilePlugin(tmpFile, table, pagination, true);
        plugin.execute(null, pluginInteraction, null);
        final String outputtedFile = new String(IOUtils.toByteArray(new FileInputStream(tmpFile)), StandardCharsets.UTF_8);
        assertEquals(csv, outputtedFile);
        assertEquals(plugin.getName(), "Table View: Export to Delimited File");
    } finally {
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
}
Also used : Pagination(javafx.scene.control.Pagination) ObservableList(javafx.collections.ObservableList) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) TableViewUtilities(au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 5 with TableViewUtilities

use of au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities in project constellation by constellation-app.

the class ExportMenuNGTest method createExportButtons.

@Test
public void createExportButtons() throws InterruptedException, PluginException, ExecutionException {
    exportMenu.init();
    assertNotNull(exportMenu.getExportButton());
    assertNotNull(exportMenu.getExportCsvMenu());
    assertNotNull(exportMenu.getExportCsvSelectionMenu());
    assertNotNull(exportMenu.getExportExcelMenu());
    assertNotNull(exportMenu.getExportExcelSelectionMenu());
    assertEquals(FXCollections.observableList(List.of(exportMenu.getExportCsvMenu(), exportMenu.getExportCsvSelectionMenu(), exportMenu.getExportExcelMenu(), exportMenu.getExportExcelSelectionMenu())), exportMenu.getExportButton().getItems());
    final Graph graph = mock(Graph.class);
    when(tableViewTopComponent.getCurrentGraph()).thenReturn(graph);
    when(graph.getId()).thenReturn(GRAPH_ID);
    // Export Button
    final ImageView icon = (ImageView) exportMenu.getExportButton().getGraphic();
    assertTrue(isImageEqual(UserInterfaceIconProvider.UPLOAD.buildImage(16), icon.getImage()));
    assertEquals(120.0d, exportMenu.getExportButton().getMaxWidth());
    assertEquals(Side.RIGHT, exportMenu.getExportButton().getPopupSide());
    // Export Whole Table as CSV Menu Item
    assertEquals("Export to CSV", exportMenu.getExportCsvMenu().getText());
    verifyExportCSVAction(exportMenu.getExportCsvMenu().getOnAction(), false, false);
    verifyExportCSVAction(exportMenu.getExportCsvMenu().getOnAction(), true, false);
    reset(activeTableReference, table);
    // Export Selected Rows as CSV Menu Item
    assertEquals("Export to CSV (Selection)", exportMenu.getExportCsvSelectionMenu().getText());
    verifyExportCSVAction(exportMenu.getExportCsvSelectionMenu().getOnAction(), false, true);
    verifyExportCSVAction(exportMenu.getExportCsvSelectionMenu().getOnAction(), true, true);
    reset(activeTableReference, table);
    // Export Whole Table as Excel Menu Item
    assertEquals("Export to Excel", exportMenu.getExportExcelMenu().getText());
    verifyExportExcelAction(exportMenu.getExportExcelMenu().getOnAction(), false, false);
    verifyExportExcelAction(exportMenu.getExportExcelMenu().getOnAction(), true, false);
    reset(activeTableReference, table);
    // Export Selected Rows as Excel Menu Item
    assertEquals("Export to Excel (Selection)", exportMenu.getExportExcelSelectionMenu().getText());
    verifyExportExcelAction(exportMenu.getExportExcelSelectionMenu().getOnAction(), false, true);
    verifyExportExcelAction(exportMenu.getExportExcelSelectionMenu().getOnAction(), true, true);
    reset(activeTableReference, table);
    // The following verifies that the ExportMenuItemActionHandler wont run if
    // the current graph is null
    when(tableViewTopComponent.getCurrentGraph()).thenReturn(null);
    try (MockedStatic<TableViewUtilities> tableViewUtilsMockedStatic = Mockito.mockStatic(TableViewUtilities.class)) {
        final ActionEvent actionEvent = mock(ActionEvent.class);
        exportMenu.getExportCsvMenu().getOnAction().handle(actionEvent);
        tableViewUtilsMockedStatic.verifyNoInteractions();
        verify(actionEvent).consume();
    }
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph) ActionEvent(javafx.event.ActionEvent) ImageView(javafx.scene.image.ImageView) TableViewUtilities(au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities) Test(org.testng.annotations.Test)

Aggregations

TableViewUtilities (au.gov.asd.tac.constellation.views.tableview.utilities.TableViewUtilities)9 Test (org.testng.annotations.Test)7 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)5 ObservableList (javafx.collections.ObservableList)4 ActionEvent (javafx.event.ActionEvent)3 Graph (au.gov.asd.tac.constellation.graph.Graph)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ListChangeListener (javafx.collections.ListChangeListener)2 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Platform (javafx.application.Platform)1 Pagination (javafx.scene.control.Pagination)1 ImageView (javafx.scene.image.ImageView)1