1
0

test-log.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright © 2014 Red Hat, Inc.
  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. #include <config.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <libinput.h>
  27. #include <stdarg.h>
  28. #include <unistd.h>
  29. #include "litest.h"
  30. static int log_handler_called;
  31. static struct libinput *log_handler_context;
  32. static void
  33. simple_log_handler(struct libinput *libinput,
  34. enum libinput_log_priority priority,
  35. const char *format,
  36. va_list args)
  37. {
  38. log_handler_called++;
  39. if (log_handler_context)
  40. litest_assert_ptr_eq(libinput, log_handler_context);
  41. litest_assert_notnull(format);
  42. }
  43. static int
  44. open_restricted(const char *path, int flags, void *data)
  45. {
  46. int fd;
  47. fd = open(path, flags);
  48. return fd < 0 ? -errno : fd;
  49. }
  50. static void
  51. close_restricted(int fd, void *data)
  52. {
  53. close(fd);
  54. }
  55. static const struct libinput_interface simple_interface = {
  56. .open_restricted = open_restricted,
  57. .close_restricted = close_restricted,
  58. };
  59. START_TEST(log_default_priority)
  60. {
  61. enum libinput_log_priority pri;
  62. _unref_(libinput) *li = libinput_path_create_context(&simple_interface, NULL);
  63. pri = libinput_log_get_priority(li);
  64. litest_assert_enum_eq(pri, LIBINPUT_LOG_PRIORITY_ERROR);
  65. }
  66. END_TEST
  67. START_TEST(log_handler_invoked)
  68. {
  69. log_handler_context = NULL;
  70. log_handler_called = 0;
  71. _litest_context_destroy_ struct libinput *li = litest_create_context();
  72. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
  73. libinput_log_set_handler(li, simple_log_handler);
  74. log_handler_context = li;
  75. libinput_path_add_device(li, "/tmp");
  76. litest_assert_int_gt(log_handler_called, 0);
  77. log_handler_context = NULL;
  78. log_handler_called = 0;
  79. }
  80. END_TEST
  81. START_TEST(log_handler_NULL)
  82. {
  83. log_handler_called = 0;
  84. _litest_context_destroy_ struct libinput *li = litest_create_context();
  85. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
  86. libinput_log_set_handler(li, NULL);
  87. libinput_path_add_device(li, "/tmp");
  88. litest_assert_int_eq(log_handler_called, 0);
  89. log_handler_called = 0;
  90. }
  91. END_TEST
  92. START_TEST(log_priority)
  93. {
  94. log_handler_context = NULL;
  95. log_handler_called = 0;
  96. _litest_context_destroy_ struct libinput *li = litest_create_context();
  97. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_ERROR);
  98. libinput_log_set_handler(li, simple_log_handler);
  99. log_handler_context = li;
  100. libinput_path_add_device(li, "/tmp");
  101. litest_assert_int_eq(log_handler_called, 1);
  102. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
  103. /* event0 exists on any box we care to run the test suite on and we
  104. * currently prints *something* for each device */
  105. libinput_path_add_device(li, "/dev/input/event0");
  106. litest_assert_int_gt(log_handler_called, 1);
  107. log_handler_context = NULL;
  108. log_handler_called = 0;
  109. }
  110. END_TEST
  111. static int axisrange_log_handler_called = 0;
  112. static void
  113. axisrange_warning_log_handler(struct libinput *libinput,
  114. enum libinput_log_priority priority,
  115. const char *format,
  116. va_list args)
  117. {
  118. axisrange_log_handler_called++;
  119. litest_assert_notnull(format);
  120. litest_assert_str_in("is outside expected range", format);
  121. }
  122. START_TEST(log_axisrange_warning)
  123. {
  124. struct litest_device *dev = litest_current_device();
  125. struct libinput *li = dev->libinput;
  126. const struct input_absinfo *abs;
  127. int axis = litest_test_param_get_i32(test_env->params, "axis");
  128. litest_touch_down(dev, 0, 90, 100);
  129. litest_drain_events(li);
  130. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
  131. libinput_log_set_handler(li, axisrange_warning_log_handler);
  132. abs = libevdev_get_abs_info(dev->evdev, axis);
  133. for (int i = 0; i < 100; i++) {
  134. litest_event(dev,
  135. EV_ABS,
  136. ABS_MT_POSITION_X + axis,
  137. abs->maximum * 2 + i);
  138. litest_event(dev, EV_ABS, axis, abs->maximum * 2);
  139. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  140. litest_dispatch(li);
  141. }
  142. /* Expect only one message per 5 min */
  143. litest_assert_int_eq(axisrange_log_handler_called, 1);
  144. libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_ERROR);
  145. litest_restore_log_handler(li);
  146. axisrange_log_handler_called = 0;
  147. }
  148. END_TEST
  149. TEST_COLLECTION(log)
  150. {
  151. /* clang-format off */
  152. litest_add_deviceless(log_default_priority);
  153. litest_add_deviceless(log_handler_invoked);
  154. litest_add_deviceless(log_handler_NULL);
  155. litest_add_no_device(log_priority);
  156. litest_with_parameters(params, "axis", 'I', 2, litest_named_i32(ABS_X), litest_named_i32(ABS_Y)) {
  157. /* mtdev clips to axis ranges */
  158. litest_add_parametrized(log_axisrange_warning, LITEST_TOUCH, LITEST_PROTOCOL_A, params);
  159. litest_add_parametrized(log_axisrange_warning, LITEST_TOUCHPAD, LITEST_ANY, params);
  160. }
  161. /* clang-format on */
  162. }