1 /* templates.c
2  * Copyright (C) 2013 Openismus GmbH
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Tristan Van Berkom <tristanvb@openismus.com>
18  */
19 #include <gtk/gtk.h>
20 
21 #ifdef HAVE_UNIX_PRINT_WIDGETS
22 #  include <gtk/gtkunixprint.h>
23 #endif
24 
25 static gboolean
main_loop_quit_cb(gpointer data)26 main_loop_quit_cb (gpointer data)
27 {
28   gboolean *done = data;
29 
30   *done = TRUE;
31 
32   g_main_context_wakeup (NULL);
33 
34   return FALSE;
35 }
36 
37 static void
show_and_wait(GtkWidget * widget)38 show_and_wait (GtkWidget *widget)
39 {
40   gboolean done = FALSE;
41 
42   g_timeout_add (500, main_loop_quit_cb, &done);
43   gtk_widget_show (widget);
44   while (!done)
45     g_main_context_iteration (NULL, FALSE);
46 }
47 
48 static void
test_dialog_basic(void)49 test_dialog_basic (void)
50 {
51   GtkWidget *dialog;
52 
53   dialog = gtk_dialog_new ();
54   g_assert_true (GTK_IS_DIALOG (dialog));
55   g_assert_nonnull (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
56 
57   gtk_window_destroy (GTK_WINDOW (dialog));
58 }
59 
60 static void
test_dialog_override_property(void)61 test_dialog_override_property (void)
62 {
63   GtkWidget *dialog;
64 
65   dialog = g_object_new (GTK_TYPE_DIALOG,
66                          "use-header-bar", 1,
67                          NULL);
68   g_assert_true (GTK_IS_DIALOG (dialog));
69 
70   gtk_window_destroy (GTK_WINDOW (dialog));
71 }
72 
73 static void
test_message_dialog_basic(void)74 test_message_dialog_basic (void)
75 {
76   GtkWidget *dialog;
77 
78   dialog = gtk_message_dialog_new (NULL, 0,
79                                    GTK_MESSAGE_INFO,
80                                    GTK_BUTTONS_CLOSE,
81                                    "Do it hard !");
82   g_assert_true (GTK_IS_DIALOG (dialog));
83   gtk_window_destroy (GTK_WINDOW (dialog));
84 }
85 
86 static void
test_about_dialog_basic(void)87 test_about_dialog_basic (void)
88 {
89   GtkWidget *dialog;
90 
91   dialog = gtk_about_dialog_new ();
92   g_assert_true (GTK_IS_ABOUT_DIALOG (dialog));
93   gtk_window_destroy (GTK_WINDOW (dialog));
94 }
95 
96 static void
test_about_dialog_show(void)97 test_about_dialog_show (void)
98 {
99   GtkWidget *dialog;
100 
101   dialog = gtk_about_dialog_new ();
102   g_assert_true (GTK_IS_ABOUT_DIALOG (dialog));
103   show_and_wait (dialog);
104   gtk_window_destroy (GTK_WINDOW (dialog));
105 }
106 
107 static void
test_info_bar_basic(void)108 test_info_bar_basic (void)
109 {
110   GtkWidget *infobar;
111 
112   infobar = gtk_info_bar_new ();
113   g_assert_true (GTK_IS_INFO_BAR (infobar));
114   g_object_unref (g_object_ref_sink (infobar));
115 }
116 
117 static void
test_lock_button_basic(void)118 test_lock_button_basic (void)
119 {
120   GtkWidget *button;
121   GPermission *permission;
122 
123   permission = g_simple_permission_new (TRUE);
124   button = gtk_lock_button_new (permission);
125   g_assert_true (GTK_IS_LOCK_BUTTON (button));
126   g_object_unref (g_object_ref_sink (button));
127   g_object_unref (permission);
128 }
129 
130 static void
test_assistant_basic(void)131 test_assistant_basic (void)
132 {
133   GtkWidget *widget;
134 
135   widget = gtk_assistant_new ();
136   g_assert_true (GTK_IS_ASSISTANT (widget));
137   gtk_window_destroy (GTK_WINDOW (widget));
138 }
139 
140 static void
test_assistant_show(void)141 test_assistant_show (void)
142 {
143   GtkWidget *widget;
144 
145   widget = gtk_assistant_new ();
146   g_assert_true (GTK_IS_ASSISTANT (widget));
147   show_and_wait (widget);
148   gtk_window_destroy (GTK_WINDOW (widget));
149 }
150 
151 static void
test_scale_button_basic(void)152 test_scale_button_basic (void)
153 {
154   GtkWidget *widget;
155 
156   widget = gtk_scale_button_new (0, 100, 10, NULL);
157   g_assert_true (GTK_IS_SCALE_BUTTON (widget));
158   g_object_unref (g_object_ref_sink (widget));
159 }
160 
161 static void
test_volume_button_basic(void)162 test_volume_button_basic (void)
163 {
164   GtkWidget *widget;
165 
166   widget = gtk_volume_button_new ();
167   g_assert_true (GTK_IS_VOLUME_BUTTON (widget));
168   g_object_unref (g_object_ref_sink (widget));
169 }
170 
171 static void
test_statusbar_basic(void)172 test_statusbar_basic (void)
173 {
174   GtkWidget *widget;
175 
176   widget = gtk_statusbar_new ();
177   g_assert_true (GTK_IS_STATUSBAR (widget));
178   g_object_unref (g_object_ref_sink (widget));
179 }
180 
181 static void
test_search_bar_basic(void)182 test_search_bar_basic (void)
183 {
184   GtkWidget *widget;
185 
186   widget = gtk_search_bar_new ();
187   g_assert_true (GTK_IS_SEARCH_BAR (widget));
188   g_object_unref (g_object_ref_sink (widget));
189 }
190 
191 static void
test_action_bar_basic(void)192 test_action_bar_basic (void)
193 {
194   GtkWidget *widget;
195 
196   widget = gtk_action_bar_new ();
197   g_assert_true (GTK_IS_ACTION_BAR (widget));
198   g_object_unref (g_object_ref_sink (widget));
199 }
200 
201 static void
test_app_chooser_widget_basic(void)202 test_app_chooser_widget_basic (void)
203 {
204   GtkWidget *widget;
205 
206   widget = gtk_app_chooser_widget_new (NULL);
207   g_assert_true (GTK_IS_APP_CHOOSER_WIDGET (widget));
208   g_object_unref (g_object_ref_sink (widget));
209 }
210 
211 static void
test_app_chooser_dialog_basic(void)212 test_app_chooser_dialog_basic (void)
213 {
214   GtkWidget *widget;
215   gboolean done = FALSE;
216 
217   widget = gtk_app_chooser_dialog_new_for_content_type (NULL, 0, "text/plain");
218   g_assert_true (GTK_IS_APP_CHOOSER_DIALOG (widget));
219 
220   /* GtkAppChooserDialog bug, if destroyed before spinning
221    * the main context then app_chooser_online_get_default_ready_cb()
222    * will be eventually called and segfault.
223    */
224   g_timeout_add (500, main_loop_quit_cb, &done);
225   while (!done)
226     g_main_context_iteration (NULL,  TRUE);
227   gtk_window_destroy (GTK_WINDOW (widget));
228 }
229 
230 static void
test_color_chooser_dialog_basic(void)231 test_color_chooser_dialog_basic (void)
232 {
233   GtkWidget *widget;
234 
235   /* This test also tests the internal GtkColorEditor widget */
236   widget = gtk_color_chooser_dialog_new (NULL, NULL);
237   g_assert_true (GTK_IS_COLOR_CHOOSER_DIALOG (widget));
238   gtk_window_destroy (GTK_WINDOW (widget));
239 }
240 
241 static void
test_color_chooser_dialog_show(void)242 test_color_chooser_dialog_show (void)
243 {
244   GtkWidget *widget;
245 
246   /* This test also tests the internal GtkColorEditor widget */
247   widget = gtk_color_chooser_dialog_new (NULL, NULL);
248   g_assert_true (GTK_IS_COLOR_CHOOSER_DIALOG (widget));
249   show_and_wait (widget);
250   gtk_window_destroy (GTK_WINDOW (widget));
251 }
252 
253 /* Avoid warnings from GVFS-RemoteVolumeMonitor */
254 static gboolean
ignore_gvfs_warning(const char * log_domain,GLogLevelFlags log_level,const char * message,gpointer user_data)255 ignore_gvfs_warning (const char *log_domain,
256                      GLogLevelFlags log_level,
257                      const char *message,
258                      gpointer user_data)
259 {
260   if (g_strcmp0 (log_domain, "GVFS-RemoteVolumeMonitor") == 0)
261     return FALSE;
262 
263   return TRUE;
264 }
265 
266 static void
test_file_chooser_widget_basic(void)267 test_file_chooser_widget_basic (void)
268 {
269   GtkWidget *widget;
270   gboolean done = FALSE;
271 
272   /* This test also tests the internal GtkPathBar widget */
273   g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
274 
275   widget = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
276   g_assert_true (GTK_IS_FILE_CHOOSER_WIDGET (widget));
277 
278   /* XXX BUG:
279    *
280    * Spin the mainloop for a bit, this allows the file operations
281    * to complete, GtkFileChooserWidget has a bug where it leaks
282    * GtkTreeRowReferences to the internal shortcuts_model
283    *
284    * Since we assert all automated children are finalized we
285    * can catch this
286    */
287   g_timeout_add (100, main_loop_quit_cb, &done);
288   while (!done)
289     g_main_context_iteration (NULL,  TRUE);
290 
291   g_object_unref (g_object_ref_sink (widget));
292 }
293 
294 static void
test_file_chooser_dialog_basic(void)295 test_file_chooser_dialog_basic (void)
296 {
297   GtkWidget *widget;
298   gboolean done;
299 
300   g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
301 
302   widget = gtk_file_chooser_dialog_new ("The Dialog", NULL,
303                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
304                                         "_OK", GTK_RESPONSE_OK,
305                                         NULL);
306 
307   g_assert_true (GTK_IS_FILE_CHOOSER_DIALOG (widget));
308   done = FALSE;
309   g_timeout_add (100, main_loop_quit_cb, &done);
310   while (!done)
311     g_main_context_iteration (NULL,  TRUE);
312 
313   gtk_window_destroy (GTK_WINDOW (widget));
314 }
315 
316 static void
test_file_chooser_dialog_show(void)317 test_file_chooser_dialog_show (void)
318 {
319   GtkWidget *widget;
320 
321   g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
322 
323   widget = gtk_file_chooser_dialog_new ("The Dialog", NULL,
324                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
325                                         "_OK", GTK_RESPONSE_OK,
326                                         NULL);
327 
328   g_assert_true (GTK_IS_FILE_CHOOSER_DIALOG (widget));
329   show_and_wait (widget);
330   gtk_window_destroy (GTK_WINDOW (widget));
331 }
332 
333 static void
test_font_button_basic(void)334 test_font_button_basic (void)
335 {
336   GtkWidget *widget;
337 
338   widget = gtk_font_button_new ();
339   g_assert_true (GTK_IS_FONT_BUTTON (widget));
340   g_object_unref (g_object_ref_sink (widget));
341 }
342 
343 static void
test_font_chooser_widget_basic(void)344 test_font_chooser_widget_basic (void)
345 {
346   GtkWidget *widget;
347 
348   widget = gtk_font_chooser_widget_new ();
349   g_assert_true (GTK_IS_FONT_CHOOSER_WIDGET (widget));
350   g_object_unref (g_object_ref_sink (widget));
351 }
352 
353 static void
test_font_chooser_dialog_basic(void)354 test_font_chooser_dialog_basic (void)
355 {
356   GtkWidget *widget;
357 
358   widget = gtk_font_chooser_dialog_new ("Choose a font !", NULL);
359   g_assert_true (GTK_IS_FONT_CHOOSER_DIALOG (widget));
360   gtk_window_destroy (GTK_WINDOW (widget));
361 }
362 
363 static void
test_font_chooser_dialog_show(void)364 test_font_chooser_dialog_show (void)
365 {
366   GtkWidget *widget;
367 
368   widget = gtk_font_chooser_dialog_new ("Choose a font !", NULL);
369   g_assert_true (GTK_IS_FONT_CHOOSER_DIALOG (widget));
370   show_and_wait (widget);
371   gtk_window_destroy (GTK_WINDOW (widget));
372 }
373 
374 #ifdef HAVE_UNIX_PRINT_WIDGETS
375 static void
test_page_setup_unix_dialog_basic(void)376 test_page_setup_unix_dialog_basic (void)
377 {
378   GtkWidget *widget;
379 
380   widget = gtk_page_setup_unix_dialog_new ("Setup your Page !", NULL);
381   g_assert_true (GTK_IS_PAGE_SETUP_UNIX_DIALOG (widget));
382   gtk_window_destroy (GTK_WINDOW (widget));
383 }
384 
385 static void
test_page_setup_unix_dialog_show(void)386 test_page_setup_unix_dialog_show (void)
387 {
388   GtkWidget *widget;
389 
390   widget = gtk_page_setup_unix_dialog_new ("Setup your Page !", NULL);
391   g_assert_true (GTK_IS_PAGE_SETUP_UNIX_DIALOG (widget));
392   show_and_wait (widget);
393   gtk_window_destroy (GTK_WINDOW (widget));
394 }
395 
396 static void
test_print_unix_dialog_basic(void)397 test_print_unix_dialog_basic (void)
398 {
399   GtkWidget *widget;
400 
401   widget = gtk_print_unix_dialog_new ("Go Print !", NULL);
402   g_assert_true (GTK_IS_PRINT_UNIX_DIALOG (widget));
403   gtk_window_destroy (GTK_WINDOW (widget));
404 }
405 
406 static void
test_print_unix_dialog_show(void)407 test_print_unix_dialog_show (void)
408 {
409   GtkWidget *widget;
410 
411   widget = gtk_print_unix_dialog_new ("Go Print !", NULL);
412   g_assert_true (GTK_IS_PRINT_UNIX_DIALOG (widget));
413   show_and_wait (widget);
414   gtk_window_destroy (GTK_WINDOW (widget));
415 }
416 #endif
417 
418 int
main(int argc,char ** argv)419 main (int argc, char **argv)
420 {
421   /* These must be set before gtk_test_init */
422   g_setenv ("GIO_USE_VFS", "local", TRUE);
423   g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
424 
425   /* initialize test program */
426   gtk_test_init (&argc, &argv);
427 
428   /* This environment variable cooperates with widget dispose()
429    * to assert that all automated compoenents are properly finalized
430    * when a given composite widget is destroyed.
431    */
432   g_assert_true (g_setenv ("GTK_WIDGET_ASSERT_COMPONENTS", "1", TRUE));
433 
434   g_test_add_func ("/template/GtkDialog/basic", test_dialog_basic);
435   g_test_add_func ("/template/GtkDialog/OverrideProperty", test_dialog_override_property);
436   g_test_add_func ("/template/GtkMessageDialog/basic", test_message_dialog_basic);
437   g_test_add_func ("/template/GtkAboutDialog/basic", test_about_dialog_basic);
438   g_test_add_func ("/template/GtkAboutDialog/show", test_about_dialog_show);
439   g_test_add_func ("/template/GtkInfoBar/basic", test_info_bar_basic);
440   g_test_add_func ("/template/GtkLockButton/basic", test_lock_button_basic);
441   g_test_add_func ("/template/GtkAssistant/basic", test_assistant_basic);
442   g_test_add_func ("/template/GtkAssistant/show", test_assistant_show);
443   g_test_add_func ("/template/GtkScaleButton/basic", test_scale_button_basic);
444   g_test_add_func ("/template/GtkVolumeButton/basic", test_volume_button_basic);
445   g_test_add_func ("/template/GtkStatusBar/basic", test_statusbar_basic);
446   g_test_add_func ("/template/GtkSearchBar/basic", test_search_bar_basic);
447   g_test_add_func ("/template/GtkActionBar/basic", test_action_bar_basic);
448   g_test_add_func ("/template/GtkAppChooserWidget/basic", test_app_chooser_widget_basic);
449   g_test_add_func ("/template/GtkAppChooserDialog/basic", test_app_chooser_dialog_basic);
450   g_test_add_func ("/template/GtkColorChooserDialog/basic", test_color_chooser_dialog_basic);
451   g_test_add_func ("/template/GtkColorChooserDialog/show", test_color_chooser_dialog_show);
452   g_test_add_func ("/template/GtkFileChooserWidget/basic", test_file_chooser_widget_basic);
453   g_test_add_func ("/template/GtkFileChooserDialog/basic", test_file_chooser_dialog_basic);
454   g_test_add_func ("/template/GtkFileChooserDialog/show", test_file_chooser_dialog_show);
455   g_test_add_func ("/template/GtkFontButton/basic", test_font_button_basic);
456   g_test_add_func ("/template/GtkFontChooserWidget/basic", test_font_chooser_widget_basic);
457   g_test_add_func ("/template/GtkFontChooserDialog/basic", test_font_chooser_dialog_basic);
458   g_test_add_func ("/template/GtkFontChooserDialog/show", test_font_chooser_dialog_show);
459 
460 #ifdef HAVE_UNIX_PRINT_WIDGETS
461   g_test_add_func ("/template/GtkPageSetupUnixDialog/basic", test_page_setup_unix_dialog_basic);
462   g_test_add_func ("/template/GtkPageSetupUnixDialog/show", test_page_setup_unix_dialog_show);
463   g_test_add_func ("/template/GtkPrintUnixDialog/basic", test_print_unix_dialog_basic);
464   g_test_add_func ("/template/GtkPrintUnixDialog/show", test_print_unix_dialog_show);
465 #endif
466 
467   return g_test_run();
468 }
469