protocol-logger-test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright © 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include <sys/un.h>
  31. #include <unistd.h>
  32. #include "wayland-client.h"
  33. #include "wayland-server.h"
  34. #include "test-runner.h"
  35. /* Ensure the connection doesn't fail due to lack of XDG_RUNTIME_DIR. */
  36. static const char *
  37. require_xdg_runtime_dir(void)
  38. {
  39. char *val = getenv("XDG_RUNTIME_DIR");
  40. assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
  41. return val;
  42. }
  43. struct compositor {
  44. struct wl_display *display;
  45. struct wl_event_loop *loop;
  46. int message;
  47. struct wl_client *client;
  48. };
  49. struct message {
  50. enum wl_protocol_logger_type type;
  51. const char *class;
  52. int opcode;
  53. const char *message_name;
  54. int args_count;
  55. } messages[] = {
  56. {
  57. .type = WL_PROTOCOL_LOGGER_REQUEST,
  58. .class = "wl_display",
  59. .opcode = 0,
  60. .message_name = "sync",
  61. .args_count = 1,
  62. },
  63. {
  64. .type = WL_PROTOCOL_LOGGER_EVENT,
  65. .class = "wl_callback",
  66. .opcode = 0,
  67. .message_name = "done",
  68. .args_count = 1,
  69. },
  70. {
  71. .type = WL_PROTOCOL_LOGGER_EVENT,
  72. .class = "wl_display",
  73. .opcode = 1,
  74. .message_name = "delete_id",
  75. .args_count = 1,
  76. },
  77. };
  78. static void
  79. logger_func(void *user_data, enum wl_protocol_logger_type type,
  80. const struct wl_protocol_logger_message *message)
  81. {
  82. struct compositor *c = user_data;
  83. struct message *msg = &messages[c->message++];
  84. assert(msg->type == type);
  85. assert(strcmp(msg->class, wl_resource_get_class(message->resource)) == 0);
  86. assert(msg->opcode == message->message_opcode);
  87. assert(strcmp(msg->message_name, message->message->name) == 0);
  88. assert(msg->args_count == message->arguments_count);
  89. c->client = wl_resource_get_client(message->resource);
  90. }
  91. static void
  92. callback_done(void *data, struct wl_callback *cb, uint32_t time)
  93. {
  94. wl_callback_destroy(cb);
  95. }
  96. static const struct wl_callback_listener callback_listener = {
  97. callback_done,
  98. };
  99. TEST(logger)
  100. {
  101. test_set_timeout(1);
  102. const char *socket;
  103. struct compositor compositor = { 0 };
  104. struct {
  105. struct wl_display *display;
  106. struct wl_callback *cb;
  107. } client;
  108. struct wl_protocol_logger *logger;
  109. require_xdg_runtime_dir();
  110. compositor.display = wl_display_create();
  111. compositor.loop = wl_display_get_event_loop(compositor.display);
  112. socket = wl_display_add_socket_auto(compositor.display);
  113. logger = wl_display_add_protocol_logger(compositor.display,
  114. logger_func, &compositor);
  115. client.display = wl_display_connect(socket);
  116. client.cb = wl_display_sync(client.display);
  117. wl_callback_add_listener(client.cb, &callback_listener, NULL);
  118. wl_display_flush(client.display);
  119. while (compositor.message < 3) {
  120. wl_event_loop_dispatch(compositor.loop, -1);
  121. wl_display_flush_clients(compositor.display);
  122. }
  123. wl_display_dispatch(client.display);
  124. wl_display_disconnect(client.display);
  125. wl_client_destroy(compositor.client);
  126. wl_protocol_logger_destroy(logger);
  127. wl_display_destroy(compositor.display);
  128. }