1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <ctk/ctk.h>

#ifdef CDK_WINDOWING_X11
#include <cdk/cdkx.h>
#include <X11/Xatom.h>
#endif

static gboolean interactive = FALSE;

static gboolean
stop_main (gpointer data)
{
  ctk_main_quit ();

  return G_SOURCE_REMOVE;
}

static gboolean
on_draw (CtkWidget *widget, cairo_t *cr)
{
  gint i, j;

  for (i = 0; 20 * i < ctk_widget_get_allocated_width (widget); i++)
    {
      for (j = 0; 20 * j < ctk_widget_get_allocated_height (widget); j++)
        {
          if ((i + j) % 2 == 1)
            cairo_set_source_rgb (cr, 1., 1., 1.);
          else
            cairo_set_source_rgb (cr, 0., 0., 0.);

          cairo_rectangle (cr, 20. * i, 20. *j, 20., 20.);
          cairo_fill (cr);
        }
    }

  return FALSE;
}

static gboolean
on_keypress (CtkWidget *widget)
{
  ctk_main_quit ();

  return TRUE;
}

static void
test_default_size (void)
{
  CtkWidget *window;
  CtkWidget *box;
  gint w, h;

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "draw", G_CALLBACK (on_draw), NULL);
  if (interactive)
    g_signal_connect (window, "key-press-event", G_CALLBACK (on_keypress), NULL);

  box = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 0);
  ctk_container_add (CTK_CONTAINER (window), box);

  /* check that default size is unset initially */
  ctk_window_get_default_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, -1);<--- syntax error
  g_assert_cmpint (h, ==, -1);

  /* check that setting default size before realize works */
  ctk_window_set_default_size (CTK_WINDOW (window), 300, 300);

  ctk_window_get_default_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 300);
  g_assert_cmpint (h, ==, 300);

  /* check that the window size is also reported accordingly */
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 300);
  g_assert_cmpint (h, ==, 300);

  ctk_widget_show_all (window);

  if (!interactive)
    g_timeout_add (200, stop_main, NULL);
  ctk_main ();

  /* check that the window and its content actually gets the right size */
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 300);
  g_assert_cmpint (h, ==, 300);

  g_assert_cmpint (ctk_widget_get_allocated_width (box), ==, 300);
  g_assert_cmpint (ctk_widget_get_allocated_height (box), ==, 300);

  /* check that setting default size after the fact does not change
   * window size
   */
  ctk_window_set_default_size (CTK_WINDOW (window), 100, 600);
  ctk_window_get_default_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 100);
  g_assert_cmpint (h, ==, 600);

  if (!interactive)
    g_timeout_add (200, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 300);
  g_assert_cmpint (h, ==, 300);

  /* check that even hide/show does not pull in the new default */
  ctk_widget_hide (window);
  ctk_widget_show (window);

  if (!interactive)
    g_timeout_add (200, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 300);
  g_assert_cmpint (h, ==, 300);

  ctk_widget_destroy (window);
}

static void
test_resize (void)
{
  CtkWidget *window;
  CtkWidget *box;
  gint w, h;

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "draw", G_CALLBACK (on_draw), NULL);
  if (interactive)
    g_signal_connect (window, "key-press-event", G_CALLBACK (on_keypress), NULL);

  box = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 0);
  ctk_container_add (CTK_CONTAINER (window), box);

  /* test that resize before show overrides default size */
  ctk_window_set_default_size (CTK_WINDOW (window), 500, 500);

  ctk_window_resize (CTK_WINDOW (window), 1, 1);

  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 1);
  g_assert_cmpint (h, ==, 1);

  ctk_window_resize (CTK_WINDOW (window), 400, 200);

  ctk_widget_show_all (window);

  if (!interactive)
    g_timeout_add (200, stop_main, NULL);
  ctk_main ();

  /* test that resize before show works */
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 400);
  g_assert_cmpint (h, ==, 200);

  /* test that resize after show works, both
   * for making things bigger and for making things
   * smaller
   */
  ctk_window_resize (CTK_WINDOW (window), 200, 400);

  if (!interactive)
    g_timeout_add (200, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (w, ==, 200);
  g_assert_cmpint (h, ==, 400);

  ctk_widget_destroy (window);
}

static void
test_resize_popup (void)
{
  CtkWidget *window;
  gint x, y, w, h;

  /* testcase for the dnd window */
  window = ctk_window_new (CTK_WINDOW_POPUP);
  ctk_window_set_screen (CTK_WINDOW (window), cdk_screen_get_default ());
  ctk_window_resize (CTK_WINDOW (window), 1, 1);
  ctk_window_move (CTK_WINDOW (window), -99, -99);

  ctk_window_get_position (CTK_WINDOW (window), &x, &y);
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (x, ==, -99);
  g_assert_cmpint (y, ==, -99);
  g_assert_cmpint (w, ==, 1);
  g_assert_cmpint (h, ==, 1);

  ctk_widget_show (window);

  g_timeout_add (200, stop_main, NULL);
  ctk_main ();

  ctk_window_get_position (CTK_WINDOW (window), &x, &y);
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  g_assert_cmpint (x, ==, -99);
  g_assert_cmpint (y, ==, -99);
  g_assert_cmpint (w, ==, 1);
  g_assert_cmpint (h, ==, 1);

  ctk_widget_destroy (window);
}

