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);
361 /* Copied from gtkfilechooserutils.c:_gtk_file_chooser_delegate_get_quark() */
363 delegate_get_quark (void)
365 static GQuark quark = 0;
367 if (G_UNLIKELY (quark == 0))
368 quark = g_quark_from_static_string ("gtk-file-chooser-delegate");
374 sleep_timeout_cb (gpointer data)
381 sleep_in_main_loop (void)
383 /* process all pending idles and events */
384 while (g_main_context_pending (NULL))
385 g_main_context_iteration (NULL, FALSE);
386 /* sleeping probably isn't strictly necessary here */
387 gdk_threads_add_timeout_full (G_MAXINT, 250, sleep_timeout_cb, NULL, NULL);
389 /* process any pending idles or events that arrived during sleep */
390 while (g_main_context_pending (NULL))
391 g_main_context_iteration (NULL, FALSE);
395 build_children_list (GtkWidget *widget, gpointer data)
400 *list = g_list_prepend (*list, widget);
404 find_child_widget_with_atk_role (GtkWidget *widget, AtkRole role)
406 AtkObject *accessible;
409 accessible = gtk_widget_get_accessible (widget);
410 a_role = atk_object_get_role (accessible);
416 GtkWidget *found_child;
420 if (GTK_IS_CONTAINER (widget))
426 gtk_container_forall (GTK_CONTAINER (widget), build_children_list, &children);
430 while (l && !found_child)
434 child = GTK_WIDGET (l->data);
436 found_child = find_child_widget_with_atk_role (child, role);
441 g_list_free (children);
449 get_atk_name_for_filechooser_button (GtkFileChooserButton *button)
451 GtkFileChooserAction action;
453 AtkObject *accessible;
455 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (button));
456 g_assert (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
458 if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
459 widget = find_child_widget_with_atk_role (GTK_WIDGET (button), ATK_ROLE_PUSH_BUTTON);
461 widget = find_child_widget_with_atk_role (GTK_WIDGET (button), ATK_ROLE_COMBO_BOX);
463 accessible = gtk_widget_get_accessible (widget);
464 return atk_object_get_name (accessible);
468 check_that_basename_is_shown (GtkFileChooserButton *button, const char *expected_filename)
470 GtkFileChooserAction action;
471 const char *name_on_button;
472 char *expected_basename;
474 name_on_button = get_atk_name_for_filechooser_button (button);
476 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (button));
477 g_assert (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
479 if (expected_filename)
480 expected_basename = g_path_get_basename (expected_filename);
482 expected_basename = NULL;
484 if (expected_basename)
485 g_assert_cmpstr (expected_basename, ==, name_on_button);
487 g_assert_cmpstr (name_on_button, ==, "(None)"); /* see gtkfilechooserbutton.c:FALLBACK_DISPLAY_NAME */ /* FIXME: how do we translate this? */
489 g_free (expected_basename);
493 get_expected_shown_filename (GtkFileChooserAction action, const char *folder_name, const char *filename)
495 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
507 test_file_chooser_button (gconstpointer data)
509 const FileChooserButtonTest *setup = data;
511 GtkWidget *fc_button;
512 GtkWidget *fc_dialog;
516 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
518 fc_button = gtk_file_chooser_button_new (setup->action == GTK_FILE_CHOOSER_ACTION_OPEN ? "Select a file" : "Select a folder",
520 gtk_container_add (GTK_CONTAINER (window), fc_button);
522 if (setup->initial_current_folder)
523 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->initial_current_folder);
525 if (setup->initial_filename)
526 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->initial_filename);
528 gtk_widget_show_all (window);
529 sleep_in_main_loop ();
531 check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button),
532 get_expected_shown_filename (setup->action, setup->initial_current_folder, setup->initial_filename));
534 /* If there is a dialog to be opened, we actually test going through it a
535 * couple of times. This ensures that any state that the button frobs for
536 * each appearance of the dialog will make sense.
538 if (setup->open_dialog)
543 for (i = 0; i < iterations; i++)
545 if (setup->open_dialog)
549 /* Hack our way into the file chooser button; get its GtkButton child and click it */
550 children = gtk_container_get_children (GTK_CONTAINER (fc_button));
551 g_assert (children && GTK_IS_BUTTON (children->data));
552 gtk_button_clicked (GTK_BUTTON (children->data));
553 g_list_free (children);
555 sleep_in_main_loop ();
557 /* Give me the internal dialog, damnit */
558 fc_dialog = g_object_get_qdata (G_OBJECT (fc_button), delegate_get_quark ());
559 g_assert (GTK_IS_FILE_CHOOSER (fc_dialog));
560 g_assert (GTK_IS_DIALOG (fc_dialog));
563 /* Okay, now frob the button and its optional dialog */
565 if (setup->tweak_current_folder)
566 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->tweak_current_folder);
568 if (setup->tweak_filename)
569 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->tweak_filename);
571 sleep_in_main_loop ();
573 if (setup->open_dialog)
575 gtk_dialog_response (GTK_DIALOG (fc_dialog), setup->dialog_response);
579 if (setup->final_current_folder)
581 char *folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (fc_button));
583 g_assert_cmpstr (folder, ==, setup->final_current_folder);
587 if (setup->final_filename)
589 char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fc_button));
591 g_assert_cmpstr (filename, ==, setup->final_filename);
595 check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button),
596 get_expected_shown_filename (setup->action, setup->final_current_folder, setup->final_filename));
599 gtk_widget_destroy (window);
602 static FileChooserButtonTest button_tests[] =
604 /* OPEN tests without dialog */
608 GTK_FILE_CHOOSER_ACTION_OPEN,
609 NULL, /* initial_current_folder */
610 NULL, /* initial_filename */
611 FALSE, /* open_dialog */
612 NULL, /* tweak_current_folder */
613 NULL, /* tweak_filename */
614 0, /* dialog_response */
615 NULL, /* final_current_folder */
616 NULL /* final_filename */
620 GTK_FILE_CHOOSER_ACTION_OPEN,
621 NULL, /* initial_current_folder */
622 FILE_NAME, /* initial_filename */
623 FALSE, /* open_dialog */
624 NULL, /* tweak_current_folder */
625 NULL, /* tweak_filename */
626 0, /* dialog_response */
627 NULL, /* final_current_folder */
628 FILE_NAME /* final_filename */
632 GTK_FILE_CHOOSER_ACTION_OPEN,
633 NULL, /* initial_current_folder */
634 NULL, /* initial_filename */
635 FALSE, /* open_dialog */
636 NULL, /* tweak_current_folder */
637 FILE_NAME, /* tweak_filename */
638 0, /* dialog_response */
639 NULL, /* final_current_folder */
640 FILE_NAME /* final_filename */
644 GTK_FILE_CHOOSER_ACTION_OPEN,
645 NULL, /* initial_current_folder */
646 FILE_NAME, /* initial_filename */
647 FALSE, /* open_dialog */
648 NULL, /* tweak_current_folder */
649 FILE_NAME_2, /* tweak_filename */
650 0, /* dialog_response */
651 NULL, /* final_current_folder */
652 FILE_NAME_2 /* final_filename */
656 GTK_FILE_CHOOSER_ACTION_OPEN,
657 FOLDER_NAME, /* initial_current_folder */
658 NULL, /* initial_filename */
659 FALSE, /* open_dialog */
660 NULL, /* tweak_current_folder */
661 NULL, /* tweak_filename */
662 0, /* dialog_response */
663 FOLDER_NAME, /* final_current_folder */
664 NULL /* final_filename */
668 GTK_FILE_CHOOSER_ACTION_OPEN,
669 FOLDER_NAME, /* initial_current_folder */
670 NULL, /* initial_filename */
671 FALSE, /* open_dialog */
672 FOLDER_NAME_2, /* tweak_current_folder */
673 NULL, /* tweak_filename */
674 0, /* dialog_response */
675 FOLDER_NAME_2, /* final_current_folder */
676 NULL /* final_filename */
679 /* SELECT_FOLDER tests without dialog */
683 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
684 NULL, /* initial_current_folder */
685 NULL, /* initial_filename */
686 FALSE, /* open_dialog */
687 NULL, /* tweak_current_folder */
688 NULL, /* tweak_filename */
689 0, /* dialog_response */
690 NULL, /* final_current_folder */
691 NULL /* final_filename */
695 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
696 NULL, /* initial_current_folder */
697 FOLDER_NAME, /* initial_filename */
698 FALSE, /* open_dialog */
699 NULL, /* tweak_current_folder */
700 NULL, /* tweak_filename */
701 0, /* dialog_response */
702 NULL, /* final_current_folder */
703 FOLDER_NAME /* final_filename */
707 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
708 NULL, /* initial_current_folder */
709 FOLDER_NAME, /* initial_filename */
710 FALSE, /* open_dialog */
711 NULL, /* tweak_current_folder */
712 FOLDER_NAME_2, /* tweak_filename */
713 0, /* dialog_response */
714 NULL, /* final_current_folder */
715 FOLDER_NAME_2 /* final_filename */
719 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
720 FOLDER_NAME, /* initial_current_folder */
721 NULL, /* initial_filename */
722 FALSE, /* open_dialog */
723 NULL, /* tweak_current_folder */
724 NULL, /* tweak_filename */
725 0, /* dialog_response */
726 NULL, /* final_current_folder */
727 FOLDER_NAME /* final_filename */
731 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
732 FOLDER_NAME, /* initial_current_folder */
733 NULL, /* initial_filename */
734 FALSE, /* open_dialog */
735 NULL, /* tweak_current_folder */
736 NULL, /* tweak_filename */
737 0, /* dialog_response */
738 FOLDER_NAME, /* final_current_folder */
739 NULL /* final_filename */
743 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
744 FOLDER_NAME, /* initial_current_folder */
745 NULL, /* initial_filename */
746 FALSE, /* open_dialog */
747 FOLDER_NAME_2, /* tweak_current_folder */
748 NULL, /* tweak_filename */
749 0, /* dialog_response */
750 NULL, /* final_current_folder */
751 FOLDER_NAME_2 /* final_filename */
755 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
756 FOLDER_NAME, /* initial_current_folder */
757 NULL, /* initial_filename */
758 FALSE, /* open_dialog */
759 FOLDER_NAME_2, /* tweak_current_folder */
760 NULL, /* tweak_filename */
761 0, /* dialog_response */
762 FOLDER_NAME_2, /* final_current_folder */
763 NULL /* final_filename */
767 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
768 FOLDER_NAME, /* initial_current_folder */
769 NULL, /* initial_filename */
770 FALSE, /* open_dialog */
771 NULL, /* tweak_current_folder */
772 FOLDER_NAME_2, /* tweak_filename */
773 0, /* dialog_response */
774 NULL, /* final_current_folder */
775 FOLDER_NAME_2 /* final_filename */
778 /* OPEN tests with dialog, cancelled */
781 "open-dialog-cancel-1",
782 GTK_FILE_CHOOSER_ACTION_OPEN,
783 NULL, /* initial_current_folder */
784 NULL, /* initial_filename */
785 TRUE, /* open_dialog */
786 NULL, /* tweak_current_folder */
787 NULL, /* tweak_filename */
788 GTK_RESPONSE_CANCEL, /* dialog_response */
789 NULL, /* final_current_folder */
790 NULL /* final_filename */
793 "open-dialog-cancel-2",
794 GTK_FILE_CHOOSER_ACTION_OPEN,
795 NULL, /* initial_current_folder */
796 FILE_NAME, /* initial_filename */
797 TRUE, /* open_dialog */
798 NULL, /* tweak_current_folder */
799 NULL, /* tweak_filename */
800 GTK_RESPONSE_CANCEL, /* dialog_response */
801 NULL, /* final_current_folder */
802 FILE_NAME /* final_filename */
805 "open-dialog-cancel-3",
806 GTK_FILE_CHOOSER_ACTION_OPEN,
807 FOLDER_NAME, /* initial_current_folder */
808 NULL, /* initial_filename */
809 TRUE, /* open_dialog */
810 NULL, /* tweak_current_folder */
811 NULL, /* tweak_filename */
812 GTK_RESPONSE_CANCEL, /* dialog_response */
813 FOLDER_NAME, /* final_current_folder */
814 NULL /* final_filename */
817 "open-dialog-cancel-4",
818 GTK_FILE_CHOOSER_ACTION_OPEN,
819 NULL, /* initial_current_folder */
820 NULL, /* initial_filename */
821 TRUE, /* open_dialog */
822 NULL, /* tweak_current_folder */
823 FILE_NAME, /* tweak_filename */
824 GTK_RESPONSE_CANCEL, /* dialog_response */
825 NULL, /* final_current_folder */
826 NULL /* final_filename */
829 "open-dialog-cancel-5",
830 GTK_FILE_CHOOSER_ACTION_OPEN,
831 NULL, /* initial_current_folder */
832 FILE_NAME, /* initial_filename */
833 TRUE, /* open_dialog */
834 NULL, /* tweak_current_folder */
835 FILE_NAME_2, /* tweak_filename */
836 GTK_RESPONSE_CANCEL, /* dialog_response */
837 NULL, /* final_current_folder */
838 FILE_NAME /* final_filename */
841 "open-dialog-cancel-6",
842 GTK_FILE_CHOOSER_ACTION_OPEN,
843 FOLDER_NAME, /* initial_current_folder */
844 NULL, /* initial_filename */
845 TRUE, /* open_dialog */
846 NULL, /* tweak_current_folder */
847 FILE_NAME_2, /* tweak_filename */
848 GTK_RESPONSE_CANCEL, /* dialog_response */
849 FOLDER_NAME, /* final_current_folder */
850 NULL /* final_filename */
853 /* OPEN tests with dialog, cancelled via closing the dialog (not by selecting the Cancel button) */
856 "open-dialog-close-1",
857 GTK_FILE_CHOOSER_ACTION_OPEN,
858 NULL, /* initial_current_folder */
859 NULL, /* initial_filename */
860 TRUE, /* open_dialog */
861 NULL, /* tweak_current_folder */
862 NULL, /* tweak_filename */
863 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
864 NULL, /* final_current_folder */
865 NULL /* final_filename */
868 "open-dialog-close-2",
869 GTK_FILE_CHOOSER_ACTION_OPEN,
870 NULL, /* initial_current_folder */
871 FILE_NAME, /* initial_filename */
872 TRUE, /* open_dialog */
873 NULL, /* tweak_current_folder */
874 NULL, /* tweak_filename */
875 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
876 NULL, /* final_current_folder */
877 FILE_NAME /* final_filename */
880 "open-dialog-close-3",
881 GTK_FILE_CHOOSER_ACTION_OPEN,
882 FOLDER_NAME, /* initial_current_folder */
883 NULL, /* initial_filename */
884 TRUE, /* open_dialog */
885 NULL, /* tweak_current_folder */
886 NULL, /* tweak_filename */
887 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
888 FOLDER_NAME, /* final_current_folder */
889 NULL /* final_filename */
892 "open-dialog-close-4",
893 GTK_FILE_CHOOSER_ACTION_OPEN,
894 NULL, /* initial_current_folder */
895 NULL, /* initial_filename */
896 TRUE, /* open_dialog */
897 NULL, /* tweak_current_folder */
898 FILE_NAME, /* tweak_filename */
899 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
900 NULL, /* final_current_folder */
901 NULL /* final_filename */
904 "open-dialog-close-5",
905 GTK_FILE_CHOOSER_ACTION_OPEN,
906 NULL, /* initial_current_folder */
907 FILE_NAME, /* initial_filename */
908 TRUE, /* open_dialog */
909 NULL, /* tweak_current_folder */
910 FILE_NAME_2, /* tweak_filename */
911 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
912 NULL, /* final_current_folder */
913 FILE_NAME /* final_filename */
916 "open-dialog-close-6",
917 GTK_FILE_CHOOSER_ACTION_OPEN,
918 FOLDER_NAME, /* initial_current_folder */
919 NULL, /* initial_filename */
920 TRUE, /* open_dialog */
921 NULL, /* tweak_current_folder */
922 FILE_NAME_2, /* tweak_filename */
923 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
924 FOLDER_NAME, /* final_current_folder */
925 NULL /* final_filename */
928 /* SELECT_FOLDER tests with dialog, cancelled */
931 "select-folder-dialog-cancel-1",
932 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
933 NULL, /* initial_current_folder */
934 NULL, /* initial_filename */
935 TRUE, /* open_dialog */
936 NULL, /* tweak_current_folder */
937 NULL, /* tweak_filename */
938 GTK_RESPONSE_CANCEL, /* dialog_response */
939 NULL, /* final_current_folder */
940 NULL /* final_filename */
943 "select-folder-dialog-cancel-2",
944 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
945 NULL, /* initial_current_folder */
946 FOLDER_NAME, /* initial_filename */
947 TRUE, /* open_dialog */
948 NULL, /* tweak_current_folder */
949 NULL, /* tweak_filename */
950 GTK_RESPONSE_CANCEL, /* dialog_response */
951 NULL, /* final_current_folder */
952 FOLDER_NAME /* final_filename */
955 "select-folder-dialog-cancel-3",
956 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
957 FOLDER_NAME, /* initial_current_folder */
958 NULL, /* initial_filename */
959 TRUE, /* open_dialog */
960 NULL, /* tweak_current_folder */
961 NULL, /* tweak_filename */
962 GTK_RESPONSE_CANCEL, /* dialog_response */
963 FOLDER_NAME, /* final_current_folder */
964 NULL /* final_filename */
967 "select-folder-dialog-cancel-4",
968 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
969 FOLDER_NAME, /* initial_current_folder */
970 NULL, /* initial_filename */
971 TRUE, /* open_dialog */
972 NULL, /* tweak_current_folder */
973 NULL, /* tweak_filename */
974 GTK_RESPONSE_CANCEL, /* dialog_response */
975 NULL, /* final_current_folder */
976 FOLDER_NAME /* final_filename */
979 "select-folder-dialog-cancel-5",
980 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
981 NULL, /* initial_current_folder */
982 NULL, /* initial_filename */
983 TRUE, /* open_dialog */
984 NULL, /* tweak_current_folder */
985 FOLDER_NAME, /* tweak_filename */
986 GTK_RESPONSE_CANCEL, /* dialog_response */
987 NULL, /* final_current_folder */
988 NULL /* final_filename */
991 "select-folder-dialog-cancel-6",
992 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
993 NULL, /* initial_current_folder */
994 FOLDER_NAME, /* initial_filename */
995 TRUE, /* open_dialog */
996 NULL, /* tweak_current_folder */
997 FOLDER_NAME_2, /* tweak_filename */
998 GTK_RESPONSE_CANCEL, /* dialog_response */
999 NULL, /* final_current_folder */
1000 FOLDER_NAME /* final_filename */
1003 "select-folder-dialog-cancel-7",
1004 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1005 FOLDER_NAME, /* initial_current_folder */
1006 NULL, /* initial_filename */
1007 TRUE, /* open_dialog */
1008 NULL, /* tweak_current_folder */
1009 FOLDER_NAME_2, /* tweak_filename */
1010 GTK_RESPONSE_CANCEL, /* dialog_response */
1011 FOLDER_NAME, /* final_current_folder */
1012 NULL /* final_filename */
1015 "select-folder-dialog-cancel-8",
1016 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1017 FOLDER_NAME, /* initial_current_folder */
1018 NULL, /* initial_filename */
1019 TRUE, /* open_dialog */
1020 NULL, /* tweak_current_folder */
1021 FOLDER_NAME_2, /* tweak_filename */
1022 GTK_RESPONSE_CANCEL, /* dialog_response */
1023 NULL, /* final_current_folder */
1024 FOLDER_NAME /* final_filename */
1027 /* SELECT_FOLDER tests with dialog, cancelled via closing the dialog (not selecting the Cancel button) */
1030 "select-folder-dialog-close-1",
1031 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1032 NULL, /* initial_current_folder */
1033 NULL, /* initial_filename */
1034 TRUE, /* open_dialog */
1035 NULL, /* tweak_current_folder */
1036 NULL, /* tweak_filename */
1037 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1038 NULL, /* final_current_folder */
1039 NULL /* final_filename */
1042 "select-folder-dialog-close-2",
1043 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1044 NULL, /* initial_current_folder */
1045 FOLDER_NAME, /* initial_filename */
1046 TRUE, /* open_dialog */
1047 NULL, /* tweak_current_folder */
1048 NULL, /* tweak_filename */
1049 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1050 NULL, /* final_current_folder */
1051 FOLDER_NAME /* final_filename */
1054 "select-folder-dialog-close-3",
1055 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1056 FOLDER_NAME, /* initial_current_folder */
1057 NULL, /* initial_filename */
1058 TRUE, /* open_dialog */
1059 NULL, /* tweak_current_folder */
1060 NULL, /* tweak_filename */
1061 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1062 FOLDER_NAME, /* final_current_folder */
1063 NULL /* final_filename */
1066 "select-folder-dialog-close-4",
1067 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1068 FOLDER_NAME, /* initial_current_folder */
1069 NULL, /* initial_filename */
1070 TRUE, /* open_dialog */
1071 NULL, /* tweak_current_folder */
1072 NULL, /* tweak_filename */
1073 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1074 NULL, /* final_current_folder */
1075 FOLDER_NAME /* final_filename */
1078 "select-folder-dialog-close-5",
1079 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1080 NULL, /* initial_current_folder */
1081 NULL, /* initial_filename */
1082 TRUE, /* open_dialog */
1083 NULL, /* tweak_current_folder */
1084 FOLDER_NAME, /* tweak_filename */
1085 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1086 NULL, /* final_current_folder */
1087 NULL /* final_filename */
1090 "select-folder-dialog-close-6",
1091 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1092 NULL, /* initial_current_folder */
1093 FOLDER_NAME, /* initial_filename */
1094 TRUE, /* open_dialog */
1095 NULL, /* tweak_current_folder */
1096 FOLDER_NAME_2, /* tweak_filename */
1097 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1098 NULL, /* final_current_folder */
1099 FOLDER_NAME /* final_filename */
1102 "select-folder-dialog-close-7",
1103 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1104 FOLDER_NAME, /* initial_current_folder */
1105 NULL, /* initial_filename */
1106 TRUE, /* open_dialog */
1107 NULL, /* tweak_current_folder */
1108 FOLDER_NAME_2, /* tweak_filename */
1109 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1110 FOLDER_NAME, /* final_current_folder */
1111 NULL /* final_filename */
1114 "select-folder-dialog-close-8",
1115 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1116 FOLDER_NAME, /* initial_current_folder */
1117 NULL, /* initial_filename */
1118 TRUE, /* open_dialog */
1119 NULL, /* tweak_current_folder */
1120 FOLDER_NAME_2, /* tweak_filename */
1121 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1122 NULL, /* final_current_folder */
1123 FOLDER_NAME /* final_filename */
1126 /* OPEN tests with dialog */
1130 GTK_FILE_CHOOSER_ACTION_OPEN,
1131 NULL, /* initial_current_folder */
1132 NULL, /* initial_filename */
1133 TRUE, /* open_dialog */
1134 NULL, /* tweak_current_folder */
1135 FILE_NAME, /* tweak_filename */
1136 GTK_RESPONSE_ACCEPT, /* dialog_response */
1137 NULL, /* final_current_folder */
1138 FILE_NAME /* final_filename */
1142 GTK_FILE_CHOOSER_ACTION_OPEN,
1143 NULL, /* initial_current_folder */
1144 FILE_NAME, /* initial_filename */
1145 TRUE, /* open_dialog */
1146 NULL, /* tweak_current_folder */
1147 NULL, /* tweak_filename */
1148 GTK_RESPONSE_ACCEPT, /* dialog_response */
1149 NULL, /* final_current_folder */
1150 FILE_NAME /* final_filename */
1154 GTK_FILE_CHOOSER_ACTION_OPEN,
1155 NULL, /* initial_current_folder */
1156 FILE_NAME, /* initial_filename */
1157 TRUE, /* open_dialog */
1158 NULL, /* tweak_current_folder */
1159 FILE_NAME_2, /* tweak_filename */
1160 GTK_RESPONSE_ACCEPT, /* dialog_response */
1161 NULL, /* final_current_folder */
1162 FILE_NAME_2 /* final_filename */
1166 GTK_FILE_CHOOSER_ACTION_OPEN,
1167 FOLDER_NAME, /* initial_current_folder */
1168 NULL, /* initial_filename */
1169 TRUE, /* open_dialog */
1170 NULL, /* tweak_current_folder */
1171 FILE_NAME, /* tweak_filename */
1172 GTK_RESPONSE_ACCEPT, /* dialog_response */
1173 NULL, /* final_current_folder */
1174 FILE_NAME /* final_filename */
1177 /* SELECT_FOLDER tests with dialog */
1180 "select-folder-dialog-1",
1181 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1182 NULL, /* initial_current_folder */
1183 FOLDER_NAME, /* initial_filename */
1184 TRUE, /* open_dialog */
1185 NULL, /* tweak_current_folder */
1186 NULL, /* tweak_filename */
1187 GTK_RESPONSE_ACCEPT, /* dialog_response */
1188 NULL, /* final_current_folder */
1189 FOLDER_NAME /* final_filename */
1192 "select-folder-dialog-2",
1193 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1194 FOLDER_NAME, /* initial_current_folder */
1195 NULL, /* initial_filename */
1196 TRUE, /* open_dialog */
1197 NULL, /* tweak_current_folder */
1198 NULL, /* tweak_filename */
1199 GTK_RESPONSE_ACCEPT, /* dialog_response */
1200 NULL, /* final_current_folder */
1201 FOLDER_NAME /* final_filename */
1204 "select-folder-dialog-3",
1205 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1206 NULL, /* initial_current_folder */
1207 FOLDER_NAME, /* initial_filename */
1208 TRUE, /* open_dialog */
1209 NULL, /* tweak_current_folder */
1210 FOLDER_NAME_2, /* tweak_filename */
1211 GTK_RESPONSE_ACCEPT, /* dialog_response */
1212 NULL, /* final_current_folder */
1213 FOLDER_NAME_2 /* final_filename */
1216 "select-folder-dialog-4",
1217 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1218 FOLDER_NAME, /* initial_current_folder */
1219 NULL, /* initial_filename */
1220 TRUE, /* open_dialog */
1221 NULL, /* tweak_current_folder */
1222 FOLDER_NAME_2, /* tweak_filename */
1223 GTK_RESPONSE_ACCEPT, /* dialog_response */
1224 NULL, /* final_current_folder */
1225 FOLDER_NAME_2 /* final_filename */
1231 setup_file_chooser_button_tests (void)
1235 for (i = 0; i < G_N_ELEMENTS (button_tests); i++)
1239 test_name = make_button_test_name (&button_tests[i]);
1240 g_test_add_data_func (test_name, &button_tests[i], test_file_chooser_button);
1246 struct confirm_overwrite_closure {
1248 GtkWidget *accept_button;
1249 gint confirm_overwrite_signal_emitted;
1253 static GtkFileChooserConfirmation
1254 confirm_overwrite_cb (GtkFileChooser *chooser, gpointer data)
1256 struct confirm_overwrite_closure *closure = data;
1258 if (g_test_verbose())
1259 printf ("bling!\n");
1260 closure->confirm_overwrite_signal_emitted += 1;
1262 return GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
1266 overwrite_response_cb (GtkFileChooser *chooser, gint response, gpointer data)
1268 struct confirm_overwrite_closure *closure = data;
1271 if (g_test_verbose())
1272 printf ("plong!\n");
1274 if (response != GTK_RESPONSE_ACCEPT)
1277 filename = gtk_file_chooser_get_filename (chooser);
1279 if (!g_str_has_suffix (filename, closure->extension))
1283 basename = g_path_get_basename (filename);
1286 filename = g_strconcat (basename, closure->extension, NULL);
1287 gtk_file_chooser_set_current_name (chooser, filename);
1289 g_signal_stop_emission_by_name (chooser, "response");
1290 gtk_dialog_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
1295 confirm_overwrite_timeout_cb (gpointer data)
1297 struct confirm_overwrite_closure *closure;
1300 gtk_button_clicked (GTK_BUTTON (closure->accept_button));
1305 /* http://bugzilla.gnome.org/show_bug.cgi?id=347883 */
1307 test_confirm_overwrite_for_path (const char *path, gboolean append_extension)
1310 struct confirm_overwrite_closure closure;
1315 closure.extension = NULL;
1316 closure.confirm_overwrite_signal_emitted = 0;
1317 closure.chooser = gtk_file_chooser_dialog_new ("hello", NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1318 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1320 closure.accept_button = gtk_dialog_add_button (GTK_DIALOG (closure.chooser),
1321 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT);
1322 gtk_dialog_set_default_response (GTK_DIALOG (closure.chooser), GTK_RESPONSE_ACCEPT);
1324 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (closure.chooser), TRUE);
1326 g_signal_connect (closure.chooser, "confirm-overwrite",
1327 G_CALLBACK (confirm_overwrite_cb), &closure);
1329 if (append_extension)
1333 filename = g_path_get_dirname (path);
1334 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (closure.chooser), filename);
1337 filename = g_path_get_basename (path);
1338 extension = strchr (filename, '.');
1342 closure.extension = g_strdup (extension);
1346 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (closure.chooser), filename);
1349 g_signal_connect (closure.chooser, "response",
1350 G_CALLBACK (overwrite_response_cb), &closure);
1354 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (closure.chooser), path);
1357 gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, confirm_overwrite_timeout_cb, &closure, NULL);
1358 gtk_dialog_run (GTK_DIALOG (closure.chooser));
1360 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (closure.chooser));
1361 passed = passed && filename && (strcmp (filename, path) == 0);
1364 gtk_widget_destroy (closure.chooser);
1366 passed = passed && (1 == closure.confirm_overwrite_signal_emitted);
1368 log_test (passed, "Confirm overwrite for %s", path);
1374 test_confirm_overwrite (void)
1376 gboolean passed = TRUE;
1378 /* first test for a file we know will always exist */
1379 passed = passed && test_confirm_overwrite_for_path ("/etc/passwd", FALSE);
1381 passed = passed && test_confirm_overwrite_for_path ("/etc/resolv.conf", TRUE);
1386 static const GtkFileChooserAction open_actions[] = {
1387 GTK_FILE_CHOOSER_ACTION_OPEN,
1388 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1391 static const GtkFileChooserAction save_actions[] = {
1392 GTK_FILE_CHOOSER_ACTION_SAVE,
1393 GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
1399 has_action (const GtkFileChooserAction *actions,
1401 GtkFileChooserAction sought_action)
1405 for (i = 0; i < n_actions; i++)
1406 if (actions[i] == sought_action)
1412 static GtkFileChooserDefault *
1413 get_impl_from_dialog (GtkWidget *dialog)
1415 GtkFileChooserDialog *d;
1416 GtkFileChooserDialogPrivate *dialog_priv;
1417 GtkFileChooserWidget *chooser_widget;
1418 GtkFileChooserWidgetPrivate *widget_priv;
1419 GtkFileChooserDefault *impl;
1421 d = GTK_FILE_CHOOSER_DIALOG (dialog);
1422 dialog_priv = d->priv;
1423 chooser_widget = GTK_FILE_CHOOSER_WIDGET (dialog_priv->widget);
1424 if (!chooser_widget)
1425 g_error ("BUG: dialog_priv->widget is not a GtkFileChooserWidget");
1427 widget_priv = chooser_widget->priv;
1428 impl = (GtkFileChooserDefault *) (widget_priv->impl);
1430 g_error ("BUG: widget_priv->impl is not a GtkFileChooserDefault");
1436 test_widgets_for_current_action (GtkFileChooserDialog *dialog,
1437 GtkFileChooserAction expected_action)
1439 GtkFileChooserDefault *impl;
1442 if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) != expected_action)
1445 impl = get_impl_from_dialog (GTK_WIDGET (dialog));
1447 g_assert (impl->action == expected_action);
1451 /* OPEN implies that the "new folder" button is hidden; otherwise it is shown */
1452 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
1453 passed = passed && !gtk_widget_get_visible (impl->browse_new_folder_button);
1455 passed = passed && gtk_widget_get_visible (impl->browse_new_folder_button);
1457 /* Check that the widgets are present/visible or not */
1458 if (has_action (open_actions, G_N_ELEMENTS (open_actions), impl->action))
1460 passed = passed && (impl->save_widgets == NULL
1461 && (impl->location_mode == LOCATION_MODE_PATH_BAR
1462 ? impl->location_entry == NULL
1463 : impl->location_entry != NULL)
1464 && impl->save_folder_label == NULL
1465 && impl->save_folder_combo == NULL
1466 && impl->save_expander == NULL
1467 && GTK_IS_CONTAINER (impl->browse_widgets) && gtk_widget_is_drawable (impl->browse_widgets));
1469 else if (has_action (save_actions, G_N_ELEMENTS (save_actions), impl->action))
1471 /* FIXME: we can't use GTK_IS_FILE_CHOOSER_ENTRY() because it uses
1472 * _gtk_file_chooser_entry_get_type(), which is a non-exported symbol.
1473 * So, we just test impl->location_entry for being non-NULL
1475 passed = passed && (GTK_IS_CONTAINER (impl->save_widgets) && gtk_widget_is_drawable (impl->save_widgets)
1476 && impl->location_entry != NULL && gtk_widget_is_drawable (impl->location_entry)
1477 && GTK_IS_LABEL (impl->save_folder_label) && gtk_widget_is_drawable (impl->save_folder_label)
1478 && GTK_IS_COMBO_BOX (impl->save_folder_combo) && gtk_widget_is_drawable (impl->save_folder_combo)
1479 && GTK_IS_EXPANDER (impl->save_expander) && gtk_widget_is_drawable (impl->save_expander)
1480 && GTK_IS_CONTAINER (impl->browse_widgets));
1482 /* FIXME: we are in a SAVE mode; test the visibility and sensitivity of
1483 * the children that change depending on the state of the expander.
1488 g_error ("BAD TEST: test_widgets_for_current_action() doesn't know about %s", get_action_name (impl->action));
1495 typedef gboolean (* ForeachActionCallback) (GtkFileChooserDialog *dialog,
1496 GtkFileChooserAction action,
1497 gpointer user_data);
1500 foreach_action (GtkFileChooserDialog *dialog,
1501 ForeachActionCallback callback,
1504 GEnumClass *enum_class;
1507 enum_class = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
1509 g_error ("BUG: get_action_name(): no GEnumClass for GTK_TYPE_FILE_CHOOSER_ACTION");
1511 for (i = 0; i < enum_class->n_values; i++)
1513 GEnumValue *enum_value;
1514 GtkFileChooserAction action;
1517 enum_value = enum_class->values + i;
1518 action = enum_value->value;
1520 passed = (* callback) (dialog, action, user_data);
1528 struct action_closure {
1529 GtkFileChooserAction from_action;
1533 switch_from_to_action_cb (GtkFileChooserDialog *dialog,
1534 GtkFileChooserAction action,
1537 struct action_closure *closure;
1540 closure = user_data;
1542 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), closure->from_action);
1544 passed = test_widgets_for_current_action (dialog, closure->from_action);
1545 log_test (passed, "switch_from_to_action_cb(): reset to action %s", get_action_name (closure->from_action));
1549 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), action);
1551 passed = test_widgets_for_current_action (dialog, action);
1552 log_test (passed, "switch_from_to_action_cb(): transition from %s to %s",
1553 get_action_name (closure->from_action),
1554 get_action_name (action));
1559 switch_from_action_cb (GtkFileChooserDialog *dialog,
1560 GtkFileChooserAction action,
1563 struct action_closure closure;
1565 closure.from_action = action;
1567 return foreach_action (dialog, switch_from_to_action_cb, &closure);
1571 test_action_widgets (void)
1574 GtkFileChooserAction action;
1577 dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1579 GTK_FILE_CHOOSER_ACTION_OPEN,
1581 GTK_RESPONSE_CANCEL,
1583 GTK_RESPONSE_ACCEPT,
1585 gtk_widget_show_now (dialog);
1587 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog));
1589 passed = test_widgets_for_current_action (GTK_FILE_CHOOSER_DIALOG (dialog), action);
1590 log_test (passed, "test_action_widgets(): widgets for initial action %s", get_action_name (action));
1593 passed = foreach_action (GTK_FILE_CHOOSER_DIALOG (dialog), switch_from_action_cb, NULL);
1594 log_test (passed, "test_action_widgets(): all transitions through property change");
1597 gtk_widget_destroy (dialog);
1603 test_reload_sequence (gboolean set_folder_before_map)
1606 GtkFileChooserDefault *impl;
1609 char *current_working_dir;
1613 current_working_dir = g_get_current_dir ();
1615 dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1617 GTK_FILE_CHOOSER_ACTION_OPEN,
1619 GTK_RESPONSE_CANCEL,
1621 GTK_RESPONSE_ACCEPT,
1623 impl = get_impl_from_dialog (dialog);
1625 if (set_folder_before_map)
1627 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1631 passed = passed && (impl->current_folder != NULL
1632 && impl->browse_files_model != NULL
1633 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1634 && impl->reload_state == RELOAD_HAS_FOLDER
1635 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1636 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1637 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1642 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1643 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1648 /* Initially, no folder is not loaded or pending */
1649 passed = passed && (impl->current_folder == NULL
1650 && impl->sort_model == NULL
1651 && impl->browse_files_model == NULL
1652 && impl->load_state == LOAD_EMPTY
1653 && impl->reload_state == RELOAD_EMPTY
1654 && impl->load_timeout_id == 0);
1658 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1659 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1662 log_test (passed, "test_reload_sequence(): initial status");
1664 /* After mapping, it is loading some folder, either the one that was explicitly set or the default one */
1666 gtk_widget_show_now (dialog);
1670 passed = passed && (impl->current_folder != NULL
1671 && impl->browse_files_model != NULL
1672 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1673 && impl->reload_state == RELOAD_HAS_FOLDER
1674 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1675 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1676 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1679 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1680 if (set_folder_before_map)
1681 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1683 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1687 log_test (passed, "test_reload_sequence(): status after map");
1689 /* Unmap it; we should still have a folder */
1691 gtk_widget_hide (dialog);
1695 passed = passed && (impl->current_folder != NULL
1696 && impl->browse_files_model != NULL
1697 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1698 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1699 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1700 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1703 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1704 if (set_folder_before_map)
1705 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1707 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1711 log_test (passed, "test_reload_sequence(): status after unmap");
1715 gtk_widget_show_now (dialog);
1719 passed = passed && (impl->current_folder != NULL
1720 && impl->browse_files_model != NULL
1721 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1722 && impl->reload_state == RELOAD_HAS_FOLDER
1723 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1724 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1725 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1728 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1729 if (set_folder_before_map)
1730 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1732 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1736 log_test (passed, "test_reload_sequence(): status after re-map");
1738 gtk_widget_destroy (dialog);
1739 g_free (current_working_dir);
1749 passed = test_reload_sequence (FALSE);
1750 log_test (passed, "test_reload(): create and use the default folder");
1753 passed = test_reload_sequence (TRUE);
1754 log_test (passed, "test_reload(): set a folder explicitly before mapping");
1759 test_button_folder_states_for_action (GtkFileChooserAction action, gboolean use_dialog, gboolean set_folder_on_dialog)
1766 char *current_working_dir;
1767 gboolean must_have_cwd;
1771 current_working_dir = g_get_current_dir ();
1772 must_have_cwd = !(use_dialog && set_folder_on_dialog);
1774 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1778 dialog = gtk_file_chooser_dialog_new ("Test", NULL, action,
1779 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1780 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1782 button = gtk_file_chooser_button_new_with_dialog (dialog);
1784 if (set_folder_on_dialog)
1785 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1789 button = gtk_file_chooser_button_new ("Test", action);
1790 dialog = NULL; /* keep gcc happy */
1793 gtk_container_add (GTK_CONTAINER (window), button);
1795 /* Pre-map; no folder is set */
1798 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1800 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1802 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1804 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, pre-map, %s",
1805 get_action_name (action),
1807 set_folder_on_dialog,
1808 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1810 /* Map; folder should be set */
1812 gtk_widget_show_all (window);
1813 gtk_widget_show_now (window);
1817 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1820 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1822 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1824 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, mapped, %s",
1825 get_action_name (action),
1827 set_folder_on_dialog,
1828 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1831 /* Unmap; folder should be set */
1833 gtk_widget_hide (window);
1835 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1838 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1840 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1842 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, unmapped, %s",
1843 get_action_name (action),
1845 set_folder_on_dialog,
1846 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1849 /* Re-map; folder should be set */
1851 gtk_widget_show_now (window);
1852 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1855 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1857 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1859 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, re-mapped, %s",
1860 get_action_name (action),
1862 set_folder_on_dialog,
1863 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1866 g_free (current_working_dir);
1868 gtk_widget_destroy (window);
1874 test_button_folder_states (void)
1876 /* GtkFileChooserButton only supports OPEN and SELECT_FOLDER */
1877 static const GtkFileChooserAction actions_to_test[] = {
1878 GTK_FILE_CHOOSER_ACTION_OPEN,
1879 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1886 for (i = 0; i < G_N_ELEMENTS (actions_to_test); i++)
1888 passed = passed && test_button_folder_states_for_action (actions_to_test[i], FALSE, FALSE);
1890 passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, FALSE);
1892 passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, TRUE);
1894 log_test (passed, "test_button_folder_states(): action %s", get_action_name (actions_to_test[i]));
1897 log_test (passed, "test_button_folder_states(): all supported actions");
1901 test_folder_switch_and_filters (void)
1907 GFile *base_dir_file;
1909 GtkFileFilter *all_filter;
1910 GtkFileFilter *txt_filter;
1911 GtkFileChooserDefault *impl;
1915 cwd = g_get_current_dir ();
1916 base_dir = g_build_filename (cwd, "file-chooser-test-dir", NULL);
1918 dialog = gtk_file_chooser_dialog_new ("Test", NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
1919 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1920 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1922 impl = get_impl_from_dialog (dialog);
1924 cwd_file = g_file_new_for_path (cwd);
1925 base_dir_file = g_file_new_for_path (base_dir);
1927 passed = passed && gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
1930 /* All files filter */
1932 all_filter = gtk_file_filter_new ();
1933 gtk_file_filter_set_name (all_filter, "All files");
1934 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), all_filter);
1938 txt_filter = gtk_file_filter_new ();
1939 gtk_file_filter_set_name (all_filter, "*.txt");
1940 gtk_file_filter_add_pattern (txt_filter, "*.txt");
1941 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
1943 /* Test filter set */
1945 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), all_filter);
1946 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == all_filter);
1949 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
1950 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
1951 log_test (passed, "test_folder_switch_and_filters(): set and get filter");
1954 gtk_widget_show (dialog);
1956 /* Test that filter is unchanged when we switch folders */
1958 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd);
1959 sleep_in_main_loop ();
1960 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
1963 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
1964 sleep_in_main_loop ();
1966 g_signal_emit_by_name (impl->browse_path_bar, "path-clicked",
1970 sleep_in_main_loop ();
1971 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
1972 log_test (passed, "test_folder_switch_and_filters(): filter after changing folder");
1978 g_object_unref (cwd_file);
1979 g_object_unref (base_dir_file);
1981 gtk_widget_destroy (dialog);
1983 log_test (passed, "test_folder_switch_and_filters(): all filter tests");
1991 /* initialize test program */
1992 gtk_test_init (&argc, &argv);
1994 /* Register tests */
1996 setup_file_chooser_button_tests ();
1998 setup_set_filename_tests ();
1999 setup_set_current_name_tests ();
2001 g_test_add_func ("/GtkFileChooser/confirm_overwrite", test_confirm_overwrite);
2002 g_test_add_func ("/GtkFileChooser/action_widgets", test_action_widgets);
2003 g_test_add_func ("/GtkFileChooser/reload", test_reload);
2004 g_test_add_func ("/GtkFileChooser/button_folder_states", test_button_folder_states);
2005 g_test_add_func ("/GtkFileChooser/folder_switch_and_filters", test_folder_switch_and_filters);
2008 /* run and check selected tests */
2009 return g_test_run();