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 log_test (gboolean passed, const char *test_name, ...)
47 va_start (args, test_name);
48 str = g_strdup_vprintf (test_name, args);
52 g_printf ("%s: %s\n", passed ? "PASSED" : "FAILED", str);
56 static const char *get_action_name (GtkFileChooserAction action);
58 typedef void (* SetFilenameFn) (GtkFileChooser *chooser, gpointer data);
59 typedef void (* CompareFilenameFn) (GtkFileChooser *chooser, gpointer data);
61 struct test_set_filename_closure {
63 GtkWidget *accept_button;
64 gboolean focus_button;
68 set_filename_timeout_cb (gpointer data)
70 struct test_set_filename_closure *closure;
74 if (closure->focus_button)
75 gtk_widget_grab_focus (closure->accept_button);
77 gtk_button_clicked (GTK_BUTTON (closure->accept_button));
84 static guint wait_for_idle_id = 0;
87 wait_for_idle_idle (gpointer data)
91 return G_SOURCE_REMOVE;
97 wait_for_idle_id = g_idle_add_full (G_PRIORITY_LOW + 100,
101 while (wait_for_idle_id)
102 gtk_main_iteration ();
107 test_set_filename (GtkFileChooserAction action,
108 gboolean focus_button,
109 SetFilenameFn set_filename_fn,const
110 CompareFilenameFn compare_filename_fn,
114 struct test_set_filename_closure closure;
116 chooser = gtk_file_chooser_dialog_new ("hello", NULL, action,
117 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
120 closure.chooser = chooser;
121 closure.accept_button = gtk_dialog_add_button (GTK_DIALOG (chooser), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
122 closure.focus_button = focus_button;
124 gtk_dialog_set_default_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
126 (* set_filename_fn) (GTK_FILE_CHOOSER (chooser), data);
128 gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, set_filename_timeout_cb, &closure, NULL);
129 gtk_dialog_run (GTK_DIALOG (chooser));
131 (* compare_filename_fn) (GTK_FILE_CHOOSER (chooser), data);
133 gtk_widget_destroy (chooser);
137 set_filename_cb (GtkFileChooser *chooser, gpointer data)
139 const char *filename;
142 gtk_file_chooser_set_filename (chooser, filename);
146 compare_filename_cb (GtkFileChooser *chooser, gpointer data)
148 const char *filename;
152 out_filename = gtk_file_chooser_get_filename (chooser);
154 g_assert_cmpstr (out_filename, ==, filename);
157 g_free (out_filename);
162 const char *test_name;
163 GtkFileChooserAction action;
164 const char *filename;
165 gboolean focus_button;
166 } TestSetFilenameSetup;
169 test_black_box_set_filename (gconstpointer data)
171 const TestSetFilenameSetup *setup = data;
173 test_set_filename (setup->action, setup->focus_button, set_filename_cb, compare_filename_cb, (char *) setup->filename);
176 struct current_name_closure {
178 const char *current_name;
182 set_current_name_cb (GtkFileChooser *chooser, gpointer data)
184 struct current_name_closure *closure;
188 gtk_file_chooser_set_current_folder (chooser, closure->path);
189 gtk_file_chooser_set_current_name (chooser, closure->current_name);
193 compare_current_name_cb (GtkFileChooser *chooser, gpointer data)
195 struct current_name_closure *closure;
201 out_filename = gtk_file_chooser_get_filename (chooser);
203 g_assert (out_filename != NULL);
205 filename = g_build_filename (closure->path, closure->current_name, NULL);
206 g_assert_cmpstr (filename, ==, out_filename);
209 g_free (out_filename);
214 const char *test_name;
215 GtkFileChooserAction action;
216 const char *current_name;
217 gboolean focus_button;
218 } TestSetCurrentNameSetup;
221 test_black_box_set_current_name (gconstpointer data)
223 const TestSetCurrentNameSetup *setup = data;
224 struct current_name_closure closure;
227 cwd = g_get_current_dir ();
230 closure.current_name = setup->current_name;
232 test_set_filename (setup->action, setup->focus_button, set_current_name_cb, compare_current_name_cb, &closure);
238 /* FIXME: fails in CREATE_FOLDER mode when FOLDER_NAME == "/" */
241 #define FILE_NAME "/nonexistent"
242 #define FILE_NAME_2 "/nonexistent2"
243 #define FOLDER_NAME "/etc"
244 #define FOLDER_NAME_2 "/usr"
246 #define FILE_NAME "/etc/passwd"
247 #define FILE_NAME_2 "/etc/group"
248 #define FOLDER_NAME "/etc"
249 #define FOLDER_NAME_2 "/usr"
252 #define CURRENT_NAME "parangaricutirimicuaro.txt"
253 #define CURRENT_NAME_FOLDER "parangaricutirimicuaro"
255 /* https://bugzilla.novell.com/show_bug.cgi?id=184875
256 * http://bugzilla.gnome.org/show_bug.cgi?id=347066
257 * http://bugzilla.gnome.org/show_bug.cgi?id=346058
262 setup_set_filename_tests (void)
264 static TestSetFilenameSetup tests[] =
266 { "/GtkFileChooser/black_box/set_filename/open/no_focus", GTK_FILE_CHOOSER_ACTION_OPEN, FILE_NAME, FALSE },
267 { "/GtkFileChooser/black_box/set_filename/open/focus", GTK_FILE_CHOOSER_ACTION_OPEN, FILE_NAME, TRUE },
268 { "/GtkFileChooser/black_box/set_filename/save/no_focus", GTK_FILE_CHOOSER_ACTION_SAVE, FILE_NAME, FALSE },
269 { "/GtkFileChooser/black_box/set_filename/save/focus", GTK_FILE_CHOOSER_ACTION_SAVE, FILE_NAME, TRUE },
270 { "/GtkFileChooser/black_box/set_filename/select_folder/no_focus", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, FOLDER_NAME,FALSE },
271 { "/GtkFileChooser/black_box/set_filename/select_folder/focus", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, FOLDER_NAME,TRUE },
272 { "/GtkFileChooser/black_box/set_filename/create_folder/no_focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, FOLDER_NAME,FALSE },
273 { "/GtkFileChooser/black_box/set_filename/create_folder/focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, FOLDER_NAME,TRUE },
277 for (i = 0; i < G_N_ELEMENTS (tests); i++)
278 g_test_add_data_func (tests[i].test_name, &tests[i], test_black_box_set_filename);
282 setup_set_current_name_tests (void)
284 static TestSetCurrentNameSetup tests[] =
286 { "/GtkFileChooser/black_box/set_current_name/save/no_focus", GTK_FILE_CHOOSER_ACTION_SAVE, CURRENT_NAME, FALSE },
287 { "/GtkFileChooser/black_box/set_current_name/save/focus", GTK_FILE_CHOOSER_ACTION_SAVE, CURRENT_NAME, TRUE },
288 { "/GtkFileChooser/black_box/set_current_name/create_folder/no_focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, CURRENT_NAME_FOLDER, FALSE },
289 { "/GtkFileChooser/black_box/set_current_name/create_folder/focus", GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, CURRENT_NAME_FOLDER, 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_current_name);
300 const char *shortname;
301 GtkFileChooserAction action;
302 const char *initial_current_folder;
303 const char *initial_filename;
304 gboolean open_dialog;
305 const char *tweak_current_folder;
306 const char *tweak_filename;
307 gint dialog_response;
308 const char *final_current_folder;
309 const char *final_filename;
310 } FileChooserButtonTest;
313 make_button_test_name (FileChooserButtonTest *t)
315 return g_strdup_printf ("/GtkFileChooserButton/%s", t->shortname);
317 GString *s = g_string_new ("/GtkFileChooserButton");
319 g_string_append_printf (s, "/%s/%s/%s/%s",
320 get_action_name (t->action),
321 t->initial_current_folder ? "set_initial_folder" : "no_default_folder",
322 t->initial_filename ? "set_initial_filename" : "no_initial_filename",
323 t->open_dialog ? "open_dialog" : "no_dialog");
325 if (t->tweak_current_folder)
326 g_string_append (s, "/tweak_current_folder");
328 if (t->tweak_filename)
329 g_string_append (s, "/tweak_filename");
332 g_string_append_printf (s, "/%s",
333 t->dialog_response == GTK_RESPONSE_ACCEPT ? "accept" : "cancel");
335 if (t->final_current_folder)
336 g_string_append (s, "/final_current_folder");
338 if (t->final_filename)
339 g_string_append (s, "/final_filename");
341 return g_string_free (s, FALSE);
345 /* Copied from gtkfilechooserutils.c:_gtk_file_chooser_delegate_get_quark() */
347 delegate_get_quark (void)
349 static GQuark quark = 0;
351 if (G_UNLIKELY (quark == 0))
352 quark = g_quark_from_static_string ("gtk-file-chooser-delegate");
358 sleep_timeout_cb (gpointer data)
365 sleep_in_main_loop (void)
367 /* process all pending idles and events */
368 while (g_main_context_pending (NULL))
369 g_main_context_iteration (NULL, FALSE);
370 /* sleeping probably isn't strictly necessary here */
371 gdk_threads_add_timeout_full (G_MAXINT, 250, sleep_timeout_cb, NULL, NULL);
373 /* process any pending idles or events that arrived during sleep */
374 while (g_main_context_pending (NULL))
375 g_main_context_iteration (NULL, FALSE);
379 test_file_chooser_button (gconstpointer data)
381 const FileChooserButtonTest *setup = data;
383 GtkWidget *fc_button;
384 GtkWidget *fc_dialog;
388 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
390 fc_button = gtk_file_chooser_button_new (setup->action == GTK_FILE_CHOOSER_ACTION_OPEN ? "Select a file" : "Select a folder",
392 gtk_container_add (GTK_CONTAINER (window), fc_button);
394 if (setup->initial_current_folder)
395 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->initial_current_folder);
397 if (setup->initial_filename)
398 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->initial_filename);
400 gtk_widget_show_all (window);
403 /* If there is a dialog to be opened, we actually test going through it a
404 * couple of times. This ensures that any state that the button frobs for
405 * each appearance of the dialog will make sense.
407 if (setup->open_dialog)
412 for (i = 0; i < iterations; i++)
414 if (setup->open_dialog)
418 /* Hack our way into the file chooser button; get its GtkButton child and click it */
419 children = gtk_container_get_children (GTK_CONTAINER (fc_button));
420 g_assert (children && GTK_IS_BUTTON (children->data));
421 gtk_button_clicked (GTK_BUTTON (children->data));
422 g_list_free (children);
424 sleep_in_main_loop ();
426 /* Give me the internal dialog, damnit */
427 fc_dialog = g_object_get_qdata (G_OBJECT (fc_button), delegate_get_quark ());
428 g_assert (GTK_IS_FILE_CHOOSER (fc_dialog));
429 g_assert (GTK_IS_DIALOG (fc_dialog));
432 /* Okay, now frob the button and its optional dialog */
434 if (setup->tweak_current_folder)
435 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->tweak_current_folder);
437 if (setup->tweak_filename)
438 gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->tweak_filename);
440 sleep_in_main_loop ();
442 if (setup->open_dialog)
444 gtk_dialog_response (GTK_DIALOG (fc_dialog), setup->dialog_response);
448 if (setup->final_current_folder)
450 char *folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (fc_button));
452 g_assert_cmpstr (folder, ==, setup->final_current_folder);
456 if (setup->final_filename)
458 char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fc_button));
460 g_assert_cmpstr (filename, ==, setup->final_filename);
465 gtk_widget_destroy (window);
468 static FileChooserButtonTest button_tests[] =
470 /* OPEN tests without dialog */
474 GTK_FILE_CHOOSER_ACTION_OPEN,
475 NULL, /* initial_current_folder */
476 NULL, /* initial_filename */
477 FALSE, /* open_dialog */
478 NULL, /* tweak_current_folder */
479 NULL, /* tweak_filename */
480 0, /* dialog_response */
481 NULL, /* final_current_folder */
482 NULL /* final_filename */
486 GTK_FILE_CHOOSER_ACTION_OPEN,
487 NULL, /* initial_current_folder */
488 FILE_NAME, /* initial_filename */
489 FALSE, /* open_dialog */
490 NULL, /* tweak_current_folder */
491 NULL, /* tweak_filename */
492 0, /* dialog_response */
493 NULL, /* final_current_folder */
494 FILE_NAME /* final_filename */
498 GTK_FILE_CHOOSER_ACTION_OPEN,
499 NULL, /* initial_current_folder */
500 NULL, /* initial_filename */
501 FALSE, /* open_dialog */
502 NULL, /* tweak_current_folder */
503 FILE_NAME, /* tweak_filename */
504 0, /* dialog_response */
505 NULL, /* final_current_folder */
506 FILE_NAME /* final_filename */
510 GTK_FILE_CHOOSER_ACTION_OPEN,
511 NULL, /* initial_current_folder */
512 FILE_NAME, /* initial_filename */
513 FALSE, /* open_dialog */
514 NULL, /* tweak_current_folder */
515 FILE_NAME_2, /* tweak_filename */
516 0, /* dialog_response */
517 NULL, /* final_current_folder */
518 FILE_NAME_2 /* final_filename */
522 GTK_FILE_CHOOSER_ACTION_OPEN,
523 FOLDER_NAME, /* initial_current_folder */
524 NULL, /* initial_filename */
525 FALSE, /* open_dialog */
526 NULL, /* tweak_current_folder */
527 NULL, /* tweak_filename */
528 0, /* dialog_response */
529 FOLDER_NAME, /* final_current_folder */
530 NULL /* final_filename */
534 GTK_FILE_CHOOSER_ACTION_OPEN,
535 FOLDER_NAME, /* initial_current_folder */
536 NULL, /* initial_filename */
537 FALSE, /* open_dialog */
538 FOLDER_NAME_2, /* tweak_current_folder */
539 NULL, /* tweak_filename */
540 0, /* dialog_response */
541 FOLDER_NAME_2, /* final_current_folder */
542 NULL /* final_filename */
545 /* SELECT_FOLDER tests without dialog */
549 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
550 NULL, /* initial_current_folder */
551 NULL, /* initial_filename */
552 FALSE, /* open_dialog */
553 NULL, /* tweak_current_folder */
554 NULL, /* tweak_filename */
555 0, /* dialog_response */
556 NULL, /* final_current_folder */
557 NULL /* final_filename */
561 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
562 NULL, /* initial_current_folder */
563 FOLDER_NAME, /* initial_filename */
564 FALSE, /* open_dialog */
565 NULL, /* tweak_current_folder */
566 NULL, /* tweak_filename */
567 0, /* dialog_response */
568 NULL, /* final_current_folder */
569 FOLDER_NAME /* final_filename */
573 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
574 NULL, /* initial_current_folder */
575 FOLDER_NAME, /* initial_filename */
576 FALSE, /* open_dialog */
577 NULL, /* tweak_current_folder */
578 FOLDER_NAME_2, /* tweak_filename */
579 0, /* dialog_response */
580 NULL, /* final_current_folder */
581 FOLDER_NAME_2 /* final_filename */
585 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
586 FOLDER_NAME, /* initial_current_folder */
587 NULL, /* initial_filename */
588 FALSE, /* open_dialog */
589 NULL, /* tweak_current_folder */
590 NULL, /* tweak_filename */
591 0, /* dialog_response */
592 NULL, /* final_current_folder */
593 FOLDER_NAME /* final_filename */
597 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
598 FOLDER_NAME, /* initial_current_folder */
599 NULL, /* initial_filename */
600 FALSE, /* open_dialog */
601 NULL, /* tweak_current_folder */
602 NULL, /* tweak_filename */
603 0, /* dialog_response */
604 FOLDER_NAME, /* final_current_folder */
605 NULL /* final_filename */
609 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
610 FOLDER_NAME, /* initial_current_folder */
611 NULL, /* initial_filename */
612 FALSE, /* open_dialog */
613 FOLDER_NAME_2, /* tweak_current_folder */
614 NULL, /* tweak_filename */
615 0, /* dialog_response */
616 NULL, /* final_current_folder */
617 FOLDER_NAME_2 /* final_filename */
621 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
622 FOLDER_NAME, /* initial_current_folder */
623 NULL, /* initial_filename */
624 FALSE, /* open_dialog */
625 FOLDER_NAME_2, /* tweak_current_folder */
626 NULL, /* tweak_filename */
627 0, /* dialog_response */
628 FOLDER_NAME_2, /* final_current_folder */
629 NULL /* final_filename */
633 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
634 FOLDER_NAME, /* initial_current_folder */
635 NULL, /* initial_filename */
636 FALSE, /* open_dialog */
637 NULL, /* tweak_current_folder */
638 FOLDER_NAME_2, /* tweak_filename */
639 0, /* dialog_response */
640 NULL, /* final_current_folder */
641 FOLDER_NAME_2 /* final_filename */
644 /* OPEN tests with dialog, cancelled */
647 "open-dialog-cancel-1",
648 GTK_FILE_CHOOSER_ACTION_OPEN,
649 NULL, /* initial_current_folder */
650 NULL, /* initial_filename */
651 TRUE, /* open_dialog */
652 NULL, /* tweak_current_folder */
653 NULL, /* tweak_filename */
654 GTK_RESPONSE_CANCEL, /* dialog_response */
655 NULL, /* final_current_folder */
656 NULL /* final_filename */
659 "open-dialog-cancel-2",
660 GTK_FILE_CHOOSER_ACTION_OPEN,
661 NULL, /* initial_current_folder */
662 FILE_NAME, /* initial_filename */
663 TRUE, /* open_dialog */
664 NULL, /* tweak_current_folder */
665 NULL, /* tweak_filename */
666 GTK_RESPONSE_CANCEL, /* dialog_response */
667 NULL, /* final_current_folder */
668 FILE_NAME /* final_filename */
671 "open-dialog-cancel-3",
672 GTK_FILE_CHOOSER_ACTION_OPEN,
673 FOLDER_NAME, /* initial_current_folder */
674 NULL, /* initial_filename */
675 TRUE, /* open_dialog */
676 NULL, /* tweak_current_folder */
677 NULL, /* tweak_filename */
678 GTK_RESPONSE_CANCEL, /* dialog_response */
679 FOLDER_NAME, /* final_current_folder */
680 NULL /* final_filename */
683 "open-dialog-cancel-4",
684 GTK_FILE_CHOOSER_ACTION_OPEN,
685 NULL, /* initial_current_folder */
686 NULL, /* initial_filename */
687 TRUE, /* open_dialog */
688 NULL, /* tweak_current_folder */
689 FILE_NAME, /* tweak_filename */
690 GTK_RESPONSE_CANCEL, /* dialog_response */
691 NULL, /* final_current_folder */
692 NULL /* final_filename */
695 "open-dialog-cancel-5",
696 GTK_FILE_CHOOSER_ACTION_OPEN,
697 NULL, /* initial_current_folder */
698 FILE_NAME, /* initial_filename */
699 TRUE, /* open_dialog */
700 NULL, /* tweak_current_folder */
701 FILE_NAME_2, /* tweak_filename */
702 GTK_RESPONSE_CANCEL, /* dialog_response */
703 NULL, /* final_current_folder */
704 FILE_NAME /* final_filename */
707 "open-dialog-cancel-6",
708 GTK_FILE_CHOOSER_ACTION_OPEN,
709 FOLDER_NAME, /* initial_current_folder */
710 NULL, /* initial_filename */
711 TRUE, /* open_dialog */
712 NULL, /* tweak_current_folder */
713 FILE_NAME_2, /* tweak_filename */
714 GTK_RESPONSE_CANCEL, /* dialog_response */
715 FOLDER_NAME, /* final_current_folder */
716 NULL /* final_filename */
719 /* OPEN tests with dialog, cancelled via closing the dialog (not by selecting the Cancel button) */
722 "open-dialog-close-1",
723 GTK_FILE_CHOOSER_ACTION_OPEN,
724 NULL, /* initial_current_folder */
725 NULL, /* initial_filename */
726 TRUE, /* open_dialog */
727 NULL, /* tweak_current_folder */
728 NULL, /* tweak_filename */
729 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
730 NULL, /* final_current_folder */
731 NULL /* final_filename */
734 "open-dialog-close-2",
735 GTK_FILE_CHOOSER_ACTION_OPEN,
736 NULL, /* initial_current_folder */
737 FILE_NAME, /* initial_filename */
738 TRUE, /* open_dialog */
739 NULL, /* tweak_current_folder */
740 NULL, /* tweak_filename */
741 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
742 NULL, /* final_current_folder */
743 FILE_NAME /* final_filename */
746 "open-dialog-close-3",
747 GTK_FILE_CHOOSER_ACTION_OPEN,
748 FOLDER_NAME, /* initial_current_folder */
749 NULL, /* initial_filename */
750 TRUE, /* open_dialog */
751 NULL, /* tweak_current_folder */
752 NULL, /* tweak_filename */
753 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
754 FOLDER_NAME, /* final_current_folder */
755 NULL /* final_filename */
758 "open-dialog-close-4",
759 GTK_FILE_CHOOSER_ACTION_OPEN,
760 NULL, /* initial_current_folder */
761 NULL, /* initial_filename */
762 TRUE, /* open_dialog */
763 NULL, /* tweak_current_folder */
764 FILE_NAME, /* tweak_filename */
765 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
766 NULL, /* final_current_folder */
767 NULL /* final_filename */
770 "open-dialog-close-5",
771 GTK_FILE_CHOOSER_ACTION_OPEN,
772 NULL, /* initial_current_folder */
773 FILE_NAME, /* initial_filename */
774 TRUE, /* open_dialog */
775 NULL, /* tweak_current_folder */
776 FILE_NAME_2, /* tweak_filename */
777 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
778 NULL, /* final_current_folder */
779 FILE_NAME /* final_filename */
782 "open-dialog-close-6",
783 GTK_FILE_CHOOSER_ACTION_OPEN,
784 FOLDER_NAME, /* initial_current_folder */
785 NULL, /* initial_filename */
786 TRUE, /* open_dialog */
787 NULL, /* tweak_current_folder */
788 FILE_NAME_2, /* tweak_filename */
789 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
790 FOLDER_NAME, /* final_current_folder */
791 NULL /* final_filename */
794 /* SELECT_FOLDER tests with dialog, cancelled */
797 "select-folder-dialog-cancel-1",
798 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
799 NULL, /* initial_current_folder */
800 NULL, /* initial_filename */
801 TRUE, /* open_dialog */
802 NULL, /* tweak_current_folder */
803 NULL, /* tweak_filename */
804 GTK_RESPONSE_CANCEL, /* dialog_response */
805 NULL, /* final_current_folder */
806 NULL /* final_filename */
809 "select-folder-dialog-cancel-2",
810 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
811 NULL, /* initial_current_folder */
812 FOLDER_NAME, /* initial_filename */
813 TRUE, /* open_dialog */
814 NULL, /* tweak_current_folder */
815 NULL, /* tweak_filename */
816 GTK_RESPONSE_CANCEL, /* dialog_response */
817 NULL, /* final_current_folder */
818 FOLDER_NAME /* final_filename */
821 "select-folder-dialog-cancel-3",
822 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
823 FOLDER_NAME, /* initial_current_folder */
824 NULL, /* initial_filename */
825 TRUE, /* open_dialog */
826 NULL, /* tweak_current_folder */
827 NULL, /* tweak_filename */
828 GTK_RESPONSE_CANCEL, /* dialog_response */
829 FOLDER_NAME, /* final_current_folder */
830 NULL /* final_filename */
833 "select-folder-dialog-cancel-4",
834 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
835 FOLDER_NAME, /* initial_current_folder */
836 NULL, /* initial_filename */
837 TRUE, /* open_dialog */
838 NULL, /* tweak_current_folder */
839 NULL, /* tweak_filename */
840 GTK_RESPONSE_CANCEL, /* dialog_response */
841 NULL, /* final_current_folder */
842 FOLDER_NAME /* final_filename */
845 "select-folder-dialog-cancel-5",
846 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
847 NULL, /* initial_current_folder */
848 NULL, /* initial_filename */
849 TRUE, /* open_dialog */
850 NULL, /* tweak_current_folder */
851 FOLDER_NAME, /* tweak_filename */
852 GTK_RESPONSE_CANCEL, /* dialog_response */
853 NULL, /* final_current_folder */
854 NULL /* final_filename */
857 "select-folder-dialog-cancel-6",
858 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
859 NULL, /* initial_current_folder */
860 FOLDER_NAME, /* initial_filename */
861 TRUE, /* open_dialog */
862 NULL, /* tweak_current_folder */
863 FOLDER_NAME_2, /* tweak_filename */
864 GTK_RESPONSE_CANCEL, /* dialog_response */
865 NULL, /* final_current_folder */
866 FOLDER_NAME /* final_filename */
869 "select-folder-dialog-cancel-7",
870 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
871 FOLDER_NAME, /* initial_current_folder */
872 NULL, /* initial_filename */
873 TRUE, /* open_dialog */
874 NULL, /* tweak_current_folder */
875 FOLDER_NAME_2, /* tweak_filename */
876 GTK_RESPONSE_CANCEL, /* dialog_response */
877 FOLDER_NAME, /* final_current_folder */
878 NULL /* final_filename */
881 "select-folder-dialog-cancel-8",
882 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
883 FOLDER_NAME, /* initial_current_folder */
884 NULL, /* initial_filename */
885 TRUE, /* open_dialog */
886 NULL, /* tweak_current_folder */
887 FOLDER_NAME_2, /* tweak_filename */
888 GTK_RESPONSE_CANCEL, /* dialog_response */
889 NULL, /* final_current_folder */
890 FOLDER_NAME /* final_filename */
893 /* SELECT_FOLDER tests with dialog, cancelled via closing the dialog (not selecting the Cancel button) */
896 "select-folder-dialog-close-1",
897 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
898 NULL, /* initial_current_folder */
899 NULL, /* initial_filename */
900 TRUE, /* open_dialog */
901 NULL, /* tweak_current_folder */
902 NULL, /* tweak_filename */
903 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
904 NULL, /* final_current_folder */
905 NULL /* final_filename */
908 "select-folder-dialog-close-2",
909 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
910 NULL, /* initial_current_folder */
911 FOLDER_NAME, /* initial_filename */
912 TRUE, /* open_dialog */
913 NULL, /* tweak_current_folder */
914 NULL, /* tweak_filename */
915 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
916 NULL, /* final_current_folder */
917 FOLDER_NAME /* final_filename */
920 "select-folder-dialog-close-3",
921 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
922 FOLDER_NAME, /* initial_current_folder */
923 NULL, /* initial_filename */
924 TRUE, /* open_dialog */
925 NULL, /* tweak_current_folder */
926 NULL, /* tweak_filename */
927 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
928 FOLDER_NAME, /* final_current_folder */
929 NULL /* final_filename */
932 "select-folder-dialog-close-4",
933 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
934 FOLDER_NAME, /* initial_current_folder */
935 NULL, /* initial_filename */
936 TRUE, /* open_dialog */
937 NULL, /* tweak_current_folder */
938 NULL, /* tweak_filename */
939 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
940 NULL, /* final_current_folder */
941 FOLDER_NAME /* final_filename */
944 "select-folder-dialog-close-5",
945 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
946 NULL, /* initial_current_folder */
947 NULL, /* initial_filename */
948 TRUE, /* open_dialog */
949 NULL, /* tweak_current_folder */
950 FOLDER_NAME, /* tweak_filename */
951 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
952 NULL, /* final_current_folder */
953 NULL /* final_filename */
956 "select-folder-dialog-close-6",
957 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
958 NULL, /* initial_current_folder */
959 FOLDER_NAME, /* initial_filename */
960 TRUE, /* open_dialog */
961 NULL, /* tweak_current_folder */
962 FOLDER_NAME_2, /* tweak_filename */
963 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
964 NULL, /* final_current_folder */
965 FOLDER_NAME /* final_filename */
968 "select-folder-dialog-close-7",
969 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
970 FOLDER_NAME, /* initial_current_folder */
971 NULL, /* initial_filename */
972 TRUE, /* open_dialog */
973 NULL, /* tweak_current_folder */
974 FOLDER_NAME_2, /* tweak_filename */
975 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
976 FOLDER_NAME, /* final_current_folder */
977 NULL /* final_filename */
980 "select-folder-dialog-close-8",
981 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
982 FOLDER_NAME, /* initial_current_folder */
983 NULL, /* initial_filename */
984 TRUE, /* open_dialog */
985 NULL, /* tweak_current_folder */
986 FOLDER_NAME_2, /* tweak_filename */
987 GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
988 NULL, /* final_current_folder */
989 FOLDER_NAME /* final_filename */
992 /* OPEN tests with dialog */
996 GTK_FILE_CHOOSER_ACTION_OPEN,
997 NULL, /* initial_current_folder */
998 NULL, /* initial_filename */
999 TRUE, /* open_dialog */
1000 NULL, /* tweak_current_folder */
1001 FILE_NAME, /* tweak_filename */
1002 GTK_RESPONSE_ACCEPT, /* dialog_response */
1003 NULL, /* final_current_folder */
1004 FILE_NAME /* final_filename */
1008 GTK_FILE_CHOOSER_ACTION_OPEN,
1009 NULL, /* initial_current_folder */
1010 FILE_NAME, /* initial_filename */
1011 TRUE, /* open_dialog */
1012 NULL, /* tweak_current_folder */
1013 NULL, /* tweak_filename */
1014 GTK_RESPONSE_ACCEPT, /* dialog_response */
1015 NULL, /* final_current_folder */
1016 FILE_NAME /* final_filename */
1020 GTK_FILE_CHOOSER_ACTION_OPEN,
1021 NULL, /* initial_current_folder */
1022 FILE_NAME, /* initial_filename */
1023 TRUE, /* open_dialog */
1024 NULL, /* tweak_current_folder */
1025 FILE_NAME_2, /* tweak_filename */
1026 GTK_RESPONSE_ACCEPT, /* dialog_response */
1027 NULL, /* final_current_folder */
1028 FILE_NAME_2 /* final_filename */
1032 GTK_FILE_CHOOSER_ACTION_OPEN,
1033 FOLDER_NAME, /* initial_current_folder */
1034 NULL, /* initial_filename */
1035 TRUE, /* open_dialog */
1036 NULL, /* tweak_current_folder */
1037 FILE_NAME, /* tweak_filename */
1038 GTK_RESPONSE_ACCEPT, /* dialog_response */
1039 NULL, /* final_current_folder */
1040 FILE_NAME /* final_filename */
1043 /* SELECT_FOLDER tests with dialog */
1046 "select-folder-dialog-1",
1047 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1048 NULL, /* initial_current_folder */
1049 FOLDER_NAME, /* initial_filename */
1050 TRUE, /* open_dialog */
1051 NULL, /* tweak_current_folder */
1052 NULL, /* tweak_filename */
1053 GTK_RESPONSE_ACCEPT, /* dialog_response */
1054 NULL, /* final_current_folder */
1055 FOLDER_NAME /* final_filename */
1058 "select-folder-dialog-2",
1059 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1060 FOLDER_NAME, /* initial_current_folder */
1061 NULL, /* initial_filename */
1062 TRUE, /* open_dialog */
1063 NULL, /* tweak_current_folder */
1064 NULL, /* tweak_filename */
1065 GTK_RESPONSE_ACCEPT, /* dialog_response */
1066 NULL, /* final_current_folder */
1067 FOLDER_NAME /* final_filename */
1070 "select-folder-dialog-3",
1071 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1072 NULL, /* initial_current_folder */
1073 FOLDER_NAME, /* initial_filename */
1074 TRUE, /* open_dialog */
1075 NULL, /* tweak_current_folder */
1076 FOLDER_NAME_2, /* tweak_filename */
1077 GTK_RESPONSE_ACCEPT, /* dialog_response */
1078 NULL, /* final_current_folder */
1079 FOLDER_NAME_2 /* final_filename */
1082 "select-folder-dialog-4",
1083 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1084 FOLDER_NAME, /* initial_current_folder */
1085 NULL, /* initial_filename */
1086 TRUE, /* open_dialog */
1087 NULL, /* tweak_current_folder */
1088 FOLDER_NAME_2, /* tweak_filename */
1089 GTK_RESPONSE_ACCEPT, /* dialog_response */
1090 NULL, /* final_current_folder */
1091 FOLDER_NAME_2 /* final_filename */
1097 setup_file_chooser_button_tests (void)
1101 for (i = 0; i < G_N_ELEMENTS (button_tests); i++)
1105 test_name = make_button_test_name (&button_tests[i]);
1106 g_test_add_data_func (test_name, &button_tests[i], test_file_chooser_button);
1112 struct confirm_overwrite_closure {
1114 GtkWidget *accept_button;
1115 gint confirm_overwrite_signal_emitted;
1119 static GtkFileChooserConfirmation
1120 confirm_overwrite_cb (GtkFileChooser *chooser, gpointer data)
1122 struct confirm_overwrite_closure *closure = data;
1124 if (g_test_verbose())
1125 printf ("bling!\n");
1126 closure->confirm_overwrite_signal_emitted += 1;
1128 return GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
1132 overwrite_response_cb (GtkFileChooser *chooser, gint response, gpointer data)
1134 struct confirm_overwrite_closure *closure = data;
1137 if (g_test_verbose())
1138 printf ("plong!\n");
1140 if (response != GTK_RESPONSE_ACCEPT)
1143 filename = gtk_file_chooser_get_filename (chooser);
1145 if (!g_str_has_suffix (filename, closure->extension))
1149 basename = g_path_get_basename (filename);
1152 filename = g_strconcat (basename, closure->extension, NULL);
1153 gtk_file_chooser_set_current_name (chooser, filename);
1155 g_signal_stop_emission_by_name (chooser, "response");
1156 gtk_dialog_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
1161 confirm_overwrite_timeout_cb (gpointer data)
1163 struct confirm_overwrite_closure *closure;
1166 gtk_button_clicked (GTK_BUTTON (closure->accept_button));
1171 /* http://bugzilla.gnome.org/show_bug.cgi?id=347883 */
1173 test_confirm_overwrite_for_path (const char *path, gboolean append_extension)
1176 struct confirm_overwrite_closure closure;
1181 closure.extension = NULL;
1182 closure.confirm_overwrite_signal_emitted = 0;
1183 closure.chooser = gtk_file_chooser_dialog_new ("hello", NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1184 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1186 closure.accept_button = gtk_dialog_add_button (GTK_DIALOG (closure.chooser),
1187 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT);
1188 gtk_dialog_set_default_response (GTK_DIALOG (closure.chooser), GTK_RESPONSE_ACCEPT);
1190 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (closure.chooser), TRUE);
1192 g_signal_connect (closure.chooser, "confirm-overwrite",
1193 G_CALLBACK (confirm_overwrite_cb), &closure);
1195 if (append_extension)
1199 filename = g_path_get_dirname (path);
1200 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (closure.chooser), filename);
1203 filename = g_path_get_basename (path);
1204 extension = strchr (filename, '.');
1208 closure.extension = g_strdup (extension);
1212 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (closure.chooser), filename);
1215 g_signal_connect (closure.chooser, "response",
1216 G_CALLBACK (overwrite_response_cb), &closure);
1220 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (closure.chooser), path);
1223 gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, confirm_overwrite_timeout_cb, &closure, NULL);
1224 gtk_dialog_run (GTK_DIALOG (closure.chooser));
1226 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (closure.chooser));
1227 passed = passed && filename && (strcmp (filename, path) == 0);
1230 gtk_widget_destroy (closure.chooser);
1232 passed = passed && (1 == closure.confirm_overwrite_signal_emitted);
1234 log_test (passed, "Confirm overwrite for %s", path);
1240 test_confirm_overwrite (void)
1242 gboolean passed = TRUE;
1244 /* first test for a file we know will always exist */
1245 passed = passed && test_confirm_overwrite_for_path ("/etc/passwd", FALSE);
1247 passed = passed && test_confirm_overwrite_for_path ("/etc/resolv.conf", TRUE);
1252 static const GtkFileChooserAction open_actions[] = {
1253 GTK_FILE_CHOOSER_ACTION_OPEN,
1254 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1257 static const GtkFileChooserAction save_actions[] = {
1258 GTK_FILE_CHOOSER_ACTION_SAVE,
1259 GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
1265 has_action (const GtkFileChooserAction *actions,
1267 GtkFileChooserAction sought_action)
1271 for (i = 0; i < n_actions; i++)
1272 if (actions[i] == sought_action)
1279 get_action_name (GtkFileChooserAction action)
1283 case GTK_FILE_CHOOSER_ACTION_OPEN: return "OPEN";
1284 case GTK_FILE_CHOOSER_ACTION_SAVE: return "SAVE";
1285 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER: return "SELECT_FOLDER";
1286 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER: return "CREATE_FOLDER";
1289 g_assert_not_reached ();
1294 static GtkFileChooserDefault *
1295 get_impl_from_dialog (GtkWidget *dialog)
1297 GtkFileChooserDialog *d;
1298 GtkFileChooserDialogPrivate *dialog_priv;
1299 GtkFileChooserWidget *chooser_widget;
1300 GtkFileChooserWidgetPrivate *widget_priv;
1301 GtkFileChooserDefault *impl;
1303 d = GTK_FILE_CHOOSER_DIALOG (dialog);
1304 dialog_priv = d->priv;
1305 chooser_widget = GTK_FILE_CHOOSER_WIDGET (dialog_priv->widget);
1306 if (!chooser_widget)
1307 g_error ("BUG: dialog_priv->widget is not a GtkFileChooserWidget");
1309 widget_priv = chooser_widget->priv;
1310 impl = (GtkFileChooserDefault *) (widget_priv->impl);
1312 g_error ("BUG: widget_priv->impl is not a GtkFileChooserDefault");
1318 test_widgets_for_current_action (GtkFileChooserDialog *dialog,
1319 GtkFileChooserAction expected_action)
1321 GtkFileChooserDefault *impl;
1324 if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) != expected_action)
1327 impl = get_impl_from_dialog (GTK_WIDGET (dialog));
1329 g_assert (impl->action == expected_action);
1333 /* OPEN implies that the "new folder" button is hidden; otherwise it is shown */
1334 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
1335 passed = passed && !gtk_widget_get_visible (impl->browse_new_folder_button);
1337 passed = passed && gtk_widget_get_visible (impl->browse_new_folder_button);
1339 /* Check that the widgets are present/visible or not */
1340 if (has_action (open_actions, G_N_ELEMENTS (open_actions), impl->action))
1342 passed = passed && (impl->save_widgets == NULL
1343 && (impl->location_mode == LOCATION_MODE_PATH_BAR
1344 ? impl->location_entry == NULL
1345 : impl->location_entry != NULL)
1346 && impl->save_folder_label == NULL
1347 && impl->save_folder_combo == NULL
1348 && impl->save_expander == NULL
1349 && GTK_IS_CONTAINER (impl->browse_widgets) && gtk_widget_is_drawable (impl->browse_widgets));
1351 else if (has_action (save_actions, G_N_ELEMENTS (save_actions), impl->action))
1353 /* FIXME: we can't use GTK_IS_FILE_CHOOSER_ENTRY() because it uses
1354 * _gtk_file_chooser_entry_get_type(), which is a non-exported symbol.
1355 * So, we just test impl->location_entry for being non-NULL
1357 passed = passed && (GTK_IS_CONTAINER (impl->save_widgets) && gtk_widget_is_drawable (impl->save_widgets)
1358 && impl->location_entry != NULL && gtk_widget_is_drawable (impl->location_entry)
1359 && GTK_IS_LABEL (impl->save_folder_label) && gtk_widget_is_drawable (impl->save_folder_label)
1360 && GTK_IS_COMBO_BOX (impl->save_folder_combo) && gtk_widget_is_drawable (impl->save_folder_combo)
1361 && GTK_IS_EXPANDER (impl->save_expander) && gtk_widget_is_drawable (impl->save_expander)
1362 && GTK_IS_CONTAINER (impl->browse_widgets));
1364 /* FIXME: we are in a SAVE mode; test the visibility and sensitivity of
1365 * the children that change depending on the state of the expander.
1370 g_error ("BAD TEST: test_widgets_for_current_action() doesn't know about %s", get_action_name (impl->action));
1377 typedef gboolean (* ForeachActionCallback) (GtkFileChooserDialog *dialog,
1378 GtkFileChooserAction action,
1379 gpointer user_data);
1382 foreach_action (GtkFileChooserDialog *dialog,
1383 ForeachActionCallback callback,
1386 GEnumClass *enum_class;
1389 enum_class = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
1391 g_error ("BUG: get_action_name(): no GEnumClass for GTK_TYPE_FILE_CHOOSER_ACTION");
1393 for (i = 0; i < enum_class->n_values; i++)
1395 GEnumValue *enum_value;
1396 GtkFileChooserAction action;
1399 enum_value = enum_class->values + i;
1400 action = enum_value->value;
1402 passed = (* callback) (dialog, action, user_data);
1410 struct action_closure {
1411 GtkFileChooserAction from_action;
1415 switch_from_to_action_cb (GtkFileChooserDialog *dialog,
1416 GtkFileChooserAction action,
1419 struct action_closure *closure;
1422 closure = user_data;
1424 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), closure->from_action);
1426 passed = test_widgets_for_current_action (dialog, closure->from_action);
1427 log_test (passed, "switch_from_to_action_cb(): reset to action %s", get_action_name (closure->from_action));
1431 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), action);
1433 passed = test_widgets_for_current_action (dialog, action);
1434 log_test (passed, "switch_from_to_action_cb(): transition from %s to %s",
1435 get_action_name (closure->from_action),
1436 get_action_name (action));
1441 switch_from_action_cb (GtkFileChooserDialog *dialog,
1442 GtkFileChooserAction action,
1445 struct action_closure closure;
1447 closure.from_action = action;
1449 return foreach_action (dialog, switch_from_to_action_cb, &closure);
1453 test_action_widgets (void)
1456 GtkFileChooserAction action;
1459 dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1461 GTK_FILE_CHOOSER_ACTION_OPEN,
1463 GTK_RESPONSE_CANCEL,
1465 GTK_RESPONSE_ACCEPT,
1467 gtk_widget_show_now (dialog);
1469 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog));
1471 passed = test_widgets_for_current_action (GTK_FILE_CHOOSER_DIALOG (dialog), action);
1472 log_test (passed, "test_action_widgets(): widgets for initial action %s", get_action_name (action));
1475 passed = foreach_action (GTK_FILE_CHOOSER_DIALOG (dialog), switch_from_action_cb, NULL);
1476 log_test (passed, "test_action_widgets(): all transitions through property change");
1479 gtk_widget_destroy (dialog);
1485 test_reload_sequence (gboolean set_folder_before_map)
1488 GtkFileChooserDefault *impl;
1491 char *current_working_dir;
1495 current_working_dir = g_get_current_dir ();
1497 dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1499 GTK_FILE_CHOOSER_ACTION_OPEN,
1501 GTK_RESPONSE_CANCEL,
1503 GTK_RESPONSE_ACCEPT,
1505 impl = get_impl_from_dialog (dialog);
1507 if (set_folder_before_map)
1509 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1513 passed = passed && (impl->current_folder != NULL
1514 && impl->browse_files_model != NULL
1515 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1516 && impl->reload_state == RELOAD_HAS_FOLDER
1517 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1518 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1519 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1524 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1525 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1530 /* Initially, no folder is not loaded or pending */
1531 passed = passed && (impl->current_folder == NULL
1532 && impl->sort_model == NULL
1533 && impl->browse_files_model == NULL
1534 && impl->load_state == LOAD_EMPTY
1535 && impl->reload_state == RELOAD_EMPTY
1536 && impl->load_timeout_id == 0);
1540 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1541 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1544 log_test (passed, "test_reload_sequence(): initial status");
1546 /* After mapping, it is loading some folder, either the one that was explicitly set or the default one */
1548 gtk_widget_show_now (dialog);
1552 passed = passed && (impl->current_folder != NULL
1553 && impl->browse_files_model != NULL
1554 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1555 && impl->reload_state == RELOAD_HAS_FOLDER
1556 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1557 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1558 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1561 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1562 if (set_folder_before_map)
1563 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1565 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1569 log_test (passed, "test_reload_sequence(): status after map");
1571 /* Unmap it; we should still have a folder */
1573 gtk_widget_hide (dialog);
1577 passed = passed && (impl->current_folder != NULL
1578 && impl->browse_files_model != NULL
1579 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1580 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1581 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1582 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1585 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1586 if (set_folder_before_map)
1587 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1589 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1593 log_test (passed, "test_reload_sequence(): status after unmap");
1597 gtk_widget_show_now (dialog);
1601 passed = passed && (impl->current_folder != NULL
1602 && impl->browse_files_model != NULL
1603 && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1604 && impl->reload_state == RELOAD_HAS_FOLDER
1605 && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1606 && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1607 ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1610 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1611 if (set_folder_before_map)
1612 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1614 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1618 log_test (passed, "test_reload_sequence(): status after re-map");
1620 gtk_widget_destroy (dialog);
1621 g_free (current_working_dir);
1631 passed = test_reload_sequence (FALSE);
1632 log_test (passed, "test_reload(): create and use the default folder");
1635 passed = test_reload_sequence (TRUE);
1636 log_test (passed, "test_reload(): set a folder explicitly before mapping");
1641 test_button_folder_states_for_action (GtkFileChooserAction action, gboolean use_dialog, gboolean set_folder_on_dialog)
1648 char *current_working_dir;
1649 gboolean must_have_cwd;
1653 current_working_dir = g_get_current_dir ();
1654 must_have_cwd = !(use_dialog && set_folder_on_dialog);
1656 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1660 dialog = gtk_file_chooser_dialog_new ("Test", NULL, action,
1661 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1662 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1664 button = gtk_file_chooser_button_new_with_dialog (dialog);
1666 if (set_folder_on_dialog)
1667 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1671 button = gtk_file_chooser_button_new ("Test", action);
1672 dialog = NULL; /* keep gcc happy */
1675 gtk_container_add (GTK_CONTAINER (window), button);
1677 /* Pre-map; no folder is set */
1680 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1682 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1684 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1686 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, pre-map, %s",
1687 get_action_name (action),
1689 set_folder_on_dialog,
1690 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1692 /* Map; folder should be set */
1694 gtk_widget_show_all (window);
1695 gtk_widget_show_now (window);
1699 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1702 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1704 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1706 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, mapped, %s",
1707 get_action_name (action),
1709 set_folder_on_dialog,
1710 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1713 /* Unmap; folder should be set */
1715 gtk_widget_hide (window);
1717 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1720 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1722 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1724 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, unmapped, %s",
1725 get_action_name (action),
1727 set_folder_on_dialog,
1728 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1731 /* Re-map; folder should be set */
1733 gtk_widget_show_now (window);
1734 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1737 passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1739 passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1741 log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, re-mapped, %s",
1742 get_action_name (action),
1744 set_folder_on_dialog,
1745 must_have_cwd ? "must have $cwd" : "must have explicit folder");
1748 g_free (current_working_dir);
1750 gtk_widget_destroy (window);
1756 test_button_folder_states (void)
1758 /* GtkFileChooserButton only supports OPEN and SELECT_FOLDER */
1759 static const GtkFileChooserAction actions_to_test[] = {
1760 GTK_FILE_CHOOSER_ACTION_OPEN,
1761 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1768 for (i = 0; i < G_N_ELEMENTS (actions_to_test); i++)
1770 passed = passed && test_button_folder_states_for_action (actions_to_test[i], FALSE, FALSE);
1772 passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, FALSE);
1774 passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, TRUE);
1776 log_test (passed, "test_button_folder_states(): action %s", get_action_name (actions_to_test[i]));
1779 log_test (passed, "test_button_folder_states(): all supported actions");
1783 test_folder_switch_and_filters (void)
1789 GFile *base_dir_file;
1791 GtkFileFilter *all_filter;
1792 GtkFileFilter *txt_filter;
1793 GtkFileChooserDefault *impl;
1797 cwd = g_get_current_dir ();
1798 base_dir = g_build_filename (cwd, "file-chooser-test-dir", NULL);
1800 dialog = gtk_file_chooser_dialog_new ("Test", NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
1801 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1802 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1804 impl = get_impl_from_dialog (dialog);
1806 cwd_file = g_file_new_for_path (cwd);
1807 base_dir_file = g_file_new_for_path (base_dir);
1809 passed = passed && gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
1812 /* All files filter */
1814 all_filter = gtk_file_filter_new ();
1815 gtk_file_filter_set_name (all_filter, "All files");
1816 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), all_filter);
1820 txt_filter = gtk_file_filter_new ();
1821 gtk_file_filter_set_name (all_filter, "*.txt");
1822 gtk_file_filter_add_pattern (txt_filter, "*.txt");
1823 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
1825 /* Test filter set */
1827 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), all_filter);
1828 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == all_filter);
1831 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
1832 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
1833 log_test (passed, "test_folder_switch_and_filters(): set and get filter");
1836 gtk_widget_show (dialog);
1838 /* Test that filter is unchanged when we switch folders */
1840 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd);
1841 sleep_in_main_loop ();
1842 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
1845 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
1846 sleep_in_main_loop ();
1848 g_signal_emit_by_name (impl->browse_path_bar, "path-clicked",
1852 sleep_in_main_loop ();
1853 passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
1854 log_test (passed, "test_folder_switch_and_filters(): filter after changing folder");
1860 g_object_unref (cwd_file);
1861 g_object_unref (base_dir_file);
1863 gtk_widget_destroy (dialog);
1865 log_test (passed, "test_folder_switch_and_filters(): all filter tests");
1873 /* initialize test program */
1874 gtk_test_init (&argc, &argv);
1876 /* Register tests */
1878 setup_file_chooser_button_tests ();
1880 setup_set_filename_tests ();
1881 setup_set_current_name_tests ();
1883 g_test_add_func ("/GtkFileChooser/confirm_overwrite", test_confirm_overwrite);
1884 g_test_add_func ("/GtkFileChooser/action_widgets", test_action_widgets);
1885 g_test_add_func ("/GtkFileChooser/reload", test_reload);
1886 g_test_add_func ("/GtkFileChooser/button_folder_states", test_button_folder_states);
1887 g_test_add_func ("/GtkFileChooser/folder_switch_and_filters", test_folder_switch_and_filters);
1890 /* run and check selected tests */
1891 return g_test_run();