static void
test_show_hide (void)
{
  CtkWidget *window;
  gint w, h, w1, h1;

  g_test_bug ("696882");

  /* test that hide/show does not affect the size */

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);

  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w, &h);

  ctk_widget_hide (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w1, &h1);
  g_assert_cmpint (w, ==, w1);
  g_assert_cmpint (h, ==, h1);

  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w1, &h1);
  g_assert_cmpint (w, ==, w1);
  g_assert_cmpint (h, ==, h1);

  ctk_widget_destroy (window);
}

static void
test_show_hide2 (void)
{
  CtkWidget *window;
  gint x, y, w, h, w1, h1;

  g_test_bug ("696882");

  /* test that hide/show does not affect the size,
   * even when get_position/move is called
   */

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);

  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_position (CTK_WINDOW (window), &x, &y);
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  ctk_widget_hide (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w1, &h1);
  g_assert_cmpint (w, ==, w1);
  g_assert_cmpint (h, ==, h1);

  ctk_window_move (CTK_WINDOW (window), x, y);
  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w1, &h1);
  g_assert_cmpint (w, ==, w1);
  g_assert_cmpint (h, ==, h1);

  ctk_widget_destroy (window);
}

static void
test_show_hide3 (void)
{
  CtkWidget *window;
  gint x, y, w, h, w1, h1;

  g_test_bug ("696882");

  /* test that hide/show does not affect the size,
   * even when get_position/move is called and
   * a default size is set
   */

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
  ctk_window_set_default_size (CTK_WINDOW (window), 200, 200);

  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_position (CTK_WINDOW (window), &x, &y);
  ctk_window_get_size (CTK_WINDOW (window), &w, &h);
  ctk_widget_hide (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w1, &h1);
  g_assert_cmpint (w, ==, w1);
  g_assert_cmpint (h, ==, h1);

  ctk_window_move (CTK_WINDOW (window), x, y);
  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

  ctk_window_get_size (CTK_WINDOW (window), &w1, &h1);
  g_assert_cmpint (w, ==, w1);
  g_assert_cmpint (h, ==, h1);

  ctk_widget_destroy (window);
}

static gboolean
on_map_event (CtkWidget *window)
{
  ctk_main_quit ();

  return FALSE;
}

static void
test_hide_titlebar_when_maximized (void)
{
  CtkWidget *window;

  g_test_bug ("740287");

  /* test that hide-titlebar-when-maximized gets set appropriately
   * on the window, if it's set before the window is realized.
   */

  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);

  g_signal_connect (window,
                    "map-event",
                    G_CALLBACK (on_map_event),
                    NULL);

  ctk_window_set_hide_titlebar_when_maximized (CTK_WINDOW (window), TRUE);

  ctk_widget_show (window);

  g_timeout_add (100, stop_main, NULL);
  ctk_main ();

#ifdef CDK_WINDOWING_X11
  if (CDK_IS_X11_SCREEN (ctk_widget_get_screen (window)))
    {
      Atom type;
      gint format;
      gulong nitems;
      gulong bytes_after;
      gulong *hide = NULL;

      XGetWindowProperty (cdk_x11_get_default_xdisplay (),
                          CDK_WINDOW_XID (ctk_widget_get_window (window)),
                          cdk_x11_get_xatom_by_name ("_CTK_HIDE_TITLEBAR_WHEN_MAXIMIZED"),
                          0,
                          G_MAXLONG,
                          False,
                          XA_CARDINAL,
                          &type,
                          &format,
                          &nitems,
                          &bytes_after,
                          (guchar **) &hide);

      g_assert_cmpint (type, !=, None);
      g_assert_cmpint (type, ==, XA_CARDINAL);
      g_assert_cmpint (format, ==, 32);
      g_assert_cmpint (nitems, ==, 1);
      g_assert_cmpint (hide[0], ==, 1);

      XFree (hide);
    }
#endif

  ctk_widget_destroy (window);
}

int
main (int argc, char *argv[])
{
  gint i;

  ctk_test_init (&argc, &argv);
  g_test_bug_base ("http://bugzilla.gnome.org/");

  for (i = 0; i < argc; i++)
    {
      if (g_strcmp0 (argv[i], "--interactive") == 0)
        interactive = TRUE;
    }

  g_test_add_func ("/window/default-size", test_default_size);
  g_test_add_func ("/window/resize", test_resize);
  g_test_add_func ("/window/resize-popup", test_resize_popup);
  g_test_add_func ("/window/show-hide", test_show_hide);
  g_test_add_func ("/window/show-hide2", test_show_hide2);
  g_test_add_func ("/window/show-hide3", test_show_hide3);
  g_test_add_func ("/window/hide-titlebar-when-maximized", test_hide_titlebar_when_maximized);

  return g_test_run ();
}