summaryrefslogtreecommitdiff
path: root/tests/testerrors.c
blob: 24dd5d3309f5669f83af89e9ebaa45416a5d2bd9 (plain)
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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Red Hat, Inc.
 * Author: Matthias Clasen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */


#include <X11/Xlib.h>
#include <gtk/gtk.h>
#include "x11/gdkx.h"

static void
test_error_trapping (GdkDisplay *gdk_display)
{
  Display *d;
  int dummy;
  int error;

  d = GDK_DISPLAY_XDISPLAY (gdk_display);

  /* verify that we can catch errors */
  gdk_error_trap_push ();
  XListProperties (d, 0, &dummy); /* round trip */
  error = gdk_error_trap_pop ();
  g_assert (error == BadWindow);

  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345); /* not a round trip */
  XSetCloseDownMode (d, DestroyAll);
  error = gdk_error_trap_pop ();
  g_assert (error == BadValue);

  /* try the same without sync */
  gdk_error_trap_push ();
  XListProperties (d, 0, &dummy);
  gdk_error_trap_pop_ignored ();

  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  XSetCloseDownMode (d, DestroyAll);
  gdk_error_trap_pop_ignored ();

  XSync (d, TRUE);

  /* verify that we can catch with nested traps; inner-most
   * active trap gets the error */
  gdk_error_trap_push ();
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  error = gdk_error_trap_pop ();
  g_assert (error == BadValue);
  error = gdk_error_trap_pop ();
  g_assert (error == Success);

  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_push ();
  error = gdk_error_trap_pop ();
  g_assert (error == Success);
  error = gdk_error_trap_pop ();
  g_assert (error == BadValue);

  /* try nested, without sync */
  gdk_error_trap_push ();
  gdk_error_trap_push ();
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_pop_ignored ();
  gdk_error_trap_pop_ignored ();
  gdk_error_trap_pop_ignored ();

  XSync (d, TRUE);

  /* try nested, without sync, with interleaved calls */
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_pop_ignored ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_pop_ignored ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_pop_ignored ();

  XSync (d, TRUE);

  /* don't want to get errors that weren't in our push range */
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  gdk_error_trap_push ();
  XSync (d, TRUE); /* not an error */
  error = gdk_error_trap_pop ();
  g_assert (error == Success);
  error = gdk_error_trap_pop ();
  g_assert (error == BadValue);

  /* non-roundtrip non-error request after error request, inside trap */
  gdk_error_trap_push ();
  XSetCloseDownMode (d, 12345);
  XMapWindow (d, DefaultRootWindow (d));
  error = gdk_error_trap_pop ();
  g_assert (error == BadValue);

  /* a non-roundtrip non-error request before error request, inside trap */
  gdk_error_trap_push ();
  XMapWindow (d, DefaultRootWindow (d));
  XSetCloseDownMode (d, 12345);
  error = gdk_error_trap_pop ();
  g_assert (error == BadValue);

  /* Not part of any test, just a double-check
   * that all errors have arrived
   */
  XSync (d, TRUE);
}

gint
main (gint argc, gchar *argv[])
{
  GdkDisplay *extra_display;

  gtk_init (&argc, &argv);

  test_error_trapping (gdk_display_get_default ());

  extra_display = gdk_display_open (NULL);
  test_error_trapping (extra_display);
  gdk_display_close (extra_display);

  test_error_trapping (gdk_display_get_default ());

  /* open a display with a trap pushed and see if we
   * get confused
   */
  gdk_error_trap_push ();
  gdk_error_trap_push ();

  extra_display = gdk_display_open (NULL);
  test_error_trapping (extra_display);
  gdk_display_close (extra_display);

  gdk_error_trap_pop_ignored ();
  gdk_error_trap_pop_ignored ();

  test_error_trapping (gdk_display_get_default ());

  /* reassure us that the tests ran. */
  g_print("All errors properly trapped.\n");

  return 0;
}