]> Pileus Git - ~andy/gtk/blob - gtk/tests/filechooser.c
2c89c04e15c8d07166366e51eeb8810d28a7f0a1
[~andy/gtk] / gtk / tests / filechooser.c
1 /* GTK - The GIMP Toolkit
2  * autotestfilechooser.c: Automated unit tests for the GtkFileChooser widget
3  * Copyright (C) 2005, Novell, Inc.
4  *
5  * Authors:
6  *   Federico Mena-Quintero <federico@novell.com>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 /* TODO:
23  *
24  * - In test_reload_sequence(), test that the selection is preserved properly
25  *   between unmap/map.
26  *
27  * - More tests!
28  */
29
30 #define SLEEP_DURATION  100
31
32 #include "config.h"
33 #include <string.h>
34 #include <glib/gprintf.h>
35 #include <gtk/gtk.h>
36 #include "gtk/gtkfilechooserprivate.h"
37 #include "gtk/gtkfilechooserdefault.h"
38 #include "gtk/gtkfilechooserentry.h"
39
40 #if 0
41 static const char *
42 get_action_name (GtkFileChooserAction action)
43 {
44   switch (action)
45     {
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";
50
51     default:
52       g_assert_not_reached ();
53       return NULL;
54     }
55 }
56 #endif
57
58 #ifdef BROKEN_TESTS
59 static void
60 log_test (gboolean passed, const char *test_name, ...)
61 {
62   va_list args;
63   char *str;
64
65   va_start (args, test_name);
66   str = g_strdup_vprintf (test_name, args);
67   va_end (args);
68
69   if (g_test_verbose())
70     g_printf ("%s: %s\n", passed ? "PASSED" : "FAILED", str);
71   g_free (str);
72 }
73
74 typedef void (* SetFilenameFn) (GtkFileChooser *chooser, gpointer data);
75 typedef void (* CompareFilenameFn) (GtkFileChooser *chooser, gpointer data);
76
77 struct test_set_filename_closure {
78   GtkWidget *chooser;
79   GtkWidget *accept_button;
80   gboolean focus_button;
81 };
82
83 static gboolean
84 set_filename_timeout_cb (gpointer data)
85 {
86   struct test_set_filename_closure *closure;
87
88   closure = data;
89
90   if (closure->focus_button)
91     gtk_widget_grab_focus (closure->accept_button);
92
93   gtk_button_clicked (GTK_BUTTON (closure->accept_button));
94
95   return FALSE;
96 }
97 #endif
98
99
100 static guint wait_for_idle_id = 0;
101
102 static gboolean
103 wait_for_idle_idle (gpointer data)
104 {
105   wait_for_idle_id = 0;
106
107   return G_SOURCE_REMOVE;
108 }
109
110 static void
111 wait_for_idle (void)
112 {
113   wait_for_idle_id = g_idle_add_full (G_PRIORITY_LOW + 100,
114                                       wait_for_idle_idle,
115                                       NULL, NULL);
116
117   while (wait_for_idle_id)
118     gtk_main_iteration ();
119 }
120
121 #ifdef BROKEN_TESTS
122 static void
123 test_set_filename (GtkFileChooserAction action,
124                    gboolean focus_button,
125                    SetFilenameFn set_filename_fn,const
126                    CompareFilenameFn compare_filename_fn,
127                    gpointer data)
128 {
129   GtkWidget *chooser;
130   struct test_set_filename_closure closure;
131
132   chooser = gtk_file_chooser_dialog_new ("hello", NULL, action,
133                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
134                                          NULL);
135
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;
139
140   gtk_dialog_set_default_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
141
142   (* set_filename_fn) (GTK_FILE_CHOOSER (chooser), data);
143
144   gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, set_filename_timeout_cb, &closure, NULL);
145   gtk_dialog_run (GTK_DIALOG (chooser));
146
147   (* compare_filename_fn) (GTK_FILE_CHOOSER (chooser), data);
148
149   gtk_widget_destroy (chooser);
150 }
151
152 static void
153 set_filename_cb (GtkFileChooser *chooser, gpointer data)
154 {
155   const char *filename;
156
157   filename = data;
158   gtk_file_chooser_set_filename (chooser, filename);
159 }
160
161 static void
162 compare_filename_cb (GtkFileChooser *chooser, gpointer data)
163 {
164   const char *filename;
165   char *out_filename;
166
167   filename = data;
168   out_filename = gtk_file_chooser_get_filename (chooser);
169
170   g_assert_cmpstr (out_filename, ==, filename);
171
172   if (out_filename)
173     g_free (out_filename);
174 }
175
176 typedef struct
177 {
178   const char *test_name;
179   GtkFileChooserAction action;
180   const char *filename;
181   gboolean focus_button;
182 } TestSetFilenameSetup;
183
184 static void
185 test_black_box_set_filename (gconstpointer data)
186 {
187   const TestSetFilenameSetup *setup = data;
188
189   test_set_filename (setup->action, setup->focus_button, set_filename_cb, compare_filename_cb, (char *) setup->filename);
190 }
191
192 struct current_name_closure {
193         const char *path;
194         const char *current_name;
195 };
196
197 static void
198 set_current_name_cb (GtkFileChooser *chooser, gpointer data)
199 {
200   struct current_name_closure *closure;
201
202   closure = data;
203
204   gtk_file_chooser_set_current_folder (chooser, closure->path);
205   gtk_file_chooser_set_current_name (chooser, closure->current_name);
206 }
207
208 static void
209 compare_current_name_cb (GtkFileChooser *chooser, gpointer data)
210 {
211   struct current_name_closure *closure;
212   char *out_filename;
213   char *filename;
214
215   closure = data;
216
217   out_filename = gtk_file_chooser_get_filename (chooser);
218
219   g_assert (out_filename != NULL);
220
221   filename = g_build_filename (closure->path, closure->current_name, NULL);
222   g_assert_cmpstr (filename, ==, out_filename);
223
224   g_free (filename);
225   g_free (out_filename);
226 }
227
228 typedef struct
229 {
230   const char *test_name;
231   GtkFileChooserAction action;
232   const char *current_name;
233   gboolean focus_button;
234 } TestSetCurrentNameSetup;
235
236 static void
237 test_black_box_set_current_name (gconstpointer data)
238 {
239   const TestSetCurrentNameSetup *setup = data;
240   struct current_name_closure closure;
241   char *cwd;
242
243   cwd = g_get_current_dir ();
244
245   closure.path = cwd;
246   closure.current_name = setup->current_name;
247
248   test_set_filename (setup->action, setup->focus_button, set_current_name_cb, compare_current_name_cb, &closure);
249
250   g_free (cwd);
251 }
252 #endif
253
254 /* FIXME: fails in CREATE_FOLDER mode when FOLDER_NAME == "/" */
255
256 #if 0
257 #define FILE_NAME "/nonexistent"
258 #define FILE_NAME_2 "/nonexistent2"
259 #define FOLDER_NAME "/etc"
260 #define FOLDER_NAME_2 "/usr"
261 #else
262 #define FILE_NAME "/etc/passwd"
263 #define FILE_NAME_2 "/etc/group"
264 #define FOLDER_NAME "/etc"
265 #define FOLDER_NAME_2 "/usr"
266 #endif
267
268 #define CURRENT_NAME "parangaricutirimicuaro.txt"
269 #define CURRENT_NAME_FOLDER "parangaricutirimicuaro"
270
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
274  */
275
276 #ifdef BROKEN_TESTS
277 static void
278 setup_set_filename_tests (void)
279 {
280   static TestSetFilenameSetup tests[] =
281     {
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  },
290     };
291   int i;
292
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);
295 }
296
297 static void
298 setup_set_current_name_tests (void)
299 {
300   static TestSetCurrentNameSetup tests[] =
301     {
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  },
306     };
307   int i;
308
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);
311 }
312 #endif
313
314 typedef struct
315 {
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;
327
328 static char *
329 make_button_test_name (FileChooserButtonTest *t)
330 {
331   return g_strdup_printf ("/GtkFileChooserButton/%s", t->shortname);
332 #if 0
333   GString *s = g_string_new ("/GtkFileChooserButton");
334
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");
340
341   if (t->tweak_current_folder)
342     g_string_append (s, "/tweak_current_folder");
343
344   if (t->tweak_filename)
345     g_string_append (s, "/tweak_filename");
346
347   if (t->open_dialog)
348     g_string_append_printf (s, "/%s",
349                             t->dialog_response == GTK_RESPONSE_ACCEPT ? "accept" : "cancel");
350
351   if (t->final_current_folder)
352     g_string_append (s, "/final_current_folder");
353
354   if (t->final_filename)
355     g_string_append (s, "/final_filename");
356
357   return g_string_free (s, FALSE);
358 #endif
359 }
360
361 static gboolean
362 sleep_timeout_cb (gpointer data)
363 {
364   gtk_main_quit ();
365   return FALSE;
366 }
367
368 static void
369 sleep_in_main_loop (void)
370 {
371   /* process all pending idles and events */
372   while (g_main_context_pending (NULL))
373     g_main_context_iteration (NULL, FALSE);
374   /* sleeping probably isn't strictly necessary here */
375   gdk_threads_add_timeout_full (G_MAXINT, 250, sleep_timeout_cb, NULL, NULL);
376   gtk_main ();
377   /* process any pending idles or events that arrived during sleep */
378   while (g_main_context_pending (NULL))
379     g_main_context_iteration (NULL, FALSE);
380 }
381
382 static void
383 build_children_list (GtkWidget *widget, gpointer data)
384 {
385   GList **list;
386
387   list = data;
388   *list = g_list_prepend (*list, widget);
389 }
390
391 static GtkWidget *
392 find_child_widget_with_atk_role (GtkWidget *widget, AtkRole role)
393 {
394   AtkObject *accessible;
395   AtkRole a_role;
396
397   accessible = gtk_widget_get_accessible (widget);
398   a_role = atk_object_get_role (accessible);
399
400   if (a_role == role)
401     return widget;
402   else
403     {
404       GtkWidget *found_child;
405
406       found_child = NULL;
407
408       if (GTK_IS_CONTAINER (widget))
409         {
410           GList *children;
411           GList *l;
412
413           children = NULL;
414           gtk_container_forall (GTK_CONTAINER (widget), build_children_list, &children);
415
416           l = children;
417
418           while (l && !found_child)
419             {
420               GtkWidget *child;
421
422               child = GTK_WIDGET (l->data);
423
424               found_child = find_child_widget_with_atk_role (child, role);
425
426               l = l->next;
427             }
428
429           g_list_free (children);
430         }
431
432       return found_child;
433     }
434 }
435
436 static const char *
437 get_atk_name_for_filechooser_button (GtkFileChooserButton *button)
438 {
439   GtkFileChooserAction action;
440   GtkWidget *widget;
441   AtkObject *accessible;
442
443   action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (button));
444   g_assert (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
445
446   if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
447     widget = find_child_widget_with_atk_role (GTK_WIDGET (button), ATK_ROLE_PUSH_BUTTON);
448   else
449     widget = find_child_widget_with_atk_role (GTK_WIDGET (button), ATK_ROLE_COMBO_BOX);
450
451   accessible = gtk_widget_get_accessible (widget);
452   return atk_object_get_name (accessible);
453 }
454
455 static void
456 check_that_basename_is_shown (GtkFileChooserButton *button, const char *expected_filename)
457 {
458   GtkFileChooserAction action;
459   const char *name_on_button;
460   char *expected_basename;
461
462   name_on_button = get_atk_name_for_filechooser_button (button);
463
464   action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (button));
465   g_assert (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
466
467   if (expected_filename)
468     expected_basename = g_path_get_basename (expected_filename);
469   else
470     expected_basename = NULL;
471
472   if (expected_basename)
473     g_assert_cmpstr (expected_basename, ==, name_on_button);
474   else
475     g_assert_cmpstr (name_on_button, ==, "(None)"); /* see gtkfilechooserbutton.c:FALLBACK_DISPLAY_NAME */ /* FIXME: how do we translate this? */
476
477   g_free (expected_basename);
478 }
479
480 static const char *
481 get_expected_shown_filename (GtkFileChooserAction action, const char *folder_name, const char *filename)
482 {
483   if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
484     {
485       if (filename)
486         return filename;
487       else
488         return folder_name;
489     }
490   else
491     return filename;
492 }
493
494 static GtkWidget *
495 get_file_chooser_dialog_from_button (GtkFileChooserButton *button)
496 {
497   GtkWidget *fc_dialog;
498
499   /* Give me the internal dialog, damnit */
500   fc_dialog = g_object_get_qdata (G_OBJECT (button), g_quark_from_static_string ("gtk-file-chooser-delegate"));
501   g_assert (GTK_IS_FILE_CHOOSER (fc_dialog));
502   g_assert (GTK_IS_DIALOG (fc_dialog));
503
504   return fc_dialog;
505 }
506
507 static void
508 test_file_chooser_button (gconstpointer data)
509 {
510   const FileChooserButtonTest *setup = data;
511   GtkWidget *window;
512   GtkWidget *fc_button;
513   GtkWidget *fc_dialog;
514   int iterations;
515   int i;
516
517   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
518
519   fc_button = gtk_file_chooser_button_new (setup->action == GTK_FILE_CHOOSER_ACTION_OPEN ? "Select a file" : "Select a folder",
520                                            setup->action);
521   gtk_container_add (GTK_CONTAINER (window), fc_button);
522
523   if (setup->initial_current_folder)
524     gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->initial_current_folder);
525
526   if (setup->initial_filename)
527     gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->initial_filename);
528
529   gtk_widget_show_all (window);
530   sleep_in_main_loop ();
531
532   check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button),
533                                 get_expected_shown_filename (setup->action, setup->initial_current_folder, setup->initial_filename));
534
535   /* If there is a dialog to be opened, we actually test going through it a
536    * couple of times.  This ensures that any state that the button frobs for
537    * each appearance of the dialog will make sense.
538    */
539   if (setup->open_dialog)
540     iterations = 2;
541   else
542     iterations = 1;
543
544   for (i = 0; i < iterations; i++)
545     {
546       if (setup->open_dialog)
547         {
548           GList *children;
549
550           /* Hack our way into the file chooser button; get its GtkButton child and click it */
551           children = gtk_container_get_children (GTK_CONTAINER (fc_button));
552           g_assert (children && GTK_IS_BUTTON (children->data));
553           gtk_button_clicked (GTK_BUTTON (children->data));
554           g_list_free (children);
555
556           sleep_in_main_loop ();
557
558           fc_dialog = get_file_chooser_dialog_from_button (GTK_FILE_CHOOSER_BUTTON (fc_button));
559         }
560
561       /* Okay, now frob the button and its optional dialog */
562
563       if (setup->tweak_current_folder)
564         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc_button), setup->tweak_current_folder);
565
566       if (setup->tweak_filename)
567         gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), setup->tweak_filename);
568
569       sleep_in_main_loop ();
570
571       if (setup->open_dialog)
572         {
573           gtk_dialog_response (GTK_DIALOG (fc_dialog), setup->dialog_response);
574           wait_for_idle ();
575         }
576
577       if (setup->final_current_folder)
578         {
579           char *folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (fc_button));
580
581           g_assert_cmpstr (folder, ==, setup->final_current_folder);
582           g_free (folder);
583         }
584
585       if (setup->final_filename)
586         {
587           char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fc_button));
588
589           g_assert_cmpstr (filename, ==, setup->final_filename);
590           g_free (filename);
591         }
592
593       check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button),
594                                     get_expected_shown_filename (setup->action, setup->final_current_folder, setup->final_filename));
595     }
596
597   gtk_widget_destroy (window);
598 }
599
600 static int
601 find_accessible_action_num (AtkObject *object, const char *action_name)
602 {
603   AtkAction *action_a;
604   int num_actions;
605   int i;
606
607   action_a = ATK_ACTION (object);
608
609   num_actions = atk_action_get_n_actions (action_a);
610
611   for (i = 0; i < num_actions; i++)
612     if (strcmp (atk_action_get_name (action_a, i), action_name) == 0)
613       return i;
614
615   return -1;
616 }
617
618 static void
619 do_accessible_action (AtkObject *object, const char *action_name)
620 {
621   int action_num;
622
623   action_num = find_accessible_action_num (object, action_name);
624   g_assert (action_num != -1);
625
626   atk_action_do_action (ATK_ACTION (object), action_num);
627 }
628
629 static void
630 test_file_chooser_button_combo_box_1 (void)
631 {
632   GtkWidget *window;
633   GtkWidget *fc_button;
634   GtkWidget *combo_box;
635   AtkObject *combo_box_a;
636   AtkObject *menu_a;
637   int num_items;
638   int other_index;
639   AtkObject *item_a;
640   GtkWidget *fc_dialog;
641
642   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
643
644   fc_button = gtk_file_chooser_button_new ("Select a folder", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
645   gtk_container_add (GTK_CONTAINER (window), fc_button);
646
647   gtk_file_chooser_select_filename (GTK_FILE_CHOOSER (fc_button), FOLDER_NAME);
648
649   gtk_widget_show_all (window);
650
651   /* Get the accessible for the combo box */
652
653   combo_box = find_child_widget_with_atk_role (GTK_WIDGET (fc_button), ATK_ROLE_COMBO_BOX);
654   combo_box_a = gtk_widget_get_accessible (combo_box);
655
656   /* Press the combo box to bring up the menu */
657
658   do_accessible_action (combo_box_a, "press");
659   sleep_in_main_loop (); /* have to wait because bringing up the menu is asynchronous... */
660
661   /* Get the menu from the combo box; it's the first child */
662
663   menu_a = atk_object_ref_accessible_child (combo_box_a, 0);
664   g_assert (atk_object_get_role (menu_a) == ATK_ROLE_MENU);
665
666   /* Check that the last item in the menu is the "Other…" one */
667
668   num_items = atk_object_get_n_accessible_children (menu_a);
669   g_assert (num_items > 0);
670
671   other_index = num_items - 1;
672
673   item_a = atk_object_ref_accessible_child (menu_a, other_index);
674   g_assert_cmpstr (atk_object_get_name (item_a), ==, "Other…");  /* FIXME: how do we translate this? */
675
676   /* Activate the item */
677
678   do_accessible_action (item_a, "click");
679
680   /* Cancel the dialog */
681
682   sleep_in_main_loop ();
683   fc_dialog = get_file_chooser_dialog_from_button (GTK_FILE_CHOOSER_BUTTON (fc_button));
684
685   gtk_dialog_response (GTK_DIALOG (fc_dialog), GTK_RESPONSE_CANCEL);
686
687   /* Now check the selection in the combo box */
688   check_that_basename_is_shown (GTK_FILE_CHOOSER_BUTTON (fc_button), FOLDER_NAME);
689
690   gtk_widget_destroy (window);
691 }
692
693 static void
694 setup_file_chooser_button_combo_box_tests (void)
695 {
696   g_test_add_func ("/GtkFileChooserButton/combo_box-1", test_file_chooser_button_combo_box_1);
697 }
698
699 static FileChooserButtonTest button_tests[] =
700   {
701     /* OPEN tests without dialog */
702
703     {
704       "open-1",
705       GTK_FILE_CHOOSER_ACTION_OPEN,
706       NULL,                     /* initial_current_folder */
707       NULL,                     /* initial_filename */
708       FALSE,                    /* open_dialog */
709       NULL,                     /* tweak_current_folder */
710       NULL,                     /* tweak_filename */
711       0,                        /* dialog_response */
712       NULL,                     /* final_current_folder */
713       NULL                      /* final_filename */
714     },
715     {
716       "open-2",
717       GTK_FILE_CHOOSER_ACTION_OPEN,
718       NULL,                     /* initial_current_folder */
719       FILE_NAME,                /* initial_filename */
720       FALSE,                    /* open_dialog */
721       NULL,                     /* tweak_current_folder */
722       NULL,                     /* tweak_filename */
723       0,                        /* dialog_response */
724       NULL,                     /* final_current_folder */
725       FILE_NAME                 /* final_filename */
726     },
727     {
728       "open-3",
729       GTK_FILE_CHOOSER_ACTION_OPEN,
730       NULL,                     /* initial_current_folder */
731       NULL,                     /* initial_filename */
732       FALSE,                    /* open_dialog */
733       NULL,                     /* tweak_current_folder */
734       FILE_NAME,                /* tweak_filename */
735       0,                        /* dialog_response */
736       NULL,                     /* final_current_folder */
737       FILE_NAME                 /* final_filename */
738     },
739     {
740       "open-4",
741       GTK_FILE_CHOOSER_ACTION_OPEN,
742       NULL,                     /* initial_current_folder */
743       FILE_NAME,                /* initial_filename */
744       FALSE,                    /* open_dialog */
745       NULL,                     /* tweak_current_folder */
746       FILE_NAME_2,              /* tweak_filename */
747       0,                        /* dialog_response */
748       NULL,                     /* final_current_folder */
749       FILE_NAME_2               /* final_filename */
750     },
751     {
752       "open-5",
753       GTK_FILE_CHOOSER_ACTION_OPEN,
754       FOLDER_NAME,              /* initial_current_folder */
755       NULL,                     /* initial_filename */
756       FALSE,                    /* open_dialog */
757       NULL,                     /* tweak_current_folder */
758       NULL,                     /* tweak_filename */
759       0,                        /* dialog_response */
760       FOLDER_NAME,              /* final_current_folder */
761       NULL                      /* final_filename */
762     },
763     {
764       "open-6",
765       GTK_FILE_CHOOSER_ACTION_OPEN,
766       FOLDER_NAME,              /* initial_current_folder */
767       NULL,                     /* initial_filename */
768       FALSE,                    /* open_dialog */
769       FOLDER_NAME_2,            /* tweak_current_folder */
770       NULL,                     /* tweak_filename */
771       0,                        /* dialog_response */
772       FOLDER_NAME_2,            /* final_current_folder */
773       NULL                      /* final_filename */
774     },
775
776     /* SELECT_FOLDER tests without dialog */
777
778     {
779       "select-folder-1",
780       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
781       NULL,                     /* initial_current_folder */
782       NULL,                     /* initial_filename */
783       FALSE,                    /* open_dialog */
784       NULL,                     /* tweak_current_folder */
785       NULL,                     /* tweak_filename */
786       0,                        /* dialog_response */
787       NULL,                     /* final_current_folder */
788       NULL                      /* final_filename */
789     },
790     {
791       "select-folder-2",
792       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
793       NULL,                     /* initial_current_folder */
794       FOLDER_NAME,              /* initial_filename */
795       FALSE,                    /* open_dialog */
796       NULL,                     /* tweak_current_folder */
797       NULL,                     /* tweak_filename */
798       0,                        /* dialog_response */
799       NULL,                     /* final_current_folder */
800       FOLDER_NAME               /* final_filename */
801     },
802     {
803       "select-folder-3",
804       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
805       NULL,                     /* initial_current_folder */
806       FOLDER_NAME,              /* initial_filename */
807       FALSE,                    /* open_dialog */
808       NULL,                     /* tweak_current_folder */
809       FOLDER_NAME_2,            /* tweak_filename */
810       0,                        /* dialog_response */
811       NULL,                     /* final_current_folder */
812       FOLDER_NAME_2             /* final_filename */
813     },
814     {
815       "select-folder-4",
816       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
817       FOLDER_NAME,              /* initial_current_folder */
818       NULL,                     /* initial_filename */
819       FALSE,                    /* open_dialog */
820       NULL,                     /* tweak_current_folder */
821       NULL,                     /* tweak_filename */
822       0,                        /* dialog_response */
823       NULL,                     /* final_current_folder */
824       FOLDER_NAME               /* final_filename */
825     },
826     {
827       "select-folder-5",
828       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
829       FOLDER_NAME,              /* initial_current_folder */
830       NULL,                     /* initial_filename */
831       FALSE,                    /* open_dialog */
832       NULL,                     /* tweak_current_folder */
833       NULL,                     /* tweak_filename */
834       0,                        /* dialog_response */
835       FOLDER_NAME,              /* final_current_folder */
836       NULL                      /* final_filename */
837     },
838     {
839       "select-folder-6",
840       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
841       FOLDER_NAME,              /* initial_current_folder */
842       NULL,                     /* initial_filename */
843       FALSE,                    /* open_dialog */
844       FOLDER_NAME_2,            /* tweak_current_folder */
845       NULL,                     /* tweak_filename */
846       0,                        /* dialog_response */
847       NULL,                     /* final_current_folder */
848       FOLDER_NAME_2             /* final_filename */
849     },
850     {
851       "select-folder-7",
852       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
853       FOLDER_NAME,              /* initial_current_folder */
854       NULL,                     /* initial_filename */
855       FALSE,                    /* open_dialog */
856       FOLDER_NAME_2,            /* tweak_current_folder */
857       NULL,                     /* tweak_filename */
858       0,                        /* dialog_response */
859       FOLDER_NAME_2,            /* final_current_folder */
860       NULL                      /* final_filename */
861     },
862     {
863       "select-folder-8",
864       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
865       FOLDER_NAME,              /* initial_current_folder */
866       NULL,                     /* initial_filename */
867       FALSE,                    /* open_dialog */
868       NULL,                     /* tweak_current_folder */
869       FOLDER_NAME_2,            /* tweak_filename */
870       0,                        /* dialog_response */
871       NULL,                     /* final_current_folder */
872       FOLDER_NAME_2             /* final_filename */
873     },
874
875     /* OPEN tests with dialog, cancelled */
876
877     {
878       "open-dialog-cancel-1",
879       GTK_FILE_CHOOSER_ACTION_OPEN,
880       NULL,                     /* initial_current_folder */
881       NULL,                     /* initial_filename */
882       TRUE,                     /* open_dialog */
883       NULL,                     /* tweak_current_folder */
884       NULL,                     /* tweak_filename */
885       GTK_RESPONSE_CANCEL,      /* dialog_response */
886       NULL,                     /* final_current_folder */
887       NULL                      /* final_filename */
888     },
889     {
890       "open-dialog-cancel-2",
891       GTK_FILE_CHOOSER_ACTION_OPEN,
892       NULL,                     /* initial_current_folder */
893       FILE_NAME,                /* initial_filename */
894       TRUE,                     /* open_dialog */
895       NULL,                     /* tweak_current_folder */
896       NULL,                     /* tweak_filename */
897       GTK_RESPONSE_CANCEL,      /* dialog_response */
898       NULL,                     /* final_current_folder */
899       FILE_NAME                 /* final_filename */
900     },
901     {
902       "open-dialog-cancel-3",
903       GTK_FILE_CHOOSER_ACTION_OPEN,
904       FOLDER_NAME,              /* initial_current_folder */
905       NULL,                     /* initial_filename */
906       TRUE,                     /* open_dialog */
907       NULL,                     /* tweak_current_folder */
908       NULL,                     /* tweak_filename */
909       GTK_RESPONSE_CANCEL,      /* dialog_response */
910       FOLDER_NAME,              /* final_current_folder */
911       NULL                      /* final_filename */
912     },
913     {
914       "open-dialog-cancel-4",
915       GTK_FILE_CHOOSER_ACTION_OPEN,
916       NULL,                     /* initial_current_folder */
917       NULL,                     /* initial_filename */
918       TRUE,                     /* open_dialog */
919       NULL,                     /* tweak_current_folder */
920       FILE_NAME,                /* tweak_filename */
921       GTK_RESPONSE_CANCEL,      /* dialog_response */
922       NULL,                     /* final_current_folder */
923       NULL                      /* final_filename */
924     },
925     {
926       "open-dialog-cancel-5",
927       GTK_FILE_CHOOSER_ACTION_OPEN,
928       NULL,                     /* initial_current_folder */
929       FILE_NAME,                /* initial_filename */
930       TRUE,                     /* open_dialog */
931       NULL,                     /* tweak_current_folder */
932       FILE_NAME_2,              /* tweak_filename */
933       GTK_RESPONSE_CANCEL,      /* dialog_response */
934       NULL,                     /* final_current_folder */
935       FILE_NAME                 /* final_filename */
936     },
937     {
938       "open-dialog-cancel-6",
939       GTK_FILE_CHOOSER_ACTION_OPEN,
940       FOLDER_NAME,              /* initial_current_folder */
941       NULL,                     /* initial_filename */
942       TRUE,                     /* open_dialog */
943       NULL,                     /* tweak_current_folder */
944       FILE_NAME_2,              /* tweak_filename */
945       GTK_RESPONSE_CANCEL,      /* dialog_response */
946       FOLDER_NAME,              /* final_current_folder */
947       NULL                      /* final_filename */
948     },
949
950     /* OPEN tests with dialog, cancelled via closing the dialog (not by selecting the Cancel button) */
951
952     {
953       "open-dialog-close-1",
954       GTK_FILE_CHOOSER_ACTION_OPEN,
955       NULL,                     /* initial_current_folder */
956       NULL,                     /* initial_filename */
957       TRUE,                     /* open_dialog */
958       NULL,                     /* tweak_current_folder */
959       NULL,                     /* tweak_filename */
960       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
961       NULL,                     /* final_current_folder */
962       NULL                      /* final_filename */
963     },
964     {
965       "open-dialog-close-2",
966       GTK_FILE_CHOOSER_ACTION_OPEN,
967       NULL,                     /* initial_current_folder */
968       FILE_NAME,                /* initial_filename */
969       TRUE,                     /* open_dialog */
970       NULL,                     /* tweak_current_folder */
971       NULL,                     /* tweak_filename */
972       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
973       NULL,                     /* final_current_folder */
974       FILE_NAME                 /* final_filename */
975     },
976     {
977       "open-dialog-close-3",
978       GTK_FILE_CHOOSER_ACTION_OPEN,
979       FOLDER_NAME,              /* initial_current_folder */
980       NULL,                     /* initial_filename */
981       TRUE,                     /* open_dialog */
982       NULL,                     /* tweak_current_folder */
983       NULL,                     /* tweak_filename */
984       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
985       FOLDER_NAME,              /* final_current_folder */
986       NULL                      /* final_filename */
987     },
988     {
989       "open-dialog-close-4",
990       GTK_FILE_CHOOSER_ACTION_OPEN,
991       NULL,                     /* initial_current_folder */
992       NULL,                     /* initial_filename */
993       TRUE,                     /* open_dialog */
994       NULL,                     /* tweak_current_folder */
995       FILE_NAME,                /* tweak_filename */
996       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
997       NULL,                     /* final_current_folder */
998       NULL                      /* final_filename */
999     },
1000     {
1001       "open-dialog-close-5",
1002       GTK_FILE_CHOOSER_ACTION_OPEN,
1003       NULL,                     /* initial_current_folder */
1004       FILE_NAME,                /* initial_filename */
1005       TRUE,                     /* open_dialog */
1006       NULL,                     /* tweak_current_folder */
1007       FILE_NAME_2,              /* tweak_filename */
1008       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1009       NULL,                     /* final_current_folder */
1010       FILE_NAME                 /* final_filename */
1011     },
1012     {
1013       "open-dialog-close-6",
1014       GTK_FILE_CHOOSER_ACTION_OPEN,
1015       FOLDER_NAME,              /* initial_current_folder */
1016       NULL,                     /* initial_filename */
1017       TRUE,                     /* open_dialog */
1018       NULL,                     /* tweak_current_folder */
1019       FILE_NAME_2,              /* tweak_filename */
1020       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1021       FOLDER_NAME,              /* final_current_folder */
1022       NULL                      /* final_filename */
1023     },
1024
1025     /* SELECT_FOLDER tests with dialog, cancelled */
1026
1027     {
1028       "select-folder-dialog-cancel-1",
1029       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1030       NULL,                     /* initial_current_folder */
1031       NULL,                     /* initial_filename */
1032       TRUE,                     /* open_dialog */
1033       NULL,                     /* tweak_current_folder */
1034       NULL,                     /* tweak_filename */
1035       GTK_RESPONSE_CANCEL,      /* dialog_response */
1036       NULL,                     /* final_current_folder */
1037       NULL                      /* final_filename */
1038     },
1039     {
1040       "select-folder-dialog-cancel-2",
1041       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1042       NULL,                     /* initial_current_folder */
1043       FOLDER_NAME,              /* initial_filename */
1044       TRUE,                     /* open_dialog */
1045       NULL,                     /* tweak_current_folder */
1046       NULL,                     /* tweak_filename */
1047       GTK_RESPONSE_CANCEL,      /* dialog_response */
1048       NULL,                     /* final_current_folder */
1049       FOLDER_NAME               /* final_filename */
1050     },
1051     {
1052       "select-folder-dialog-cancel-3",
1053       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1054       FOLDER_NAME,              /* initial_current_folder */
1055       NULL,                     /* initial_filename */
1056       TRUE,                     /* open_dialog */
1057       NULL,                     /* tweak_current_folder */
1058       NULL,                     /* tweak_filename */
1059       GTK_RESPONSE_CANCEL,      /* dialog_response */
1060       FOLDER_NAME,              /* final_current_folder */
1061       NULL                      /* final_filename */
1062     },
1063     {
1064       "select-folder-dialog-cancel-4",
1065       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1066       FOLDER_NAME,              /* initial_current_folder */
1067       NULL,                     /* initial_filename */
1068       TRUE,                     /* open_dialog */
1069       NULL,                     /* tweak_current_folder */
1070       NULL,                     /* tweak_filename */
1071       GTK_RESPONSE_CANCEL,      /* dialog_response */
1072       NULL,                     /* final_current_folder */
1073       FOLDER_NAME               /* final_filename */
1074     },
1075     {
1076       "select-folder-dialog-cancel-5",
1077       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1078       NULL,                     /* initial_current_folder */
1079       NULL,                     /* initial_filename */
1080       TRUE,                     /* open_dialog */
1081       NULL,                     /* tweak_current_folder */
1082       FOLDER_NAME,              /* tweak_filename */
1083       GTK_RESPONSE_CANCEL,      /* dialog_response */
1084       NULL,                     /* final_current_folder */
1085       NULL                      /* final_filename */
1086     },
1087     {
1088       "select-folder-dialog-cancel-6",
1089       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1090       NULL,                     /* initial_current_folder */
1091       FOLDER_NAME,              /* initial_filename */
1092       TRUE,                     /* open_dialog */
1093       NULL,                     /* tweak_current_folder */
1094       FOLDER_NAME_2,            /* tweak_filename */
1095       GTK_RESPONSE_CANCEL,      /* dialog_response */
1096       NULL,                     /* final_current_folder */
1097       FOLDER_NAME               /* final_filename */
1098     },
1099     {
1100       "select-folder-dialog-cancel-7",
1101       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1102       FOLDER_NAME,              /* initial_current_folder */
1103       NULL,                     /* initial_filename */
1104       TRUE,                     /* open_dialog */
1105       NULL,                     /* tweak_current_folder */
1106       FOLDER_NAME_2,            /* tweak_filename */
1107       GTK_RESPONSE_CANCEL,      /* dialog_response */
1108       FOLDER_NAME,              /* final_current_folder */
1109       NULL                      /* final_filename */
1110     },
1111     {
1112       "select-folder-dialog-cancel-8",
1113       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1114       FOLDER_NAME,              /* initial_current_folder */
1115       NULL,                     /* initial_filename */
1116       TRUE,                     /* open_dialog */
1117       NULL,                     /* tweak_current_folder */
1118       FOLDER_NAME_2,            /* tweak_filename */
1119       GTK_RESPONSE_CANCEL,      /* dialog_response */
1120       NULL,                     /* final_current_folder */
1121       FOLDER_NAME               /* final_filename */
1122     },
1123
1124     /* SELECT_FOLDER tests with dialog, cancelled via closing the dialog (not selecting the Cancel button) */
1125
1126     {
1127       "select-folder-dialog-close-1",
1128       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1129       NULL,                     /* initial_current_folder */
1130       NULL,                     /* initial_filename */
1131       TRUE,                     /* open_dialog */
1132       NULL,                     /* tweak_current_folder */
1133       NULL,                     /* tweak_filename */
1134       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1135       NULL,                     /* final_current_folder */
1136       NULL                      /* final_filename */
1137     },
1138     {
1139       "select-folder-dialog-close-2",
1140       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1141       NULL,                     /* initial_current_folder */
1142       FOLDER_NAME,              /* initial_filename */
1143       TRUE,                     /* open_dialog */
1144       NULL,                     /* tweak_current_folder */
1145       NULL,                     /* tweak_filename */
1146       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1147       NULL,                     /* final_current_folder */
1148       FOLDER_NAME               /* final_filename */
1149     },
1150     {
1151       "select-folder-dialog-close-3",
1152       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1153       FOLDER_NAME,              /* initial_current_folder */
1154       NULL,                     /* initial_filename */
1155       TRUE,                     /* open_dialog */
1156       NULL,                     /* tweak_current_folder */
1157       NULL,                     /* tweak_filename */
1158       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1159       FOLDER_NAME,              /* final_current_folder */
1160       NULL                      /* final_filename */
1161     },
1162     {
1163       "select-folder-dialog-close-4",
1164       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1165       FOLDER_NAME,              /* initial_current_folder */
1166       NULL,                     /* initial_filename */
1167       TRUE,                     /* open_dialog */
1168       NULL,                     /* tweak_current_folder */
1169       NULL,                     /* tweak_filename */
1170       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1171       NULL,                     /* final_current_folder */
1172       FOLDER_NAME               /* final_filename */
1173     },
1174     {
1175       "select-folder-dialog-close-5",
1176       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1177       NULL,                     /* initial_current_folder */
1178       NULL,                     /* initial_filename */
1179       TRUE,                     /* open_dialog */
1180       NULL,                     /* tweak_current_folder */
1181       FOLDER_NAME,              /* tweak_filename */
1182       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1183       NULL,                     /* final_current_folder */
1184       NULL                      /* final_filename */
1185     },
1186     {
1187       "select-folder-dialog-close-6",
1188       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1189       NULL,                     /* initial_current_folder */
1190       FOLDER_NAME,              /* initial_filename */
1191       TRUE,                     /* open_dialog */
1192       NULL,                     /* tweak_current_folder */
1193       FOLDER_NAME_2,            /* tweak_filename */
1194       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1195       NULL,                     /* final_current_folder */
1196       FOLDER_NAME               /* final_filename */
1197     },
1198     {
1199       "select-folder-dialog-close-7",
1200       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1201       FOLDER_NAME,              /* initial_current_folder */
1202       NULL,                     /* initial_filename */
1203       TRUE,                     /* open_dialog */
1204       NULL,                     /* tweak_current_folder */
1205       FOLDER_NAME_2,            /* tweak_filename */
1206       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1207       FOLDER_NAME,              /* final_current_folder */
1208       NULL                      /* final_filename */
1209     },
1210     {
1211       "select-folder-dialog-close-8",
1212       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1213       FOLDER_NAME,              /* initial_current_folder */
1214       NULL,                     /* initial_filename */
1215       TRUE,                     /* open_dialog */
1216       NULL,                     /* tweak_current_folder */
1217       FOLDER_NAME_2,            /* tweak_filename */
1218       GTK_RESPONSE_DELETE_EVENT,/* dialog_response */
1219       NULL,                     /* final_current_folder */
1220       FOLDER_NAME               /* final_filename */
1221     },
1222
1223     /* OPEN tests with dialog */
1224
1225     {
1226       "open-dialog-1",
1227       GTK_FILE_CHOOSER_ACTION_OPEN,
1228       NULL,                     /* initial_current_folder */
1229       NULL,                     /* initial_filename */
1230       TRUE,                     /* open_dialog */
1231       NULL,                     /* tweak_current_folder */
1232       FILE_NAME,                /* tweak_filename */
1233       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1234       NULL,                     /* final_current_folder */
1235       FILE_NAME                 /* final_filename */
1236     },
1237     {
1238       "open-dialog-2",
1239       GTK_FILE_CHOOSER_ACTION_OPEN,
1240       NULL,                     /* initial_current_folder */
1241       FILE_NAME,                /* initial_filename */
1242       TRUE,                     /* open_dialog */
1243       NULL,                     /* tweak_current_folder */
1244       NULL,                     /* tweak_filename */
1245       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1246       NULL,                     /* final_current_folder */
1247       FILE_NAME                 /* final_filename */
1248     },
1249     {
1250       "open-dialog-3",
1251       GTK_FILE_CHOOSER_ACTION_OPEN,
1252       NULL,                     /* initial_current_folder */
1253       FILE_NAME,                /* initial_filename */
1254       TRUE,                     /* open_dialog */
1255       NULL,                     /* tweak_current_folder */
1256       FILE_NAME_2,              /* tweak_filename */
1257       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1258       NULL,                     /* final_current_folder */
1259       FILE_NAME_2               /* final_filename */
1260     },
1261     {
1262       "open-dialog-4",
1263       GTK_FILE_CHOOSER_ACTION_OPEN,
1264       FOLDER_NAME,              /* initial_current_folder */
1265       NULL,                     /* initial_filename */
1266       TRUE,                     /* open_dialog */
1267       NULL,                     /* tweak_current_folder */
1268       FILE_NAME,                /* tweak_filename */
1269       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1270       NULL,                     /* final_current_folder */
1271       FILE_NAME                 /* final_filename */
1272     },
1273
1274     /* SELECT_FOLDER tests with dialog */
1275
1276     {
1277       "select-folder-dialog-1",
1278       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1279       NULL,                     /* initial_current_folder */
1280       FOLDER_NAME,              /* initial_filename */
1281       TRUE,                     /* open_dialog */
1282       NULL,                     /* tweak_current_folder */
1283       NULL,                     /* tweak_filename */
1284       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1285       NULL,                     /* final_current_folder */
1286       FOLDER_NAME               /* final_filename */
1287     },
1288     {
1289       "select-folder-dialog-2",
1290       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1291       FOLDER_NAME,              /* initial_current_folder */
1292       NULL,                     /* initial_filename */
1293       TRUE,                     /* open_dialog */
1294       NULL,                     /* tweak_current_folder */
1295       NULL,                     /* tweak_filename */
1296       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1297       NULL,                     /* final_current_folder */
1298       FOLDER_NAME               /* final_filename */
1299     },
1300     {
1301       "select-folder-dialog-3",
1302       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1303       NULL,                     /* initial_current_folder */
1304       FOLDER_NAME,              /* initial_filename */
1305       TRUE,                     /* open_dialog */
1306       NULL,                     /* tweak_current_folder */
1307       FOLDER_NAME_2,            /* tweak_filename */
1308       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1309       NULL,                     /* final_current_folder */
1310       FOLDER_NAME_2             /* final_filename */
1311     },
1312     {
1313       "select-folder-dialog-4",
1314       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1315       FOLDER_NAME,              /* initial_current_folder */
1316       NULL,                     /* initial_filename */
1317       TRUE,                     /* open_dialog */
1318       NULL,                     /* tweak_current_folder */
1319       FOLDER_NAME_2,            /* tweak_filename */
1320       GTK_RESPONSE_ACCEPT,      /* dialog_response */
1321       NULL,                     /* final_current_folder */
1322       FOLDER_NAME_2             /* final_filename */
1323     },
1324
1325   };
1326
1327 static void
1328 setup_file_chooser_button_tests (void)
1329 {
1330   int i;
1331
1332   for (i = 0; i < G_N_ELEMENTS (button_tests); i++)
1333     {
1334       char *test_name;
1335
1336       test_name = make_button_test_name (&button_tests[i]);
1337       g_test_add_data_func (test_name, &button_tests[i], test_file_chooser_button);
1338       g_free (test_name);
1339     }
1340
1341   setup_file_chooser_button_combo_box_tests ();
1342 }
1343
1344 #ifdef BROKEN_TESTS
1345 struct confirm_overwrite_closure {
1346   GtkWidget *chooser;
1347   GtkWidget *accept_button;
1348   gint confirm_overwrite_signal_emitted;
1349   gchar *extension;
1350 };
1351
1352 static GtkFileChooserConfirmation
1353 confirm_overwrite_cb (GtkFileChooser *chooser, gpointer data)
1354 {
1355   struct confirm_overwrite_closure *closure = data;
1356
1357   if (g_test_verbose())
1358     printf ("bling!\n");
1359   closure->confirm_overwrite_signal_emitted += 1;
1360
1361   return GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
1362 }
1363
1364 static void
1365 overwrite_response_cb (GtkFileChooser *chooser, gint response, gpointer data)
1366 {
1367   struct confirm_overwrite_closure *closure = data;
1368   char *filename;
1369
1370   if (g_test_verbose())
1371     printf ("plong!\n");
1372
1373   if (response != GTK_RESPONSE_ACCEPT)
1374     return;
1375
1376   filename = gtk_file_chooser_get_filename (chooser);
1377
1378   if (!g_str_has_suffix (filename, closure->extension))
1379     {
1380       char *basename;
1381
1382       basename = g_path_get_basename (filename);
1383       g_free (filename);
1384
1385       filename = g_strconcat (basename, closure->extension, NULL);
1386       gtk_file_chooser_set_current_name (chooser, filename);
1387
1388       g_signal_stop_emission_by_name (chooser, "response");
1389       gtk_dialog_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
1390     }
1391 }
1392
1393 static gboolean
1394 confirm_overwrite_timeout_cb (gpointer data)
1395 {
1396   struct confirm_overwrite_closure *closure;
1397
1398   closure = data;
1399   gtk_button_clicked (GTK_BUTTON (closure->accept_button));
1400
1401   return FALSE;
1402 }
1403
1404 /* http://bugzilla.gnome.org/show_bug.cgi?id=347883 */
1405 static gboolean
1406 test_confirm_overwrite_for_path (const char *path, gboolean append_extension)
1407 {
1408   gboolean passed;
1409   struct confirm_overwrite_closure closure;
1410   char *filename;
1411
1412   passed = TRUE;
1413
1414   closure.extension = NULL;
1415   closure.confirm_overwrite_signal_emitted = 0;
1416   closure.chooser = gtk_file_chooser_dialog_new ("hello", NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1417                                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1418                                                  NULL);
1419   closure.accept_button = gtk_dialog_add_button (GTK_DIALOG (closure.chooser),
1420                                                  GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT);
1421   gtk_dialog_set_default_response (GTK_DIALOG (closure.chooser), GTK_RESPONSE_ACCEPT);
1422
1423   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (closure.chooser), TRUE);
1424
1425   g_signal_connect (closure.chooser, "confirm-overwrite",
1426                     G_CALLBACK (confirm_overwrite_cb), &closure);
1427
1428   if (append_extension)
1429     {
1430       char *extension;
1431
1432       filename = g_path_get_dirname (path);
1433       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (closure.chooser), filename);
1434       g_free (filename);
1435
1436       filename = g_path_get_basename (path);
1437       extension = strchr (filename, '.');
1438
1439       if (extension)
1440         {
1441           closure.extension = g_strdup (extension);
1442           *extension = '\0';
1443         }
1444
1445       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (closure.chooser), filename);
1446       g_free (filename);
1447
1448       g_signal_connect (closure.chooser, "response",
1449                         G_CALLBACK (overwrite_response_cb), &closure);
1450     }
1451   else
1452     {
1453       gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (closure.chooser), path);
1454     }
1455
1456   gdk_threads_add_timeout_full (G_MAXINT, SLEEP_DURATION, confirm_overwrite_timeout_cb, &closure, NULL);
1457   gtk_dialog_run (GTK_DIALOG (closure.chooser));
1458
1459   filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (closure.chooser));
1460   passed = passed && filename && (strcmp (filename, path) == 0);
1461   g_free (filename);
1462   
1463   gtk_widget_destroy (closure.chooser);
1464
1465   passed = passed && (1 == closure.confirm_overwrite_signal_emitted);
1466
1467   log_test (passed, "Confirm overwrite for %s", path);
1468
1469   return passed;
1470 }
1471
1472 static void
1473 test_confirm_overwrite (void)
1474 {
1475   gboolean passed = TRUE;
1476
1477   /* first test for a file we know will always exist */
1478   passed = passed && test_confirm_overwrite_for_path ("/etc/passwd", FALSE); 
1479   g_assert (passed);
1480   passed = passed && test_confirm_overwrite_for_path ("/etc/resolv.conf", TRUE); 
1481   g_assert (passed);
1482 }
1483 #endif
1484
1485 static const GtkFileChooserAction open_actions[] = {
1486   GTK_FILE_CHOOSER_ACTION_OPEN,
1487   GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1488 };
1489
1490 static const GtkFileChooserAction save_actions[] = {
1491   GTK_FILE_CHOOSER_ACTION_SAVE,
1492   GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
1493 };
1494
1495
1496 #ifdef BROKEN_TESTS
1497 static gboolean
1498 has_action (const GtkFileChooserAction *actions,
1499             int n_actions,
1500             GtkFileChooserAction sought_action)
1501 {
1502   int i;
1503
1504   for (i = 0; i < n_actions; i++)
1505     if (actions[i] == sought_action)
1506       return TRUE;
1507
1508   return FALSE;
1509 }
1510
1511 static GtkFileChooserDefault *
1512 get_impl_from_dialog (GtkWidget *dialog)
1513 {
1514   GtkFileChooserDialog *d;
1515   GtkFileChooserDialogPrivate *dialog_priv;
1516   GtkFileChooserWidget *chooser_widget;
1517   GtkFileChooserWidgetPrivate *widget_priv;
1518   GtkFileChooserDefault *impl;
1519
1520   d = GTK_FILE_CHOOSER_DIALOG (dialog);
1521   dialog_priv = d->priv;
1522   chooser_widget = GTK_FILE_CHOOSER_WIDGET (dialog_priv->widget);
1523   if (!chooser_widget)
1524     g_error ("BUG: dialog_priv->widget is not a GtkFileChooserWidget");
1525
1526   widget_priv = chooser_widget->priv;
1527   impl = (GtkFileChooserDefault *) (widget_priv->impl);
1528   if (!impl)
1529     g_error ("BUG: widget_priv->impl is not a GtkFileChooserDefault");
1530
1531   return impl;
1532 }
1533
1534 static gboolean
1535 test_widgets_for_current_action (GtkFileChooserDialog *dialog,
1536                                  GtkFileChooserAction  expected_action)
1537 {
1538   GtkFileChooserDefault *impl;
1539   gboolean passed;
1540
1541   if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) != expected_action)
1542     return FALSE;
1543
1544   impl = get_impl_from_dialog (GTK_WIDGET (dialog));
1545
1546   g_assert (impl->action == expected_action);
1547
1548   passed = TRUE;
1549
1550   /* OPEN implies that the "new folder" button is hidden; otherwise it is shown */
1551   if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
1552     passed = passed && !gtk_widget_get_visible (impl->browse_new_folder_button);
1553   else
1554     passed = passed && gtk_widget_get_visible (impl->browse_new_folder_button);
1555
1556   /* Check that the widgets are present/visible or not */
1557   if (has_action (open_actions, G_N_ELEMENTS (open_actions), impl->action))
1558     {
1559       passed = passed && (impl->save_widgets == NULL
1560                           && (impl->location_mode == LOCATION_MODE_PATH_BAR
1561                               ? impl->location_entry == NULL
1562                               : impl->location_entry != NULL)
1563                           && impl->save_folder_label == NULL
1564                           && impl->save_folder_combo == NULL
1565                           && impl->save_expander == NULL
1566                           && GTK_IS_CONTAINER (impl->browse_widgets) && gtk_widget_is_drawable (impl->browse_widgets));
1567     }
1568   else if (has_action (save_actions, G_N_ELEMENTS (save_actions), impl->action))
1569     {
1570       /* FIXME: we can't use GTK_IS_FILE_CHOOSER_ENTRY() because it uses
1571        * _gtk_file_chooser_entry_get_type(), which is a non-exported symbol.
1572        * So, we just test impl->location_entry for being non-NULL
1573        */
1574       passed = passed && (GTK_IS_CONTAINER (impl->save_widgets) && gtk_widget_is_drawable (impl->save_widgets)
1575                           && impl->location_entry != NULL && gtk_widget_is_drawable (impl->location_entry)
1576                           && GTK_IS_LABEL (impl->save_folder_label) && gtk_widget_is_drawable (impl->save_folder_label)
1577                           && GTK_IS_COMBO_BOX (impl->save_folder_combo) && gtk_widget_is_drawable (impl->save_folder_combo)
1578                           && GTK_IS_EXPANDER (impl->save_expander) && gtk_widget_is_drawable (impl->save_expander)
1579                           && GTK_IS_CONTAINER (impl->browse_widgets));
1580
1581       /* FIXME: we are in a SAVE mode; test the visibility and sensitivity of
1582        * the children that change depending on the state of the expander.
1583        */
1584     }
1585   else
1586     {
1587       g_error ("BAD TEST: test_widgets_for_current_action() doesn't know about %s", get_action_name (impl->action));
1588       passed = FALSE;
1589     }
1590
1591   return passed;
1592 }
1593
1594 typedef gboolean (* ForeachActionCallback) (GtkFileChooserDialog *dialog,
1595                                             GtkFileChooserAction  action,
1596                                             gpointer              user_data);
1597
1598 static gboolean
1599 foreach_action (GtkFileChooserDialog *dialog,
1600                 ForeachActionCallback callback,
1601                 gpointer              user_data)
1602 {
1603   GEnumClass *enum_class;
1604   int i;
1605
1606   enum_class = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
1607   if (!enum_class)
1608     g_error ("BUG: get_action_name(): no GEnumClass for GTK_TYPE_FILE_CHOOSER_ACTION");
1609
1610   for (i = 0; i < enum_class->n_values; i++)
1611     {
1612       GEnumValue *enum_value;
1613       GtkFileChooserAction action;
1614       gboolean passed;
1615
1616       enum_value = enum_class->values + i;
1617       action = enum_value->value;
1618
1619       passed = (* callback) (dialog, action, user_data);
1620       if (!passed)
1621         return FALSE;
1622     }
1623
1624   return TRUE;
1625 }
1626
1627 struct action_closure {
1628   GtkFileChooserAction from_action;
1629 };
1630
1631 static gboolean
1632 switch_from_to_action_cb (GtkFileChooserDialog *dialog,
1633                           GtkFileChooserAction  action,
1634                           gpointer              user_data)
1635 {
1636   struct action_closure *closure;
1637   gboolean passed;
1638
1639   closure = user_data;
1640
1641   gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), closure->from_action);
1642
1643   passed = test_widgets_for_current_action (dialog, closure->from_action);
1644   log_test (passed, "switch_from_to_action_cb(): reset to action %s", get_action_name (closure->from_action));
1645   if (!passed)
1646     return FALSE;
1647
1648   gtk_file_chooser_set_action (GTK_FILE_CHOOSER (dialog), action);
1649
1650   passed = test_widgets_for_current_action (dialog, action);
1651   log_test (passed, "switch_from_to_action_cb(): transition from %s to %s",
1652             get_action_name (closure->from_action),
1653             get_action_name (action));
1654   return passed;
1655 }
1656
1657 static gboolean
1658 switch_from_action_cb (GtkFileChooserDialog *dialog,
1659                        GtkFileChooserAction  action,
1660                        gpointer              user_data)
1661 {
1662   struct action_closure closure;
1663
1664   closure.from_action = action;
1665
1666   return foreach_action (dialog, switch_from_to_action_cb, &closure);
1667 }
1668
1669 static void
1670 test_action_widgets (void)
1671 {
1672   GtkWidget *dialog;
1673   GtkFileChooserAction action;
1674   gboolean passed;
1675
1676   dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1677                                         NULL,
1678                                         GTK_FILE_CHOOSER_ACTION_OPEN,
1679                                         GTK_STOCK_CANCEL,
1680                                         GTK_RESPONSE_CANCEL,
1681                                         GTK_STOCK_OK,
1682                                         GTK_RESPONSE_ACCEPT,
1683                                         NULL);
1684   gtk_widget_show_now (dialog);
1685
1686   action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog));
1687
1688   passed = test_widgets_for_current_action (GTK_FILE_CHOOSER_DIALOG (dialog), action);
1689   log_test (passed, "test_action_widgets(): widgets for initial action %s", get_action_name (action));
1690   g_assert (passed);
1691
1692   passed = foreach_action (GTK_FILE_CHOOSER_DIALOG (dialog), switch_from_action_cb, NULL);
1693   log_test (passed, "test_action_widgets(): all transitions through property change");
1694   g_assert (passed);
1695
1696   gtk_widget_destroy (dialog);
1697 }
1698 #endif
1699
1700 #ifdef BROKEN_TESTS
1701 static gboolean
1702 test_reload_sequence (gboolean set_folder_before_map)
1703 {
1704   GtkWidget *dialog;
1705   GtkFileChooserDefault *impl;
1706   gboolean passed;
1707   char *folder;
1708   char *current_working_dir;
1709
1710   passed = TRUE;
1711
1712   current_working_dir = g_get_current_dir ();
1713
1714   dialog = gtk_file_chooser_dialog_new ("Test file chooser",
1715                                         NULL,
1716                                         GTK_FILE_CHOOSER_ACTION_OPEN,
1717                                         GTK_STOCK_CANCEL,
1718                                         GTK_RESPONSE_CANCEL,
1719                                         GTK_STOCK_OK,
1720                                         GTK_RESPONSE_ACCEPT,
1721                                         NULL);
1722   impl = get_impl_from_dialog (dialog);
1723
1724   if (set_folder_before_map)
1725     {
1726       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1727
1728       wait_for_idle ();
1729
1730       passed = passed && (impl->current_folder != NULL
1731                           && impl->browse_files_model != NULL
1732                           && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1733                           && impl->reload_state == RELOAD_HAS_FOLDER
1734                           && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1735                           && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1736                               ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1737                               : TRUE));
1738
1739       wait_for_idle ();
1740
1741       folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1742       passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1743       g_free (folder);
1744     }
1745   else
1746     {
1747       /* Initially, no folder is not loaded or pending */
1748       passed = passed && (impl->current_folder == NULL
1749                           && impl->sort_model == NULL
1750                           && impl->browse_files_model == NULL
1751                           && impl->load_state == LOAD_EMPTY
1752                           && impl->reload_state == RELOAD_EMPTY
1753                           && impl->load_timeout_id == 0);
1754
1755       wait_for_idle ();
1756
1757       folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1758       passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1759     }
1760
1761   log_test (passed, "test_reload_sequence(): initial status");
1762
1763   /* After mapping, it is loading some folder, either the one that was explicitly set or the default one */
1764
1765   gtk_widget_show_now (dialog);
1766
1767   wait_for_idle ();
1768
1769   passed = passed && (impl->current_folder != NULL
1770                       && impl->browse_files_model != NULL
1771                       && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1772                       && impl->reload_state == RELOAD_HAS_FOLDER
1773                       && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1774                       && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1775                           ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1776                           : TRUE));
1777
1778   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1779   if (set_folder_before_map)
1780     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1781   else
1782     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1783
1784   g_free (folder);
1785
1786   log_test (passed, "test_reload_sequence(): status after map");
1787
1788   /* Unmap it; we should still have a folder */
1789
1790   gtk_widget_hide (dialog);
1791
1792   wait_for_idle ();
1793
1794   passed = passed && (impl->current_folder != NULL
1795                       && impl->browse_files_model != NULL
1796                       && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1797                       && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1798                       && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1799                           ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1800                           : TRUE));
1801
1802   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1803   if (set_folder_before_map)
1804     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1805   else
1806     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1807
1808   g_free (folder);
1809
1810   log_test (passed, "test_reload_sequence(): status after unmap");
1811
1812   /* Map it again! */
1813
1814   gtk_widget_show_now (dialog);
1815
1816   wait_for_idle ();
1817
1818   passed = passed && (impl->current_folder != NULL
1819                       && impl->browse_files_model != NULL
1820                       && (impl->load_state == LOAD_PRELOAD || impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1821                       && impl->reload_state == RELOAD_HAS_FOLDER
1822                       && (impl->load_state == LOAD_PRELOAD ? (impl->load_timeout_id != 0) : TRUE)
1823                       && ((impl->load_state == LOAD_LOADING || impl->load_state == LOAD_FINISHED)
1824                           ? (impl->load_timeout_id == 0 && impl->sort_model != NULL)
1825                           : TRUE));
1826
1827   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
1828   if (set_folder_before_map)
1829     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1830   else
1831     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1832
1833   g_free (folder);
1834
1835   log_test (passed, "test_reload_sequence(): status after re-map");
1836
1837   gtk_widget_destroy (dialog);
1838   g_free (current_working_dir);
1839
1840   return passed;
1841 }
1842
1843 static void
1844 test_reload (void)
1845 {
1846   gboolean passed;
1847
1848   passed = test_reload_sequence (FALSE);
1849   log_test (passed, "test_reload(): create and use the default folder");
1850   g_assert (passed);
1851
1852   passed = test_reload_sequence (TRUE);
1853   log_test (passed, "test_reload(): set a folder explicitly before mapping");
1854   g_assert (passed);
1855 }
1856
1857 static gboolean
1858 test_button_folder_states_for_action (GtkFileChooserAction action, gboolean use_dialog, gboolean set_folder_on_dialog)
1859 {
1860   gboolean passed;
1861   GtkWidget *window;
1862   GtkWidget *button;
1863   char *folder;
1864   GtkWidget *dialog;
1865   char *current_working_dir;
1866   gboolean must_have_cwd;
1867
1868   passed = TRUE;
1869
1870   current_working_dir = g_get_current_dir ();
1871   must_have_cwd = !(use_dialog && set_folder_on_dialog);
1872
1873   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1874
1875   if (use_dialog)
1876     {
1877       dialog = gtk_file_chooser_dialog_new ("Test", NULL, action,
1878                                             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1879                                             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1880                                             NULL);
1881       button = gtk_file_chooser_button_new_with_dialog (dialog);
1882
1883       if (set_folder_on_dialog)
1884         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_home_dir ());
1885     }
1886   else
1887     {
1888       button = gtk_file_chooser_button_new ("Test", action);
1889       dialog = NULL; /* keep gcc happy */
1890     }
1891
1892   gtk_container_add (GTK_CONTAINER (window), button);
1893
1894   /* Pre-map; no folder is set */
1895   wait_for_idle ();
1896
1897   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1898   if (must_have_cwd)
1899     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1900   else
1901     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1902
1903   log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, pre-map, %s",
1904             get_action_name (action),
1905             use_dialog,
1906             set_folder_on_dialog,
1907             must_have_cwd ? "must have $cwd" : "must have explicit folder");
1908
1909   /* Map; folder should be set */
1910
1911   gtk_widget_show_all (window);
1912   gtk_widget_show_now (window);
1913
1914   wait_for_idle ();
1915
1916   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1917
1918   if (must_have_cwd)
1919     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1920   else
1921     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1922
1923   log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, mapped, %s",
1924             get_action_name (action),
1925             use_dialog,
1926             set_folder_on_dialog,
1927             must_have_cwd ? "must have $cwd" : "must have explicit folder");
1928   g_free (folder);
1929
1930   /* Unmap; folder should be set */
1931
1932   gtk_widget_hide (window);
1933   wait_for_idle ();
1934   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1935
1936   if (must_have_cwd)
1937     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1938   else
1939     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1940
1941   log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, unmapped, %s",
1942             get_action_name (action),
1943             use_dialog,
1944             set_folder_on_dialog,
1945             must_have_cwd ? "must have $cwd" : "must have explicit folder");
1946   g_free (folder);
1947
1948   /* Re-map; folder should be set */
1949
1950   gtk_widget_show_now (window);
1951   folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (button));
1952
1953   if (must_have_cwd)
1954     passed = passed && (g_strcmp0 (folder, current_working_dir) == 0);
1955   else
1956     passed = passed && (g_strcmp0 (folder, g_get_home_dir()) == 0);
1957   wait_for_idle ();
1958   log_test (passed, "test_button_folder_states_for_action(): %s, use_dialog=%d, set_folder_on_dialog=%d, re-mapped, %s",
1959             get_action_name (action),
1960             use_dialog,
1961             set_folder_on_dialog,
1962             must_have_cwd ? "must have $cwd" : "must have explicit folder");
1963   g_free (folder);
1964
1965   g_free (current_working_dir);
1966
1967   gtk_widget_destroy (window);
1968
1969   return passed;
1970 }
1971
1972 static void
1973 test_button_folder_states (void)
1974 {
1975   /* GtkFileChooserButton only supports OPEN and SELECT_FOLDER */
1976   static const GtkFileChooserAction actions_to_test[] = {
1977     GTK_FILE_CHOOSER_ACTION_OPEN,
1978     GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1979   };
1980   gboolean passed;
1981   int i;
1982
1983   passed = TRUE;
1984
1985   for (i = 0; i < G_N_ELEMENTS (actions_to_test); i++)
1986     {
1987       passed = passed && test_button_folder_states_for_action (actions_to_test[i], FALSE, FALSE);
1988       g_assert (passed);
1989       passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, FALSE);
1990       g_assert (passed);
1991       passed = passed && test_button_folder_states_for_action (actions_to_test[i], TRUE, TRUE);
1992       g_assert (passed);
1993       log_test (passed, "test_button_folder_states(): action %s", get_action_name (actions_to_test[i]));
1994     }
1995
1996   log_test (passed, "test_button_folder_states(): all supported actions");
1997 }
1998
1999 static void
2000 test_folder_switch_and_filters (void)
2001 {
2002   gboolean passed;
2003   char *cwd;
2004   char *base_dir;
2005   GFile *cwd_file;
2006   GFile *base_dir_file;
2007   GtkWidget *dialog;
2008   GtkFileFilter *all_filter;
2009   GtkFileFilter *txt_filter;
2010   GtkFileChooserDefault *impl;
2011
2012   passed = TRUE;
2013
2014   cwd = g_get_current_dir ();
2015   base_dir = g_build_filename (cwd, "file-chooser-test-dir", NULL);
2016
2017   dialog = gtk_file_chooser_dialog_new ("Test", NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
2018                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2019                                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2020                                         NULL);
2021   impl = get_impl_from_dialog (dialog);
2022
2023   cwd_file = g_file_new_for_path (cwd);
2024   base_dir_file = g_file_new_for_path (base_dir);
2025
2026   passed = passed && gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
2027   g_assert (passed);
2028
2029   /* All files filter */
2030
2031   all_filter = gtk_file_filter_new ();
2032   gtk_file_filter_set_name (all_filter, "All files");
2033   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), all_filter);
2034
2035   /* *.txt filter */
2036
2037   txt_filter = gtk_file_filter_new ();
2038   gtk_file_filter_set_name (all_filter, "*.txt");
2039   gtk_file_filter_add_pattern (txt_filter, "*.txt");
2040   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
2041
2042   /* Test filter set */
2043
2044   gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), all_filter);
2045   passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == all_filter);
2046   g_assert (passed);
2047
2048   gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), txt_filter);
2049   passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
2050   log_test (passed, "test_folder_switch_and_filters(): set and get filter");
2051   g_assert (passed);
2052
2053   gtk_widget_show (dialog);
2054
2055   /* Test that filter is unchanged when we switch folders */
2056
2057   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd);
2058   sleep_in_main_loop ();
2059   passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
2060   g_assert (passed);
2061
2062   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), base_dir);
2063   sleep_in_main_loop ();
2064
2065   g_signal_emit_by_name (impl->browse_path_bar, "path-clicked",
2066                          cwd_file,
2067                          base_dir_file,
2068                          FALSE);
2069   sleep_in_main_loop ();
2070   passed = passed && (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog)) == txt_filter);
2071   log_test (passed, "test_folder_switch_and_filters(): filter after changing folder");
2072   g_assert (passed);
2073
2074   /* cleanups */
2075   g_free (cwd);
2076   g_free (base_dir);
2077   g_object_unref (cwd_file);
2078   g_object_unref (base_dir_file);
2079
2080   gtk_widget_destroy (dialog);
2081
2082   log_test (passed, "test_folder_switch_and_filters(): all filter tests");
2083 }
2084 #endif
2085
2086 int
2087 main (int    argc,
2088       char **argv)
2089 {
2090   /* initialize test program */
2091   gtk_test_init (&argc, &argv);
2092
2093   /* Register tests */
2094
2095   setup_file_chooser_button_tests ();
2096 #ifdef BROKEN_TESTS
2097   setup_set_filename_tests ();
2098   setup_set_current_name_tests ();
2099
2100   g_test_add_func ("/GtkFileChooser/confirm_overwrite", test_confirm_overwrite);
2101   g_test_add_func ("/GtkFileChooser/action_widgets", test_action_widgets);
2102   g_test_add_func ("/GtkFileChooser/reload", test_reload);
2103   g_test_add_func ("/GtkFileChooser/button_folder_states", test_button_folder_states);
2104   g_test_add_func ("/GtkFileChooser/folder_switch_and_filters", test_folder_switch_and_filters);
2105 #endif
2106
2107   /* run and check selected tests */
2108   return g_test_run();
2109 }