1 /* GTK - The GIMP Toolkit
2 * autotestfilechooser.c: Automated unit tests for the GtkFileChooser widget
3 * Copyright (C) 2005, Novell, Inc.
6 * Federico Mena-Quintero <federico@novell.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 * - In test_reload_sequence(), test that the selection is preserved properly
30 #define SLEEP_DURATION 100
34 #include <glib/gprintf.h>
36 #include "gtk/gtkfilechooserprivate.h"
37 #include "gtk/gtkfilechooserdefault.h"
38 #include "gtk/gtkfilechooserentry.h"
42 get_action_name (GtkFileChooserAction action)
46 case GTK_FILE_CHOOSER_ACTION_OPEN: return "OPEN";
47 case GTK_FILE_CHOOSER_ACTION_SAVE: return "SAVE";
48 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER: return "SELECT_FOLDER";
49 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER: return "CREATE_FOLDER";
52 g_assert_not_reached ();
60 log_test (gboolean passed, const char *test_name, ...)
65 va_start (args, test_name);
66 str = g_strdup_vprintf (test_name, args);
70 g_printf ("%s: %s\n", passed ? "PASSED" : "FAILED", str);
74 typedef void (* SetFilenameFn) (GtkFileChooser *chooser, gpointer data);
75 typedef void (* CompareFilenameFn) (GtkFileChooser *chooser, gpointer data);
77 struct test_set_filename_closure {
79 GtkWidget *accept_button;
80 gboolean focus_button;
84 set_filename_timeout_cb (gpointer data)
86 struct test_set_filename_closure *closure;
90 if (closure->focus_button)
91 gtk_widget_grab_focus (closure->accept_button);
93 gtk_button_clicked (GTK_BUTTON (closure->accept_button));
100 static guint wait_for_idle_id = 0;
103 wait_for_idle_idle (gpointer data)
105 wait_for_idle_id = 0;
107 return G_SOURCE_REMOVE;
113 wait_for_idle_id = g_idle_add_full (G_PRIORITY_LOW + 100,
117 while (wait_for_idle_id)
118 gtk_main_iteration ();
123 test_set_filename (GtkFileChooserAction action,
124 gboolean focus_button,
125 SetFilenameFn set_filename_fn,const
126 CompareFilenameFn compare_filename_fn,
130 struct test_set_filename_closure closure;
132 chooser = gtk_file_chooser_dialog_new ("hello", NULL, action,
133 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
136 closure.chooser = chooser;
137 closure.accept_button = gtk_dialog_add_button (GTK_DIALOG (chooser), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
138 closure.focus_button = focus_button;
140 gtk_dialog_set_default_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
142 (* set_filename_fn) (GTK_FILE_CHOOSER (chooser), data);
144 gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, set_filename_timeout_cb, &closure, NULL);
145 gtk_dialog_run (GTK_DIALOG (chooser));
147 (* compare_filename_fn) (GTK_FILE_CHOOSER (chooser), data);
149 gtk_widget_destroy (chooser);
153 set_filename_cb (GtkFileChooser *chooser, gpointer data)
155 const char *filename;
158 gtk_file_chooser_set_filename (chooser, filename);
162 compare_filename_cb (GtkFileChooser *chooser, gpointer data)
164 const char *filename;
168 out_filename = gtk_file_chooser_get_filename (chooser);
170 g_assert_cmpstr (out_filename, ==, filename);
173 g_free (out_filename);
178 const char *test_name;
179 GtkFileChooserAction action;
180 const char *filename;
181 gboolean focus_button;
182 } TestSetFilenameSetup;
185 test_black_box_set_filename (gconstpointer data)
187 const TestSetFilenameSetup *setup = data;
189 test_set_filename (setup->action, setup->focus_button, set_filename_cb, compare_filename_cb, (char *) setup->filename);
192 struct current_name_closure {
194 const char *current_name;
198 set_current_name_cb (GtkFileChooser *chooser, gpointer data)
200 struct current_name_closure *closure;
204 gtk_file_chooser_set_current_folder (chooser, closure->path);
205 gtk_file_chooser_set_current_name (chooser, closure->current_name);
209 compare_current_name_cb (GtkFileChooser *chooser, gpointer data)
211 struct current_name_closure *closure;
217 out_filename = gtk_file_chooser_get_filename (chooser);
219 g_assert (out_filename != NULL);
221 filename = g_build_filename (closure->path, closure->current_name, NULL);
222 g_assert_cmpstr (filename, ==, out_filename);
225 g_free (out_filename);
230 const char *test_name;
231 GtkFileChooserAction action;
232 const char *current_name;
233 gboolean focus_button;
234 } TestSetCurrentNameSetup;
237 test_black_box_set_current_name (gconstpointer data)
239 const TestSetCurrentNameSetup *setup = data;
240 struct current_name_closure closure;
243 cwd = g_get_current_dir ();
246 closure.current_name = setup->current_name;
248 test_set_filename (setup->action, setup->focus_button, set_current_name_cb, compare_current_name_cb, &closure);
254 /* FIXME: fails in CREATE_FOLDER mode when FOLDER_NAME == "/" */
257 #define FILE_NAME "/nonexistent"
258 #define FILE_NAME_2 "/nonexistent2"
259 #define FOLDER_NAME "/etc"
260 #define FOLDER_NAME_2 "/usr"
262 #define FILE_NAME "/etc/passwd"
263 #define FILE_NAME_2 "/etc/group"
264 #define FOLDER_NAME "/etc"
265 #define FOLDER_NAME_2 "/usr"
268 #define CURRENT_NAME "parangaricutirimicuaro.txt"
269 #define CURRENT_NAME_FOLDER "parangaricutirimicuaro"
271 /* https://bugzilla.novell.com/show_bug.cgi?id=184875
272 * http://bugzilla.gnome.org/show_bug.cgi?id=347066
273 * http://bugzilla.gnome.org/show_bug.cgi?id=346058
278 setup_set_filename_tests (void)
280 static TestSetFilenameSetup tests[] =
282 { "/GtkFileChooser/black_box/set_filename/open/no_focus", GTK_FILE_CHOOSER_ACTION_OPEN, FILE_NAME, FALSE },
283 { "/GtkFileChooser/black_box/set_filename/open/focus", GTK_FILE_CHOOSER_ACTION_OPEN, FILE_NAME, TRUE },
284 { "/GtkFileChooser/black_box/set_filename/save/no_focus", GTK_FILE_CHOOSER_ACTION_SAVE, FILE_NAME, FALSE },
285 { "/GtkFileChooser/black_box/set_filename/save/focus", GTK_FILE_CHOOSER_ACTION_SAVE, FILE_NAME, TRUE },
286 { "/GtkFileChooser/black_box/set_filename/select_folder/no_focus", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, FOLDER_NAME,FALSE },
287 { "/GtkFileChooser/black_box/set_filename/select_folder/focus", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, FOLDER_NAME,TRUE },
288 { "/GtkFileChooser/black_box/set_filename/create_folder/no_focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, FOLDER_NAME,FALSE },
289 { "/GtkFileChooser/black_box/set_filename/create_folder/focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, FOLDER_NAME,TRUE },
293 for (i = 0; i < G_N_ELEMENTS (tests); i++)
294 g_test_add_data_func (tests[i].test_name, &tests[i], test_black_box_set_filename);
298 setup_set_current_name_tests (void)
300 static TestSetCurrentNameSetup tests[] =
302 { "/GtkFileChooser/black_box/set_current_name/save/no_focus", GTK_FILE_CHOOSER_ACTION_SAVE, CURRENT_NAME, FALSE },
303 { "/GtkFileChooser/black_box/set_current_name/save/focus", GTK_FILE_CHOOSER_ACTION_SAVE, CURRENT_NAME, TRUE },
304 { "/GtkFileChooser/black_box/set_current_name/create_folder/no_focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, CURRENT_NAME_FOLDER, FALSE },
305 { "/GtkFileChooser/black_box/set_current_name/create_folder/focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, CURRENT_NAME_FOLDER, TRUE },
309 for (i = 0; i < G_N_ELEMENTS (tests); i++)
310 g_test_add_data_func (tests[i].test_name, &tests[i], test_black_box_set_current_name);
316 const char *shortname;
317 GtkFileChooserAction action;
318 const char *initial_current_folder;
319 const char *initial_filename;
320 gboolean open_dialog;
321 const char *tweak_current_folder;
322 const char *tweak_filename;
323 gint dialog_response;
324 const char *final_current_folder;
325 const char *final_filename;
326 } FileChooserButtonTest;
329 make_button_test_name (FileChooserButtonTest *t)
331 return g_strdup_printf ("/GtkFileChooserButton/%s", t->shortname);
333 GString *s = g_string_new ("/GtkFileChooserButton");
335 g_string_append_printf (s, "/%s/%s/%s/%s",
336 get_action_name (t->action),
337 t->initial_current_folder ? "set_initial_folder" : "no_default_folder",
338 t->initial_filename ? "set_initial_filename" : "no_initial_filename",
339 t->open_dialog ? "open_dialog" : "no_dialog");
341 if (t->tweak_current_folder)
342 g_string_append (s, "/tweak_current_folder");
344 if (t->tweak_filename)
345 g_string_append (s, "/tweak_filename");
348 g_string_append_printf (s, "/%s",
349 t->dialog_response == GTK_RESPONSE_ACCEPT ? "accept" : "cancel");
351 if (t->final_current_folder)
352 g_string_append (s, "/final_current_folder");
354 if (t->final_filename)
355 g_string_append (s, "/final_filename");
357 return g_string_free (s, FALSE);
362 sleep_timeout_cb (gpointer data)
369 sleep_in_main_loop (void)
371 /* process all pending idles and events */
372 while (g_main_context_pending (NULL))
373 g_main_context_iteration (NULL, FALSE);
374 /* sleeping probably isn't strictly necessary here */
375 gdk_threads_add_timeout_full (G_MAXINT, 250, sleep_timeout_cb, NULL, NULL);
377 /* process any pending idles or events that arrived during sleep */
378 while (g_main_context_pending (NULL))
379 g_main_context_iteration (NULL, FALSE);
383 build_children_list (GtkWidget *widget, gpointer data)
388 *list = g_list_prepend (*list, widget);
392 find_child_widget_with_atk_role (GtkWidget *widget, AtkRole role)
394 AtkObject *accessible;
397 accessible = gtk_widget_get_accessible (widget);
398 a_role = atk_object_get_role (accessible);
404 GtkWidget *found_child;
408 if (GTK_IS_CONTAINER (widget))
414 gtk_container_forall (GTK_CONTAINER (widget), build_children_list, &children);
418 while (l && !found_child)
422 child = GTK_WIDGET (l->data);
424 found_child = find_child_widget_with_atk_role (child, role);
429 g_list_free (children);
437 get_atk_name_for_filechooser_button (GtkFileChooserButton *button)
439 GtkFileChooserAction action;
441 AtkObject *accessible;
443 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (button));
444 g_assert (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
446 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
447 widget = find_child_widget_with_atk_role (GTK_WIDGET (button), ATK_ROLE_PUSH_BUTTON);
449 widget = find_child_widget_with_atk_role (GTK_WIDGET (button), ATK_ROLE_COMBO_BOX);
451 accessible = gtk_widget_get_accessible (widget);
452 return atk_object_get_name (accessible);
456 check_that_basename_is_shown (GtkFileChooserButton *button, const char *expected_filename)
458 GtkFileChooserAction action;
459 const char *name_on_button;
460 char *expected_basename;
462 name_on_button = get_atk_name_for_filechooser_button (button);
464 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (button));
465 g_assert (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
467 if (expected_filename)
468 expected_basename = g_path_get_basename (expected_filename);
470 expected_basename = NULL;
472 if (expected_basename)
473 g_assert_cmpstr (expected_basename, ==, name_on_button);
475 g_assert_cmpstr (name_on_button, ==, "(None)"); /* see gtkfilechooserbutton.c:FALLBACK_DISPLAY_NAME */ /* FIXME: how do we translate this? */
477 g_free (expected_basename);
481 get_expected_shown_filename (GtkFileChooserAction action, const char *folder_name, const char *filename)
483 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
495 get_file_chooser_dialog_from_button (GtkFileChooserButton *button)
497 GtkWidget *fc_dialog;
499 /* Give me the internal dialog, damnit */
500 fc_dialog = g_object_get_qdata (G_OBJECT (button), g_quark_from_static_string ("gtk-file-chooser-delegate"));
501 g_assert (GTK_IS_FILE_CHOOSER (fc_dialog));
502 g_assert (GTK_IS_DIALOG (fc_dialog));
508 test_file_chooser_button (gconstpointer data)
510 const FileChooserButtonTest *setup = data;
512 GtkWidget *fc_button;
513 GtkWidget *fc_dialog;
517 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
519 fc_button = gtk_file_chooser_button_new (setup->action == GTK_FILE_CHOOSER_ACTION_OPEN ? "Select a file" : "Select a folder",
521 gtk_container_add (GTK_CONTAINER (window), fc_button);
523 if (setup->initial_current_folder)
524 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->initial_current_folder);
526 if (setup->initial_filename)
527 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->initial_filename);
529 gtk_widget_show_all (window);
530 sleep_in_main_loop ();
532 check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button),
533 get_expected_shown_filename (setup->action, setup->initial_current_folder, setup->initial_filename));
535 /* If there is a dialog to be opened, we actually test going through it a
536 * couple of times. This ensures that any state that the button frobs for
537 * each appearance of the dialog will make sense.
539 if (setup->open_dialog)
544 for (i = 0; i < iterations; i++)
546 if (setup->open_dialog)
550 /* Hack our way into the file chooser button; get its GtkButton child and click it */
551 children = gtk_container_get_children (GTK_CONTAINER (fc_button));
552 g_assert (children && GTK_IS_BUTTON (children->data));
553 gtk_button_clicked (GTK_BUTTON (children->data));
554 g_list_free (children);
556 sleep_in_main_loop ();
558 fc_dialog = get_file_chooser_dialog_from_button (GTK_FILE_CHOOSER_BUTTON (fc_button));
561 /* Okay, now frob the button and its optional dialog */
563 if (setup->tweak_current_folder)
564 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->tweak_current_folder);
566 if (setup->tweak_filename)
567 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->tweak_filename);
569 sleep_in_main_loop ();
571 if (setup->open_dialog)
573 gtk_dialog_response (GTK_DIALOG (fc_dialog), setup->dialog_response);
577 if (setup->final_current_folder)
579 char *folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (fc_button));
581 g_assert_cmpstr (folder, ==, setup->final_current_folder);
585 if (setup->final_filename)
587 char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fc_button));
589 g_assert_cmpstr (filename, ==, setup->final_filename);
593 check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button),
594 get_expected_shown_filename (setup->action, setup->final_current_folder, setup->final_filename));
597 gtk_widget_destroy (window);
601 find_accessible_action_num (AtkObject *object, const char *action_name)
607 action_a = ATK_ACTION (object);
609 num_actions = atk_action_get_n_actions (action_a);
611 for (i = 0; i < num_actions; i++)
612 if (strcmp (atk_action_get_name (action_a, i), action_name) == 0)
619 do_accessible_action (AtkObject *object, const char *action_name)
623 action_num = find_accessible_action_num (object, action_name);
624 g_assert (action_num != -1);
626 atk_action_do_action (ATK_ACTION (object), action_num);
630 test_file_chooser_button_combo_box_1 (void)
633 GtkWidget *fc_button;
634 GtkWidget *combo_box;
635 AtkObject *combo_box_a;
640 GtkWidget *fc_dialog;
642 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
644 fc_button = gtk_file_chooser_button_new ("Select a folder", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
645 gtk_container_add (GTK_CONTAINER (window), fc_button);
647 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), FOLDER_NAME);
649 gtk_widget_show_all (window);
651 /* Get the accessible for the combo box */
653 combo_box = find_child_widget_with_atk_role (GTK_WIDGET (fc_button), ATK_ROLE_COMBO_BOX);
654 combo_box_a = gtk_widget_get_accessible (combo_box);
656 /* Press the combo box to bring up the menu */
658 do_accessible_action (combo_box_a, "press");
659 sleep_in_main_loop (); /* have to wait because bringing up the menu is asynchronous... */
661 /* Get the menu from the combo box; it's the first child */
663 menu_a = atk_object_ref_accessible_child (combo_box_a, 0);
664 g_assert (atk_object_get_role (menu_a) == ATK_ROLE_MENU);
666 /* Check that the last item in the menu is the "Other…" one */
668 num_items = atk_object_get_n_accessible_children (menu_a);
669 g_assert (num_items > 0);
671 other_index = num_items - 1;
673 item_a = atk_object_ref_accessible_child (menu_a, other_index);
674 g_assert_cmpstr (atk_object_get_name (item_a), ==, "Other…"); /* FIXME: how do we translate this? */
676 /* Activate the item */
678 do_accessible_action (item_a, "click");
680 /* Cancel the dialog */
682 sleep_in_main_loop ();
683 fc_dialog = get_file_chooser_dialog_from_button (GTK_FILE_CHOOSER_BUTTON (fc_button));
685 gtk_dialog_response (GTK_DIALOG (fc_dialog), GTK_RESPONSE_CANCEL);
687 /* Now check the selection in the combo box */
688 check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button), FOLDER_NAME);
690 gtk_widget_destroy (window);
694 setup_file_chooser_button_combo_box_tests (void)
696 g_test_add_func ("/GtkFileChooserButton/combo_box-1", test_file_chooser_button_combo_box_1);
699 static FileChooserButtonTest button_tests[] =
701 /* OPEN tests without dialog */
705 GTK_FILE_CHOOSER_ACTION_OPEN,
706 NULL, /* initial_current_folder */
707 NULL, /* initial_filename */
708 FALSE, /* open_dialog */
709 NULL, /* tweak_current_folder */
710 NULL, /* tweak_filename */
711 0, /* dialog_response */
712 NULL, /* final_current_folder */
713 NULL /* final_filename */
717 GTK_FILE_CHOOSER_ACTION_OPEN,
718 NULL, /* initial_current_folder */
719 FILE_NAME, /* initial_filename */
720 FALSE, /* open_dialog */
721 NULL, /* tweak_current_folder */
722 NULL, /* tweak_filename */
723 0, /* dialog_response */
724 NULL, /* final_current_folder */
725 FILE_NAME /* final_filename */
729 GTK_FILE_CHOOSER_ACTION_OPEN,
730 NULL, /* initial_current_folder */
731 NULL, /* initial_filename */
732 FALSE, /* open_dialog */
733 NULL, /* tweak_current_folder */
734 FILE_NAME, /* tweak_filename */
735 0, /* dialog_response */
736 NULL, /* final_current_folder */
737 FILE_NAME /* final_filename */
741 GTK_FILE_CHOOSER_ACTION_OPEN,
742 NULL, /* initial_current_folder */
743 FILE_NAME, /* initial_filename */
744 FALSE, /* open_dialog */
745 NULL, /* tweak_current_folder */
746 FILE_NAME_2, /* tweak_filename */
747 0, /* dialog_response */
748 NULL, /* final_current_folder */
749 FILE_NAME_2 /* final_filename */
753 GTK_FILE_CHOOSER_ACTION_OPEN,
754 FOLDER_NAME, /* initial_current_folder */
755 NULL, /* initial_filename */
756 FALSE, /* open_dialog */
757 NULL, /* tweak_current_folder */
758 NULL, /* tweak_filename */
759 0, /* dialog_response */
760 FOLDER_NAME, /* final_current_folder */
761 NULL /* final_filename */
765 GTK_FILE_CHOOSER_ACTION_OPEN,
766 FOLDER_NAME, /* initial_current_folder */
767 NULL, /* initial_filename */
768 FALSE, /* open_dialog */
769 FOLDER_NAME_2, /* tweak_current_folder */
770 NULL, /* tweak_filename */
771 0, /* dialog_response */
772 FOLDER_NAME_2, /* final_current_folder */
773 NULL /* final_filename */
776 /* SELECT_FOLDER tests without dialog */
780 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
781 NULL, /* initial_current_folder */
782 NULL, /* initial_filename */
783 FALSE, /* open_dialog */
784 NULL, /* tweak_current_folder */
785 NULL, /* tweak_filename */
786 0, /* dialog_response */
787 NULL, /* final_current_folder */
788 NULL /* final_filename */
792 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
793 NULL, /* initial_current_folder */
794 FOLDER_NAME, /* initial_filename */
795 FALSE, /* open_dialog */
796 NULL, /* tweak_current_folder */
797 NULL, /* tweak_filename */
798 0, /* dialog_response */
799 NULL, /* final_current_folder */
800 FOLDER_NAME /* final_filename */
804 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
805 NULL, /* initial_current_folder */
806 FOLDER_NAME, /* initial_filename */
807 FALSE, /* open_dialog */
808 NULL, /* tweak_current_folder */
809 FOLDER_NAME_2, /* tweak_filename */
810 0, /* dialog_response */
811 NULL, /* final_current_folder */
812 FOLDER_NAME_2 /* final_filename */
816 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
817 FOLDER_NAME, /* initial_current_folder */
818 NULL, /* initial_filename */
819 FALSE, /* open_dialog */
820 NULL, /* tweak_current_folder */
821 NULL, /* tweak_filename */
822 0, /* dialog_response */
823 NULL, /* final_current_folder */
824 FOLDER_NAME /* final_filename */
828 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
829 FOLDER_NAME, /* initial_current_folder */
830 NULL, /* initial_filename */
831 FALSE, /* open_dialog */
832 NULL, /* tweak_current_folder */
833 NULL, /* tweak_filename */
834 0, /* dialog_response */
835 FOLDER_NAME, /* final_current_folder */
836 NULL /* final_filename */
840 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
841 FOLDER_NAME, /* initial_current_folder */
842 NULL, /* initial_filename */
843 FALSE, /* open_dialog */
844 FOLDER_NAME_2, /* tweak_current_folder */
845 NULL, /* tweak_filename */
846 0, /* dialog_response */
847 NULL, /* final_current_folder */
848 FOLDER_NAME_2 /* final_filename */
852 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
853 FOLDER_NAME, /* initial_current_folder */
854 NULL, /* initial_filename */
855 FALSE, /* open_dialog */
856 FOLDER_NAME_2, /* tweak_current_folder */
857 NULL, /* tweak_filename */
858 0, /* dialog_response */
859 FOLDER_NAME_2, /* final_current_folder */
860 NULL /* final_filename */
864 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
865 FOLDER_NAME, /* initial_current_folder */
866 NULL, /* initial_filename */
867 FALSE, /* open_dialog */
868 NULL, /* tweak_current_folder */
869 FOLDER_NAME_2, /* tweak_filename */
870 0, /* dialog_response */
871 NULL, /* final_current_folder */
872 FOLDER_NAME_2 /* final_filename */
875 /* OPEN tests with dialog, cancelled */
878 "open-dialog-cancel-1",
879 GTK_FILE_CHOOSER_ACTION_OPEN,
880 NULL, /* initial_current_folder */
881 NULL, /* initial_filename */
882 TRUE, /* open_dialog */
883 NULL, /* tweak_current_folder */
884 NULL, /* tweak_filename */
885 GTK_RESPONSE_CANCEL, /* dialog_response */
886 NULL, /* final_current_folder */
887 NULL /* final_filename */
890 "open-dialog-cancel-2",
891 GTK_FILE_CHOOSER_ACTION_OPEN,
892 NULL, /* initial_current_folder */
893 FILE_NAME, /* initial_filename */
894 TRUE, /* open_dialog */
895 NULL, /* tweak_current_folder */
896 NULL, /* tweak_filename */
897 GTK_RESPONSE_CANCEL, /* dialog_response */
898 NULL, /* final_current_folder */
899 FILE_NAME /* final_filename */
902 "open-dialog-cancel-3",
903 GTK_FILE_CHOOSER_ACTION_OPEN,
904 FOLDER_NAME, /* initial_current_folder */
905 NULL, /* initial_filename */
906 TRUE, /* open_dialog */
907 NULL, /* tweak_current_folder */
908 NULL, /* tweak_filename */
909 GTK_RESPONSE_CANCEL, /* dialog_response */
910 FOLDER_NAME, /* final_current_folder */
911 NULL /* final_filename */
914 "open-dialog-cancel-4",
915 GTK_FILE_CHOOSER_ACTION_OPEN,
916 NULL, /* initial_current_folder */
917 NULL, /* initial_filename */
918 TRUE, /* open_dialog */
919 NULL, /* tweak_current_folder */
920 FILE_NAME, /* tweak_filename */
921 GTK_RESPONSE_CANCEL, /* dialog_response */
922 NULL, /* final_current_folder */
923 NULL /* final_filename */
926 "open-dialog-cancel-5",
927 GTK_FILE_CHOOSER_ACTION_OPEN,
928 NULL, /* initial_current_folder */
929 FILE_NAME, /* initial_filename */
930 TRUE, /* open_dialog */
931 NULL, /* tweak_current_folder */
932 FILE_NAME_2, /* tweak_filename */
933 GTK_RESPONSE_CANCEL, /* dialog_response */
934 NULL, /* final_current_folder */
935 FILE_NAME /* final_filename */
938 "open-dialog-cancel-6",
939 GTK_FILE_CHOOSER_ACTION_OPEN,
940 FOLDER_NAME, /* initial_current_folder */
941 NULL, /* initial_filename */
942 TRUE, /* open_dialog */
943 NULL, /* tweak_current_folder */
944 FILE_NAME_2, /* tweak_filename */
945 GTK_RESPONSE_CANCEL, /* dialog_response */
946 FOLDER_NAME, /* final_current_folder */
947 NULL /* final_filename */
950 /* OPEN tests with dialog, cancelled via closing the dialog (not by selecting the Cancel button) */
953 "open-dialog-close-1",
954 GTK_FILE_CHOOSER_ACTION_OPEN,
955 NULL, /* initial_current_folder */
956 NULL, /* initial_filename */
957 TRUE, /* open_dialog */
958 NULL, /* tweak_current_folder */
959 NULL, /* tweak_filename */
960 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
961 NULL, /* final_current_folder */
962 NULL /* final_filename */
965 "open-dialog-close-2",
966 GTK_FILE_CHOOSER_ACTION_OPEN,
967 NULL, /* initial_current_folder */
968 FILE_NAME, /* initial_filename */
969 TRUE, /* open_dialog */
970 NULL, /* tweak_current_folder */
971 NULL, /* tweak_filename */
972 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
973 NULL, /* final_current_folder */
974 FILE_NAME /* final_filename */
977 "open-dialog-close-3",
978 GTK_FILE_CHOOSER_ACTION_OPEN,
979 FOLDER_NAME, /* initial_current_folder */
980 NULL, /* initial_filename */
981 TRUE, /* open_dialog */
982 NULL, /* tweak_current_folder */
983 NULL, /* tweak_filename */
984 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
985 FOLDER_NAME, /* final_current_folder */
986 NULL /* final_filename */
989 "open-dialog-close-4",
990 GTK_FILE_CHOOSER_ACTION_OPEN,
991 NULL, /* initial_current_folder */
992 NULL, /* initial_filename */
993 TRUE, /* open_dialog */
994 NULL, /* tweak_current_folder */
995 FILE_NAME, /* tweak_filename */
996 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
997 NULL, /* final_current_folder */
998 NULL /* final_filename */
1001 "open-dialog-close-5",
1002 GTK_FILE_CHOOSER_ACTION_OPEN,
1003 NULL, /* initial_current_folder */
1004 FILE_NAME, /* initial_filename */
1005 TRUE, /* open_dialog */
1006 NULL, /* tweak_current_folder */
1007 FILE_NAME_2, /* tweak_filename */
1008 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1009 NULL, /* final_current_folder */
1010 FILE_NAME /* final_filename */
1013 "open-dialog-close-6",
1014 GTK_FILE_CHOOSER_ACTION_OPEN,
1015 FOLDER_NAME, /* initial_current_folder */
1016 NULL, /* initial_filename */
1017 TRUE, /* open_dialog */
1018 NULL, /* tweak_current_folder */
1019 FILE_NAME_2, /* tweak_filename */
1020 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1021 FOLDER_NAME, /* final_current_folder */
1022 NULL /* final_filename */
1025 /* SELECT_FOLDER tests with dialog, cancelled */
1028 "select-folder-dialog-cancel-1",
1029 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1030 NULL, /* initial_current_folder */
1031 NULL, /* initial_filename */
1032 TRUE, /* open_dialog */
1033 NULL, /* tweak_current_folder */
1034 NULL, /* tweak_filename */
1035 GTK_RESPONSE_CANCEL, /* dialog_response */
1036 NULL, /* final_current_folder */
1037 NULL /* final_filename */
1040 "select-folder-dialog-cancel-2",
1041 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1042 NULL, /* initial_current_folder */
1043 FOLDER_NAME, /* initial_filename */
1044 TRUE, /* open_dialog */
1045 NULL, /* tweak_current_folder */
1046 NULL, /* tweak_filename */
1047 GTK_RESPONSE_CANCEL, /* dialog_response */
1048 NULL, /* final_current_folder */
1049 FOLDER_NAME /* final_filename */
1052 "select-folder-dialog-cancel-3",
1053 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1054 FOLDER_NAME, /* initial_current_folder */
1055 NULL, /* initial_filename */
1056 TRUE, /* open_dialog */
1057 NULL, /* tweak_current_folder */
1058 NULL, /* tweak_filename */
1059 GTK_RESPONSE_CANCEL, /* dialog_response */
1060 FOLDER_NAME, /* final_current_folder */
1061 NULL /* final_filename */
1064 "select-folder-dialog-cancel-4",
1065 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1066 FOLDER_NAME, /* initial_current_folder */
1067 NULL, /* initial_filename */
1068 TRUE, /* open_dialog */
1069 NULL, /* tweak_current_folder */
1070 NULL, /* tweak_filename */
1071 GTK_RESPONSE_CANCEL, /* dialog_response */
1072 NULL, /* final_current_folder */
1073 FOLDER_NAME /* final_filename */
1076 "select-folder-dialog-cancel-5",
1077 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1078 NULL, /* initial_current_folder */
1079 NULL, /* initial_filename */
1080 TRUE, /* open_dialog */
1081 NULL, /* tweak_current_folder */
1082 FOLDER_NAME, /* tweak_filename */
1083 GTK_RESPONSE_CANCEL, /* dialog_response */
1084 NULL, /* final_current_folder */
1085 NULL /* final_filename */
1088 "select-folder-dialog-cancel-6",
1089 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1090 NULL, /* initial_current_folder */
1091 FOLDER_NAME, /* initial_filename */
1092 TRUE, /* open_dialog */
1093 NULL, /* tweak_current_folder */
1094 FOLDER_NAME_2, /* tweak_filename */
1095 GTK_RESPONSE_CANCEL, /* dialog_response */
1096 NULL, /* final_current_folder */
1097 FOLDER_NAME /* final_filename */
1100 "select-folder-dialog-cancel-7",
1101 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1102 FOLDER_NAME, /* initial_current_folder */
1103 NULL, /* initial_filename */
1104 TRUE, /* open_dialog */
1105 NULL, /* tweak_current_folder */
1106 FOLDER_NAME_2, /* tweak_filename */
1107 GTK_RESPONSE_CANCEL, /* dialog_response */
1108 FOLDER_NAME, /* final_current_folder */
1109 NULL /* final_filename */
1112 "select-folder-dialog-cancel-8",
1113 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1114 FOLDER_NAME, /* initial_current_folder */
1115 NULL, /* initial_filename */
1116 TRUE, /* open_dialog */
1117 NULL, /* tweak_current_folder */
1118 FOLDER_NAME_2, /* tweak_filename */
1119 GTK_RESPONSE_CANCEL, /* dialog_response */
1120 NULL, /* final_current_folder */
1121 FOLDER_NAME /* final_filename */
1124 /* SELECT_FOLDER tests with dialog, cancelled via closing the dialog (not selecting the Cancel button) */
1127 "select-folder-dialog-close-1",
1128 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1129 NULL, /* initial_current_folder */
1130 NULL, /* initial_filename */
1131 TRUE, /* open_dialog */
1132 NULL, /* tweak_current_folder */
1133 NULL, /* tweak_filename */
1134 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1135 NULL, /* final_current_folder */
1136 NULL /* final_filename */
1139 "select-folder-dialog-close-2",
1140 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1141 NULL, /* initial_current_folder */
1142 FOLDER_NAME, /* initial_filename */
1143 TRUE, /* open_dialog */
1144 NULL, /* tweak_current_folder */
1145 NULL, /* tweak_filename */
1146 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1147 NULL, /* final_current_folder */
1148 FOLDER_NAME /* final_filename */
1151 "select-folder-dialog-close-3",
1152 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1153 FOLDER_NAME, /* initial_current_folder */
1154 NULL, /* initial_filename */
1155 TRUE, /* open_dialog */
1156 NULL, /* tweak_current_folder */
1157 NULL, /* tweak_filename */
1158 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1159 FOLDER_NAME, /* final_current_folder */
1160 NULL /* final_filename */
1163 "select-folder-dialog-close-4",
1164 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1165 FOLDER_NAME, /* initial_current_folder */
1166 NULL, /* initial_filename */
1167 TRUE, /* open_dialog */
1168 NULL, /* tweak_current_folder */
1169 NULL, /* tweak_filename */
1170 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1171 NULL, /* final_current_folder */
1172 FOLDER_NAME /* final_filename */
1175 "select-folder-dialog-close-5",
1176 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1177 NULL, /* initial_current_folder */
1178 NULL, /* initial_filename */
1179 TRUE, /* open_dialog */
1180 NULL, /* tweak_current_folder */
1181 FOLDER_NAME, /* tweak_filename */
1182 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1183 NULL, /* final_current_folder */
1184 NULL /* final_filename */
1187 "select-folder-dialog-close-6",
1188 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1189 NULL, /* initial_current_folder */
1190 FOLDER_NAME, /* initial_filename */
1191 TRUE, /* open_dialog */
1192 NULL, /* tweak_current_folder */
1193 FOLDER_NAME_2, /* tweak_filename */
1194 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1195 NULL, /* final_current_folder */
1196 FOLDER_NAME /* final_filename */
1199 "select-folder-dialog-close-7",
1200 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1201 FOLDER_NAME, /* initial_current_folder */
1202 NULL, /* initial_filename */
1203 TRUE, /* open_dialog */
1204 NULL, /* tweak_current_folder */
1205 FOLDER_NAME_2, /* tweak_filename */
1206 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1207 FOLDER_NAME, /* final_current_folder */
1208 NULL /* final_filename */
1211 "select-folder-dialog-close-8",
1212 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1213 FOLDER_NAME, /* initial_current_folder */
1214 NULL, /* initial_filename */
1215 TRUE, /* open_dialog */
1216 NULL, /* tweak_current_folder */
1217 FOLDER_NAME_2, /* tweak_filename */
1218 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1219 NULL, /* final_current_folder */
1220 FOLDER_NAME /* final_filename */
1223 /* OPEN tests with dialog */
1227 GTK_FILE_CHOOSER_ACTION_OPEN,
1228 NULL, /* initial_current_folder */
1229 NULL, /* initial_filename */
1230 TRUE, /* open_dialog */
1231 NULL, /* tweak_current_folder */
1232 FILE_NAME, /* tweak_filename */
1233 GTK_RESPONSE_ACCEPT, /* dialog_response */
1234 NULL, /* final_current_folder */
1235 FILE_NAME /* final_filename */
1239 GTK_FILE_CHOOSER_ACTION_OPEN,
1240 NULL, /* initial_current_folder */
1241 FILE_NAME, /* initial_filename */
1242 TRUE, /* open_dialog */
1243 NULL, /* tweak_current_folder */
1244 NULL, /* tweak_filename */
1245 GTK_RESPONSE_ACCEPT, /* dialog_response */
1246 NULL, /* final_current_folder */
1247 FILE_NAME /* final_filename */
1251 GTK_FILE_CHOOSER_ACTION_OPEN,
1252 NULL, /* initial_current_folder */
1253 FILE_NAME, /* initial_filename */
1254 TRUE, /* open_dialog */
1255 NULL, /* tweak_current_folder */
1256 FILE_NAME_2, /* tweak_filename */
1257 GTK_RESPONSE_ACCEPT, /* dialog_response */
1258 NULL, /* final_current_folder */
1259 FILE_NAME_2 /* final_filename */
1263 GTK_FILE_CHOOSER_ACTION_OPEN,
1264 FOLDER_NAME, /* initial_current_folder */
1265 NULL, /* initial_filename */
1266 TRUE, /* open_dialog */
1267 NULL, /* tweak_current_folder */
1268 FILE_NAME, /* tweak_filename */
1269 GTK_RESPONSE_ACCEPT, /* dialog_response */
1270 NULL, /* final_current_folder */
1271 FILE_NAME /* final_filename */
1274 /* SELECT_FOLDER tests with dialog */
1277 "select-folder-dialog-1",
1278 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1279 NULL, /* initial_current_folder */
1280 FOLDER_NAME, /* initial_filename */
1281 TRUE, /* open_dialog */
1282 NULL, /* tweak_current_folder */
1283 NULL, /* tweak_filename */
1284 GTK_RESPONSE_ACCEPT, /* dialog_response */
1285 NULL, /* final_current_folder */
1286 FOLDER_NAME /* final_filename */
1289 "select-folder-dialog-2",
1290 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1291 FOLDER_NAME, /* initial_current_folder */
1292 NULL, /* initial_filename */
1293 TRUE, /* open_dialog */
1294 NULL, /* tweak_current_folder */
1295 NULL, /* tweak_filename */
1296 GTK_RESPONSE_ACCEPT, /* dialog_response */
1297 NULL, /* final_current_folder */
1298 FOLDER_NAME /* final_filename */
1301 "select-folder-dialog-3",
1302 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1303 NULL, /* initial_current_folder */
1304 FOLDER_NAME, /* initial_filename */
1305 TRUE, /* open_dialog */
1306 NULL, /* tweak_current_folder */
1307 FOLDER_NAME_2, /* tweak_filename */
1308 GTK_RESPONSE_ACCEPT, /* dialog_response */
1309 NULL, /* final_current_folder */
1310 FOLDER_NAME_2 /* final_filename */
1313 "select-folder-dialog-4",
1314 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1315 FOLDER_NAME, /* initial_current_folder */
1316 NULL, /* initial_filename */
1317 TRUE, /* open_dialog */
1318 NULL, /* tweak_current_folder */
1319 FOLDER_NAME_2, /* tweak_filename */
1320 GTK_RESPONSE_ACCEPT, /* dialog_response */
1321 NULL, /* final_current_folder */
1322 FOLDER_NAME_2 /* final_filename */
1328 setup_file_chooser_button_tests (void)
1332 for (i = 0; i < G_N_ELEMENTS (button_tests); i++)
1336 test_name = make_button_test_name (&button_tests[i]);
1337 g_test_add_data_func (test_name, &button_tests[i], test_file_chooser_button);
1341 setup_file_chooser_button_combo_box_tests ();
1345 struct confirm_overwrite_closure {
1347 GtkWidget *accept_button;
1348 gint confirm_overwrite_signal_emitted;
1352 static GtkFileChooserConfirmation
1353 confirm_overwrite_cb (GtkFileChooser *chooser, gpointer data)
1355 struct confirm_overwrite_closure *closure = data;
1357 if (g_test_verbose())
1358 printf ("bling!\n");
1359 closure->confirm_overwrite_signal_emitted += 1;
1361 return GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
1365 overwrite_response_cb (GtkFileChooser *chooser, gint response, gpointer data)
1367 struct confirm_overwrite_closure *closure = data;
1370 if (g_test_verbose())
1371 printf ("plong!\n");
1373 if (response != GTK_RESPONSE_ACCEPT)
1376 filename = gtk_file_chooser_get_filename (chooser);
1378 if (!g_str_has_suffix (filename, closure->extension))
1382 basename = g_path_get_basename (filename);
1385 filename = g_strconcat (basename, closure->extension, NULL);
1386 gtk_file_chooser_set_current_name (chooser, filename);
1388 g_signal_stop_emission_by_name (chooser, "response");
1389 gtk_dialog_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
1394 confirm_overwrite_timeout_cb (gpointer data)
1396 struct confirm_overwrite_closure *closure;
1399 gtk_button_clicked (GTK_BUTTON (closure->accept_button));
1404 /* http://bugzilla.gnome.org/show_bug.cgi?id=347883 */
1406 test_confirm_overwrite_for_path (const char *path, gboolean append_extension)
1409 struct confirm_overwrite_closure closure;
1414 closure.extension = NULL;
1415 closure.confirm_overwrite_signal_emitted = 0;
1416 closure.chooser = gtk_file_chooser_dialog_new ("hello", NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1417 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1419 closure.accept_button = gtk_dialog_add_button (GTK_DIALOG (closure.chooser),
1420 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT);
1421 gtk_dialog_set_default_response (GTK_DIALOG (closure.chooser), GTK_RESPONSE_ACCEPT);
1423 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (closure.chooser), TRUE);
1425 g_signal_connect (closure.chooser, "confirm-overwrite",
1426 G_CALLBACK (confirm_overwrite_cb), &closure);
1428 if (append_extension)
1432 filename = g_path_get_dirname (path);
1433 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (closure.chooser), filename);
1436 filename = g_path_get_basename (path);
1437 extension = strchr (filename, '.');
1441 closure.extension = g_strdup (extension);
1445 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (closure.chooser), filename);
1448 g_signal_connect (closure.chooser, "response",
1449 G_CALLBACK (overwrite_response_cb), &closure);
1453 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (closure.chooser), path);
1456 gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, confirm_overwrite_timeout_cb, &closure, NULL);
1457 gtk_dialog_run (GTK_DIALOG (closure.chooser));
1459 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (closure.chooser));
1460 passed = passed && filename && (strcmp (filename, path) == 0);
1463 gtk_widget_destroy (closure.chooser);
1465 passed = passed && (1 == closure.confirm_overwrite_signal_emitted);
1467 log_test (passed, "Confirm overwrite for %s", path);
1473 test_confirm_overwrite (void)
1475 gboolean passed = TRUE;
1477 /* first test for a file we know will always exist */
1478 passed = passed && test_confirm_overwrite_for_path ("/etc/passwd", FALSE);
1480 passed = passed && test_confirm_overwrite_for_path ("/etc/resolv.conf", TRUE);
1485 static const GtkFileChooserAction open_actions[] = {
1486 GTK_FILE_CHOOSER_ACTION_OPEN,
1487 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1490 static const GtkFileChooserAction save_actions[] = {
1491 GTK_FILE_CHOOSER_ACTION_SAVE,
1492 GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
1498 has_action (const GtkFileChooserAction *actions,
1500 GtkFileChooserAction sought_action)
1504 for (i = 0; i < n_actions; i++)
1505 if (actions[i] == sought_action)
1511 static GtkFileChooserDefault *
1512 get_impl_from_dialog (GtkWidget *dialog)
1514 GtkFileChooserDialog *d;
1515 GtkFileChooserDialogPrivate *dialog_priv;
1516 GtkFileChooserWidget *chooser_widget;
1517 GtkFileChooserWidgetPrivate *widget_priv;
1518 GtkFileChooserDefault *impl;
1520 d = GTK_FILE_CHOOSER_DIALOG (dialog);
1521 dialog_priv = d->priv;
1522 chooser_widget = GTK_FILE_CHOOSER_WIDGET (dialog_priv->widget);
1523 if (!chooser_widget)
1524 g_error ("BUG: dialog_priv->widget is not a GtkFileChooserWidget");
1526 widget_priv = chooser_widget->priv;
1527 impl = (GtkFileChooserDefault *) (widget_priv->impl);
1529 g_error ("BUG: widget_priv->impl is not a GtkFileChooserDefault");
1535 test_widgets_for_current_action (GtkFileChooserDialog *dialog,
1536 GtkFileChooserAction expected_action)
1538 GtkFileChooserDefault *impl;
1541 if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) != expected_action)
1544 impl = get_impl_from_dialog (GTK_WIDGET (dialog));
1546 g_assert (impl->action == expected_action);
1550 /* OPEN implies that the "new folder" button is hidden; otherwise it is shown */
1551 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
1552 passed = passed && !gtk_widget_get_visible (impl->browse_new_folder_button);
1554 passed = passed && gtk_widget_get_visible (impl->browse_new_folder_button);
1556 /* Check that the widgets are present/visible or not */
1557 if (has_action (open_actions, G_N_ELEMENTS (open_actions), impl->action))
1559 passed = passed && (impl->save_widgets == NULL
1560 && (impl->location_mode == LOCATION_MODE_PATH_BAR
1561 ? impl->location_entry == NULL
1562 : impl->location_entry != NULL)
1563 && impl->save_folder_label == NULL
1564 && impl->save_folder_combo == NULL
1565 && impl->save_expander == NULL
1566 && GTK_IS_CONTAINER (impl->browse_widgets) && gtk_widget_is_drawable (impl->browse_widgets));
1568 else if (has_action (save_actions, G_N_ELEMENTS (save_actions), impl->action))
1570 /* FIXME: we can't use GTK_IS_FILE_CHOOSER_ENTRY() because it uses
1571 * _gtk_file_chooser_entry_get_type(), which is a non-exported symbol.
1572 * So, we just test impl->location_entry for being non-NULL
1574 passed = passed && (GTK_IS_CONTAINER (impl->save_widgets) && gtk_widget_is_drawable (impl->save_widgets)
1575 && impl->location_entry != NULL && gtk_widget_is_drawable (impl->location_entry)
1576 && GTK_IS_LABEL (impl->save_folder_label) && gtk_widget_is_drawable (impl->save_folder_label)
1577 && GTK_IS_COMBO_BOX (impl->save_folder_combo) && gtk_widget_is_drawable (impl->save_folder_combo)
1578 && GTK_IS_EXPANDER (impl->save_expander) && gtk_widget_is_drawable (impl->save_expander)
1579 && GTK_IS_CONTAINER (impl->browse_widgets));
1581 /* FIXME: we are in a SAVE mode; test the visibility and sensitivity of
1582 * the children that change depending on the state of the expander.
1587 g_error ("BAD TEST: test_widgets_for_current_action() doesn't know about %s", get_action_name (impl->action));
1594 typedef gboolean (* ForeachActionCallback) (GtkFileChooserDialog *dialog,
1595 GtkFileChooserAction action,
1596 gpointer user_data);
1599 foreach_action (GtkFileChooserDialog *dialog,
1600 ForeachActionCallback callback,
1603 GEnumClass *enum_class;
1606 enum_class = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
1608 g_error ("BUG: get_action_name(): no GEnumClass for GTK_TYPE_FILE_CHOOSER_ACTION");
1610 for (i = 0; i < enum_class->n_values; i++)
1612 GEnumValue *enum_value;
1613 GtkFileChooserAction action;
1616 enum_value = enum_class->values + i;
1617 action = enum_value->value;
1619 passed = (* callback) (dialog, action, user_data);
1627 struct action_closure {
1628 GtkFileChooserAction from_action;
1632 switch_from_to_action_cb (GtkFileChooserDialog *dialog,
1633 GtkFileChooserAction action,
1636 struct action_closure *closure;
1639 closure = user_data;
1641 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), closure->from_action);
1643 passed = test_widgets_for_current_action (dialog, closure->from_action);
1644 log_test (passed, "switch_from_to_action_cb(): reset to action %s", get_action_name (closure->from_action));
1648 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), action);
1650 passed = test_widgets_for_current_action (dialog, action);
1651 log_test (passed, "switch_from_to_action_cb(): transition from %s to %s",
1652 get_action_name (closure->from_action),
1653 get_action_name (action));
1658 switch_from_action_cb (GtkFileChooserDialog *dialog,
1659 GtkFileChooserAction action,
1662 struct action_closure closure;
1664 closure.from_action = action;
1666 return foreach_action (dialog, switch_from_to_action_cb, &closure);
1670 test_action_widgets (void)
1673 GtkFileChooserAction action;
1676 dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1678 GTK_FILE_CHOOSER_ACTION_OPEN,
1680 GTK_RESPONSE_CANCEL,
1682 GTK_RESPONSE_ACCEPT,
1684 gtk_widget_show_now (dialog);
1686 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog));
1688 passed = test_widgets_for_current_action (GTK_FILE_CHOOSER_DIALOG (dialog), action);
1689 log_test (passed, "test_action_widgets(): widgets for initial action %s", get_action_name (action));
1692 passed = foreach_action (GTK_FILE_CHOOSER_DIALOG (dialog), switch_from_action_cb, NULL);
1693 log_test (passed, "test_action_widgets(): all transitions through property change");
1696 gtk_widget_destroy (dialog);
1702 test_reload_sequence (gboolean set_folder_before_map)
1705 GtkFileChooserDefault *impl;
1708 char *current_working_dir;
1712 current_working_dir = g_get_current_dir ();
1714 dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1716 GTK_FILE_CHOOSER_ACTION_OPEN,
1718 GTK_RESPONSE_CANCEL,
1720 GTK_RESPONSE_ACCEPT,
1722 impl = get_impl_from_dialog (dialog);
1724 if (set_folder_before_map)
1726 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1730 passed = passed && (impl->current_folder != NULL
1731 && impl->browse_files_model != NULL
1732 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1733 && impl->reload_state == RELOAD_HAS_FOLDER
1734 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1735 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1736 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1741 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1742 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1747 /* Initially, no folder is not loaded or pending */
1748 passed = passed && (impl->current_folder == NULL
1749 && impl->sort_model == NULL
1750 && impl->browse_files_model == NULL
1751 && impl->load_state == LOAD_EMPTY
1752 && impl->reload_state == RELOAD_EMPTY
1753 && impl->load_timeout_id == 0);
1757 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1758 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1761 log_test (passed, "test_reload_sequence(): initial status");
1763 /* After mapping, it is loading some folder, either the one that was explicitly set or the default one */
1765 gtk_widget_show_now (dialog);
1769 passed = passed && (impl->current_folder != NULL
1770 && impl->browse_files_model != NULL
1771 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1772 && impl->reload_state == RELOAD_HAS_FOLDER
1773 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1774 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1775 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1778 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1779 if (set_folder_before_map)
1780 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1782 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1786 log_test (passed, "test_reload_sequence(): status after map");
1788 /* Unmap it; we should still have a folder */
1790 gtk_widget_hide (dialog);
1794 passed = passed && (impl->current_folder != NULL
1795 && impl->browse_files_model != NULL
1796 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1797 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1798 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1799 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1802 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1803 if (set_folder_before_map)
1804 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1806 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1810 log_test (passed, "test_reload_sequence(): status after unmap");
1814 gtk_widget_show_now (dialog);
1818 passed = passed && (impl->current_folder != NULL
1819 && impl->browse_files_model != NULL
1820 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1821 && impl->reload_state == RELOAD_HAS_FOLDER
1822 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1823 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1824 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1827 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1828 if (set_folder_before_map)
1829 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1831 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1835 log_test (passed, "test_reload_sequence(): status after re-map");
1837 gtk_widget_destroy (dialog);
1838 g_free (current_working_dir);
1848 passed = test_reload_sequence (FALSE);
1849 log_test (passed, "test_reload(): create and use the default folder");
1852 passed = test_reload_sequence (TRUE);
1853 log_test (passed, "test_reload(): set a folder explicitly before mapping");
1858 test_button_folder_states_for_action (GtkFileChooserAction action, gboolean use_dialog, gboolean set_folder_on_dialog)
1865 char *current_working_dir;
1866 gboolean must_have_cwd;
1870 current_working_dir = g_get_current_dir ();
1871 must_have_cwd = !(use_dialog && set_folder_on_dialog);
1873 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1877 dialog = gtk_file_chooser_dialog_new ("Test", NULL, action,
1878 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1879 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1881 button = gtk_file_chooser_button_new_with_dialog (dialog);
1883 if (set_folder_on_dialog)
1884 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1888 button = gtk_file_chooser_button_new ("Test", action);
1889 dialog = NULL; /* keep gcc happy */
1892 gtk_container_add (GTK_CONTAINER (window), button);
1894 /* Pre-map; no folder is set */
1897 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1899 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1901 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1903 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, pre-map, %s",
1904 get_action_name (action),
1906 set_folder_on_dialog,
1907 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1909 /* Map; folder should be set */
1911 gtk_widget_show_all (window);
1912 gtk_widget_show_now (window);
1916 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1919 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1921 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1923 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, mapped, %s",
1924 get_action_name (action),
1926 set_folder_on_dialog,
1927 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1930 /* Unmap; folder should be set */
1932 gtk_widget_hide (window);
1934 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1937 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1939 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1941 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, unmapped, %s",
1942 get_action_name (action),
1944 set_folder_on_dialog,
1945 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1948 /* Re-map; folder should be set */
1950 gtk_widget_show_now (window);
1951 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1954 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1956 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1958 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, re-mapped, %s",
1959 get_action_name (action),
1961 set_folder_on_dialog,
1962 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1965 g_free (current_working_dir);
1967 gtk_widget_destroy (window);
1973 test_button_folder_states (void)
1975 /* GtkFileChooserButton only supports OPEN and SELECT_FOLDER */
1976 static const GtkFileChooserAction actions_to_test[] = {
1977 GTK_FILE_CHOOSER_ACTION_OPEN,
1978 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1985 for (i = 0; i < G_N_ELEMENTS (actions_to_test); i++)
1987 passed = passed && test_button_folder_states_for_action (actions_to_test[i], FALSE, FALSE);
1989 passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, FALSE);
1991 passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, TRUE);
1993 log_test (passed, "test_button_folder_states(): action %s", get_action_name (actions_to_test[i]));
1996 log_test (passed, "test_button_folder_states(): all supported actions");
2000 test_folder_switch_and_filters (void)
2006 GFile *base_dir_file;
2008 GtkFileFilter *all_filter;
2009 GtkFileFilter *txt_filter;
2010 GtkFileChooserDefault *impl;
2014 cwd = g_get_current_dir ();
2015 base_dir = g_build_filename (cwd, "file-chooser-test-dir", NULL);
2017 dialog = gtk_file_chooser_dialog_new ("Test", NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
2018 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2019 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2021 impl = get_impl_from_dialog (dialog);
2023 cwd_file = g_file_new_for_path (cwd);
2024 base_dir_file = g_file_new_for_path (base_dir);
2026 passed = passed && gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
2029 /* All files filter */
2031 all_filter = gtk_file_filter_new ();
2032 gtk_file_filter_set_name (all_filter, "All files");
2033 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), all_filter);
2037 txt_filter = gtk_file_filter_new ();
2038 gtk_file_filter_set_name (all_filter, "*.txt");
2039 gtk_file_filter_add_pattern (txt_filter, "*.txt");
2040 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
2042 /* Test filter set */
2044 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), all_filter);
2045 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == all_filter);
2048 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
2049 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
2050 log_test (passed, "test_folder_switch_and_filters(): set and get filter");
2053 gtk_widget_show (dialog);
2055 /* Test that filter is unchanged when we switch folders */
2057 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd);
2058 sleep_in_main_loop ();
2059 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
2062 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
2063 sleep_in_main_loop ();
2065 g_signal_emit_by_name (impl->browse_path_bar, "path-clicked",
2069 sleep_in_main_loop ();
2070 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
2071 log_test (passed, "test_folder_switch_and_filters(): filter after changing folder");
2077 g_object_unref (cwd_file);
2078 g_object_unref (base_dir_file);
2080 gtk_widget_destroy (dialog);
2082 log_test (passed, "test_folder_switch_and_filters(): all filter tests");
2090 /* initialize test program */
2091 gtk_test_init (&argc, &argv);
2093 /* Register tests */
2095 setup_file_chooser_button_tests ();
2097 setup_set_filename_tests ();
2098 setup_set_current_name_tests ();
2100 g_test_add_func ("/GtkFileChooser/confirm_overwrite", test_confirm_overwrite);
2101 g_test_add_func ("/GtkFileChooser/action_widgets", test_action_widgets);
2102 g_test_add_func ("/GtkFileChooser/reload", test_reload);
2103 g_test_add_func ("/GtkFileChooser/button_folder_states", test_button_folder_states);
2104 g_test_add_func ("/GtkFileChooser/folder_switch_and_filters", test_folder_switch_and_filters);
2107 /* run and check selected tests */
2108 return g_test_run();