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   gtk_main_quit ();
29 
30   return FALSE;
31 }
32 
33 static void
test_dialog_basic(void)34 test_dialog_basic (void)
35 {
36   GtkWidget *dialog;
37 
38   dialog = gtk_dialog_new();
39   g_assert (GTK_IS_DIALOG (dialog));
40 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
41   g_assert (gtk_dialog_get_action_area (GTK_DIALOG (dialog)) != NULL);
42 G_GNUC_END_IGNORE_DEPRECATIONS
43   g_assert (gtk_dialog_get_content_area (GTK_DIALOG (dialog)) != NULL);
44 
45   gtk_widget_destroy (dialog);
46 }
47 
48 static void
test_dialog_override_property(void)49 test_dialog_override_property (void)
50 {
51   GtkWidget *dialog;
52 
53   dialog = g_object_new (GTK_TYPE_DIALOG,
54 			 "type-hint", GDK_WINDOW_TYPE_HINT_UTILITY,
55 			 NULL);
56   g_assert (GTK_IS_DIALOG (dialog));
57   g_assert (gtk_window_get_type_hint (GTK_WINDOW (dialog)) == GDK_WINDOW_TYPE_HINT_UTILITY);
58 
59   gtk_widget_destroy (dialog);
60 }
61 
62 static void
test_message_dialog_basic(void)63 test_message_dialog_basic (void)
64 {
65   GtkWidget *dialog;
66 
67   dialog = gtk_message_dialog_new (NULL, 0,
68 				   GTK_MESSAGE_INFO,
69 				   GTK_BUTTONS_CLOSE,
70 				   "Do it hard !");
71   g_assert (GTK_IS_DIALOG (dialog));
72   gtk_widget_destroy (dialog);
73 }
74 
75 static void
test_about_dialog_basic(void)76 test_about_dialog_basic (void)
77 {
78   GtkWidget *dialog;
79 
80   dialog = gtk_about_dialog_new ();
81   g_assert (GTK_IS_ABOUT_DIALOG (dialog));
82   gtk_widget_destroy (dialog);
83 }
84 
85 static void
test_info_bar_basic(void)86 test_info_bar_basic (void)
87 {
88   GtkWidget *infobar;
89 
90   infobar = gtk_info_bar_new ();
91   g_assert (GTK_IS_INFO_BAR (infobar));
92   gtk_widget_destroy (infobar);
93 }
94 
95 static void
test_lock_button_basic(void)96 test_lock_button_basic (void)
97 {
98   GtkWidget *button;
99   GPermission *permission;
100 
101   permission = g_simple_permission_new (TRUE);
102   button = gtk_lock_button_new (permission);
103   g_assert (GTK_IS_LOCK_BUTTON (button));
104   gtk_widget_destroy (button);
105   g_object_unref (permission);
106 }
107 
108 static void
test_assistant_basic(void)109 test_assistant_basic (void)
110 {
111   GtkWidget *widget;
112 
113   widget = gtk_assistant_new ();
114   g_assert (GTK_IS_ASSISTANT (widget));
115   gtk_widget_destroy (widget);
116 }
117 
118 static void
test_scale_button_basic(void)119 test_scale_button_basic (void)
120 {
121   GtkWidget *widget;
122 
123   widget = gtk_scale_button_new (GTK_ICON_SIZE_MENU,
124 				 0, 100, 10, NULL);
125   g_assert (GTK_IS_SCALE_BUTTON (widget));
126   gtk_widget_destroy (widget);
127 }
128 
129 static void
test_volume_button_basic(void)130 test_volume_button_basic (void)
131 {
132   GtkWidget *widget;
133 
134   widget = gtk_volume_button_new ();
135   g_assert (GTK_IS_VOLUME_BUTTON (widget));
136   gtk_widget_destroy (widget);
137 }
138 
139 static void
test_statusbar_basic(void)140 test_statusbar_basic (void)
141 {
142   GtkWidget *widget;
143 
144   widget = gtk_statusbar_new ();
145   g_assert (GTK_IS_STATUSBAR (widget));
146   gtk_widget_destroy (widget);
147 }
148 
149 static void
test_search_bar_basic(void)150 test_search_bar_basic (void)
151 {
152   GtkWidget *widget;
153 
154   widget = gtk_search_bar_new ();
155   g_assert (GTK_IS_SEARCH_BAR (widget));
156   gtk_widget_destroy (widget);
157 }
158 
159 static void
test_action_bar_basic(void)160 test_action_bar_basic (void)
161 {
162   GtkWidget *widget;
163 
164   widget = gtk_action_bar_new ();
165   g_assert (GTK_IS_ACTION_BAR (widget));
166   gtk_widget_destroy (widget);
167 }
168 
169 static void
test_app_chooser_widget_basic(void)170 test_app_chooser_widget_basic (void)
171 {
172   GtkWidget *widget;
173 
174   widget = gtk_app_chooser_widget_new (NULL);
175   g_assert (GTK_IS_APP_CHOOSER_WIDGET (widget));
176   gtk_widget_destroy (widget);
177 }
178 
179 static void
test_app_chooser_dialog_basic(void)180 test_app_chooser_dialog_basic (void)
181 {
182   GtkWidget *widget;
183 
184   widget = gtk_app_chooser_dialog_new_for_content_type (NULL, 0, "text/plain");
185   g_assert (GTK_IS_APP_CHOOSER_DIALOG (widget));
186 
187   /* GtkAppChooserDialog bug, if destroyed before spinning
188    * the main context then app_chooser_online_get_default_ready_cb()
189    * will be eventually called and segfault.
190    */
191   g_timeout_add (500, main_loop_quit_cb, NULL);
192   gtk_main();
193   gtk_widget_destroy (widget);
194 }
195 
196 static void
test_color_chooser_dialog_basic(void)197 test_color_chooser_dialog_basic (void)
198 {
199   GtkWidget *widget;
200 
201   /* This test also tests the internal GtkColorEditor widget */
202   widget = gtk_color_chooser_dialog_new (NULL, NULL);
203   g_assert (GTK_IS_COLOR_CHOOSER_DIALOG (widget));
204   gtk_widget_destroy (widget);
205 }
206 
207 /* Avoid warnings from GVFS-RemoteVolumeMonitor */
208 static gboolean
ignore_gvfs_warning(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)209 ignore_gvfs_warning (const gchar *log_domain,
210 		     GLogLevelFlags log_level,
211 		     const gchar *message,
212 		     gpointer user_data)
213 {
214   if (g_strcmp0 (log_domain, "GVFS-RemoteVolumeMonitor") == 0)
215     return FALSE;
216 
217   return TRUE;
218 }
219 
220 static void
test_file_chooser_widget_basic(void)221 test_file_chooser_widget_basic (void)
222 {
223   GtkWidget *widget;
224 
225   /* This test also tests the internal GtkPathBar widget */
226   g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
227 
228   widget = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
229   g_assert (GTK_IS_FILE_CHOOSER_WIDGET (widget));
230 
231   /* XXX BUG:
232    *
233    * Spin the mainloop for a bit, this allows the file operations
234    * to complete, GtkFileChooserWidget has a bug where it leaks
235    * GtkTreeRowReferences to the internal shortcuts_model
236    *
237    * Since we assert all automated children are finalized we
238    * can catch this
239    */
240   g_timeout_add (100, main_loop_quit_cb, NULL);
241   gtk_main();
242 
243   gtk_widget_destroy (widget);
244 }
245 
246 static void
test_file_chooser_dialog_basic(void)247 test_file_chooser_dialog_basic (void)
248 {
249   GtkWidget *widget;
250 
251   g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
252 
253   widget = gtk_file_chooser_dialog_new ("The Dialog", NULL,
254 					GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
255 					"_OK", GTK_RESPONSE_OK,
256 					NULL);
257 
258   g_assert (GTK_IS_FILE_CHOOSER_DIALOG (widget));
259   g_timeout_add (100, main_loop_quit_cb, NULL);
260   gtk_main();
261 
262   gtk_widget_destroy (widget);
263 }
264 
265 static void
test_file_chooser_button_basic(void)266 test_file_chooser_button_basic (void)
267 {
268   GtkWidget *widget;
269 
270   g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);
271 
272   widget = gtk_file_chooser_button_new ("Choose a file !", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
273   g_assert (GTK_IS_FILE_CHOOSER_BUTTON (widget));
274   g_timeout_add (100, main_loop_quit_cb, NULL);
275   gtk_main();
276 
277   gtk_widget_destroy (widget);
278 }
279 
280 static void
test_font_button_basic(void)281 test_font_button_basic (void)
282 {
283   GtkWidget *widget;
284 
285   widget = gtk_font_button_new ();
286   g_assert (GTK_IS_FONT_BUTTON (widget));
287   gtk_widget_destroy (widget);
288 }
289 
290 static void
test_font_chooser_widget_basic(void)291 test_font_chooser_widget_basic (void)
292 {
293   GtkWidget *widget;
294 
295   widget = gtk_font_chooser_widget_new ();
296   g_assert (GTK_IS_FONT_CHOOSER_WIDGET (widget));
297   gtk_widget_destroy (widget);
298 }
299 
300 static void
test_font_chooser_dialog_basic(void)301 test_font_chooser_dialog_basic (void)
302 {
303   GtkWidget *widget;
304 
305   widget = gtk_font_chooser_dialog_new ("Choose a font !", NULL);
306   g_assert (GTK_IS_FONT_CHOOSER_DIALOG (widget));
307   gtk_widget_destroy (widget);
308 }
309 
310 static void
test_recent_chooser_widget_basic(void)311 test_recent_chooser_widget_basic (void)
312 {
313   GtkWidget *widget;
314 
315   widget = gtk_recent_chooser_widget_new ();
316   g_assert (GTK_IS_RECENT_CHOOSER_WIDGET (widget));
317   gtk_widget_destroy (widget);
318 }
319 
320 #ifdef HAVE_UNIX_PRINT_WIDGETS
321 static void
test_page_setup_unix_dialog_basic(void)322 test_page_setup_unix_dialog_basic (void)
323 {
324   GtkWidget *widget;
325 
326   widget = gtk_page_setup_unix_dialog_new ("Setup your Page !", NULL);
327   g_assert (GTK_IS_PAGE_SETUP_UNIX_DIALOG (widget));
328   gtk_widget_destroy (widget);
329 }
330 
331 static void
test_print_unix_dialog_basic(void)332 test_print_unix_dialog_basic (void)
333 {
334   GtkWidget *widget;
335 
336   widget = gtk_print_unix_dialog_new ("Go Print !", NULL);
337   g_assert (GTK_IS_PRINT_UNIX_DIALOG (widget));
338   gtk_widget_destroy (widget);
339 }
340 #endif
341 
342 int
main(int argc,char ** argv)343 main (int argc, char **argv)
344 {
345   gchar *schema_dir;
346 
347   /* These must be set before before gtk_test_init */
348   g_setenv ("GIO_USE_VFS", "local", TRUE);
349   g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
350 
351   /* initialize test program */
352   gtk_test_init (&argc, &argv);
353 
354   /* g_test_build_filename must be called after gtk_test_init */
355   schema_dir = g_test_build_filename (G_TEST_BUILT, "", NULL);
356   if (g_getenv ("GTK_TEST_MESON") == NULL)
357     g_setenv ("GSETTINGS_SCHEMA_DIR", schema_dir, TRUE);
358 
359   /* This environment variable cooperates with gtk_widget_destroy()
360    * to assert that all automated compoenents are properly finalized
361    * when a given composite widget is destroyed.
362    */
363   g_assert (g_setenv ("GTK_WIDGET_ASSERT_COMPONENTS", "1", TRUE));
364 
365   g_test_add_func ("/Template/GtkDialog/Basic", test_dialog_basic);
366   g_test_add_func ("/Template/GtkDialog/OverrideProperty", test_dialog_override_property);
367   g_test_add_func ("/Template/GtkMessageDialog/Basic", test_message_dialog_basic);
368   g_test_add_func ("/Template/GtkAboutDialog/Basic", test_about_dialog_basic);
369   g_test_add_func ("/Template/GtkInfoBar/Basic", test_info_bar_basic);
370   g_test_add_func ("/Template/GtkLockButton/Basic", test_lock_button_basic);
371   g_test_add_func ("/Template/GtkAssistant/Basic", test_assistant_basic);
372   g_test_add_func ("/Template/GtkScaleButton/Basic", test_scale_button_basic);
373   g_test_add_func ("/Template/GtkVolumeButton/Basic", test_volume_button_basic);
374   g_test_add_func ("/Template/GtkStatusBar/Basic", test_statusbar_basic);
375   g_test_add_func ("/Template/GtkSearchBar/Basic", test_search_bar_basic);
376   g_test_add_func ("/Template/GtkActionBar/Basic", test_action_bar_basic);
377   g_test_add_func ("/Template/GtkAppChooserWidget/Basic", test_app_chooser_widget_basic);
378   g_test_add_func ("/Template/GtkAppChooserDialog/Basic", test_app_chooser_dialog_basic);
379   g_test_add_func ("/Template/GtkColorChooserDialog/Basic", test_color_chooser_dialog_basic);
380   g_test_add_func ("/Template/GtkFileChooserWidget/Basic", test_file_chooser_widget_basic);
381   g_test_add_func ("/Template/GtkFileChooserDialog/Basic", test_file_chooser_dialog_basic);
382   g_test_add_func ("/Template/GtkFileChooserButton/Basic", test_file_chooser_button_basic);
383   g_test_add_func ("/Template/GtkFontButton/Basic", test_font_button_basic);
384   g_test_add_func ("/Template/GtkFontChooserWidget/Basic", test_font_chooser_widget_basic);
385   g_test_add_func ("/Template/GtkFontChooserDialog/Basic", test_font_chooser_dialog_basic);
386   g_test_add_func ("/Template/GtkRecentChooserWidget/Basic", test_recent_chooser_widget_basic);
387 
388 #ifdef HAVE_UNIX_PRINT_WIDGETS
389   g_test_add_func ("/Template/UnixPrint/GtkPageSetupUnixDialog/Basic", test_page_setup_unix_dialog_basic);
390   g_test_add_func ("/Template/UnixPrint/GtkPrintUnixDialog/Basic", test_print_unix_dialog_basic);
391 #endif
392 
393   g_free (schema_dir);
394 
395   return g_test_run();
396 }
397