Event.c revision 9e7bcd65
1/*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <X11/Intrinsic.h>
25#include <glib.h>
26#include <unistd.h>
27#include <signal.h>
28#include <setjmp.h>
29
30/* Test XtAppMainLoop without a display doesn't wait for an XEvent.
31   Based on https://bugs.freedesktop.org/show_bug.cgi?id=34715 */
32static void _Tick(XtPointer baton, XtIntervalId* id)
33{
34    static int count = 0;
35    XtAppContext app = (XtAppContext)baton;
36
37    count ++;
38#ifdef DEBUG
39    printf("%d beep!\n", count);
40#endif
41
42    if (3 == count)
43        XtAppSetExitFlag(app);
44    else
45        XtAppAddTimeOut(app, 1000, _Tick, app);
46}
47
48static sigjmp_buf jump_env;
49
50static void sigalrm (int sig)
51{
52    siglongjmp(jump_env, 1);
53}
54
55static void test_XtAppMainLoop_34715(void)
56{
57    XtAppContext app;
58    struct sigaction sa;
59
60    XtToolkitInitialize();
61    app = XtCreateApplicationContext();
62    XtAppAddTimeOut(app, 1000, _Tick, app);
63
64    /* AppTimeouts should finish and exit in 3 seconds.
65       Give them 10 seconds just in case system is busy, then fail. */
66    sa.sa_handler = sigalrm;
67    sa.sa_flags = SA_RESTART;
68    sigemptyset (&sa.sa_mask);
69
70    if (sigsetjmp(jump_env, 1) == 0) {
71	sigaction(SIGALRM, &sa, NULL);
72	alarm(10);
73
74	XtAppMainLoop(app);
75    } else {
76	g_assert(XtAppGetExitFlag(app) == TRUE);
77	g_assert(0 /* timed out */);
78    }
79    g_assert(XtAppGetExitFlag(app) == TRUE);
80    XtDestroyApplicationContext(app);
81}
82
83int main(int argc, char** argv)
84{
85    g_test_init(&argc, &argv, NULL);
86    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
87
88    g_test_add_func("/Event/XtAppMainLoop/34715", test_XtAppMainLoop_34715);
89
90    return g_test_run();
91}
92