1 #include <gtk/gtk.h>
2
3 #ifdef GDK_WINDOWING_X11
4 #include <gdk/x11/gdkx.h>
5 #include <X11/Xatom.h>
6 #endif
7
8 static gboolean interactive = FALSE;
9
10 static gboolean
stop_main(gpointer data)11 stop_main (gpointer data)
12 {
13 gboolean *done = data;
14
15 *done = TRUE;
16
17 g_main_context_wakeup (NULL);
18
19 return G_SOURCE_REMOVE;
20 }
21
22 static void
on_draw(GtkDrawingArea * da,cairo_t * cr,int width,int height,gpointer data)23 on_draw (GtkDrawingArea *da,
24 cairo_t *cr,
25 int width,
26 int height,
27 gpointer data)
28 {
29 int i, j;
30
31 for (i = 0; 20 * i < width; i++)
32 {
33 for (j = 0; 20 * j < height; j++)
34 {
35 if ((i + j) % 2 == 1)
36 cairo_set_source_rgb (cr, 1., 1., 1.);
37 else
38 cairo_set_source_rgb (cr, 0., 0., 0.);
39
40 cairo_rectangle (cr, 20. * i, 20. *j, 20., 20.);
41 cairo_fill (cr);
42 }
43 }
44 }
45
46 static gboolean
on_keypress(GtkEventControllerKey * key,guint keyval,guint keycode,GdkModifierType state,gpointer data)47 on_keypress (GtkEventControllerKey *key,
48 guint keyval,
49 guint keycode,
50 GdkModifierType state,
51 gpointer data)
52 {
53 gboolean *done = data;
54
55 *done = TRUE;
56
57 g_main_context_wakeup (NULL);
58
59 return GDK_EVENT_PROPAGATE;
60 }
61
62 static void
test_default_size(void)63 test_default_size (void)
64 {
65 GtkWidget *window;
66 GtkWidget *da;
67 int w, h;
68 gboolean done;
69
70 window = gtk_window_new ();
71 if (interactive)
72 {
73 GtkEventController *controller = gtk_event_controller_key_new ();
74 g_signal_connect (controller, "key-pressed", G_CALLBACK (on_keypress), &done);
75 gtk_widget_add_controller (window, controller);
76 }
77
78 da = gtk_drawing_area_new ();
79 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), on_draw, NULL, NULL);
80 gtk_window_set_child (GTK_WINDOW (window), da);
81
82 /* check that default size is unset initially */
83 gtk_window_get_default_size (GTK_WINDOW (window), &w, &h);
84 g_assert_cmpint (w, ==, -1);
85 g_assert_cmpint (h, ==, -1);
86
87 /* check that setting default size before realize works */
88 gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
89
90 gtk_window_get_default_size (GTK_WINDOW (window), &w, &h);
91 g_assert_cmpint (w, ==, 300);
92 g_assert_cmpint (h, ==, 300);
93
94 /* check that the window size is also reported accordingly */
95 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
96 g_assert_cmpint (w, ==, 300);
97 g_assert_cmpint (h, ==, 300);
98
99 gtk_widget_show (window);
100
101 done = FALSE;
102 if (!interactive)
103 g_timeout_add (200, stop_main, &done);
104 while (!done)
105 g_main_context_iteration (NULL, TRUE);
106
107 /* check that the window and its content actually gets the right size */
108 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
109 g_assert_cmpint (w, ==, 300);
110 g_assert_cmpint (h, ==, 300);
111
112 g_assert_cmpint (gtk_widget_get_allocated_width (da), ==, 300);
113 g_assert_cmpint (gtk_widget_get_allocated_height (da), ==, 300);
114
115 /* check that setting default size after the fact does not change
116 * window size
117 */
118 gtk_window_set_default_size (GTK_WINDOW (window), 100, 600);
119 gtk_window_get_default_size (GTK_WINDOW (window), &w, &h);
120 g_assert_cmpint (w, ==, 100);
121 g_assert_cmpint (h, ==, 600);
122
123 done = FALSE;
124 if (!interactive)
125 g_timeout_add (200, stop_main, &done);
126 while (!done)
127 g_main_context_iteration (NULL, TRUE);
128
129 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
130 g_assert_cmpint (w, ==, 300);
131 g_assert_cmpint (h, ==, 300);
132
133 /* check that even hide/show does not pull in the new default */
134 gtk_widget_hide (window);
135 gtk_widget_show (window);
136
137 done = FALSE;
138 if (!interactive)
139 g_timeout_add (200, stop_main, &done);
140 while (!done)
141 g_main_context_iteration (NULL, TRUE);
142
143 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
144 g_assert_cmpint (w, ==, 300);
145 g_assert_cmpint (h, ==, 300);
146
147 gtk_window_destroy (GTK_WINDOW (window));
148 }
149
150 static void
test_resize_popup(void)151 test_resize_popup (void)
152 {
153 GtkWidget *window;
154 int w, h;
155 gboolean done;
156
157 /* testcase for the dnd window */
158 window = gtk_window_new ();
159 gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
160 gtk_window_set_default_size (GTK_WINDOW (window), 1, 1);
161 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
162 g_assert_cmpint (w, ==, 1);
163 g_assert_cmpint (h, ==, 1);
164
165 gtk_widget_show (window);
166
167 done = FALSE;
168 if (!interactive)
169 g_timeout_add (200, stop_main, &done);
170 while (!done)
171 g_main_context_iteration (NULL, TRUE);
172
173 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
174 g_assert_cmpint (w, ==, 1);
175 g_assert_cmpint (h, ==, 1);
176
177 gtk_window_destroy (GTK_WINDOW (window));
178 }
179
180 static void
test_show_hide(void)181 test_show_hide (void)
182 {
183 GtkWidget *window;
184 int w, h, w1, h1;
185 gboolean done;
186
187 /*http://bugzilla.gnome.org/show_bug.cgi?id=696882 */
188
189 /* test that hide/show does not affect the size */
190
191 window = gtk_window_new ();
192
193 gtk_widget_show (window);
194
195 done = FALSE;
196 if (!interactive)
197 g_timeout_add (200, stop_main, &done);
198 while (!done)
199 g_main_context_iteration (NULL, TRUE);
200
201 gtk_window_get_size (GTK_WINDOW (window), &w, &h);
202
203 gtk_widget_hide (window);
204
205 done = FALSE;
206 if (!interactive)
207 g_timeout_add (200, stop_main, &done);
208 while (!done)
209 g_main_context_iteration (NULL, TRUE);
210
211 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
212 g_assert_cmpint (w, ==, w1);
213 g_assert_cmpint (h, ==, h1);
214
215 gtk_widget_show (window);
216
217 done = FALSE;
218 if (!interactive)
219 g_timeout_add (200, stop_main, &done);
220 while (!done)
221 g_main_context_iteration (NULL, TRUE);
222
223 gtk_window_get_size (GTK_WINDOW (window), &w1, &h1);
224 g_assert_cmpint (w, ==, w1);
225 g_assert_cmpint (h, ==, h1);
226
227 gtk_window_destroy (GTK_WINDOW (window));
228 }
229
230 int
main(int argc,char * argv[])231 main (int argc, char *argv[])
232 {
233 int i;
234
235 gtk_test_init (&argc, &argv);
236
237 for (i = 0; i < argc; i++)
238 {
239 if (g_strcmp0 (argv[i], "--interactive") == 0)
240 interactive = TRUE;
241 }
242
243 g_test_add_func ("/window/default-size", test_default_size);
244 g_test_add_func ("/window/resize-popup", test_resize_popup);
245 g_test_add_func ("/window/show-hide", test_show_hide);
246
247 return g_test_run ();
248 }
249