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