From 8f98ecbb449a65a745377da0ec1c568f9a8f0823 Mon Sep 17 00:00:00 2001 From: Khalid Abu Shawarib Date: Thu, 11 Jan 2024 16:35:28 +0300 Subject: [PATCH] test/file: Add batch rename tests --- test/automated/displayless/test-file.c | 164 +++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/test/automated/displayless/test-file.c b/test/automated/displayless/test-file.c index 1dfba7f77..e947ba9a3 100644 --- a/test/automated/displayless/test-file.c +++ b/test/automated/displayless/test-file.c @@ -4,6 +4,7 @@ #include #include #include +#include static void @@ -87,10 +88,167 @@ test_file_sort_with_self (void) g_assert_cmpint (order, ==, 0); } +typedef struct +{ + const gsize len; + GStrv expected_names; + GStrv expected_content; + gboolean *success; +} NautilusFileBatchRenameTestData; + +static void +batch_rename_callback (NautilusFile *file, + GFile *result_location, + GError *error, + gpointer callback_data) +{ + NautilusFileBatchRenameTestData *data = callback_data; + g_autoptr (GFile) root = g_file_new_for_path (test_get_tmp_dir ()); + g_autoptr (GStrvBuilder) name_builder = g_strv_builder_new (); + g_autoptr (GStrvBuilder) content_builder = g_strv_builder_new (); + g_auto (GStrv) result_names = NULL, result_content = NULL; + + g_assert_no_error (error); + g_assert_cmpint (data->len, ==, g_strv_length (data->expected_names)); + g_assert_cmpint (data->len, ==, g_strv_length (data->expected_content)); + + for (guint i = 0; i < data->len; i++) + { + g_autoptr (GFile) location = g_file_get_child (root, data->expected_names[i]); + g_autoptr (GFileInfo) info = g_file_query_info (location, NAUTILUS_FILE_DEFAULT_ATTRIBUTES, + G_FILE_QUERY_INFO_NONE, NULL, NULL); + g_autoptr (GFileInputStream) stream = g_file_read (location, NULL, NULL); + gchar content[1024]; + + g_assert_nonnull (stream); + g_assert_true (g_input_stream_read_all (G_INPUT_STREAM (stream), content, sizeof (content), + NULL, NULL, NULL)); + g_assert_true (g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL)); + + g_strv_builder_add (name_builder, g_file_info_get_display_name (info)); + g_strv_builder_add (content_builder, content); + } + + result_names = g_strv_builder_end (name_builder); + result_content = g_strv_builder_end (content_builder); + + for (guint i = 0; i < data->len; i++) + { + g_assert_cmpstr (result_names[i], ==, data->expected_names[i]); + g_assert_cmpstr (result_content[i], ==, data->expected_content[i]); + } + + *data->success = TRUE; +} + +static void +batch_rename_test (const GStrv original_names, + const GStrv expected_names) +{ + g_autoptr (GFile) root = g_file_new_for_path (test_get_tmp_dir ()); + g_autolist (NautilusFile) files = NULL; + g_autolist (GString) new_names = NULL; + const gsize len = g_strv_length (expected_names); + gboolean success = FALSE; + NautilusFileBatchRenameTestData data = { len, expected_names, original_names, &success }; + + create_hierarchy_from_template (original_names, ""); + + for (gint i = len - 1; i >= 0; i--) + { + g_autoptr (GFile) location = g_file_get_child (root, original_names[i]); + NautilusFile *file = nautilus_file_get (location); + GString *new_name = g_string_new (expected_names[i]); + + files = g_list_prepend (files, file); + new_names = g_list_prepend (new_names, new_name); + } + + nautilus_file_batch_rename (files, new_names, batch_rename_callback, &data); + + g_assert_true (success); + + /* Test undo by changing the expected names */ + data.expected_names = original_names; + success = FALSE; + + test_operation_undo (); + + batch_rename_callback (NULL, NULL, NULL, &data); + + g_assert_true (success); + + test_clear_tmp_dir (); +} + +static void +test_file_batch_rename_cycles (void) +{ + char *test_cases[][2][10] = + { + /* Small cycle */ + {{"file_1", "file_2", NULL}, + {"file_2", "file_1", NULL}}, + /* Medium cycle */ + {{"file_1", "file_2", "file_3", "file_4", "file_5", "file_6", "file_7", "file_8", "file_9", NULL}, + {"file_9", "file_1", "file_2", "file_3", "file_4", "file_5", "file_6", "file_7", "file_8", NULL}}, + /* Multi-cycle */ + {{"file_1", "file_2", "file_3", "file_4", "file_5", "file_6", "file_7", "file_8", NULL}, + {"file_8", "file_3", "file_4", "file_5", "file_6", "file_7", "file_2", "file_1", NULL}}, + }; + + g_test_bug ("https://gitlab.gnome.org/GNOME/nautilus/-/issues/1443"); + + for (guint i = 0; i < G_N_ELEMENTS (test_cases); i++) + { + batch_rename_test (test_cases[i][0], test_cases[i][1]); + } +} + +static void +test_file_batch_rename_chains (void) +{ + char *test_cases[][2][10] = + { + /* Medium chain */ + {{"file_1", "file_2", "file_3", "file_4", "file_5", "file_6", "file_7", "file_8", "file_9", NULL}, + {"file_2", "file_3", "file_4", "file_5", "file_6", "file_7", "file_8", "file_9", "file_10", NULL}}, + }; + + for (guint i = 0; i < G_N_ELEMENTS (test_cases); i++) + { + batch_rename_test (test_cases[i][0], test_cases[i][1]); + } +} + +static void +test_file_batch_rename_replace (void) +{ + char *test_cases[][2][10] = + { + /* File Extension replacement */ + {{ + "file_1.jpg", "file_2.jpeg", "file_3.gif", "file_4.png", "file_5.webm", + "file_6.avif", "file_7.jxl", "file_8.jpeg", "file_9.bmp", NULL + }, + { + "file_1.jpg", "file_2.jpg", "file_3.gif", "file_4.png", "file_5.webm", + "file_6.avif", "file_7.jxl", "file_8.jpg", "file_9.bmp", NULL + }}, + }; + + for (guint i = 0; i < G_N_ELEMENTS (test_cases); i++) + { + batch_rename_test (test_cases[i][0], test_cases[i][1]); + } +} + int main (int argc, char *argv[]) { + g_autoptr (NautilusFileUndoManager) undo_manager = nautilus_file_undo_manager_new (); + g_test_init (&argc, &argv, NULL); g_test_set_nonfatal_assertions (); nautilus_ensure_extension_points (); @@ -107,6 +265,12 @@ main (int argc, test_file_sort_order); g_test_add_func ("/file-sort/with-self", test_file_sort_with_self); + g_test_add_func ("/file-batch-rename/cycles", + test_file_batch_rename_cycles); + g_test_add_func ("/file-batch-rename/chains", + test_file_batch_rename_chains); + g_test_add_func ("/file-batch-rename/replace", + test_file_batch_rename_replace); return g_test_run (); }