From aafa19fc0d267ff490cbc95edaaa8dd575cfc8b6 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 27 Apr 2023 22:42:13 +0100 Subject: tests: Add a test for progress callbacks from g_file_copy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This wasn’t previously tested. Signed-off-by: Philip Withnall Helps: #2863 --- gio/tests/file.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/gio/tests/file.c b/gio/tests/file.c index 754c6c326..7fbb3ef0c 100644 --- a/gio/tests/file.c +++ b/gio/tests/file.c @@ -2513,6 +2513,80 @@ test_copy_preserve_mode (void) #endif } +typedef struct +{ + goffset current_num_bytes; + goffset total_num_bytes; +} CopyProgressData; + +static void +file_copy_progress_cb (goffset current_num_bytes, + goffset total_num_bytes, + gpointer user_data) +{ + CopyProgressData *prev_data = user_data; + + g_assert_cmpuint (total_num_bytes, ==, prev_data->total_num_bytes); + g_assert_cmpuint (current_num_bytes, >=, prev_data->current_num_bytes); + + /* Update it for the next callback. */ + prev_data->current_num_bytes = current_num_bytes; +} + +static void +test_copy_progress (void) +{ + GFile *src_tmpfile = NULL; + GFile *dest_tmpfile = NULL; + GFileIOStream *iostream; + GOutputStream *ostream; + GError *local_error = NULL; + const guint8 buffer[] = { 1, 2, 3, 4, 5 }; + CopyProgressData progress_data; + + src_tmpfile = g_file_new_tmp ("tmp-copy-progressXXXXXX", + &iostream, &local_error); + g_assert_no_error (local_error); + + /* Write some content to the file for testing. */ + ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream)); + g_output_stream_write (ostream, buffer, sizeof (buffer), NULL, &local_error); + g_assert_no_error (local_error); + + g_io_stream_close ((GIOStream *) iostream, NULL, &local_error); + g_assert_no_error (local_error); + g_clear_object (&iostream); + + /* Grab a unique destination filename. */ + dest_tmpfile = g_file_new_tmp ("tmp-copy-progressXXXXXX", + &iostream, &local_error); + g_assert_no_error (local_error); + g_io_stream_close ((GIOStream *) iostream, NULL, &local_error); + g_assert_no_error (local_error); + g_clear_object (&iostream); + + /* Set the progress data to an initial offset of zero. The callback will + * assert that progress is non-decreasing and reaches the total length of + * the file. */ + progress_data.current_num_bytes = 0; + progress_data.total_num_bytes = sizeof (buffer); + + /* Copy the file with progress reporting. */ + g_file_copy (src_tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE, + NULL, file_copy_progress_cb, &progress_data, &local_error); + g_assert_no_error (local_error); + + g_assert_cmpuint (progress_data.current_num_bytes, ==, progress_data.total_num_bytes); + g_assert_cmpuint (progress_data.total_num_bytes, ==, sizeof (buffer)); + + /* Clean up. */ + (void) g_file_delete (src_tmpfile, NULL, NULL); + (void) g_file_delete (dest_tmpfile, NULL, NULL); + + g_clear_object (&src_tmpfile); + g_clear_object (&dest_tmpfile); +} + static void test_measure (void) { @@ -3844,6 +3918,7 @@ main (int argc, char *argv[]) g_test_add_func ("/file/async-delete", test_async_delete); g_test_add_func ("/file/async-make-symlink", test_async_make_symlink); g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode); + g_test_add_func ("/file/copy/progress", test_copy_progress); g_test_add_func ("/file/measure", test_measure); g_test_add_func ("/file/measure-async", test_measure_async); g_test_add_func ("/file/load-bytes", test_load_bytes); @@ -3869,3 +3944,4 @@ main (int argc, char *argv[]) return g_test_run (); } + -- cgit v1.2.1