proxy-test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2019 Red Hat, Inc.
  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 <assert.h>
  26. #include <string.h>
  27. #include "wayland-server.h"
  28. #include "wayland-client.h"
  29. #include "test-runner.h"
  30. static struct {
  31. struct wl_display *display;
  32. struct wl_event_loop *loop;
  33. int sync_count;
  34. } server;
  35. static struct {
  36. struct wl_display *display;
  37. struct wl_callback *callback_a;
  38. struct wl_callback *callback_b;
  39. int callback_count;
  40. } client;
  41. static const char *tag_a = "tag";
  42. static const char *tag_b = "tag";
  43. static void
  44. callback_done(void *data, struct wl_callback *cb, uint32_t time)
  45. {
  46. const char * const *expected_tag;
  47. const char * const *tag;
  48. if (cb == client.callback_a)
  49. expected_tag = &tag_a;
  50. else if (cb == client.callback_b)
  51. expected_tag = &tag_b;
  52. else
  53. assert(!"unexpected callback");
  54. tag = wl_proxy_get_tag((struct wl_proxy *) cb);
  55. assert(tag == expected_tag);
  56. assert(strcmp(*tag, "tag") == 0);
  57. wl_callback_destroy(cb);
  58. client.callback_count++;
  59. }
  60. static const struct wl_callback_listener callback_listener = {
  61. callback_done,
  62. };
  63. static void
  64. logger_func(void *user_data,
  65. enum wl_protocol_logger_type type,
  66. const struct wl_protocol_logger_message *message)
  67. {
  68. if (type != WL_PROTOCOL_LOGGER_REQUEST)
  69. return;
  70. assert(strcmp(wl_resource_get_class(message->resource),
  71. "wl_display") == 0);
  72. assert(strcmp(message->message->name, "sync") == 0);
  73. server.sync_count++;
  74. }
  75. TEST(proxy_tag)
  76. {
  77. const char *socket;
  78. struct wl_protocol_logger *logger;
  79. assert(&tag_a != &tag_b);
  80. server.display = wl_display_create();
  81. assert(server.display);
  82. server.loop = wl_display_get_event_loop(server.display);
  83. assert(server.loop);
  84. socket = wl_display_add_socket_auto(server.display);
  85. assert(socket);
  86. logger = wl_display_add_protocol_logger(server.display,
  87. logger_func, NULL);
  88. assert(logger);
  89. client.display = wl_display_connect(socket);
  90. assert(client.display);
  91. client.callback_a = wl_display_sync(client.display);
  92. wl_callback_add_listener(client.callback_a, &callback_listener, NULL);
  93. wl_proxy_set_tag((struct wl_proxy *) client.callback_a,
  94. &tag_a);
  95. client.callback_b = wl_display_sync(client.display);
  96. wl_callback_add_listener(client.callback_b, &callback_listener, NULL);
  97. wl_proxy_set_tag((struct wl_proxy *) client.callback_b,
  98. &tag_b);
  99. assert(wl_proxy_get_display((struct wl_proxy *) client.callback_b) == client.display);
  100. wl_display_flush(client.display);
  101. while (server.sync_count < 2) {
  102. wl_event_loop_dispatch(server.loop, -1);
  103. wl_display_flush_clients(server.display);
  104. }
  105. wl_display_dispatch(client.display);
  106. assert(client.callback_count == 2);
  107. wl_protocol_logger_destroy(logger);
  108. wl_display_disconnect(client.display);
  109. wl_event_loop_dispatch(server.loop, 100);
  110. wl_display_destroy(server.display);
  111. }