]> Pileus Git - ~andy/gtk/blob - tests/testerrors.c
Only store error codes in inner-most X error trap
[~andy/gtk] / tests / testerrors.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Red Hat, Inc.
3  * Author: Matthias Clasen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21
22 #include <X11/Xlib.h>
23 #include <gtk/gtk.h>
24 #include "x11/gdkx.h"
25
26 static void
27 test_error_trapping (GdkDisplay *gdk_display)
28 {
29   Display *d;
30   int dummy;
31   int error;
32
33   d = GDK_DISPLAY_XDISPLAY (gdk_display);
34
35   /* verify that we can catch errors */
36   gdk_error_trap_push ();
37   XListProperties (d, 0, &dummy); /* round trip */
38   error = gdk_error_trap_pop ();
39   g_assert (error == BadWindow);
40
41   gdk_error_trap_push ();
42   XSetCloseDownMode (d, 12345); /* not a round trip */
43   XSetCloseDownMode (d, DestroyAll);
44   error = gdk_error_trap_pop ();
45   g_assert (error == BadValue);
46
47   /* try the same without sync */
48   gdk_error_trap_push ();
49   XListProperties (d, 0, &dummy);
50   gdk_error_trap_pop_ignored ();
51
52   gdk_error_trap_push ();
53   XSetCloseDownMode (d, 12345);
54   XSetCloseDownMode (d, DestroyAll);
55   gdk_error_trap_pop_ignored ();
56
57   XSync (d, TRUE);
58
59   /* verify that we can catch with nested traps; inner-most
60    * active trap gets the error */
61   gdk_error_trap_push ();
62   gdk_error_trap_push ();
63   XSetCloseDownMode (d, 12345);
64   error = gdk_error_trap_pop ();
65   g_assert (error == BadValue);
66   error = gdk_error_trap_pop ();
67   g_assert (error == Success);
68
69   gdk_error_trap_push ();
70   XSetCloseDownMode (d, 12345);
71   gdk_error_trap_push ();
72   error = gdk_error_trap_pop ();
73   g_assert (error == Success);
74   error = gdk_error_trap_pop ();
75   g_assert (error == BadValue);
76
77   /* try nested, without sync */
78   gdk_error_trap_push ();
79   gdk_error_trap_push ();
80   gdk_error_trap_push ();
81   XSetCloseDownMode (d, 12345);
82   gdk_error_trap_pop_ignored ();
83   gdk_error_trap_pop_ignored ();
84   gdk_error_trap_pop_ignored ();
85
86   XSync (d, TRUE);
87
88   /* try nested, without sync, with interleaved calls */
89   gdk_error_trap_push ();
90   XSetCloseDownMode (d, 12345);
91   gdk_error_trap_push ();
92   XSetCloseDownMode (d, 12345);
93   gdk_error_trap_push ();
94   XSetCloseDownMode (d, 12345);
95   gdk_error_trap_pop_ignored ();
96   XSetCloseDownMode (d, 12345);
97   gdk_error_trap_pop_ignored ();
98   XSetCloseDownMode (d, 12345);
99   gdk_error_trap_pop_ignored ();
100
101   XSync (d, TRUE);
102
103   /* don't want to get errors that weren't in our push range */
104   gdk_error_trap_push ();
105   XSetCloseDownMode (d, 12345);
106   gdk_error_trap_push ();
107   XSync (d, TRUE); /* not an error */
108   error = gdk_error_trap_pop ();
109   g_assert (error == Success);
110   error = gdk_error_trap_pop ();
111   g_assert (error == BadValue);
112
113   /* non-roundtrip non-error request after error request, inside trap */
114   gdk_error_trap_push ();
115   XSetCloseDownMode (d, 12345);
116   XMapWindow (d, DefaultRootWindow (d));
117   error = gdk_error_trap_pop ();
118   g_assert (error == BadValue);
119
120   /* a non-roundtrip non-error request before error request, inside trap */
121   gdk_error_trap_push ();
122   XMapWindow (d, DefaultRootWindow (d));
123   XSetCloseDownMode (d, 12345);
124   error = gdk_error_trap_pop ();
125   g_assert (error == BadValue);
126
127   /* Not part of any test, just a double-check
128    * that all errors have arrived
129    */
130   XSync (d, TRUE);
131 }
132
133 gint
134 main (gint argc, gchar *argv[])
135 {
136   GdkDisplay *extra_display;
137
138   gtk_init (&argc, &argv);
139
140   test_error_trapping (gdk_display_get_default ());
141
142   extra_display = gdk_display_open (NULL);
143   test_error_trapping (extra_display);
144   gdk_display_close (extra_display);
145
146   test_error_trapping (gdk_display_get_default ());
147
148   /* open a display with a trap pushed and see if we
149    * get confused
150    */
151   gdk_error_trap_push ();
152   gdk_error_trap_push ();
153
154   extra_display = gdk_display_open (NULL);
155   test_error_trapping (extra_display);
156   gdk_display_close (extra_display);
157
158   gdk_error_trap_pop_ignored ();
159   gdk_error_trap_pop_ignored ();
160
161   test_error_trapping (gdk_display_get_default ());
162
163   /* reassure us that the tests ran. */
164   g_print("All errors properly trapped.\n");
165
166   return 0;
167 }
168