Add LICENSE.BSD-3-Clause file
[platform/upstream/libxkbcommon.git] / test / x11comp.c
index c63c4cc..d2cc447 100644 (file)
@@ -21,6 +21,8 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include "config.h"
+
 #include <stdio.h>
 #include <spawn.h>
 #include <unistd.h>
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <linux/input.h>
 
 #include "test.h"
+#include "xvfb-wrapper.h"
 #include "xkbcommon/xkbcommon-x11.h"
 
-int
-main(void)
+X11_TEST(test_basic)
 {
-    struct xkb_context *ctx = test_get_context(0);
     struct xkb_keymap *keymap;
     xcb_connection_t *conn;
     int32_t device_id;
-    int pipefds[2];
     int ret, status;
-    char displayfd[128], display[128];
-    char *search_path, *search_path_arg, *xkb_path;
+    char *xkb_path;
     char *original, *dump;
     char *envp[] = { NULL };
-    char *xvfb_argv[] = { "Xvfb", "-displayfd", displayfd, NULL };
-    pid_t xvfb_pid;
-    char *xkbcomp_argv[] = { "xkbcomp", "-I", NULL /* search_path_arg */,
-                             NULL /* xkb_path */, display, NULL };
+    char *xkbcomp_argv[] = {
+        (char *) "xkbcomp", (char *) "-I", NULL /* xkb_path */, display, NULL
+    };
     pid_t xkbcomp_pid;
 
-    /*
-     * What all of this mess does is:
-     * 1. Launch Xvfb on the next available DISPLAY. Xvfb reports the
-     *    display number to an fd passed with -displayfd once it's
-     *    initialized. We pass a pipe there to read it.
-     * 2. Make an xcb connection to this display.
-     * 3. Launch xkbcomp to change the keymap of the new display (doing
-     *    this programmatically is major work [which we may yet do some
-     *    day for xkbcommon-x11] so we use xkbcomp for now).
-     * 4. Download the keymap back from the display using xkbcommon-x11.
-     * 5. Compare received keymap to the uploaded keymap.
-     * 6. Kill the server & clean up.
-     */
-
-    ret = pipe(pipefds);
-    assert(ret == 0);
-
-    ret = snprintf(displayfd, sizeof(displayfd), "%d", pipefds[1]);
-    assert(ret >= 0 && ret < sizeof(displayfd));
-
-    ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
-    if (ret != 0) {
-        ret = SKIP_TEST;
-        goto err_ctx;
-    }
-
-    display[0] = ':';
-    ret = read(pipefds[0], display + 1, sizeof(display) - 1);
-    assert(ret > 0 && 1 + ret < sizeof(display) - 1);
-    if (display[ret] == '\n')
-        display[ret] = '\0';
-    display[1 + ret] = '\0';
-    close(pipefds[0]);
-    close(pipefds[1]);
-
     conn = xcb_connect(display, NULL);
     if (xcb_connection_has_error(conn)) {
         ret = SKIP_TEST;
-        goto err_xvfd;
+        goto err_conn;
     }
     ret = xkb_x11_setup_xkb_extension(conn,
                                       XKB_X11_MIN_MAJOR_XKB_VERSION,
@@ -105,15 +67,9 @@ main(void)
     assert(device_id != -1);
 
     xkb_path = test_get_path("keymaps/host.xkb");
-    search_path = test_get_path("");
-    assert(search_path);
-    ret = asprintf(&search_path_arg, "-I%s", search_path);
     assert(ret >= 0);
-    xkbcomp_argv[2] = search_path_arg;
-    xkbcomp_argv[3] = xkb_path;
+    xkbcomp_argv[2] = xkb_path;
     ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", NULL, NULL, xkbcomp_argv, envp);
-    free(search_path_arg);
-    free(search_path);
     free(xkb_path);
     if (ret != 0) {
         ret = SKIP_TEST;
@@ -125,6 +81,7 @@ main(void)
         goto err_xcb;
     }
 
+    struct xkb_context *ctx = test_get_context(0);
     keymap = xkb_x11_keymap_new_from_device(ctx, conn, device_id,
                                             XKB_KEYMAP_COMPILE_NO_FLAGS);
     assert(keymap);
@@ -135,8 +92,7 @@ main(void)
     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
     assert(dump);
 
-    ret = streq(original, dump);
-    if (!ret) {
+    if (!streq(original, dump)) {
         fprintf(stderr,
                 "round-trip test failed: dumped map differs from original\n");
         fprintf(stderr, "length: dumped %lu, original %lu\n",
@@ -153,11 +109,13 @@ err_dump:
     free(original);
     free(dump);
     xkb_keymap_unref(keymap);
+    xkb_context_unref(ctx);
 err_xcb:
     xcb_disconnect(conn);
-err_xvfd:
-    kill(xvfb_pid, SIGTERM);
-err_ctx:
-    xkb_context_unref(ctx);
+err_conn:
     return ret;
 }
+
+int main(void) {
+    return x11_tests_run();
+}