diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 15:59:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 15:59:36 +0000 |
commit | ec52555862913a23417735f9f7f5402f5230da13 (patch) | |
tree | 5e43a30d289a3daa69dddfbb060216ff6332f197 /test | |
parent | Initial commit. (diff) | |
download | nautilus-upstream.tar.xz nautilus-upstream.zip |
Adding upstream version 3.38.2.upstream/3.38.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
26 files changed, 6923 insertions, 0 deletions
diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..06eb5d9 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,6 @@ +/test-eel-string-get-common-prefix +/test-eel-string-rtrim-punctuation +/test-file-utilities-get-common-filename-prefix +/test-nautilus-copy +/test-nautilus-directory-async +/test-nautilus-search-engine diff --git a/test/automated/display/meson.build b/test/automated/display/meson.build new file mode 100644 index 0000000..a7e2e0b --- /dev/null +++ b/test/automated/display/meson.build @@ -0,0 +1,23 @@ +test_env += [ + 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), + 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()) +] + +tests = [ + ['test-nautilus-directory-async', [ + 'test-nautilus-directory-async.c' + ]], +] + +foreach t: tests + test( + t[0], + executable(t[0], t[1], dependencies: libnautilus_dep), + env: [ + test_env, + 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), + 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()) + ], + timeout: 480 + ) +endforeach diff --git a/test/automated/display/test-nautilus-directory-async.c b/test/automated/display/test-nautilus-directory-async.c new file mode 100644 index 0000000..a4dc24e --- /dev/null +++ b/test/automated/display/test-nautilus-directory-async.c @@ -0,0 +1,101 @@ +#include <src/nautilus-directory.h> +#include <src/nautilus-file-utilities.h> +#include <src/nautilus-search-directory.h> +#include <src/nautilus-file.h> +#include <unistd.h> + +static GMainLoop *main_loop; +void *client1, *client2; + +static void +files_added (NautilusDirectory *directory, + GList *added_files) +{ +#if 0 + GList *list; + + for (list = added_files; list != NULL; list = list->next) + { + NautilusFile *file = list->data; + + g_print (" - %s\n", nautilus_file_get_uri (file)); + } +#endif + + g_print ("files added: %d files\n", + g_list_length (added_files)); +} + +static void +files_changed (NautilusDirectory *directory, + GList *changed_files) +{ +#if 0 + GList *list; + + for (list = changed_files; list != NULL; list = list->next) + { + NautilusFile *file = list->data; + + g_print (" - %s\n", nautilus_file_get_uri (file)); + } +#endif + g_print ("files changed: %d\n", + g_list_length (changed_files)); +} + +static void +done_loading (NautilusDirectory *directory) +{ + g_print ("done loading\n"); + g_main_loop_quit (main_loop); +} + +int +main (int argc, + char **argv) +{ + NautilusDirectory *directory; + NautilusFileAttributes attributes; + const char *uri; + + client1 = g_new0 (int, 1); + client2 = g_new0 (int, 1); + + main_loop = g_main_loop_new (NULL, FALSE); + + nautilus_ensure_extension_points (); + + if (argv[1] == NULL) + { + uri = "file:///tmp"; + } + else + { + uri = argv[1]; + } + g_print ("loading %s", uri); + directory = nautilus_directory_get_by_uri (uri); + + g_signal_connect (directory, "files-added", G_CALLBACK (files_added), NULL); + g_signal_connect (directory, "files-changed", G_CALLBACK (files_changed), NULL); + g_signal_connect (directory, "done-loading", G_CALLBACK (done_loading), NULL); + + attributes = + NAUTILUS_FILE_ATTRIBUTES_FOR_ICON | + NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT | + NAUTILUS_FILE_ATTRIBUTE_INFO | + NAUTILUS_FILE_ATTRIBUTE_MOUNT | + NAUTILUS_FILE_ATTRIBUTE_EXTENSION_INFO; + + nautilus_directory_file_monitor_add (directory, client1, TRUE, + attributes, + NULL, NULL); + + + g_main_loop_run (main_loop); + + g_main_loop_unref (main_loop); + + return 0; +} diff --git a/test/automated/display/test-nautilus-mime-actions-set.c b/test/automated/display/test-nautilus-mime-actions-set.c new file mode 100644 index 0000000..a08f817 --- /dev/null +++ b/test/automated/display/test-nautilus-mime-actions-set.c @@ -0,0 +1,187 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-mime.c - Test for the mime handler detection features of the GNOME + * Virtual File System Library + * + * Copyright (C) 2000 Eazel + * + * The Gnome Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * The Gnome Library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with the Gnome Library; see the file COPYING.LIB. If not, + * see <http://www.gnu.org/licenses/>. + * + * Author: Maciej Stachowiak <mjs@eazel.com> + */ + +#include <config.h> + +#include <gtk/gtk.h> +#include <src/nautilus-mime-actions.h> +#include <stdio.h> + +static gboolean ready = FALSE; + +static void +usage (const char *name) +{ + fprintf (stderr, "Usage: %s uri field value\n", name); + fprintf (stderr, "Valid field values are: \n"); + fprintf (stderr, "\tdefault_action_type\n"); + fprintf (stderr, "\tdefault_application\n"); + fprintf (stderr, "\tdefault_component\n"); + fprintf (stderr, "\tshort_list_applicationss\n"); + fprintf (stderr, "\tshort_list_components\n"); + fprintf (stderr, "\tadd_to_all_applicationss\n"); + fprintf (stderr, "\tremove_from_all_applications\n"); + exit (1); +} + +static GnomeVFSMimeActionType +str_to_action_type (const char *str) +{ + if (g_ascii_strcasecmp (str, "component") == 0) + { + return GNOME_VFS_MIME_ACTION_TYPE_COMPONENT; + } + else if (g_ascii_strcasecmp (str, "application") == 0) + { + return GNOME_VFS_MIME_ACTION_TYPE_APPLICATION; + } + else + { + return GNOME_VFS_MIME_ACTION_TYPE_NONE; + } +} + +static char ** +strsplit_handle_null (const char *str, + const char *delim, + int max) +{ + return g_strsplit ((str == NULL ? "" : str), delim, max); +} + + +static GList * +strsplit_to_list (const char *str, + const char *delim, + int max) +{ + char **strv; + GList *retval; + int i; + + strv = strsplit_handle_null (str, delim, max); + + retval = NULL; + + for (i = 0; strv[i] != NULL; i++) + { + retval = g_list_prepend (retval, strv[i]); + } + + retval = g_list_reverse (retval); + /* Don't strfreev, since we didn't copy the individual strings. */ + g_free (strv); + + return retval; +} + +static GList * +comma_separated_str_to_str_list (const char *str) +{ + return strsplit_to_list (str, ",", 0); +} + +static void +ready_callback (NautilusFile *file, + gpointer callback_data) +{ + ready = TRUE; +} + +int +main (int argc, + char **argv) +{ + const char *uri; + const char *field; + const char *value; + NautilusFile *file; + NautilusFileAttributes attributes; + + gtk_init (&argc, &argv); + + if (argc < 3) + { + usage (argv[0]); + } + + uri = argv[1]; + field = argv[2]; + value = argv[3]; + + file = nautilus_file_get_by_uri (uri); + + attributes = nautilus_mime_actions_get_full_file_attributes (); + nautilus_file_call_when_ready (file, attributes, ready_callback, NULL); + + while (!ready) + { + gtk_main_iteration (); + } + + if (strcmp (field, "default_action_type") == 0) + { + puts ("default_action_type"); + nautilus_mime_set_default_action_type_for_file (file, str_to_action_type (value)); + } + else if (strcmp (field, "default_application") == 0) + { + puts ("default_application"); + nautilus_mime_set_default_application_for_file (file, value); + } + else if (strcmp (field, "default_component") == 0) + { + puts ("default_component"); + nautilus_mime_set_default_component_for_file (file, value); + } + else if (strcmp (field, "short_list_applicationss") == 0) + { + puts ("short_list_applications"); + nautilus_mime_set_short_list_applications_for_file + (file, comma_separated_str_to_str_list (value)); + } + else if (strcmp (field, "short_list_components") == 0) + { + puts ("short_list_components"); + nautilus_mime_set_short_list_components_for_file + (file, comma_separated_str_to_str_list (value)); + } + else if (strcmp (field, "add_to_all_applicationss") == 0) + { + puts ("add_to_all_applications"); + nautilus_mime_extend_all_applications_for_file + (file, comma_separated_str_to_str_list (value)); + } + else if (strcmp (field, "remove_from_all_applications") == 0) + { + puts ("remove_from_all_applications"); + nautilus_mime_remove_from_all_applications_for_file + (file, comma_separated_str_to_str_list (value)); + } + else + { + usage (argv[0]); + } + + return 0; +} diff --git a/test/automated/display/test-nautilus-mime-actions.c b/test/automated/display/test-nautilus-mime-actions.c new file mode 100644 index 0000000..38e140f --- /dev/null +++ b/test/automated/display/test-nautilus-mime-actions.c @@ -0,0 +1,145 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-mime.c - Test for the mime handler detection features of the GNOME + * Virtual File System Library + * + * Copyright (C) 2000 Eazel + * + * The Gnome Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * The Gnome Library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with the Gnome Library; see the file COPYING.LIB. If not, + * see <http://www.gnu.org/licenses/>. + * + * Author: Maciej Stachowiak <mjs@eazel.com> + */ + +#include <config.h> + +#include <gtk/gtk.h> +#include <src/nautilus-mime-actions.h> +#include <stdio.h> + +static gboolean ready = FALSE; + + +static void +append_comma_and_scheme (gpointer scheme, + gpointer user_data) +{ + char **string; + + string = (char **) user_data; + if (strlen (*string) > 0) + { + *string = g_strconcat (*string, ", ", scheme, NULL); + } + else + { + *string = g_strdup (scheme); + } +} + + +static char * +format_supported_uri_schemes_for_display (GList *supported_uri_schemes) +{ + char *string; + + string = g_strdup (""); + g_list_foreach (supported_uri_schemes, + append_comma_and_scheme, + &string); + return string; +} + +static void +print_application (GAppInfo *application) +{ + if (application == NULL) + { + puts ("(none)"); + } + else + { + printf ("name: %s\ncommand: %s\nexpects_uris: %s\n", + g_application_get_name (application), + g_application_get_executable (application), + (g_app_info_supports_uris (application) ? "TRUE" : "FALSE")); + } +} + +static void +print_application_list (GList *applications) +{ + GList *p; + + if (applications == NULL) + { + puts ("(none)"); + } + else + { + for (p = applications; p != NULL; p = p->next) + { + print_application (p->data); + puts ("------"); + } + } +} + +static void +ready_callback (NautilusFile *file, + gpointer callback_data) +{ + ready = TRUE; +} + +int +main (int argc, + char **argv) +{ + const char *uri; + GAppInfo *default_application; + GList *all_applications; + NautilusFile *file; + NautilusFileAttributes attributes; + + gtk_init (&argc, &argv); + + if (argc != 2) + { + fprintf (stderr, "Usage: %s uri\n", *argv); + return 1; + } + + uri = argv[1]; + file = nautilus_file_get_by_uri (uri); + + attributes = nautilus_mime_actions_get_full_file_attributes (); + nautilus_file_call_when_ready (file, attributes, ready_callback, NULL); + + while (!ready) + { + gtk_main_iteration (); + } + + default_application = nautilus_mime_get_default_application_for_file (file); + puts ("Default Application"); + print_application (default_application); + puts (""); + + all_applications = nautilus_mime_get_applications_for_file (file); + puts ("All Applications"); + print_application_list (all_applications); + puts (""); + + return 0; +} diff --git a/test/automated/displayless/meson.build b/test/automated/displayless/meson.build new file mode 100644 index 0000000..64b23bd --- /dev/null +++ b/test/automated/displayless/meson.build @@ -0,0 +1,74 @@ +trackertestutils = dependency('tracker-testutils-3.0') + +tracker_sandbox = find_program(trackertestutils.get_pkgconfig_variable('command')) + +tests = [ + ['test-file-utilities-get-common-filename-prefix', [ + 'test-file-utilities-get-common-filename-prefix.c' + ]], + ['test-eel-string-get-common-prefix', [ + 'test-eel-string-get-common-prefix.c' + ]], + ['test-file-utilities', [ + 'test-file-utilities.c' + ]], + ['test-file-operations-dir-has-files', [ + 'test-file-operations-dir-has-files.c' + ]], + ['test-file-operations-move-files', [ + 'test-file-operations-move-files.c' + ]], + ['test-nautilus-search-engine', [ + 'test-nautilus-search-engine.c' + ]], + ['test-nautilus-search-engine-simple', [ + 'test-nautilus-search-engine-simple.c' + ]], + ['test-nautilus-search-engine-model', [ + 'test-nautilus-search-engine-model.c' + ]], + ['test-file-operations-copy-files', [ + 'test-file-operations-copy-files.c' + ]], + ['test-file-operations-trash-or-delete', [ + 'test-file-operations-trash-or-delete.c' + ]] +] + +tracker_tests = [ + ['test-nautilus-search-engine-tracker', [ + 'test-nautilus-search-engine-tracker.c', + ]], +] + +foreach t: tests + test( + t[0], + executable(t[0], t[1], files('test-utilities.c'), dependencies: libnautilus_dep), + env: [ + test_env, + 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), + 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()) + ], + timeout: 480 + ) +endforeach + + + +# Tests that read and write from the Tracker index are run using 'tracker-sandbox' +# script to use a temporary instance of tracker-miner-fs instead of the session one. +foreach t: tracker_tests + test_exe = executable(t[0], t[1], files('test-utilities.c'), dependencies: libnautilus_dep) + test( + t[0], + tracker_sandbox, + args: ['--store-tmpdir', '--index-recursive-tmpdir', test_exe], + env: [ + test_env, + 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), + 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()) + ], + timeout: 480 + ) +endforeach diff --git a/test/automated/displayless/test-eel-string-get-common-prefix.c b/test/automated/displayless/test-eel-string-get-common-prefix.c new file mode 100644 index 0000000..6634c68 --- /dev/null +++ b/test/automated/displayless/test-eel-string-get-common-prefix.c @@ -0,0 +1,190 @@ +#include <glib.h> +#include <glib/gprintf.h> + +#include "eel/eel-string.h" + + +static void +free_list_and_result (GList *list, + char *result) +{ + g_list_free (list); + g_free (result); +} + +static void +test_has_large_enough_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "foo-1.txt"); + list = g_list_append (list, "foo-1.tar"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_cmpstr ("foo-1.t", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_has_common_prefix_that_equals_the_min_required_length (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "food"); + list = g_list_append (list, "foody"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_cmpstr ("food", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_has_common_prefix_that_equals_the_min_required_length2 (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "foody"); + list = g_list_append (list, "food"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_cmpstr ("food", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_many_strings_with_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "some text that matches abcde"); + list = g_list_append (list, "some text that matches abc22"); + list = g_list_append (list, "some text that 11"); + list = g_list_append (list, "some text that matches---"); + list = g_list_append (list, "some text that matches £$$"); + list = g_list_append (list, "some text that matches.txt"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_cmpstr ("some text that ", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_strings_with_unicode_characters_that_have_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "ƹƱƱƬ"); + list = g_list_append (list, "ƹƱƱƬƧƥƧ"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_cmpstr ("ƹƱƱƬ", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_no_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "fyod"); + list = g_list_append (list, "completely different string"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_has_common_prefix_but_smaller_than_min_required_length (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "fyod"); + list = g_list_append (list, "fyoa"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_first_character_differs (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "tyodaa"); + list = g_list_append (list, "fyodaa"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_strings_with_unicode_characters_that_dont_have_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "ƹƱƱƬ"); + list = g_list_append (list, "ƹƱƢƱƬƧƥƧ"); + + actual = eel_str_get_common_prefix (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + + +static void +setup_test_suite (void) +{ + g_test_add_func ("/get-common-prefix/1.0", + test_has_large_enough_common_prefix); + g_test_add_func ("/get-common-prefix/1.1", + test_has_common_prefix_that_equals_the_min_required_length); + g_test_add_func ("/get-common-prefix/1.2", + test_has_common_prefix_that_equals_the_min_required_length2); + g_test_add_func ("/get-common-prefix/1.3", + test_many_strings_with_common_prefix); + g_test_add_func ("/get-common-prefix/1.4", + test_strings_with_unicode_characters_that_have_common_prefix); + + g_test_add_func ("/get-common-prefix/2.0", + test_no_common_prefix); + g_test_add_func ("/get-common-prefix/2.1", + test_has_common_prefix_but_smaller_than_min_required_length); + g_test_add_func ("/get-common-prefix/2.2", + test_first_character_differs); + g_test_add_func ("/get-common-prefix/2.3", + test_strings_with_unicode_characters_that_dont_have_common_prefix); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=747907"); + g_test_set_nonfatal_assertions (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-file-operations-copy-files.c b/test/automated/displayless/test-file-operations-copy-files.c new file mode 100644 index 0000000..1a7d802 --- /dev/null +++ b/test/automated/displayless/test-file-operations-copy-files.c @@ -0,0 +1,1316 @@ +#include "test-utilities.h" + +static void +test_copy_one_file (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (result_file, NULL)); + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_one_file_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "copy_first_dir_child"); + g_assert_false (g_file_query_exists (result_file, NULL)); + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_one_empty_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + first_dir = g_file_get_child (root, "copy_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (result_file, NULL)); + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_one_empty_directory_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + first_dir = g_file_get_child (root, "copy_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "copy_first_dir_child"); + g_assert_false (g_file_query_exists (result_file, NULL)); + + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_files_small (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("copy", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_files_small_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("copy", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_files_medium (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("copy", 1000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_files_medium_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("copy", 1000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + test_operation_undo (); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_files_large (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("copy", 10000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_files_large_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("copy", 10000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_directories_small (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("copy", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_directories_small_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("copy", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_directories_medium (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("copy", 1000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_directories_medium_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("copy", 1000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 1000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_directories_large (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("copy", 10000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_directories_large_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("copy", 10000); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (root, file_name); + g_assert_true (file != NULL); + g_free (file_name); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "copy_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_copy_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 10000; i++) + { + file_name = g_strdup_printf ("copy_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_child + * /tmp/second_dir + * We're copying first_dir to second_dir. + */ +static void +test_copy_full_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_full_directory_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_child + * /tmp/first_dir/second_child + * /tmp/second_dir + * We're copying first_dir to second_dir. + */ +static void +test_copy_first_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "copy_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "copy_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_first_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "copy_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "copy_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_child/second_child + * /tmp/second_dir + * We're copying first_dir to second_dir. + */ +static void +test_copy_second_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_second_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (file, "copy_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (file, "copy_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "copy_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_child"); + file = g_file_get_child (file, "copy_second_child"); + + file = g_file_get_child (first_dir, "copy_first_child"); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_second_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_second_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (file, "copy_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (file, "copy_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "copy_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_child"); + file = g_file_get_child (file, "copy_second_child"); + + file = g_file_get_child (first_dir, "copy_first_child"); + + empty_directory_by_prefix (root, "copy"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_dir1/dir1_child + * /tmp/first_dir/first_dir_dir2/dir2_child + * /tmp/second_dir + * We're copying first_dir to second_dir. + */ +static void +test_copy_third_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_third_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_dir1"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "copy_dir1_child"); + g_assert_true (file != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_dir2"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "copy_dir2_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_dir1"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir1_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_dir1"); + + file = g_file_get_child (result_file, "copy_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir2_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_dir2"); + + file = g_file_get_child (first_dir, "copy_first_dir_dir1"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir1_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "copy_first_dir_dir1"); + + file = g_file_get_child (first_dir, "copy_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir2_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "copy_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_third_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_third_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_dir1"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "copy_dir1_child"); + g_assert_true (file != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_dir2"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "copy_dir2_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_copy_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "copy_first_dir"); + + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_dir1"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir1_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "copy_first_dir_dir2"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir2_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_dir_dir1"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir1_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "copy_first_dir_dir1"); + + file = g_file_get_child (first_dir, "copy_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "copy_dir2_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "copy_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_child + * /tmp/second_dir/second_dir_child + * /tmp/third_dir + * We're copying first_dir and second_dir to third_dir. + */ +static void +test_copy_fourth_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) third_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_fourth_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + files = g_list_prepend (files, g_object_ref (second_dir)); + g_assert_true (second_dir != NULL); + + file = g_file_get_child (second_dir, "copy_second_dir_child"); + g_assert_true (file != NULL); + + third_dir = g_file_get_child (root, "copy_third_dir"); + g_assert_true (third_dir != NULL); + + nautilus_file_operations_copy_sync (files, + third_dir); + + result_file = g_file_get_child (third_dir, "copy_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + result_file = g_file_get_child (third_dir, "copy_second_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_second_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (first_dir, NULL)); + + file = g_file_get_child (second_dir, "copy_second_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (second_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +test_copy_fourth_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) third_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_fourth_hierarchy ("copy"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "copy_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "copy_second_dir"); + files = g_list_prepend (files, g_object_ref (second_dir)); + g_assert_true (second_dir != NULL); + + file = g_file_get_child (second_dir, "copy_second_dir_child"); + g_assert_true (file != NULL); + + third_dir = g_file_get_child (root, "copy_third_dir"); + g_assert_true (third_dir != NULL); + + nautilus_file_operations_copy_sync (files, + third_dir); + + test_operation_undo (); + + result_file = g_file_get_child (third_dir, "copy_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + result_file = g_file_get_child (third_dir, "copy_second_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "copy_second_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "copy_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (first_dir, NULL)); + + file = g_file_get_child (second_dir, "copy_second_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (second_dir, NULL)); + + empty_directory_by_prefix (root, "copy"); +} + +static void +setup_test_suite (void) +{ + g_test_add_func ("/test-copy-one-file/1.0", + test_copy_one_file); + g_test_add_func ("/test-copy-one-file-undo/1.0", + test_copy_one_file_undo); + g_test_add_func ("/test-copy-one-empty-directory/1.0", + test_copy_one_empty_directory); + g_test_add_func ("/test-copy-one-empty-directory-undo/1.0", + test_copy_one_empty_directory_undo); + g_test_add_func ("/test-copy-files/1.0", + test_copy_files_small); + g_test_add_func ("/test-copy-files-undo/1.0", + test_copy_files_small_undo); + g_test_add_func ("/test-copy-files/1.1", + test_copy_files_medium); + g_test_add_func ("/test-copy-files-undo/1.1", + test_copy_files_medium_undo); + g_test_add_func ("/test-copy-files/1.2", + test_copy_files_large); + g_test_add_func ("/test-copy-files-undo/1.2", + test_copy_files_large_undo); + g_test_add_func ("/test-copy-directories/1.0", + test_copy_directories_small); + g_test_add_func ("/test-copy-directories-undo/1.0", + test_copy_directories_small_undo); + g_test_add_func ("/test-copy-directories/1.1", + test_copy_directories_medium); + g_test_add_func ("/test-copy-directories-undo/1.1", + test_copy_directories_medium_undo); + g_test_add_func ("/test-copy-directories/1.2", + test_copy_directories_large); + g_test_add_func ("/test-copy-directories-undo/1.2", + test_copy_directories_large_undo); + g_test_add_func ("/test-copy-hierarchy/1.0", + test_copy_full_directory); + g_test_add_func ("/test-copy-hierarchy-undo/1.0", + test_copy_full_directory_undo); + g_test_add_func ("/test-copy-hierarchy/1.1", + test_copy_first_hierarchy); + g_test_add_func ("/test-copy-hierarchy-undo/1.1", + test_copy_first_hierarchy_undo); + g_test_add_func ("/test-copy-hierarchy/1.2", + test_copy_second_hierarchy); + g_test_add_func ("/test-copy-hierarchy-undo/1.2", + test_copy_second_hierarchy_undo); + g_test_add_func ("/test-copy-hierarchy/1.3", + test_copy_third_hierarchy); + g_test_add_func ("/test-copy-hierarchy-undo/1.3", + test_copy_third_hierarchy_undo); + g_test_add_func ("/test-copy-hierarchy/1.4", + test_copy_fourth_hierarchy); + g_test_add_func ("/test-copy-hierarchy-undo/1.4", + test_copy_fourth_hierarchy_undo); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (NautilusFileUndoManager) undo_manager = NULL; + + undo_manager = nautilus_file_undo_manager_new (); + g_test_init (&argc, &argv, NULL); + g_test_set_nonfatal_assertions (); + nautilus_ensure_extension_points (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-file-operations-dir-has-files.c b/test/automated/displayless/test-file-operations-dir-has-files.c new file mode 100644 index 0000000..06c7df2 --- /dev/null +++ b/test/automated/displayless/test-file-operations-dir-has-files.c @@ -0,0 +1,87 @@ +#include <glib.h> +#include <glib/gprintf.h> +#include "src/nautilus-directory.h" +#include "src/nautilus-file-utilities.h" +#include "src/nautilus-search-directory.h" +#include "src/nautilus-directory.h" +#include "src/nautilus-file-operations.c" +#include <unistd.h> +#include "eel/eel-string.h" + +/* Tests the function for a simple file */ +static void +test_simple_file (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + + root = g_file_new_for_path (g_get_tmp_dir ()); + file = g_file_get_child (root, "simple_file"); + g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_assert_false (dir_has_files (file)); + g_assert_true (g_file_delete (file, NULL, NULL)); +} + +/* Tests the function for an empty directory */ +static void +test_empty_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) child = NULL; + + root = g_file_new_for_path (g_get_tmp_dir ()); + child = g_file_get_child (root, "empty_dir"); + + g_assert_true (child != NULL); + + g_file_make_directory (child, NULL, NULL); + + g_assert_false (dir_has_files (child)); + g_assert_true (g_file_delete (child, NULL, NULL)); +} + +/* Tests the function for a directory containing one directory */ +static void +test_directory_one_file (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) parent_dir = NULL; + g_autoptr (GFile) child_file = NULL; + + root = g_file_new_for_path (g_get_tmp_dir ()); + parent_dir = g_file_get_child (root, "parent_dir"); + g_assert_true (parent_dir != NULL); + g_file_make_directory (parent_dir, NULL, NULL); + + child_file = g_file_get_child (parent_dir, "child_file"); + g_assert_true (child_file != NULL); + g_file_make_directory (child_file, NULL, NULL); + + g_assert_true (dir_has_files (parent_dir)); + g_assert_true (g_file_delete (child_file, NULL, NULL)); + g_assert_true (g_file_delete (parent_dir, NULL, NULL)); +} + +static void +setup_test_suite (void) +{ + g_test_add_func ("/dir-has-files-simple-file/1.0", + test_simple_file); + g_test_add_func ("/dir-has-files-empty-dir/1.0", + test_empty_directory); + g_test_add_func ("/dir-has-files-directroy-one-file/1.0", + test_directory_one_file); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + g_test_set_nonfatal_assertions (); + nautilus_ensure_extension_points (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-file-operations-move-files.c b/test/automated/displayless/test-file-operations-move-files.c new file mode 100644 index 0000000..5ad8ccb --- /dev/null +++ b/test/automated/displayless/test-file-operations-move-files.c @@ -0,0 +1,1947 @@ +#include "test-utilities.h" + +static void +test_move_one_file (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "move_first_dir_child"); + + g_assert_true (g_file_query_exists (result_file, NULL)); + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_one_file_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "move_first_dir_child"); + + g_assert_false (g_file_query_exists (result_file, NULL)); + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_one_file_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (second_dir, "move_first_dir_child"); + + g_assert_true (g_file_query_exists (result_file, NULL)); + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_one_empty_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "move_first_dir_child"); + g_assert_true (g_file_query_exists (result_file, NULL)); + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_one_empty_directory_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "move_first_dir_child"); + g_assert_false (g_file_query_exists (result_file, NULL)); + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_one_empty_directory_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (second_dir, "move_first_dir_child"); + g_assert_true (g_file_query_exists (result_file, NULL)); + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_small (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_small_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_small_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo_redo (); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_medium (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_medium_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_medium_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo_redo (); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_large (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 500); + + root = g_file_new_for_path (g_get_tmp_dir ()); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_large_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 500); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + test_operation_undo_redo (); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_files_large_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 500); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo_redo (); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_small (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_small_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_small_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 10); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo_redo (); + + for (int i = 0; i < 10; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_medium (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_medium_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_medium_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_files ("move", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo_redo (); + + for (int i = 0; i < 50; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_large (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 500); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_large_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 500); + + root = g_file_new_for_path (g_get_tmp_dir ()); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo (); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_directories_large_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + gchar *file_name; + + create_multiple_directories ("move", 500); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + dir = g_file_get_child (root, "move_dir"); + g_assert_true (dir != NULL); + + nautilus_file_operations_move_sync (files, + dir); + + test_operation_undo_redo (); + + for (int i = 0; i < 500; i++) + { + file_name = g_strdup_printf ("move_file_%i", i); + file = g_file_get_child (dir, file_name); + g_free (file_name); + g_assert_true (g_file_query_exists (file, NULL)); + } + + g_assert_true (g_file_query_exists (dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_child + * /tmp/second_dir + * We're moving first_dir to second_dir. + */ +static void +test_move_full_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_full_directory_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_full_directory_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_child + * /tmp/first_dir/second_child + * /tmp/second_dir + * We're moving first_dir to second_dir. + */ +static void +test_move_first_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "move_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "move_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_first_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "move_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "move_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_first_hierarchy_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "move_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "move_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_child/second_child + * /tmp/second_dir + * We're moving first_dir to second_dir. + */ +static void +test_move_second_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_second_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (file, "move_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (file, "move_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_second_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_second_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (file, "move_second_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (file, "move_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + file = g_file_get_child (file, "move_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_second_hierarchy_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_second_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + file = g_file_get_child (file, "move_second_child"); + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (file, "move_second_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "move_first_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_child"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_second_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_dir1/dir1_child + * /tmp/first_dir/first_dir_dir2/dir2_child + * /tmp/second_dir + * We're moving first_dir to second_dir. + */ +static void +test_move_third_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_third_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "move_dir1_child"); + g_assert_true (file != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "move_dir2_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_dir1"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir1_child"); + file = g_file_get_child (result_file, "move_first_dir_dir1"); + + file = g_file_get_child (result_file, "move_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir2_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_dir2"); + + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir1_child"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir2_child"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_third_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_third_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "move_dir1_child"); + g_assert_true (file != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "move_dir2_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_dir1"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir1_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (result_file, "move_first_dir_dir2"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir2_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir1_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir2_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + + g_assert_true (g_file_query_exists (first_dir, NULL)); + + g_assert_true (g_file_delete (second_dir, NULL, NULL)); +} + +static void +test_move_third_hierarchy_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_third_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "move_dir1_child"); + g_assert_true (file != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_true (file != NULL); + + file = g_file_get_child (file, "move_dir2_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + g_assert_true (second_dir != NULL); + + nautilus_file_operations_move_sync (files, + second_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (second_dir, "move_first_dir"); + + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_dir1"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir1_child"); + file = g_file_get_child (result_file, "move_first_dir_dir1"); + + file = g_file_get_child (result_file, "move_first_dir_dir2"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir2_child"); + g_assert_true (g_file_query_exists (file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_dir2"); + + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir1_child"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "move_first_dir_dir1"); + + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (file, "move_dir2_child"); + g_assert_false (g_file_query_exists (file, NULL)); + file = g_file_get_child (first_dir, "move_first_dir_dir2"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_child + * /tmp/second_dir/second_dir_child + * /tmp/third_dir + * We're moving first_dir and second_dir to third_dir. + */ +static void +test_move_fourth_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) third_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_fourth_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + files = g_list_prepend (files, g_object_ref (second_dir)); + g_assert_true (second_dir != NULL); + + file = g_file_get_child (second_dir, "move_second_dir_child"); + g_assert_true (file != NULL); + + third_dir = g_file_get_child (root, "move_third_dir"); + g_assert_true (third_dir != NULL); + + nautilus_file_operations_move_sync (files, + third_dir); + + result_file = g_file_get_child (third_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + result_file = g_file_get_child (third_dir, "move_second_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_second_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + g_assert_false (g_file_query_exists (first_dir, NULL)); + + file = g_file_get_child (second_dir, "move_second_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + g_assert_false (g_file_query_exists (second_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_fourth_hierarchy_undo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) third_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_fourth_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + files = g_list_prepend (files, g_object_ref (second_dir)); + g_assert_true (second_dir != NULL); + + file = g_file_get_child (second_dir, "move_second_dir_child"); + g_assert_true (file != NULL); + + third_dir = g_file_get_child (root, "move_third_dir"); + g_assert_true (third_dir != NULL); + + nautilus_file_operations_move_sync (files, + third_dir); + + test_operation_undo (); + + result_file = g_file_get_child (third_dir, "move_first_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + result_file = g_file_get_child (third_dir, "move_second_dir"); + g_assert_false (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_second_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (first_dir, NULL)); + + file = g_file_get_child (second_dir, "move_second_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + g_assert_true (g_file_query_exists (second_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +test_move_fourth_hierarchy_undo_redo (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) third_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + g_autolist (GFile) files = NULL; + + create_fourth_hierarchy ("move"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + first_dir = g_file_get_child (root, "move_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_true (file != NULL); + + second_dir = g_file_get_child (root, "move_second_dir"); + files = g_list_prepend (files, g_object_ref (second_dir)); + g_assert_true (second_dir != NULL); + + file = g_file_get_child (second_dir, "move_second_dir_child"); + g_assert_true (file != NULL); + + third_dir = g_file_get_child (root, "move_third_dir"); + g_assert_true (third_dir != NULL); + + nautilus_file_operations_move_sync (files, + third_dir); + + test_operation_undo_redo (); + + result_file = g_file_get_child (third_dir, "move_first_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_first_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + result_file = g_file_get_child (third_dir, "move_second_dir"); + g_assert_true (g_file_query_exists (result_file, NULL)); + file = g_file_get_child (result_file, "move_second_dir_child"); + g_assert_true (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "move_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + g_assert_false (g_file_query_exists (first_dir, NULL)); + + file = g_file_get_child (second_dir, "move_second_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + g_assert_false (g_file_query_exists (second_dir, NULL)); + + empty_directory_by_prefix (root, "move"); +} + +static void +setup_test_suite (void) +{ + g_test_add_func ("/test-move-one-file/1.0", + test_move_one_file); + g_test_add_func ("/test-move-one-file-undo/1.0", + test_move_one_file_undo); + g_test_add_func ("/test-move-one-file-undo-redo/1.0", + test_move_one_file_undo_redo); + g_test_add_func ("/test-move-one-empty-directory/1.0", + test_move_one_empty_directory); + g_test_add_func ("/test-move-one-empty-directory-undo/1.0", + test_move_one_empty_directory_undo); + g_test_add_func ("/test-move-one-empty-directory-undo-redo/1.0", + test_move_one_empty_directory_undo_redo); + g_test_add_func ("/test-move-files/1.0", + test_move_files_small); + g_test_add_func ("/test-move-files-undo/1.0", + test_move_files_small_undo); + g_test_add_func ("/test-move-files-undo-redo/1.0", + test_move_files_small_undo_redo); + g_test_add_func ("/test-move-files/1.1", + test_move_files_medium); + g_test_add_func ("/test-move-files-undo/1.1", + test_move_files_medium_undo); + g_test_add_func ("/test-move-files-undo-redo/1.1", + test_move_files_medium_undo_redo); + g_test_add_func ("/test-move-files/1.2", + test_move_files_large); + g_test_add_func ("/test-move-files-undo/1.2", + test_move_files_large_undo); + g_test_add_func ("/test-move-files-undo-redo/1.2", + test_move_files_large_undo_redo); + g_test_add_func ("/test-move-directories/1.0", + test_move_directories_small); + g_test_add_func ("/test-move-directories-undo/1.0", + test_move_directories_small_undo); + g_test_add_func ("/test-move-directories-undo-redo/1.0", + test_move_directories_small_undo_redo); + g_test_add_func ("/test-move-directories/1.1", + test_move_directories_medium); + g_test_add_func ("/test-move-directories-undo/1.1", + test_move_directories_medium_undo); + g_test_add_func ("/test-move-directories-undo-redo/1.1", + test_move_directories_medium_undo_redo); + g_test_add_func ("/test-move-directories/1.2", + test_move_directories_large); + g_test_add_func ("/test-move-directories-undo/1.2", + test_move_directories_large_undo); + g_test_add_func ("/test-move-directories-undo-redo/1.2", + test_move_directories_large_undo_redo); + g_test_add_func ("/test-move-hierarchy/1.0", + test_move_full_directory); + g_test_add_func ("/test-move-hierarchy-undo/1.0", + test_move_full_directory_undo); + g_test_add_func ("/test-move-hierarchy-undo-redo/1.0", + test_move_full_directory_undo_redo); + g_test_add_func ("/test-move-hierarchy/1.1", + test_move_first_hierarchy); + g_test_add_func ("/test-move-hierarchy-undo/1.1", + test_move_first_hierarchy_undo); + g_test_add_func ("/test-move-hierarchy-undo-redo/1.1", + test_move_first_hierarchy_undo_redo); + g_test_add_func ("/test-move-hierarchy/1.2", + test_move_second_hierarchy); + g_test_add_func ("/test-move-hierarchy-undo/1.2", + test_move_second_hierarchy_undo); + g_test_add_func ("/test-move-hierarchy-undo-redo/1.2", + test_move_second_hierarchy_undo_redo); + g_test_add_func ("/test-move-hierarchy/1.3", + test_move_third_hierarchy); + g_test_add_func ("/test-move-hierarchy-undo/1.3", + test_move_third_hierarchy_undo); + g_test_add_func ("/test-move-hierarchy-undo-redo/1.3", + test_move_third_hierarchy_undo_redo); + g_test_add_func ("/test-move-hierarchy/1.4", + test_move_fourth_hierarchy); + g_test_add_func ("/test-move-hierarchy-undo/1.4", + test_move_fourth_hierarchy_undo); + g_test_add_func ("/test-move-hierarchy-undo-redo/1.4", + test_move_fourth_hierarchy_undo_redo); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (NautilusFileUndoManager) undo_manager = NULL; + + undo_manager = nautilus_file_undo_manager_new (); + g_test_init (&argc, &argv, NULL); + g_test_set_nonfatal_assertions (); + nautilus_ensure_extension_points (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-file-operations-trash-or-delete.c b/test/automated/displayless/test-file-operations-trash-or-delete.c new file mode 100644 index 0000000..b1f4109 --- /dev/null +++ b/test/automated/displayless/test-file-operations-trash-or-delete.c @@ -0,0 +1,582 @@ +#include "test-utilities.h" + +static void +test_trash_one_file (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + nautilus_file_operations_trash_or_delete_sync (files); + + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_trash_more_files_func (gint files_to_trash) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_multiple_files ("trash_or_delete", files_to_trash); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < files_to_trash; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + nautilus_file_operations_trash_or_delete_sync (files); + + for (int i = 0; i < files_to_trash; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_false (g_file_query_exists (file, NULL)); + } + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_trash_more_files (void) +{ + test_trash_more_files_func (100); +} + +static void +test_delete_one_file (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + + nautilus_file_operations_delete_sync (files); + + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_delete_more_files_func (gint files_to_delete) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_multiple_files ("trash_or_delete", files_to_delete); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < files_to_delete; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + nautilus_file_operations_delete_sync (files); + + for (int i = 0; i < files_to_delete; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_false (g_file_query_exists (file, NULL)); + } + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_delete_more_files (void) +{ + test_delete_more_files_func (100); +} + +static void +test_trash_one_empty_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_true (file != NULL); + + files = g_list_prepend (files, g_object_ref (file)); + + nautilus_file_operations_trash_or_delete_sync (files); + + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_trash_more_empty_directories_func (gint directories_to_trash) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + + create_multiple_directories ("trash_or_delete", directories_to_trash); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < directories_to_trash; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + nautilus_file_operations_trash_or_delete_sync (files); + + for (int i = 0; i < directories_to_trash; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + g_assert_false (g_file_query_exists (file, NULL)); + } + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_trash_more_empty_directories (void) +{ + test_trash_more_empty_directories_func (100); +} + +static void +test_delete_one_empty_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_one_empty_directory ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + g_assert_true (first_dir != NULL); + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_true (file != NULL); + + files = g_list_prepend (files, g_object_ref (file)); + + nautilus_file_operations_delete_sync (files); + + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_delete_more_empty_directories_func (gint directories_to_delete) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + g_autolist (GFile) files = NULL; + + create_multiple_directories ("trash_or_delete", directories_to_delete); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < directories_to_delete; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + files = g_list_prepend (files, g_object_ref (file)); + } + + nautilus_file_operations_delete_sync (files); + + + for (int i = 0; i < directories_to_delete; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_true (file != NULL); + g_assert_false (g_file_query_exists (file, NULL)); + } + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +test_delete_more_empty_directories (void) +{ + test_delete_more_empty_directories_func (100); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_child + * We're trashing first_dir. + */ +static void +test_trash_full_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_true (file != NULL); + + files = g_list_prepend (files, g_object_ref (first_dir)); + + nautilus_file_operations_trash_or_delete_sync (files); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_child + * /tmp/first_dir/second_child + * We're trashing first_dir. + */ +static void +test_trash_first_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "trash_or_delete_second_child"); + g_assert_true (file != NULL); + + nautilus_file_operations_trash_or_delete_sync (files); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "trash_or_delete_second_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +/* We're creating 50 directories each containing one file + * and trashing all of the directories. + */ +static void +test_trash_third_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) directory = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_multiple_full_directories ("trash_or_delete", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 50; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_directory_%i", i); + + directory = g_file_get_child (root, file_name); + g_free (file_name); + files = g_list_prepend (files, g_object_ref (directory)); + } + + nautilus_file_operations_trash_or_delete_sync (files); + + for (int i = 0; i < 50; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_directory_%i", i); + + directory = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_false (g_file_query_exists (directory, NULL)); + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (directory, file_name); + g_free (file_name); + g_assert_false (g_file_query_exists (file, NULL)); + } + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_dir_child + * We're deleting first_dir. + */ +static void +test_delete_full_directory (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_one_file ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_true (file != NULL); + + files = g_list_prepend (files, g_object_ref (first_dir)); + + nautilus_file_operations_delete_sync (files); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + g_assert_false (g_file_query_exists (file, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +/* The hierarchy looks like this: + * /tmp/first_dir/first_child + * /tmp/first_dir/second_child + * We're deleting first_dir. + */ +static void +test_delete_first_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_first_hierarchy ("trash_or_delete"); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + first_dir = g_file_get_child (root, "trash_or_delete_first_dir"); + files = g_list_prepend (files, g_object_ref (first_dir)); + g_assert_true (first_dir != NULL); + + file = g_file_get_child (first_dir, "trash_or_delete_first_child"); + g_assert_true (file != NULL); + file = g_file_get_child (first_dir, "trash_or_delete_second_child"); + g_assert_true (file != NULL); + + nautilus_file_operations_delete_sync (files); + + file = g_file_get_child (first_dir, "trash_or_delete_first_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + file = g_file_get_child (first_dir, "trash_or_delete_second_dir_child"); + g_assert_false (g_file_query_exists (file, NULL)); + + g_assert_false (g_file_query_exists (first_dir, NULL)); + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +/* We're creating 50 directories each containing one file + * and deleting all of the directories. + */ +static void +test_delete_third_hierarchy (void) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) directory = NULL; + g_autoptr (GFile) file = NULL; + g_autolist (GFile) files = NULL; + + create_multiple_full_directories ("trash_or_delete", 50); + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < 50; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_directory_%i", i); + + directory = g_file_get_child (root, file_name); + g_free (file_name); + files = g_list_prepend (files, g_object_ref (directory)); + } + + nautilus_file_operations_delete_sync (files); + + for (int i = 0; i < 50; i++) + { + gchar *file_name; + + file_name = g_strdup_printf ("trash_or_delete_directory_%i", i); + + directory = g_file_get_child (root, file_name); + g_free (file_name); + g_assert_false (g_file_query_exists (directory, NULL)); + + file_name = g_strdup_printf ("trash_or_delete_file_%i", i); + file = g_file_get_child (directory, file_name); + g_free (file_name); + g_assert_false (g_file_query_exists (file, NULL)); + } + + empty_directory_by_prefix (root, "trash_or_delete"); +} + +static void +setup_test_suite (void) +{ + g_test_add_func ("/test-trash-one-file/1.0", + test_trash_one_file); + g_test_add_func ("/test-trash-more-files/1.0", + test_trash_more_files); + g_test_add_func ("/test-delete-one-file/1.0", + test_delete_one_file); + g_test_add_func ("/test-delete-more-files/1.0", + test_delete_more_files); + g_test_add_func ("/test-trash-one-empty-directory/1.0", + test_trash_one_empty_directory); + g_test_add_func ("/test-trash-more-empty-directories/1.0", + test_trash_more_empty_directories); + g_test_add_func ("/test-delete-one-empty-directory/1.0", + test_delete_one_empty_directory); + g_test_add_func ("/test-delete-more-directories/1.0", + test_delete_more_empty_directories); + g_test_add_func ("/test-trash-one-full-directory/1.0", + test_trash_full_directory); + g_test_add_func ("/test-trash-one-full-directory/1.1", + test_trash_first_hierarchy); + g_test_add_func ("/test-trash-more-full-directories/1.2", + test_trash_third_hierarchy); + g_test_add_func ("/test-delete-one-full-directory/1.0", + test_delete_full_directory); + g_test_add_func ("/test-delete-one-full-directory/1.1", + test_delete_first_hierarchy); + g_test_add_func ("/test-delete-more-full-directories/1.6", + test_delete_third_hierarchy); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (NautilusFileUndoManager) undo_manager = NULL; + + g_test_init (&argc, &argv, NULL); + g_test_set_nonfatal_assertions (); + nautilus_ensure_extension_points (); + undo_manager = nautilus_file_undo_manager_new (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-file-utilities-get-common-filename-prefix.c b/test/automated/displayless/test-file-utilities-get-common-filename-prefix.c new file mode 100644 index 0000000..4c044a0 --- /dev/null +++ b/test/automated/displayless/test-file-utilities-get-common-filename-prefix.c @@ -0,0 +1,459 @@ +#include <glib.h> +#include <glib/gprintf.h> + +#include "src/nautilus-file-utilities.h" + + +static void +free_list_and_result (GList *list, + char *result) +{ + g_list_free (list); + g_free (result); +} + +static void +test_has_large_enough_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "test"); + list = g_list_append (list, "tests"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("test", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_has_large_enough_common_prefix_with_spaces_in_middle (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "Cpt J Yossarian r1"); + list = g_list_append (list, "Cpt J Yossarian a1"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("Cpt J Yossarian", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_has_large_enough_common_prefix_with_punctuation_in_middle (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "Cpt-J_Yossarian r1"); + list = g_list_append (list, "Cpt-J_Yossarian a1"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("Cpt-J_Yossarian", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_has_large_enough_common_prefix_with_punctuation_in_middle_and_extension (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "Cpt-J, Yossarian.xml"); + list = g_list_append (list, "Cpt-J, Yossarian.xsl"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("Cpt-J, Yossarian", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_doesnt_have_large_enough_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "foo"); + list = g_list_append (list, "foob"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_doesnt_have_large_enough_common_prefix_completely_different_strings (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "this string really"); + list = g_list_append (list, "isn't the same as the other"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_doesnt_have_large_enough_common_prefix_first_character_differs (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "foo"); + list = g_list_append (list, "roo"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_doesnt_have_large_enough_common_prefix_first_character_differs_longer_string (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "fools"); + list = g_list_append (list, "rools"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_has_large_enough_common_prefix_until_extension_removed (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "tes.txt"); + list = g_list_append (list, "tes.tar"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + +static void +test_extension_is_removed (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "nau tilus.c"); + list = g_list_append (list, "nau tilus.cpp"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("nau tilus", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_whitespace_is_removed (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "nautilus "); + list = g_list_append (list, "nautilus two"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("nautilus", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_whitespace_and_extension_are_removed (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "nautilus !£ $\" foo.tar.gz"); + list = g_list_append (list, "nautilus !£ $\" .lzma"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("nautilus !£ $\"", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_punctuation_is_preserved (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "nautilus (2018!£$%^&* ()_+-={}[ ];':@#~<>?,./\".mp4"); + list = g_list_append (list, "nautilus (2018!£$%^&* ()_+-={}[ ];':@#~<>?,./\".srt"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("nautilus (2018!£$%^&* ()_+-={}[ ];':@#~<>?,./\"", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_unicode_on_outside (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "ӶtestӶ234"); + list = g_list_append (list, "ӶtestӶ1"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("ӶtestӶ", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_unicode_on_inside (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "QQӶtestӶabb234"); + list = g_list_append (list, "QQӶtestӶabb1"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("QQӶtestӶabb", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_unicode_whole_string (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "ǣȸʸͻͻΎΘΛ"); + list = g_list_append (list, "ǣȸʸͻͻΎΘ"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("ǣȸʸͻͻΎΘ", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_unicode_extension (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "test.ǣȸʸͻͻΎΘΛ"); + list = g_list_append (list, "test.ǣȸʸͻͻΎΘ"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("test", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_unicode_with_punctuation (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "ǣȸʸ- ͻͻΎΘ$%%^"); + list = g_list_append (list, "ǣȸʸ- ͻͻΎΘ$%%&"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("ǣȸʸ- ͻͻΎΘ$%%", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_many_strings (void) +{ + GList *list = NULL; + char *actual; + char *filename; + int i; + + for (i = 0; i < 500; ++i) + { + filename = g_strdup_printf ("we are no longer the knights who say nii%d", i); + list = g_list_append (list, filename); + } + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_cmpstr ("we are no longer the knights who say nii", ==, actual); + + g_free (actual); + g_list_free_full (list, g_free); +} + +static void +test_many_strings_last_differs (void) +{ + GList *list = NULL; + char *actual; + char *filename; + int i; + + for (i = 0; i < 500; ++i) + { + filename = g_strdup_printf ("we are no longer the knights who say nii%d", i); + + if (i == 499) + { + filename[2] = 'X'; + } + + list = g_list_append (list, filename); + } + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + g_free (actual); + g_list_free_full (list, g_free); +} + +static void +test_many_strings_first_differs (void) +{ + GList *list = NULL; + char *actual; + char *filename; + int i; + + for (i = 0; i < 500; ++i) + { + filename = g_strdup_printf ("we are no longer the knights who say nii%d", i); + + if (i == 0) + { + filename[2] = 'X'; + } + + list = g_list_append (list, filename); + } + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 4); + g_assert_null (actual); + + g_free (actual); + g_list_free_full (list, g_free); +} + +static void +test_smaller_min_length_and_does_have_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "CA"); + list = g_list_append (list, "CB"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 1); + g_assert_cmpstr ("C", ==, actual); + + free_list_and_result (list, actual); +} + +static void +test_smaller_min_length_and_doesnt_have_common_prefix (void) +{ + GList *list = NULL; + char *actual; + + list = g_list_append (list, "CA"); + list = g_list_append (list, "BB"); + + actual = nautilus_get_common_filename_prefix_from_filenames (list, 1); + g_assert_null (actual); + + free_list_and_result (list, actual); +} + + +static void +setup_test_suite (void) +{ + g_test_add_func ("/get-common-filename-prefix/1.0", + test_has_large_enough_common_prefix); + g_test_add_func ("/get-common-filename-prefix/1.1", + test_has_large_enough_common_prefix_with_spaces_in_middle); + g_test_add_func ("/get-common-filename-prefix/1.2", + test_has_large_enough_common_prefix_with_punctuation_in_middle); + g_test_add_func ("/get-common-filename-prefix/1.3", + test_has_large_enough_common_prefix_with_punctuation_in_middle_and_extension); + + g_test_add_func ("/get-common-filename-prefix/2.0", + test_doesnt_have_large_enough_common_prefix); + g_test_add_func ("/get-common-filename-prefix/2.1", + test_doesnt_have_large_enough_common_prefix_completely_different_strings); + g_test_add_func ("/get-common-filename-prefix/2.2", + test_doesnt_have_large_enough_common_prefix_first_character_differs); + g_test_add_func ("/get-common-filename-prefix/2.3", + test_doesnt_have_large_enough_common_prefix_first_character_differs_longer_string); + + g_test_add_func ("/get-common-filename-prefix/3.0", + test_has_large_enough_common_prefix_until_extension_removed); + + g_test_add_func ("/get-common-filename-prefix/4.0", + test_extension_is_removed); + g_test_add_func ("/get-common-filename-prefix/4.1", + test_whitespace_is_removed); + g_test_add_func ("/get-common-filename-prefix/4.2", + test_whitespace_and_extension_are_removed); + g_test_add_func ("/get-common-filename-prefix/4.3", + test_punctuation_is_preserved); + + g_test_add_func ("/get-common-filename-prefix/5.0", + test_unicode_on_inside); + g_test_add_func ("/get-common-filename-prefix/5.1", + test_unicode_on_outside); + g_test_add_func ("/get-common-filename-prefix/5.2", + test_unicode_whole_string); + g_test_add_func ("/get-common-filename-prefix/5.3", + test_unicode_extension); + g_test_add_func ("/get-common-filename-prefix/5.4", + test_unicode_with_punctuation); + + g_test_add_func ("/get-common-filename-prefix/6.0", + test_many_strings); + g_test_add_func ("/get-common-filename-prefix/6.1", + test_many_strings_last_differs); + g_test_add_func ("/get-common-filename-prefix/6.2", + test_many_strings_first_differs); + + g_test_add_func ("/get-common-filename-prefix/7.0", + test_smaller_min_length_and_does_have_common_prefix); + g_test_add_func ("/get-common-filename-prefix/7.1", + test_smaller_min_length_and_doesnt_have_common_prefix); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=747907"); + g_test_set_nonfatal_assertions (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-file-utilities.c b/test/automated/displayless/test-file-utilities.c new file mode 100644 index 0000000..17a7c92 --- /dev/null +++ b/test/automated/displayless/test-file-utilities.c @@ -0,0 +1,240 @@ +#include <glib.h> +#include <glib/gprintf.h> +#include "src/nautilus-directory.h" +#include "src/nautilus-file-utilities.h" +#include "src/nautilus-search-directory.h" +#include "src/nautilus-directory.h" +#include "src/nautilus-file.h" +#include "src/nautilus-file.c" +#include <unistd.h> +#include "eel/eel-string.h" + +#define ROOT_DIR "file:///tmp" + +/* Tests the function for empty selections */ +static void +test_both_null (void) +{ + g_assert_true (nautilus_file_selection_equal (NULL, NULL)); +} + +/* Tests the function for an empty and a non-empty selection */ +static void +test_either_null (void) +{ + g_autoptr (NautilusFile) file = NULL; + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) selection = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + + file = nautilus_file_new_from_filename (directory, "null_first", FALSE); + nautilus_directory_add_file (directory, file); + + selection = g_list_append (selection, g_object_ref (file)); + g_assert_false (nautilus_file_selection_equal (NULL, selection)); + g_assert_false (nautilus_file_selection_equal (selection, NULL)); +} + +/* tests the function for 2 identical selections, each containing one file */ +static void +test_one_file_equal (void) +{ + g_autoptr (NautilusFile) file = NULL; + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) selection = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + + file = nautilus_file_new_from_filename (directory, "one_file_equal", FALSE); + nautilus_directory_add_file (directory, file); + + selection = g_list_append (selection, g_object_ref (file)); + g_assert_true (nautilus_file_selection_equal (selection, selection)); +} + +/* Tests the function for 2 different selections, each containing one file */ +static void +test_one_file_different (void) +{ + g_autoptr (NautilusFile) one_file_first = NULL; + g_autoptr (NautilusFile) one_file_second = NULL; + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) first_selection = NULL; + g_autolist (NautilusFile) second_selection = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + + one_file_first = nautilus_file_new_from_filename (directory, "one_file_first", FALSE); + nautilus_directory_add_file (directory, one_file_first); + + one_file_second = nautilus_file_new_from_filename (directory, "one_file_second", FALSE); + nautilus_directory_add_file (directory, one_file_second); + + first_selection = g_list_append (first_selection, g_object_ref (one_file_first)); + second_selection = g_list_append (second_selection, g_object_ref (one_file_second)); + g_assert_false (nautilus_file_selection_equal (first_selection, second_selection)); +} + +/* Tests the function for 2 identical selections, each containing 50 files */ +static void +test_multiple_files_equal_medium (void) +{ + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) selection = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + for (gint index = 0; index < 50; index++) + { + g_autoptr (NautilusFile) file = NULL; + g_autofree gchar *file_name = NULL; + + file_name = g_strdup_printf ("multiple_files_equal_medium_%i", index); + file = nautilus_file_new_from_filename (directory, file_name, FALSE); + nautilus_directory_add_file (directory, file); + selection = g_list_prepend (selection, g_object_ref (file)); + } + + g_assert_true (nautilus_file_selection_equal (selection, selection)); +} + +/* Tests the function for 2 different selections, each containing 51 files, + * the last file being the different one */ +static void +test_multiple_files_different_medium (void) +{ + g_autoptr (NautilusFile) first_file = NULL; + g_autoptr (NautilusFile) second_file = NULL; + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) first_selection = NULL; + g_autolist (NautilusFile) second_selection = NULL; + g_autofree gchar *first_file_name = NULL; + g_autofree gchar *second_file_name = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + for (gint index = 0; index < 50; index++) + { + first_file_name = g_strdup_printf ("multiple_files_different_medium_%i", index); + first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE); + nautilus_directory_add_file (directory, first_file); + g_assert_true (NAUTILUS_IS_FILE (first_file)); + first_selection = g_list_prepend (first_selection, g_object_ref (first_file)); + second_selection = g_list_prepend (second_selection, g_object_ref (first_file)); + } + + first_file_name = g_strdup_printf ("multiple_files_different_medium_lastElement"); + second_file_name = g_strdup_printf ("multiple_files_different_medium_differentElement"); + first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE); + nautilus_directory_add_file (directory, first_file); + second_file = nautilus_file_new_from_filename (directory, second_file_name, FALSE); + nautilus_directory_add_file (directory, second_file); + g_assert_true (NAUTILUS_IS_FILE (first_file)); + g_assert_true (NAUTILUS_IS_FILE (second_file)); + first_selection = g_list_append (first_selection, g_object_ref (first_file)); + second_selection = g_list_append (second_selection, g_object_ref (second_file)); + + g_assert_false (nautilus_file_selection_equal (first_selection, second_selection)); +} + +/* Tests the function for 2 identical selections, each containing 1000 files */ +static void +test_multiple_files_equal_large (void) +{ + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) selection = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + for (gint index = 0; index < 1000; index++) + { + g_autoptr (NautilusFile) file = NULL; + g_autofree gchar *file_name = NULL; + + file_name = g_strdup_printf ("multiple_files_equal_large_%i", index); + file = nautilus_file_new_from_filename (directory, file_name, FALSE); + nautilus_directory_add_file (directory, file); + g_assert_true (NAUTILUS_IS_FILE (file)); + selection = g_list_prepend (selection, g_object_ref (file)); + } + + g_assert_true (nautilus_file_selection_equal (selection, selection)); +} + +/* Tests the function for 2 different selections, each containing 1001 files, + * the last file being the different one */ +static void +test_multiple_files_different_large (void) +{ + g_autoptr (NautilusFile) first_file = NULL; + g_autoptr (NautilusFile) second_file = NULL; + g_autoptr (NautilusDirectory) directory = NULL; + g_autolist (NautilusFile) first_selection = NULL; + g_autolist (NautilusFile) second_selection = NULL; + g_autofree gchar *first_file_name = NULL; + g_autofree gchar *second_file_name = NULL; + + directory = nautilus_directory_get_by_uri (ROOT_DIR); + g_assert_true (NAUTILUS_IS_DIRECTORY (directory)); + for (gint index = 0; index < 1000; index++) + { + first_file_name = g_strdup_printf ("multiple_files_different_large_%i", index); + first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE); + nautilus_directory_add_file (directory, first_file); + g_assert_true (NAUTILUS_IS_FILE (first_file)); + first_selection = g_list_prepend (first_selection, g_object_ref (first_file)); + second_selection = g_list_prepend (second_selection, g_object_ref (first_file)); + } + + first_file_name = g_strdup_printf ("multiple_files_different_large_lastElement"); + second_file_name = g_strdup_printf ("multiple_files_different_large_differentElement"); + first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE); + nautilus_directory_add_file (directory, first_file); + second_file = nautilus_file_new_from_filename (directory, second_file_name, FALSE); + nautilus_directory_add_file (directory, second_file); + g_assert_true (NAUTILUS_IS_FILE (first_file)); + g_assert_true (NAUTILUS_IS_FILE (second_file)); + + first_selection = g_list_append (first_selection, g_object_ref (first_file)); + second_selection = g_list_append (second_selection, g_object_ref (second_file)); + + g_assert_false (nautilus_file_selection_equal (first_selection, second_selection)); +} + +static void +setup_test_suite (void) +{ + g_test_add_func ("/file-selection-equal-null/1.0", + test_both_null); + g_test_add_func ("/file-selection-equal-null/1.1", + test_either_null); + g_test_add_func ("/file-selection-equal-files/1.0", + test_one_file_equal); + g_test_add_func ("/file-selection-equal-files/1.1", + test_multiple_files_equal_medium); + g_test_add_func ("/file-selection-equal-files/1.2", + test_multiple_files_equal_large); + g_test_add_func ("/file-selection-different-files/1.0", + test_one_file_different); + g_test_add_func ("/file-selection-different-files/1.1", + test_multiple_files_different_medium); + g_test_add_func ("/file-selection-different-files/1.2", + test_multiple_files_different_large); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + g_test_set_nonfatal_assertions (); + nautilus_ensure_extension_points (); + + setup_test_suite (); + + return g_test_run (); +} diff --git a/test/automated/displayless/test-nautilus-search-engine-model.c b/test/automated/displayless/test-nautilus-search-engine-model.c new file mode 100644 index 0000000..0a68bd7 --- /dev/null +++ b/test/automated/displayless/test-nautilus-search-engine-model.c @@ -0,0 +1,79 @@ +#include "test-utilities.h" + +static guint total_hits = 0; + +static void +hits_added_cb (NautilusSearchEngine *engine, + GSList *hits) +{ + g_print ("Hits added for search engine model!\n"); + for (gint hit_number = 0; hits != NULL; hits = hits->next, hit_number++) + { + g_print ("Hit %i: %s\n", hit_number, nautilus_search_hit_get_uri (hits->data)); + + total_hits += 1; + } +} + +static void +finished_cb (NautilusSearchEngine *engine, + NautilusSearchProviderStatus status, + gpointer user_data) +{ + nautilus_search_provider_stop (NAUTILUS_SEARCH_PROVIDER (engine)); + + g_print ("\nNautilus search engine model finished!\n"); + + delete_search_file_hierarchy ("model"); + + g_main_loop_quit (user_data); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (GMainLoop) loop = NULL; + NautilusSearchEngine *engine; + NautilusSearchEngineModel *model; + g_autoptr (NautilusDirectory) directory = NULL; + g_autoptr (NautilusQuery) query = NULL; + g_autoptr (GFile) location = NULL; + + loop = g_main_loop_new (NULL, FALSE); + + nautilus_ensure_extension_points (); + /* Needed for nautilus-query.c. + * FIXME: tests are not installed, so the system does not + * have the gschema. Installed tests is a long term GNOME goal. + */ + nautilus_global_preferences_init (); + + engine = nautilus_search_engine_new (); + g_signal_connect (engine, "hits-added", + G_CALLBACK (hits_added_cb), NULL); + g_signal_connect (engine, "finished", + G_CALLBACK (finished_cb), loop); + + query = nautilus_query_new (); + nautilus_query_set_text (query, "engine_model"); + nautilus_search_provider_set_query (NAUTILUS_SEARCH_PROVIDER (engine), query); + + location = g_file_new_for_path (g_get_tmp_dir ()); + directory = nautilus_directory_get (location); + model = nautilus_search_engine_get_model_provider (engine); + nautilus_search_engine_model_set_model (model, directory); + + nautilus_query_set_location (query, location); + + create_search_file_hierarchy ("model"); + + nautilus_search_engine_start_by_target (NAUTILUS_SEARCH_PROVIDER (engine), + NAUTILUS_SEARCH_ENGINE_MODEL_ENGINE); + + g_main_loop_run (loop); + + g_assert_cmpint (total_hits, ==, 3); + + return 0; +} diff --git a/test/automated/displayless/test-nautilus-search-engine-simple.c b/test/automated/displayless/test-nautilus-search-engine-simple.c new file mode 100644 index 0000000..9657bb6 --- /dev/null +++ b/test/automated/displayless/test-nautilus-search-engine-simple.c @@ -0,0 +1,74 @@ +#include "test-utilities.h" + +static guint total_hits = 0; + +static void +hits_added_cb (NautilusSearchEngine *engine, + GSList *hits) +{ + g_print ("Hits added for search engine simple!\n"); + for (gint hit_number = 0; hits != NULL; hits = hits->next, hit_number++) + { + g_print ("Hit %i: %s\n", hit_number, nautilus_search_hit_get_uri (hits->data)); + total_hits += 1; + } +} + +static void +finished_cb (NautilusSearchEngine *engine, + NautilusSearchProviderStatus status, + gpointer user_data) +{ + nautilus_search_provider_stop (NAUTILUS_SEARCH_PROVIDER (engine)); + + g_print ("\nNautilus search engine simple finished!\n"); + + delete_search_file_hierarchy ("simple"); + + g_main_loop_quit (user_data); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (GMainLoop) loop = NULL; + NautilusSearchEngine *engine; + g_autoptr (NautilusDirectory) directory = NULL; + g_autoptr (NautilusQuery) query = NULL; + g_autoptr (GFile) location = NULL; + + loop = g_main_loop_new (NULL, FALSE); + + nautilus_ensure_extension_points (); + /* Needed for nautilus-query.c. + * FIXME: tests are not installed, so the system does not + * have the gschema. Installed tests is a long term GNOME goal. + */ + nautilus_global_preferences_init (); + + engine = nautilus_search_engine_new (); + g_signal_connect (engine, "hits-added", + G_CALLBACK (hits_added_cb), NULL); + g_signal_connect (engine, "finished", + G_CALLBACK (finished_cb), loop); + + query = nautilus_query_new (); + nautilus_query_set_text (query, "engine_simple"); + nautilus_search_provider_set_query (NAUTILUS_SEARCH_PROVIDER (engine), query); + + location = g_file_new_for_path (g_get_tmp_dir ()); + directory = nautilus_directory_get (location); + nautilus_query_set_location (query, location); + + create_search_file_hierarchy ("simple"); + + nautilus_search_engine_start_by_target (NAUTILUS_SEARCH_PROVIDER (engine), + NAUTILUS_SEARCH_ENGINE_SIMPLE_ENGINE); + + g_main_loop_run (loop); + + g_assert_cmpint (total_hits, ==, 3); + + return 0; +} diff --git a/test/automated/displayless/test-nautilus-search-engine-tracker.c b/test/automated/displayless/test-nautilus-search-engine-tracker.c new file mode 100644 index 0000000..0c96ef7 --- /dev/null +++ b/test/automated/displayless/test-nautilus-search-engine-tracker.c @@ -0,0 +1,185 @@ +#include "test-utilities.h" + +/* Time in seconds we allow for Tracker Miners to index the file */ +#define TRACKER_MINERS_AWAIT_TIMEOUT 1000 + +static guint total_hits = 0; + +typedef struct +{ + GMainLoop *main_loop; + gchar *uri; + gboolean created; +} TrackerAwaitFileData; + +static TrackerAwaitFileData * +tracker_await_file_data_new (const char *uri, + GMainLoop *main_loop) +{ + TrackerAwaitFileData *data; + + data = g_slice_new0 (TrackerAwaitFileData); + data->uri = g_strdup (uri); + data->main_loop = g_main_loop_ref (main_loop); + + return data; +} + +static void +tracker_await_file_data_free (TrackerAwaitFileData *data) +{ + g_free (data->uri); + g_main_loop_unref (data->main_loop); + g_slice_free (TrackerAwaitFileData, data); +} + +static gboolean timeout_cb (gpointer user_data) +{ + TrackerAwaitFileData *data = user_data; + g_error ("Timeout waiting for %s to be indexed by Tracker.", data->uri); + return G_SOURCE_REMOVE; +} + +static void +tracker_events_cb (TrackerNotifier *self, + gchar *service, + gchar *graph, + GPtrArray *events, + gpointer user_data) +{ + TrackerAwaitFileData *data = user_data; + int i; + + for (i = 0; i < events->len; i++) + { + TrackerNotifierEvent *event = g_ptr_array_index (events, i); + + if (tracker_notifier_event_get_event_type (event) == TRACKER_NOTIFIER_EVENT_CREATE) + { + const gchar *urn = tracker_notifier_event_get_urn (event); + g_debug ("Got CREATED event for %s", urn); + if (strcmp (urn, data->uri) == 0) + { + data->created = TRUE; + g_main_loop_quit (data->main_loop); + } + } + } +} + +/* Create data that the Tracker indexer will find, and wait for the database to be updated. */ +static void +create_test_data (TrackerSparqlConnection *connection, + const gchar *indexed_tmpdir) +{ + g_autoptr (GFile) test_file = NULL; + g_autoptr (GMainLoop) main_loop = NULL; + g_autoptr (GError) error = NULL; + g_autoptr (TrackerNotifier) notifier = NULL; + TrackerAwaitFileData *await_data; + gulong signal_id, timeout_id; + + test_file = g_file_new_build_filename (indexed_tmpdir, "target_file.txt", NULL); + + main_loop = g_main_loop_new (NULL, 0); + await_data = tracker_await_file_data_new (g_file_get_uri (test_file), main_loop); + + notifier = tracker_sparql_connection_create_notifier (connection); + + signal_id = g_signal_connect (notifier, "events", G_CALLBACK (tracker_events_cb), await_data); + timeout_id = g_timeout_add_seconds (TRACKER_MINERS_AWAIT_TIMEOUT, timeout_cb, await_data); + + g_file_set_contents (g_file_peek_path (test_file), "Please show me in the search results", -1, &error); + g_assert_no_error (error); + + g_main_loop_run (main_loop); + + g_assert (await_data->created); + g_source_remove (timeout_id); + g_clear_signal_handler (&signal_id, notifier); + + tracker_await_file_data_free (await_data); +} + +static void +hits_added_cb (NautilusSearchEngine *engine, + GSList *hits) +{ + g_print ("Hits added for search engine tracker!\n"); + for (gint hit_number = 0; hits != NULL; hits = hits->next, hit_number++) + { + g_print ("Hit %i: %s\n", hit_number, nautilus_search_hit_get_uri (hits->data)); + total_hits += 1; + } +} + +static void +finished_cb (NautilusSearchEngine *engine, + NautilusSearchProviderStatus status, + gpointer user_data) +{ + nautilus_search_provider_stop (NAUTILUS_SEARCH_PROVIDER (engine)); + + g_print ("\nNautilus search engine tracker finished!\n"); + + g_main_loop_quit (user_data); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (GMainLoop) loop = NULL; + g_autoptr (TrackerSparqlConnection) connection = NULL; + NautilusSearchEngine *engine; + g_autoptr (NautilusDirectory) directory = NULL; + g_autoptr (NautilusQuery) query = NULL; + g_autoptr (GFile) location = NULL; + g_autoptr (GError) error = NULL; + const gchar *indexed_tmpdir; + + indexed_tmpdir = g_getenv ("TRACKER_INDEXED_TMPDIR"); + if (!indexed_tmpdir) + { + g_error ("This test must be inside the `tracker-sandbox` script " + "to ensure a private Tracker indexer daemon is used."); + } + + connection = tracker_sparql_connection_bus_new ("org.freedesktop.Tracker3.Miner.Files", NULL, NULL, &error); + + g_assert_no_error (error); + + loop = g_main_loop_new (NULL, FALSE); + + nautilus_ensure_extension_points (); + /* Needed for nautilus-query.c. + * FIXME: tests are not installed, so the system does not + * have the gschema. Installed tests is a long term GNOME goal. + */ + nautilus_global_preferences_init (); + + create_test_data (connection, indexed_tmpdir); + + engine = nautilus_search_engine_new (); + g_signal_connect (engine, "hits-added", + G_CALLBACK (hits_added_cb), NULL); + g_signal_connect (engine, "finished", + G_CALLBACK (finished_cb), loop); + + query = nautilus_query_new (); + nautilus_query_set_text (query, "target"); + nautilus_search_provider_set_query (NAUTILUS_SEARCH_PROVIDER (engine), query); + + location = g_file_new_for_path (indexed_tmpdir); + directory = nautilus_directory_get (location); + nautilus_query_set_location (query, location); + + nautilus_search_engine_start_by_target (NAUTILUS_SEARCH_PROVIDER (engine), + NAUTILUS_SEARCH_ENGINE_TRACKER_ENGINE); + + g_main_loop_run (loop); + + g_assert_cmpint (total_hits, ==, 1); + + return 0; +} diff --git a/test/automated/displayless/test-nautilus-search-engine.c b/test/automated/displayless/test-nautilus-search-engine.c new file mode 100644 index 0000000..cc843de --- /dev/null +++ b/test/automated/displayless/test-nautilus-search-engine.c @@ -0,0 +1,74 @@ +#include "test-utilities.h" + +static guint total_hits = 0; + +static void +hits_added_cb (NautilusSearchEngine *engine, + GSList *hits) +{ + g_print ("Hits added for search engine!\n"); + for (gint hit_number = 0; hits != NULL; hits = hits->next, hit_number++) + { + g_print ("Hit %i: %s\n", hit_number, nautilus_search_hit_get_uri (hits->data)); + total_hits += 1; + } +} + +static void +finished_cb (NautilusSearchEngine *engine, + NautilusSearchProviderStatus status, + gpointer user_data) +{ + nautilus_search_provider_stop (NAUTILUS_SEARCH_PROVIDER (engine)); + + g_print ("\nNautilus search engine finished!\n"); + + delete_search_file_hierarchy ("all_engines"); + + g_main_loop_quit (user_data); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (GMainLoop) loop = NULL; + NautilusSearchEngine *engine; + g_autoptr (NautilusDirectory) directory = NULL; + g_autoptr (NautilusQuery) query = NULL; + g_autoptr (GFile) location = NULL; + g_autoptr (GFile) file = NULL; + + loop = g_main_loop_new (NULL, FALSE); + + nautilus_ensure_extension_points (); + /* Needed for nautilus-query.c. + * FIXME: tests are not installed, so the system does not + * have the gschema. Installed tests is a long term GNOME goal. + */ + nautilus_global_preferences_init (); + + engine = nautilus_search_engine_new (); + g_signal_connect (engine, "hits-added", + G_CALLBACK (hits_added_cb), NULL); + g_signal_connect (engine, "finished", + G_CALLBACK (finished_cb), loop); + + query = nautilus_query_new (); + nautilus_query_set_text (query, "engine_all_engines"); + nautilus_search_provider_set_query (NAUTILUS_SEARCH_PROVIDER (engine), query); + + location = g_file_new_for_path (g_get_tmp_dir ()); + directory = nautilus_directory_get (location); + nautilus_query_set_location (query, location); + + create_search_file_hierarchy ("all_engines"); + + nautilus_search_provider_start (NAUTILUS_SEARCH_PROVIDER (engine)); + + g_main_loop_run (loop); + + g_assert_cmpint (total_hits, ==, 3); + + return 0; +} diff --git a/test/automated/displayless/test-utilities.c b/test/automated/displayless/test-utilities.c new file mode 100644 index 0000000..68014ea --- /dev/null +++ b/test/automated/displayless/test-utilities.c @@ -0,0 +1,558 @@ +#include "test-utilities.h" + +void +empty_directory_by_prefix (GFile *parent, + gchar *prefix) +{ + g_autoptr (GFileEnumerator) enumerator = NULL; + g_autoptr (GFile) child = NULL; + + enumerator = g_file_enumerate_children (parent, + G_FILE_ATTRIBUTE_STANDARD_NAME, + G_FILE_QUERY_INFO_NONE, + NULL, + NULL); + + g_file_enumerator_iterate (enumerator, NULL, &child, NULL, NULL); + while (child != NULL) + { + gboolean res; + + if (g_str_has_prefix (g_file_get_basename (child), prefix)) + { + res = g_file_delete (child, NULL, NULL); + /* The directory is not empty */ + if (!res) + { + empty_directory_by_prefix (child, prefix); + g_file_delete (child, NULL, NULL); + } + } + + g_file_enumerator_iterate (enumerator, NULL, &child, NULL, NULL); + } +} + +void +create_search_file_hierarchy (gchar *search_engine) +{ + g_autoptr (GFile) location = NULL; + g_autoptr (GFile) file = NULL; + GFileOutputStream *out; + gchar *file_name; + + location = g_file_new_for_path (g_get_tmp_dir ()); + + file_name = g_strdup_printf ("engine_%s", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + out = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_object_unref (out); + + file_name = g_strdup_printf ("engine_%s_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_child", search_engine); + file = g_file_get_child (file, file_name); + g_free (file_name); + out = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_object_unref (out); + + file_name = g_strdup_printf ("engine_%s_second_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("engine_%s_child", search_engine); + file = g_file_get_child (file, file_name); + g_free (file_name); + out = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_object_unref (out); + + file_name = g_strdup_printf ("%s_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("engine_%s_child", search_engine); + file = g_file_get_child (file, file_name); + g_free (file_name); + out = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_object_unref (out); +} + +void +delete_search_file_hierarchy (gchar *search_engine) +{ + g_autoptr (GFile) location = NULL; + g_autoptr (GFile) file = NULL; + gchar *file_name; + + location = g_file_new_for_path (g_get_tmp_dir ()); + + file_name = g_strdup_printf ("engine_%s", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); + + file_name = g_strdup_printf ("engine_%s_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + file_name = g_strdup_printf ("%s_child", search_engine); + file = g_file_get_child (file, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); + file_name = g_strdup_printf ("engine_%s_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); + + file_name = g_strdup_printf ("engine_%s_second_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + file_name = g_strdup_printf ("engine_%s_child", search_engine); + file = g_file_get_child (file, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); + file_name = g_strdup_printf ("engine_%s_second_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + file_name = g_strdup_printf ("engine_%s_child", search_engine); + file = g_file_get_child (file, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); + file_name = g_strdup_printf ("%s_directory", search_engine); + file = g_file_get_child (location, file_name); + g_free (file_name); + g_file_delete (file, NULL, NULL); +} + +/* This callback function quits the mainloop inside which the + * asynchronous undo/redo operation happens. + */ + +void +quit_loop_callback (NautilusFileUndoManager *undo_manager, + GMainLoop *loop) +{ + g_main_loop_quit (loop); +} + +/* This undoes the last operation blocking the current main thread. */ +void +test_operation_undo (void) +{ + g_autoptr (GMainLoop) loop = NULL; + g_autoptr (GMainContext) context = NULL; + gulong handler_id; + + context = g_main_context_new (); + g_main_context_push_thread_default (context); + loop = g_main_loop_new (context, FALSE); + + handler_id = g_signal_connect (nautilus_file_undo_manager_get (), + "undo-changed", + G_CALLBACK (quit_loop_callback), + loop); + + nautilus_file_undo_manager_undo (NULL, NULL); + + g_main_loop_run (loop); + + g_main_context_pop_thread_default (context); + + g_signal_handler_disconnect (nautilus_file_undo_manager_get (), + handler_id); +} + +/* This undoes and redoes the last move operation blocking the current main thread. */ +void +test_operation_undo_redo (void) +{ + g_autoptr (GMainLoop) loop = NULL; + g_autoptr (GMainContext) context = NULL; + gulong handler_id; + + test_operation_undo (); + + context = g_main_context_new (); + g_main_context_push_thread_default (context); + loop = g_main_loop_new (context, FALSE); + + handler_id = g_signal_connect (nautilus_file_undo_manager_get (), + "undo-changed", + G_CALLBACK (quit_loop_callback), + loop); + + nautilus_file_undo_manager_redo (NULL, NULL); + + g_main_loop_run (loop); + + g_main_context_pop_thread_default (context); + + g_signal_handler_disconnect (nautilus_file_undo_manager_get (), + handler_id); +} + +/* Creates the following hierarchy: + * /tmp/`prefix`_first_dir/`prefix`_first_dir_child + * /tmp/`prefix`_second_dir/ + */ +void +create_one_file (gchar *prefix) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + GFileOutputStream *out; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + file_name = g_strdup_printf ("%s_first_dir", prefix); + first_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_dir_child", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + out = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_object_unref (out); + + file_name = g_strdup_printf ("%s_second_dir", prefix); + second_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); +} + +/* Creates the same hierarchy as above, but all files being directories. */ +void +create_one_empty_directory (gchar *prefix) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + file_name = g_strdup_printf ("%s_first_dir", prefix); + first_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_dir_child", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_second_dir", prefix); + second_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); +} + +void +create_multiple_files (gchar *prefix, + gint number_of_files) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < number_of_files; i++) + { + GFileOutputStream *out; + + file_name = g_strdup_printf ("%s_file_%i", prefix, i); + file = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + out = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL); + g_object_unref (out); + } + + file_name = g_strdup_printf ("%s_dir", prefix); + dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (dir != NULL); + g_file_make_directory (dir, NULL, NULL); +} + +void +create_multiple_directories (gchar *prefix, + gint number_of_directories) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) dir = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < number_of_directories; i++) + { + file_name = g_strdup_printf ("%s_file_%i", prefix, i); + file = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + } + + file_name = g_strdup_printf ("%s_dir", prefix); + dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (dir != NULL); + g_file_make_directory (dir, NULL, NULL); +} + +void +create_first_hierarchy (gchar *prefix) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + file_name = g_strdup_printf ("%s_first_dir", prefix); + first_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_child", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + file_name = g_strdup_printf ("%s_second_child", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_second_dir", prefix); + second_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); +} + +void +create_second_hierarchy (gchar *prefix) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + file_name = g_strdup_printf ("%s_first_dir", prefix); + first_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_child", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + file_name = g_strdup_printf ("%s_second_child", prefix); + file = g_file_get_child (file, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_second_dir", prefix); + second_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); +} + +void +create_third_hierarchy (gchar *prefix) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + file_name = g_strdup_printf ("%s_first_dir", prefix); + first_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_dir_dir1", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_dir1_child", prefix); + file = g_file_get_child (file, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_dir_dir2", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_dir2_child", prefix); + file = g_file_get_child (file, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_second_dir", prefix); + second_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); +} + +void +create_fourth_hierarchy (gchar *prefix) +{ + g_autoptr (GFile) root = NULL; + g_autoptr (GFile) first_dir = NULL; + g_autoptr (GFile) second_dir = NULL; + g_autoptr (GFile) third_dir = NULL; + g_autoptr (GFile) file = NULL; + g_autoptr (GFile) result_file = NULL; + gchar *file_name; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + file_name = g_strdup_printf ("%s_first_dir", prefix); + first_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (first_dir != NULL); + g_file_make_directory (first_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_first_dir_child", prefix); + file = g_file_get_child (first_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_second_dir", prefix); + second_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (second_dir != NULL); + g_file_make_directory (second_dir, NULL, NULL); + + file_name = g_strdup_printf ("%s_second_dir_child", prefix); + file = g_file_get_child (second_dir, file_name); + g_free (file_name); + + g_assert_true (file != NULL); + g_file_make_directory (file, NULL, NULL); + + file_name = g_strdup_printf ("%s_third_dir", prefix); + third_dir = g_file_get_child (root, file_name); + g_free (file_name); + + g_assert_true (third_dir != NULL); + g_file_make_directory (third_dir, NULL, NULL); +} + +void +create_multiple_full_directories (gchar *prefix, + gint number_of_directories) +{ + g_autoptr (GFile) root = NULL; + + root = g_file_new_for_path (g_get_tmp_dir ()); + g_assert_true (root != NULL); + + for (int i = 0; i < number_of_directories; i++) + { + g_autoptr (GFile) directory = NULL; + g_autoptr (GFile) file = NULL; + gchar *file_name; + + file_name = g_strdup_printf ("%s_directory_%i", prefix, i); + + directory = g_file_get_child (root, file_name); + g_free (file_name); + + g_file_make_directory (directory, NULL, NULL); + + file_name = g_strdup_printf ("%s_file_%i", prefix, i); + file = g_file_get_child (directory, file_name); + g_free (file_name); + + g_file_make_directory (file, NULL, NULL); + } +} diff --git a/test/automated/displayless/test-utilities.h b/test/automated/displayless/test-utilities.h new file mode 100644 index 0000000..878c878 --- /dev/null +++ b/test/automated/displayless/test-utilities.h @@ -0,0 +1,34 @@ +#include <gio/gio.h> +#include <src/nautilus-file-utilities.h> +#include <src/nautilus-global-preferences.h> +#include <src/nautilus-search-engine.h> +#include <tracker-sparql.h> +#include <locale.h> +#include <src/nautilus-file-undo-manager.h> +#include <src/nautilus-file-operations.h> +#include <gio/gio.h> + +#pragma once + +void empty_directory_by_prefix (GFile *parent, + gchar *prefix); + +void create_search_file_hierarchy (gchar *search_engine); +void delete_search_file_hierarchy (gchar *search_engine); + +void quit_loop_callback (NautilusFileUndoManager *undo_manager, + GMainLoop *loop); +void test_operation_undo_redo (void); +void test_operation_undo (void); + +void create_one_file (gchar *prefix); +void create_one_empty_directory (gchar *prefix); + +void create_multiple_files (gchar *prefix, gint number_of_files); +void create_multiple_directories (gchar *prefix, gint number_of_directories); +void create_multiple_full_directories (gchar *prefix, gint number_of_directories); + +void create_first_hierarchy (gchar *prefix); +void create_second_hierarchy (gchar *prefix); +void create_third_hierarchy (gchar *prefix); +void create_fourth_hierarchy (gchar *prefix);
\ No newline at end of file diff --git a/test/automated/meson.build b/test/automated/meson.build new file mode 100644 index 0000000..a112242 --- /dev/null +++ b/test/automated/meson.build @@ -0,0 +1,4 @@ +subdir('displayless') +if get_option('tests') == 'all' + subdir('display') +endif diff --git a/test/interactive/file-torture.py b/test/interactive/file-torture.py new file mode 100644 index 0000000..865795f --- /dev/null +++ b/test/interactive/file-torture.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python +# +# file-torture.py - Simple torture test for file notificatins in Nautilus +# Copyright (C) 2006 Federico Mena-Quintero +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, see <http://www.gnu.org/licenses/>. +# +# Author: Federico Mena-Quintero <federico@novell.com> + +import random +import os +import sys +import optparse +import time + +output_dir = "" +random_gen = None +verbose = False + +extensions = (".doc", ".gif", ".jpg", ".png", ".xls", ".odt", ".odp", ".ods", ".txt", ".zip", ".gz") + +files = [] +directories = [] + +def get_random_file_index (): + n = len (files) + if n == 0: + return -1 + else: + return random_gen.randrange (n) + +def get_random_directory_index (): + n = len (directories) + if n == 0: + return -1 + else: + return random_gen.randrange (n) + +def get_random_filename (): + chars = [] + for i in range (20): + chars.append ("abcdefghijklmnopqrstuvwxyz"[random_gen.randrange (26)]) + + extension = extensions[random_gen.randrange (len (extensions))] + filename = "".join (chars) + extension + return filename + +def get_random_path (): + return os.path.join (output_dir, get_random_filename ()) + +def op_create_file (): + filename = get_random_path () + files.append (filename) + f = open (filename, "w") + f.close () + + if verbose: + print 'create file %s' % filename + + return True + +def op_move_file (): + idx = get_random_file_index () + if idx == -1: + return False + + new_name = get_random_path () + old_name = files[idx] + os.rename (old_name, new_name) + files[idx] = new_name + + if verbose: + print 'rename file %s to %s' % (old_name, new_name) + + return True + +def op_delete_file (): + idx = get_random_file_index () + if idx == -1: + return False + + filename = files[idx] + + os.unlink (filename) + files.pop (idx) + + if verbose: + print 'delete file %s' % filename + + return True + +def op_write_file (): + idx = get_random_file_index () + if idx == -1: + return False + + name = files[idx] + f = open (name, "a") + f.write ("blah blah blah blah blah blah blah\n") + f.close () + + if verbose: + print 'write to file %s' % name + + return True + +def op_create_dir (): + name = get_random_path () + os.mkdir (name) + directories.append (name) + + if verbose: + print 'create directory %s' % name + + return True + +def op_move_dir (): + idx = get_random_directory_index () + if idx == -1: + return False + + new_name = get_random_path () + old_name = directories[idx] + os.rename (old_name, new_name) + directories[idx] = new_name + + if verbose: + print 'move directory %s to %s' % (old_name, new_name) + + return True + +def op_delete_dir (): + idx = get_random_directory_index () + if idx == -1: + return False + + name = directories[idx] + os.rmdir (name) + directories.pop (idx) + + if verbose: + print 'delete directory %s' % name + + return True + +def op_file_to_dir (): + idx = get_random_file_index () + if idx == -1: + return False + + name = files[idx] + os.unlink (name) + files.pop (idx) + os.mkdir (name) + directories.append (name) + + if verbose: + print 'file to dir %s' % name + + return True + +def op_dir_to_file (): + idx = get_random_directory_index () + if idx == -1: + return False + + name = directories[idx] + os.rmdir (name) + directories.pop (idx) + f = open (name, "w") + f.close () + files.append (name) + + if verbose: + print 'dir to file %s' % name + + return True + +operations = ( + op_create_file, + op_move_file, + op_delete_file, + op_write_file, + op_create_dir, + op_move_dir, + op_delete_dir, + op_file_to_dir, + op_dir_to_file, + ) + +def main (): + option_parser = optparse.OptionParser (usage="usage: %prog -o <dirname>") + option_parser.add_option ("-o", + "--output", dest="output", + metavar="FILE", + help="Name of output directory") + option_parser.add_option ("-s", + "--seed", dest="seed", + metavar="NUMBER", + help="Random number seed") + option_parser.add_option ("", + "--no-sleep", dest="sleep_enabled", action="store_false", default=True, + help="Disable short sleeps between operations. Will use a lot of CPU!") + option_parser.add_option ("-v", + "--verbose", dest="verbose", action="store_true", default=False, + help="Enable verbose output") + + (options, args) = option_parser.parse_args () + + if not options.output: + print 'Please specify an output directory with "-o outputdir"' + return 1 + + sleep_enabled = options.sleep_enabled + + if len (args) != 0: + print 'No extra arguments are supported' + return 1 + + global output_dir + global random_gen + global verbose + + verbose = options.verbose + + random_gen = random.Random () + if options.seed: + seed = int (options.seed) + else: + seed = int (time.time ()) + + print 'Use "--seed=%s" to reproduce this run' % seed + random_gen.seed (seed) + + if sleep_enabled: + print 'Using short sleeps between operations (use --no-sleep to disable)' + else: + print 'Disabling short sleeps between operations' + + output_dir = options.output + try: + os.mkdir (output_dir) + except: + 1 # nothing + + while True: + op = operations [random_gen.randrange (len (operations))] + op () + if sleep_enabled: + time.sleep (random_gen.random () / 100) + + return 0 + +if __name__ == "__main__": + sys.exit (main ()) diff --git a/test/interactive/meson.build b/test/interactive/meson.build new file mode 100644 index 0000000..eb8cf8a --- /dev/null +++ b/test/interactive/meson.build @@ -0,0 +1,8 @@ +test_copy = executable( + 'test-copy', [ + 'test-copy.c', + 'test.c', + 'test.h' + ], + dependencies: libnautilus_dep +) diff --git a/test/interactive/test-copy.c b/test/interactive/test-copy.c new file mode 100644 index 0000000..194eda4 --- /dev/null +++ b/test/interactive/test-copy.c @@ -0,0 +1,102 @@ +#include "test.h" + +#include <src/nautilus-file-operations.h> +#include <src/nautilus-progress-info.h> +#include <src/nautilus-progress-info-manager.h> + +static void +copy_done (GHashTable *debuting_uris, + gboolean success, + gpointer data) +{ + g_print ("Copy done\n"); +} + +static void +changed_cb (NautilusProgressInfo *info, + gpointer data) +{ + g_print ("Changed: %s -- %s\n", + nautilus_progress_info_get_status (info), + nautilus_progress_info_get_details (info)); +} + +static void +progress_changed_cb (NautilusProgressInfo *info, + gpointer data) +{ + g_print ("Progress changed: %f\n", + nautilus_progress_info_get_progress (info)); +} + +static void +finished_cb (NautilusProgressInfo *info, + gpointer data) +{ + g_print ("Finished\n"); + gtk_main_quit (); +} + +int +main (int argc, + char *argv[]) +{ + GtkWidget *window; + GList *sources; + GFile *dest; + GFile *source; + int i; + GList *infos; + NautilusProgressInfoManager *manager; + NautilusProgressInfo *progress_info; + + test_init (&argc, &argv); + + if (argc < 3) + { + g_print ("Usage test-copy <sources...> <dest dir>\n"); + return 1; + } + + sources = NULL; + for (i = 1; i < argc - 1; i++) + { + source = g_file_new_for_commandline_arg (argv[i]); + sources = g_list_prepend (sources, source); + } + sources = g_list_reverse (sources); + + dest = g_file_new_for_commandline_arg (argv[i]); + + window = test_window_new ("copy test", 5); + + gtk_widget_show (window); + + manager = nautilus_progress_info_manager_dup_singleton (); + + nautilus_file_operations_copy_async (sources, + dest, + GTK_WINDOW (window), + NULL, + copy_done, NULL); + + infos = nautilus_progress_info_manager_get_all_infos (manager); + + if (infos == NULL) + { + g_object_unref (manager); + return 0; + } + + progress_info = NAUTILUS_PROGRESS_INFO (infos->data); + + g_signal_connect (progress_info, "changed", (GCallback) changed_cb, NULL); + g_signal_connect (progress_info, "progress-changed", (GCallback) progress_changed_cb, NULL); + g_signal_connect (progress_info, "finished", (GCallback) finished_cb, NULL); + + gtk_main (); + + g_object_unref (manager); + + return 0; +} diff --git a/test/interactive/test.c b/test/interactive/test.c new file mode 100644 index 0000000..43e8fbc --- /dev/null +++ b/test/interactive/test.c @@ -0,0 +1,125 @@ +#include "test.h" +#include <sys/types.h> +#include <unistd.h> + +void +test_init (int *argc, + char ***argv) +{ + gtk_init (argc, argv); + + eel_make_warnings_and_criticals_stop_in_debugger (); +} + +int +test_quit (int exit_code) +{ + if (gtk_main_level () > 0) + { + gtk_main_quit (); + } + + return exit_code; +} + +void +test_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer callback_data) +{ + test_quit (0); +} + +GtkWidget * +test_window_new (const char *title, + guint border_width) +{ + GtkWidget *window; + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + + if (title != NULL) + { + gtk_window_set_title (GTK_WINDOW (window), title); + } + + g_signal_connect (window, "delete_event", + G_CALLBACK (test_delete_event), NULL); + + gtk_container_set_border_width (GTK_CONTAINER (window), border_width); + + return window; +} + +GdkPixbuf * +test_pixbuf_new_named (const char *name, + float scale) +{ + GdkPixbuf *pixbuf; + char *path; + + g_return_val_if_fail (name != NULL, NULL); + g_return_val_if_fail (scale >= 0.0, NULL); + + if (name[0] == '/') + { + path = g_strdup (name); + } + else + { + path = g_strdup_printf ("%s/%s", NAUTILUS_DATADIR, name); + } + + pixbuf = gdk_pixbuf_new_from_file (path, NULL); + + g_free (path); + + g_return_val_if_fail (pixbuf != NULL, NULL); + + if (scale != 1.0) + { + GdkPixbuf *scaled; + float width = gdk_pixbuf_get_width (pixbuf) * scale; + float height = gdk_pixbuf_get_width (pixbuf) * scale; + + scaled = gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR); + + g_object_unref (pixbuf); + + g_return_val_if_fail (scaled != NULL, NULL); + + pixbuf = scaled; + } + + return pixbuf; +} + +GtkWidget * +test_label_new (const char *text, + gboolean with_background, + int num_sizes_larger) +{ + GtkWidget *label; + + if (text == NULL) + { + text = "Foo"; + } + + label = gtk_label_new (text); + + return label; +} + +void +test_window_set_title_with_pid (GtkWindow *window, + const char *title) +{ + char *tmp; + + g_return_if_fail (GTK_IS_WINDOW (window)); + + tmp = g_strdup_printf ("%lu: %s", (gulong) getpid (), title); + gtk_window_set_title (GTK_WINDOW (window), tmp); + g_free (tmp); +} diff --git a/test/interactive/test.h b/test/interactive/test.h new file mode 100644 index 0000000..fc20a77 --- /dev/null +++ b/test/interactive/test.h @@ -0,0 +1,35 @@ +#pragma once + +#include <config.h> +#include <gtk/gtk.h> + +#include <eel/eel-debug.h> +#include <eel/eel.h> +#include <src/nautilus-file-utilities.h> + +void test_init (int *argc, + char ***argv); +int test_quit (int exit_code); +void test_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer callback_data); +GtkWidget *test_window_new (const char *title, + guint border_width); +void test_gtk_widget_set_background_image (GtkWidget *widget, + const char *image_name); +void test_gtk_widget_set_background_color (GtkWidget *widget, + const char *color_spec); +GdkPixbuf *test_pixbuf_new_named (const char *name, + float scale); +GtkWidget *test_label_new (const char *text, + gboolean with_background, + int num_sizes_larger); +void test_pixbuf_draw_rectangle_tiled (GdkPixbuf *pixbuf, + const char *tile_name, + int x0, + int y0, + int x1, + int y1, + int opacity); +void test_window_set_title_with_pid (GtkWindow *window, + const char *title);
\ No newline at end of file diff --git a/test/meson.build b/test/meson.build new file mode 100644 index 0000000..ab588ef --- /dev/null +++ b/test/meson.build @@ -0,0 +1,22 @@ +# When adding new tests, it is useful to set some environment variables. +# You can do it like so: +# +# test( +# name, +# executable, +# env: [ +# test_env, +# 'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()), +# 'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()) +# ] +# ) +# +# G_TEST_BUILDDIR and G_TEST_SRCDIR cannot be preset, since +# the test sources are scattered. +test_env = [ + 'GSETTINGS_SCHEMA_DIR=@0@'.format(join_paths(meson.build_root(), 'data')), + 'RUNNING_TESTS=@0@'.format('TRUE') +] + +subdir('automated') +subdir('interactive') |