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
25static gboolean
26main_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
37static void
38show_and_wait (GtkWidget *widget)
39{
40 gboolean done = FALSE;
41
42 g_timeout_add (interval: 500, function: main_loop_quit_cb, data: &done);
43 gtk_widget_show (widget);
44 while (!done)
45 g_main_context_iteration (NULL, FALSE);
46}
47
48static void
49test_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
60static void
61test_dialog_override_property (void)
62{
63 GtkWidget *dialog;
64
65 dialog = g_object_new (GTK_TYPE_DIALOG,
66 first_property_name: "use-header-bar", 1,
67 NULL);
68 g_assert_true (GTK_IS_DIALOG (dialog));
69
70 gtk_window_destroy (GTK_WINDOW (dialog));
71}
72
73static void
74test_message_dialog_basic (void)
75{
76 GtkWidget *dialog;
77
78 dialog = gtk_message_dialog_new (NULL, flags: 0,
79 type: GTK_MESSAGE_INFO,
80 buttons: GTK_BUTTONS_CLOSE,
81 message_format: "Do it hard !");
82 g_assert_true (GTK_IS_DIALOG (dialog));
83 gtk_window_destroy (GTK_WINDOW (dialog));
84}
85
86static void
87test_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
96static void
97test_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 (widget: dialog);
104 gtk_window_destroy (GTK_WINDOW (dialog));
105}
106
107static void
108test_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
117static void
118test_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 (object: permission);
128}
129
130static void
131test_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
140static void
141test_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
151static void
152test_scale_button_basic (void)
153{
154 GtkWidget *widget;
155
156 widget = gtk_scale_button_new (min: 0, max: 100, step: 10, NULL);
157 g_assert_true (GTK_IS_SCALE_BUTTON (widget));
158 g_object_unref (g_object_ref_sink (widget));
159}
160
161static void
162test_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
171static void
172test_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
181static void
182test_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
191static void
192test_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
201static void
202test_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
211static void
212test_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, flags: 0, content_type: "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 (interval: 500, function: main_loop_quit_cb, data: &done);
225 while (!done)
226 g_main_context_iteration (NULL, TRUE);
227 gtk_window_destroy (GTK_WINDOW (widget));
228}
229
230static void
231test_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
241static void
242test_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 */
254static gboolean
255ignore_gvfs_warning (const char *log_domain,
256 GLogLevelFlags log_level,
257 const char *message,
258 gpointer user_data)
259{
260 if (g_strcmp0 (str1: log_domain, str2: "GVFS-RemoteVolumeMonitor") == 0)
261 return FALSE;
262
263 return TRUE;
264}
265
266static void
267test_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 (log_func: ignore_gvfs_warning, NULL);
274
275 widget = gtk_file_chooser_widget_new (action: 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 (interval: 100, function: main_loop_quit_cb, data: &done);
288 while (!done)
289 g_main_context_iteration (NULL, TRUE);
290
291 g_object_unref (g_object_ref_sink (widget));
292}
293
294static void
295test_file_chooser_dialog_basic (void)
296{
297 GtkWidget *widget;
298 gboolean done;
299
300 g_test_log_set_fatal_handler (log_func: ignore_gvfs_warning, NULL);
301
302 widget = gtk_file_chooser_dialog_new (title: "The Dialog", NULL,
303 action: GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
304 first_button_text: "_OK", GTK_RESPONSE_OK,
305 NULL);
306
307 g_assert_true (GTK_IS_FILE_CHOOSER_DIALOG (widget));
308 done = FALSE;
309 g_timeout_add (interval: 100, function: main_loop_quit_cb, data: &done);
310 while (!done)
311 g_main_context_iteration (NULL, TRUE);
312
313 gtk_window_destroy (GTK_WINDOW (widget));
314}
315
316static void
317test_file_chooser_dialog_show (void)
318{
319 GtkWidget *widget;
320
321 g_test_log_set_fatal_handler (log_func: ignore_gvfs_warning, NULL);
322
323 widget = gtk_file_chooser_dialog_new (title: "The Dialog", NULL,
324 action: GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
325 first_button_text: "_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
333static void
334test_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
343static void
344test_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
353static void
354test_font_chooser_dialog_basic (void)
355{
356 GtkWidget *widget;
357
358 widget = gtk_font_chooser_dialog_new (title: "Choose a font !", NULL);
359 g_assert_true (GTK_IS_FONT_CHOOSER_DIALOG (widget));
360 gtk_window_destroy (GTK_WINDOW (widget));
361}
362
363static void
364test_font_chooser_dialog_show (void)
365{
366 GtkWidget *widget;
367
368 widget = gtk_font_chooser_dialog_new (title: "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
375static void
376test_page_setup_unix_dialog_basic (void)
377{
378 GtkWidget *widget;
379
380 widget = gtk_page_setup_unix_dialog_new (title: "Setup your Page !", NULL);
381 g_assert_true (GTK_IS_PAGE_SETUP_UNIX_DIALOG (widget));
382 gtk_window_destroy (GTK_WINDOW (widget));
383}
384
385static void
386test_page_setup_unix_dialog_show (void)
387{
388 GtkWidget *widget;
389
390 widget = gtk_page_setup_unix_dialog_new (title: "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
396static void
397test_print_unix_dialog_basic (void)
398{
399 GtkWidget *widget;
400
401 widget = gtk_print_unix_dialog_new (title: "Go Print !", NULL);
402 g_assert_true (GTK_IS_PRINT_UNIX_DIALOG (widget));
403 gtk_window_destroy (GTK_WINDOW (widget));
404}
405
406static void
407test_print_unix_dialog_show (void)
408{
409 GtkWidget *widget;
410
411 widget = gtk_print_unix_dialog_new (title: "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
418int
419main (int argc, char **argv)
420{
421 /* These must be set before gtk_test_init */
422 g_setenv (variable: "GIO_USE_VFS", value: "local", TRUE);
423 g_setenv (variable: "GSETTINGS_BACKEND", value: "memory", TRUE);
424
425 /* initialize test program */
426 gtk_test_init (argcp: &argc, argvp: &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 (testpath: "/template/GtkDialog/basic", test_func: test_dialog_basic);
435 g_test_add_func (testpath: "/template/GtkDialog/OverrideProperty", test_func: test_dialog_override_property);
436 g_test_add_func (testpath: "/template/GtkMessageDialog/basic", test_func: test_message_dialog_basic);
437 g_test_add_func (testpath: "/template/GtkAboutDialog/basic", test_func: test_about_dialog_basic);
438 g_test_add_func (testpath: "/template/GtkAboutDialog/show", test_func: test_about_dialog_show);
439 g_test_add_func (testpath: "/template/GtkInfoBar/basic", test_func: test_info_bar_basic);
440 g_test_add_func (testpath: "/template/GtkLockButton/basic", test_func: test_lock_button_basic);
441 g_test_add_func (testpath: "/template/GtkAssistant/basic", test_func: test_assistant_basic);
442 g_test_add_func (testpath: "/template/GtkAssistant/show", test_func: test_assistant_show);
443 g_test_add_func (testpath: "/template/GtkScaleButton/basic", test_func: test_scale_button_basic);
444 g_test_add_func (testpath: "/template/GtkVolumeButton/basic", test_func: test_volume_button_basic);
445 g_test_add_func (testpath: "/template/GtkStatusBar/basic", test_func: test_statusbar_basic);
446 g_test_add_func (testpath: "/template/GtkSearchBar/basic", test_func: test_search_bar_basic);
447 g_test_add_func (testpath: "/template/GtkActionBar/basic", test_func: test_action_bar_basic);
448 g_test_add_func (testpath: "/template/GtkAppChooserWidget/basic", test_func: test_app_chooser_widget_basic);
449 g_test_add_func (testpath: "/template/GtkAppChooserDialog/basic", test_func: test_app_chooser_dialog_basic);
450 g_test_add_func (testpath: "/template/GtkColorChooserDialog/basic", test_func: test_color_chooser_dialog_basic);
451 g_test_add_func (testpath: "/template/GtkColorChooserDialog/show", test_func: test_color_chooser_dialog_show);
452 g_test_add_func (testpath: "/template/GtkFileChooserWidget/basic", test_func: test_file_chooser_widget_basic);
453 g_test_add_func (testpath: "/template/GtkFileChooserDialog/basic", test_func: test_file_chooser_dialog_basic);
454 g_test_add_func (testpath: "/template/GtkFileChooserDialog/show", test_func: test_file_chooser_dialog_show);
455 g_test_add_func (testpath: "/template/GtkFontButton/basic", test_func: test_font_button_basic);
456 g_test_add_func (testpath: "/template/GtkFontChooserWidget/basic", test_func: test_font_chooser_widget_basic);
457 g_test_add_func (testpath: "/template/GtkFontChooserDialog/basic", test_func: test_font_chooser_dialog_basic);
458 g_test_add_func (testpath: "/template/GtkFontChooserDialog/show", test_func: test_font_chooser_dialog_show);
459
460#ifdef HAVE_UNIX_PRINT_WIDGETS
461 g_test_add_func (testpath: "/template/GtkPageSetupUnixDialog/basic", test_func: test_page_setup_unix_dialog_basic);
462 g_test_add_func (testpath: "/template/GtkPageSetupUnixDialog/show", test_func: test_page_setup_unix_dialog_show);
463 g_test_add_func (testpath: "/template/GtkPrintUnixDialog/basic", test_func: test_print_unix_dialog_basic);
464 g_test_add_func (testpath: "/template/GtkPrintUnixDialog/show", test_func: test_print_unix_dialog_show);
465#endif
466
467 return g_test_run();
468}
469

source code of gtk/testsuite/gtk/templates.c