Debian Codegraph @

testsuite/gtk/window.c

raw
1 #include <gtk/gtk.h>
2
3 #ifdef GDK_WINDOWING_X11
4 #include <gdk/gdkx.h>
5 #include <X11/Xatom.h>
6 #endif
7
8 static gboolean interactive = FALSE;
9
10 static gboolean
11 stop_main (gpointer data)
12 {
13 gtk_main_quit ();
14
15 return G_SOURCE_REMOVE;
16 }
17
18 static gboolean
19 on_draw (GtkWidget *widget, cairo_t *cr)
20 {
21 gint i, j;
22
23 for (i = 0; 20 * i < gtk_widget_get_allocated_width (widget); i++)
24 {
25 for (j = 0; 20 * j < gtk_widget_get_allocated_height (widget); j++)
26 {
27 if ((i + j) % 2 == 1)
28 cairo_set_source_rgb (cr, 1., 1., 1.);
29 else
30 cairo_set_source_rgb (cr, 0., 0., 0.);
31
32 cairo_rectangle (cr, 20. * i, 20. *j, 20., 20.);
33 cairo_fill (cr);
34 }
35 }
36
37 return FALSE;
38 }
39
40 static gboolean
41 on_keypress (GtkWidget *widget)
42 {
43 gtk_main_quit ();
44
45 return TRUE;
46 }
47
48 static void
49 test_default_size (void)
50 {
51 GtkWidget *window;
52 GtkWidget *box;
53 gint w, h;
54
55 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
56 g_signal_connect (window, "draw", G_CALLBACK (on_draw), NULL);
57 if (interactive)
58 g_signal_connect (window, "key-press-event", G_CALLBACK (on_keypress), NULL);
59
60 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
61 gtk_container_add (GTK_CONTAINER (window), box);
62
63 /* check that default size is unset initially */
64 gtk_window_get_default_size (GTK_WINDOW (window), &w, &h);
65 g_assert_cmpint (w, ==, -1);
66 g_assert_cmpint (h, ==, -1);
67
68 /* check that setting default size before realize works */
69 gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
70
71 gtk_window_get_default_size (GTK_WINDOW (window), &w, &h);
72 g_assert_cmpint (w, ==, 300);
73 g_assert_cmpint (h, ==, 300);
74
75 /* check that the window size is also reported accordingly */
76 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
77 g_assert_cmpint (w, ==, 300);
78 g_assert_cmpint (h, ==, 300);
79
80 gtk_widget_show_all (window);
81
82 if (!interactive)
83 g_timeout_add (200, stop_main, NULL);
84 gtk_main ();
85
86 /* check that the window and its content actually gets the right size */
87 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
88 g_assert_cmpint (w, ==, 300);
89 g_assert_cmpint (h, ==, 300);
90
91 g_assert_cmpint (gtk_widget_get_allocated_width (box), ==, 300);
92 g_assert_cmpint (gtk_widget_get_allocated_height (box), ==, 300);
93
94 /* check that setting default size after the fact does not change
95 * window size
96 */
97 gtk_window_set_default_size (GTK_WINDOW (window), 100, 600);
98 gtk_window_get_default_size (GTK_WINDOW (window), &w, &h);
99 g_assert_cmpint (w, ==, 100);
100 g_assert_cmpint (h, ==, 600);
101
102 if (!interactive)
103 g_timeout_add (200, stop_main, NULL);
104 gtk_main ();
105
106 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
107 g_assert_cmpint (w, ==, 300);
108 g_assert_cmpint (h, ==, 300);
109
110 /* check that even hide/show does not pull in the new default */
111 gtk_widget_hide (window);
112 gtk_widget_show (window);
113
114 if (!interactive)
115 g_timeout_add (200, stop_main, NULL);
116 gtk_main ();
117
118 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
119 g_assert_cmpint (w, ==, 300);
120 g_assert_cmpint (h, ==, 300);
121
122 gtk_widget_destroy (window);
123 }
124
125 static void
126 test_resize (void)
127 {
128 GtkWidget *window;
129 GtkWidget *box;
130 gint w, h;
131
132 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
133 g_signal_connect (window, "draw", G_CALLBACK (on_draw), NULL);
134 if (interactive)
135 g_signal_connect (window, "key-press-event", G_CALLBACK (on_keypress), NULL);
136
137 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
138 gtk_container_add (GTK_CONTAINER (window), box);
139
140 /* test that resize before show overrides default size */
141 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
142
143 gtk_window_resize (GTK_WINDOW (window), 1, 1);
144
145 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
146 g_assert_cmpint (w, ==, 1);
147 g_assert_cmpint (h, ==, 1);
148
149 gtk_window_resize (GTK_WINDOW (window), 400, 200);
150
151 gtk_widget_show_all (window);
152
153 if (!interactive)
154 g_timeout_add (200, stop_main, NULL);
155 gtk_main ();
156
157 /* test that resize before show works */
158 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
159 g_assert_cmpint (w, ==, 400);
160 g_assert_cmpint (h, ==, 200);
161
162 /* test that resize after show works, both
163 * for making things bigger and for making things
164 * smaller
165 */
166 gtk_window_resize (GTK_WINDOW (window), 200, 400);
167
168 if (!interactive)
169 g_timeout_add (200, stop_main, NULL);
170 gtk_main ();
171
172 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
173 g_assert_cmpint (w, ==, 200);
174 g_assert_cmpint (h, ==, 400);
175
176 gtk_widget_destroy (window);
177 }
178
179 static void
180 test_resize_popup (void)
181 {
182 GtkWidget *window;
183 gint x, y, w, h;
184
185 /* testcase for the dnd window */
186 window = gtk_window_new (GTK_WINDOW_POPUP);
187 gtk_window_set_screen (GTK_WINDOW (window), gdk_screen_get_default ());
188 gtk_window_resize (GTK_WINDOW (window), 1, 1);
189 gtk_window_move (GTK_WINDOW (window), -99, -99);
190
191 gtk_window_get_position (GTK_WINDOW (window), &x, &y);
192 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
193 g_assert_cmpint (x, ==, -99);
194 g_assert_cmpint (y, ==, -99);
195 g_assert_cmpint (w, ==, 1);
196 g_assert_cmpint (h, ==, 1);
197
198 gtk_widget_show (window);
199
200 g_timeout_add (200, stop_main, NULL);
201 gtk_main ();
202
203 gtk_window_get_position (GTK_WINDOW (window), &x, &y);
204 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
205 g_assert_cmpint (x, ==, -99);
206 g_assert_cmpint (y, ==, -99);
207 g_assert_cmpint (w, ==, 1);
208 g_assert_cmpint (h, ==, 1);
209
210 gtk_widget_destroy (window);
211 }
212
213 static void
214 test_show_hide (void)
215 {
216 GtkWidget *window;
217 gint w, h, w1, h1;
218
219 g_test_bug ("696882");
220
221 /* test that hide/show does not affect the size */
222
223 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
224
225 gtk_widget_show (window);
226
227 g_timeout_add (100, stop_main, NULL);
228 gtk_main ();
229
230 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
231
232 gtk_widget_hide (window);
233
234 g_timeout_add (100, stop_main, NULL);
235 gtk_main ();
236
237 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
238 g_assert_cmpint (w, ==, w1);
239 g_assert_cmpint (h, ==, h1);
240
241 gtk_widget_show (window);
242
243 g_timeout_add (100, stop_main, NULL);
244 gtk_main ();
245
246 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
247 g_assert_cmpint (w, ==, w1);
248 g_assert_cmpint (h, ==, h1);
249
250 gtk_widget_destroy (window);
251 }
252
253 static void
254 test_show_hide2 (void)
255 {
256 GtkWidget *window;
257 gint x, y, w, h, w1, h1;
258
259 g_test_bug ("696882");
260
261 /* test that hide/show does not affect the size,
262 * even when get_position/move is called
263 */
264
265 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
266
267 gtk_widget_show (window);
268
269 g_timeout_add (100, stop_main, NULL);
270 gtk_main ();
271
272 gtk_window_get_position (GTK_WINDOW (window), &x, &y);
273 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
274 gtk_widget_hide (window);
275
276 g_timeout_add (100, stop_main, NULL);
277 gtk_main ();
278
279 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
280 g_assert_cmpint (w, ==, w1);
281 g_assert_cmpint (h, ==, h1);
282
283 gtk_window_move (GTK_WINDOW (window), x, y);
284 gtk_widget_show (window);
285
286 g_timeout_add (100, stop_main, NULL);
287 gtk_main ();
288
289 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
290 g_assert_cmpint (w, ==, w1);
291 g_assert_cmpint (h, ==, h1);
292
293 gtk_widget_destroy (window);
294 }
295
296 static void
297 test_show_hide3 (void)
298 {
299 GtkWidget *window;
300 gint x, y, w, h, w1, h1;
301
302 g_test_bug ("696882");
303
304 /* test that hide/show does not affect the size,
305 * even when get_position/move is called and
306 * a default size is set
307 */
308
309 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
310 gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
311
312 gtk_widget_show (window);
313
314 g_timeout_add (100, stop_main, NULL);
315 gtk_main ();
316
317 gtk_window_get_position (GTK_WINDOW (window), &x, &y);
318 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
319 gtk_widget_hide (window);
320
321 g_timeout_add (100, stop_main, NULL);
322 gtk_main ();
323
324 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
325 g_assert_cmpint (w, ==, w1);
326 g_assert_cmpint (h, ==, h1);
327
328 gtk_window_move (GTK_WINDOW (window), x, y);
329 gtk_widget_show (window);
330
331 g_timeout_add (100, stop_main, NULL);
332 gtk_main ();
333
334 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
335 g_assert_cmpint (w, ==, w1);
336 g_assert_cmpint (h, ==, h1);
337
338 gtk_widget_destroy (window);
339 }
340
341 static gboolean
342 on_map_event (GtkWidget *window)
343 {
344 gtk_main_quit ();
345
346 return FALSE;
347 }
348
349 static void
350 test_hide_titlebar_when_maximized (void)
351 {
352 GtkWidget *window;
353
354 g_test_bug ("740287");
355
356 /* test that hide-titlebar-when-maximized gets set appropriately
357 * on the window, if it's set before the window is realized.
358 */
359
360 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
361
362 g_signal_connect (window,
363 "map-event",
364 G_CALLBACK (on_map_event),
365 NULL);
366
367 gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window), TRUE);
368
369 gtk_widget_show (window);
370
371 g_timeout_add (100, stop_main, NULL);
372 gtk_main ();
373
374 #ifdef GDK_WINDOWING_X11
375 if (GDK_IS_X11_SCREEN (gtk_widget_get_screen (window)))
376 {
377 Atom type;
378 gint format;
379 gulong nitems;
380 gulong bytes_after;
381 gulong *hide = NULL;
382
383 XGetWindowProperty (gdk_x11_get_default_xdisplay (),
384 GDK_WINDOW_XID (gtk_widget_get_window (window)),
385 gdk_x11_get_xatom_by_name ("_GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED"),
386 0,
387 G_MAXLONG,
388 False,
389 XA_CARDINAL,
390 &type,
391 &format,
392 &nitems,
393 &bytes_after,
394 (guchar **) &hide);
395
396 g_assert_cmpint (type, !=, None);
397 g_assert_cmpint (type, ==, XA_CARDINAL);
398 g_assert_cmpint (format, ==, 32);
399 g_assert_cmpint (nitems, ==, 1);
400 g_assert_cmpint (hide[0], ==, 1);
401
402 XFree (hide);
403 }
404 #endif
405
406 gtk_widget_destroy (window);
407 }
408
409 int
410 main (int argc, char *argv[])
411 {
412 gint i;
413
414 gtk_test_init (&argc, &argv);
415 g_test_bug_base ("http://bugzilla.gnome.org/");
416
417 for (i = 0; i < argc; i++)
418 {
419 if (g_strcmp0 (argv[i], "--interactive") == 0)
420 interactive = TRUE;
421 }
422
423 g_test_add_func ("/window/default-size", test_default_size);
424 g_test_add_func ("/window/resize", test_resize);
425 g_test_add_func ("/window/resize-popup", test_resize_popup);
426 g_test_add_func ("/window/show-hide", test_show_hide);
427 g_test_add_func ("/window/show-hide2", test_show_hide2);
428 g_test_add_func ("/window/show-hide3", test_show_hide3);
429 g_test_add_func ("/window/hide-titlebar-when-maximized", test_hide_titlebar_when_maximized);
430
431 return g_test_run ();
432 }