packaging: modify some files to bump to 0.7.2
[platform/upstream/libxkbcommon.git] / test / x11comp.c
1 /*
2  * Copyright © 2014 Ran Benita <ran234@gmail.com>
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 "config.h"
25
26 #include <stdio.h>
27 #include <spawn.h>
28 #include <unistd.h>
29 #include <assert.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33
34 #include "test.h"
35 #include "xvfb-wrapper.h"
36 #include "xkbcommon/xkbcommon-x11.h"
37
38 X11_TEST(test_basic)
39 {
40     struct xkb_keymap *keymap;
41     xcb_connection_t *conn;
42     int32_t device_id;
43     int ret, status;
44     char *xkb_path;
45     char *original, *dump;
46     char *envp[] = { NULL };
47     char *xkbcomp_argv[] = {
48         (char *) "xkbcomp", (char *) "-I", NULL /* xkb_path */, display, NULL
49     };
50     pid_t xkbcomp_pid;
51
52     conn = xcb_connect(display, NULL);
53     if (xcb_connection_has_error(conn)) {
54         ret = SKIP_TEST;
55         goto err_conn;
56     }
57     ret = xkb_x11_setup_xkb_extension(conn,
58                                       XKB_X11_MIN_MAJOR_XKB_VERSION,
59                                       XKB_X11_MIN_MINOR_XKB_VERSION,
60                                       XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
61                                       NULL, NULL, NULL, NULL);
62     if (!ret) {
63         ret = SKIP_TEST;
64         goto err_xcb;
65     }
66     device_id = xkb_x11_get_core_keyboard_device_id(conn);
67     assert(device_id != -1);
68
69     xkb_path = test_get_path("keymaps/host.xkb");
70     assert(ret >= 0);
71     xkbcomp_argv[2] = xkb_path;
72     ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", NULL, NULL, xkbcomp_argv, envp);
73     free(xkb_path);
74     if (ret != 0) {
75         ret = SKIP_TEST;
76         goto err_xcb;
77     }
78     ret = waitpid(xkbcomp_pid, &status, 0);
79     if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
80         ret = SKIP_TEST;
81         goto err_xcb;
82     }
83
84     struct xkb_context *ctx = test_get_context(0);
85     keymap = xkb_x11_keymap_new_from_device(ctx, conn, device_id,
86                                             XKB_KEYMAP_COMPILE_NO_FLAGS);
87     assert(keymap);
88
89     original = test_read_file("keymaps/host.xkb");
90     assert(original);
91
92     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
93     assert(dump);
94
95     if (!streq(original, dump)) {
96         fprintf(stderr,
97                 "round-trip test failed: dumped map differs from original\n");
98         fprintf(stderr, "length: dumped %lu, original %lu\n",
99                 (unsigned long) strlen(dump),
100                 (unsigned long) strlen(original));
101         fprintf(stderr, "dumped map:\n");
102         fprintf(stderr, "%s\n", dump);
103         ret = 1;
104         goto err_dump;
105     }
106
107     ret = 0;
108 err_dump:
109     free(original);
110     free(dump);
111     xkb_keymap_unref(keymap);
112     xkb_context_unref(ctx);
113 err_xcb:
114     xcb_disconnect(conn);
115 err_conn:
116     return ret;
117 }
118
119 int main(void) {
120     return x11_tests_run();
121 }