use of au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser in project constellation by constellation-app.
the class ExportMenuNGTest method verifyExportExcelAction.
/**
* Verify that the passed event handler exports to Excel either the whole
* table or just the selected rows.
*
* @param eventHandler the handler to test
* @param expectedCopyOnlySelectedRows true if only the selected rows are
* expected to be exported, false otherwise
* @param userCancelsRequest true if the user is meant to cancel the export when
* picking a file in the file chooser
*/
private void verifyExportExcelAction(final EventHandler<ActionEvent> eventHandler, final boolean userCancelsRequest, final boolean expectedExportOnlySelectedRows) throws InterruptedException, PluginException, ExecutionException {
final ExportMenuItemActionHandler exportActionHandler = (ExportMenuItemActionHandler) eventHandler;
final ExportMenuItemActionHandler spiedExportActionHandler = spy(exportActionHandler);
final FileChooserBuilder exportFileChooser = mock(FileChooserBuilder.class);
final File exportFile = userCancelsRequest ? null : new File("test.xlsx");
final Optional<File> optionalExportFile = Optional.ofNullable(exportFile);
doReturn(exportFileChooser).when(spiedExportActionHandler).getExportFileChooser();
try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
final MockedStatic<FileChooser> fileChooserMockedStatic = Mockito.mockStatic(FileChooser.class);
final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
// This is added so that the mocked static that we would otherwise be
// trying to run in the fx thread is actually invoked properly
platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).thenAnswer(iom -> {
((Runnable) iom.getArgument(0)).run();
return null;
});
fileChooserMockedStatic.when(() -> FileChooser.openSaveDialog(exportFileChooser)).thenReturn(CompletableFuture.completedFuture(optionalExportFile));
final ActionEvent actionEvent = mock(ActionEvent.class);
final Pagination pagination = mock(Pagination.class);
final TableView<ObservableList<String>> tableView = mock(TableView.class);
final int maxRowsPerPage = 42;
final UserTablePreferences userTablePreferences = new UserTablePreferences();
userTablePreferences.setMaxRowsPerPage(maxRowsPerPage);
when(activeTableReference.getPagination()).thenReturn(pagination);
when(table.getTableView()).thenReturn(tableView);
when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
final PluginExecution pluginExecution = mock(PluginExecution.class);
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(mockitoInvocation -> {
final ExportToExcelFilePlugin plugin = (ExportToExcelFilePlugin) mockitoInvocation.getArgument(0);
if (exportFile != null) {
assertEquals(exportFile.getAbsolutePath(), plugin.getFile().getAbsolutePath());
} else {
assertEquals(exportFile, plugin.getFile());
}
assertEquals(pagination, plugin.getPagination());
assertEquals(tableView, plugin.getTable());
assertEquals(42, plugin.getRowsPerPage());
assertEquals(GRAPH_ID, plugin.getSheetName());
assertEquals(expectedExportOnlySelectedRows, plugin.isSelectedOnly());
return pluginExecution;
});
spiedExportActionHandler.handle(actionEvent);
// Wait for the export job to complete
spiedExportActionHandler.getLastExport().get();
fileChooserMockedStatic.verify(() -> FileChooser.openSaveDialog(exportFileChooser));
if (userCancelsRequest) {
verifyNoInteractions(pluginExecution);
} else {
verify(pluginExecution).executeNow((Graph) null);
}
verify(actionEvent).consume();
}
}
use of au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser in project constellation by constellation-app.
the class ExportMenuNGTest method verifyExportCSVAction.
/**
* Verify that the passed event handler exports to CSV either the whole
* table or just the selected rows.
*
* @param eventHandler the handler to test
* @param expectedCopyOnlySelectedRows true if only the selected rows are
* expected to be exported, false otherwise
* @param userCancelsRequest true if the user is meant to cancel the export when
* picking a file in the file chooser
*/
private void verifyExportCSVAction(final EventHandler<ActionEvent> eventHandler, final boolean userCancelsRequest, final boolean expectedExportOnlySelectedRows) throws InterruptedException, PluginException, ExecutionException {
final ExportMenuItemActionHandler exportActionHandler = (ExportMenuItemActionHandler) eventHandler;
final ExportMenuItemActionHandler spiedExportActionHandler = spy(exportActionHandler);
final FileChooserBuilder exportFileChooser = mock(FileChooserBuilder.class);
final File exportFile = userCancelsRequest ? null : new File("test.csv");
final Optional<File> optionalExportFile = Optional.ofNullable(exportFile);
doReturn(exportFileChooser).when(spiedExportActionHandler).getExportFileChooser();
try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
final MockedStatic<FileChooser> fileChooserMockedStatic = Mockito.mockStatic(FileChooser.class);
final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
// This is added so that the mocked static that we would otherwise be
// trying to run in the fx thread is actually invoked properly
platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).thenAnswer(iom -> {
((Runnable) iom.getArgument(0)).run();
return null;
});
fileChooserMockedStatic.when(() -> FileChooser.openSaveDialog(exportFileChooser)).thenReturn(CompletableFuture.completedFuture(optionalExportFile));
final ActionEvent actionEvent = mock(ActionEvent.class);
final Pagination pagination = mock(Pagination.class);
final TableView<ObservableList<String>> tableView = mock(TableView.class);
when(activeTableReference.getPagination()).thenReturn(pagination);
when(table.getTableView()).thenReturn(tableView);
final PluginExecution pluginExecution = mock(PluginExecution.class);
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(mockitoInvocation -> {
final ExportToCsvFilePlugin plugin = (ExportToCsvFilePlugin) mockitoInvocation.getArgument(0);
if (exportFile != null) {
assertEquals(exportFile.getAbsolutePath(), plugin.getFile().getAbsolutePath());
} else {
assertEquals(exportFile, plugin.getFile());
}
assertEquals(pagination, plugin.getPagination());
assertEquals(tableView, plugin.getTable());
assertEquals(expectedExportOnlySelectedRows, plugin.isSelectedOnly());
return pluginExecution;
});
spiedExportActionHandler.handle(actionEvent);
// Wait for the export job to complete
spiedExportActionHandler.getLastExport().get();
fileChooserMockedStatic.verify(() -> FileChooser.openSaveDialog(exportFileChooser));
if (userCancelsRequest) {
verifyNoInteractions(pluginExecution);
} else {
verify(pluginExecution).executeNow((Graph) null);
}
verify(actionEvent).consume();
}
}
Aggregations