compositor-introspection-test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_listener listener;
  46. struct wl_client *client;
  47. };
  48. static void
  49. client_created(struct wl_listener *listener, void *data)
  50. {
  51. struct compositor *c = wl_container_of(listener, c, listener);
  52. c->client = data;
  53. }
  54. static void
  55. check_client_list(struct compositor *compositor)
  56. {
  57. struct wl_list *client_list;
  58. struct wl_client *client, *client_it;
  59. int num_clients = 0;
  60. client_list = wl_display_get_client_list(compositor->display);
  61. wl_client_for_each(client_it, client_list) {
  62. num_clients++;
  63. client = client_it;
  64. }
  65. assert(num_clients == 1);
  66. /* 'client_it' is not valid here, so we took a copy of the client in the loop.
  67. * We could also do this assert in the loop directly, but in case it fails it is
  68. * easier to understand the problem when we know that the previous assert passed,
  69. * so that there is only one client but the wrong one. */
  70. assert(compositor->client == client);
  71. }
  72. static const char *
  73. setup_compositor(struct compositor *compositor)
  74. {
  75. const char *socket;
  76. require_xdg_runtime_dir();
  77. compositor->display = wl_display_create();
  78. socket = wl_display_add_socket_auto(compositor->display);
  79. compositor->listener.notify = client_created;
  80. wl_display_add_client_created_listener(compositor->display, &compositor->listener);
  81. return socket;
  82. }
  83. static void
  84. cleanup_compositor(struct compositor *compositor)
  85. {
  86. wl_client_destroy(compositor->client);
  87. wl_display_destroy(compositor->display);
  88. }
  89. TEST(new_client_connect)
  90. {
  91. const char *socket;
  92. struct compositor compositor = { 0 };
  93. struct {
  94. struct wl_display *display;
  95. } client;
  96. socket = setup_compositor(&compositor);
  97. client.display = wl_display_connect(socket);
  98. wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
  99. assert(compositor.client != NULL);
  100. check_client_list(&compositor);
  101. wl_display_disconnect(client.display);
  102. cleanup_compositor(&compositor);
  103. }
  104. struct resource_listener {
  105. struct wl_listener listener;
  106. int count;
  107. };
  108. static void
  109. resource_created(struct wl_listener *listener, void *data)
  110. {
  111. struct resource_listener *l;
  112. l = wl_container_of(listener, l, listener);
  113. l->count++;
  114. }
  115. TEST(new_resource)
  116. {
  117. const char *socket;
  118. struct compositor compositor = { 0 };
  119. struct {
  120. struct wl_display *display;
  121. struct wl_callback *cb;
  122. } client;
  123. struct resource_listener resource_listener;
  124. socket = setup_compositor(&compositor);
  125. client.display = wl_display_connect(socket);
  126. wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
  127. resource_listener.count = 0;
  128. resource_listener.listener.notify = resource_created;
  129. wl_client_add_resource_created_listener(compositor.client,
  130. &resource_listener.listener);
  131. client.cb = wl_display_sync(client.display);
  132. wl_display_flush(client.display);
  133. wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
  134. assert(resource_listener.count == 1);
  135. wl_callback_destroy(client.cb);
  136. wl_display_disconnect(client.display);
  137. cleanup_compositor(&compositor);
  138. /* This is defined to be safe also after client destruction */
  139. wl_list_remove(&resource_listener.listener.link);
  140. }