touchpad_gesture_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Host-side checks for the device-independent touchpad gesture engine. */
  2. #include <stdio.h>
  3. #include "touchpad_gesture.h"
  4. static struct tpg_state tp;
  5. static int failures;
  6. static void check(const char *name, int cond)
  7. {
  8. printf("%s: %s\n", cond ? "ok" : "FAIL", name);
  9. if (!cond)
  10. failures++;
  11. }
  12. static void frame(struct tpg_result *r, unsigned int count,
  13. int x0, int y0, int x1, int y1, int button,
  14. unsigned int now)
  15. {
  16. struct tpg_frame f;
  17. f.count = count;
  18. f.mask = 0;
  19. if (count >= 1)
  20. f.mask |= 1u;
  21. if (count >= 2)
  22. f.mask |= 2u;
  23. f.contact[0].x = x0;
  24. f.contact[0].y = y0;
  25. f.contact[1].x = x1;
  26. f.contact[1].y = y1;
  27. f.button = button;
  28. f.now_ms = now;
  29. tpg_frame(&tp, &f, r);
  30. }
  31. static unsigned int tick(unsigned int now)
  32. {
  33. struct tpg_result r;
  34. tpg_tick(&tp, now, &r);
  35. return r.buttons;
  36. }
  37. static void reset(void)
  38. {
  39. tpg_init(&tp, 3000, 2000, 2);
  40. }
  41. int main(void)
  42. {
  43. struct tpg_result r;
  44. unsigned int now;
  45. int scroll;
  46. reset();
  47. check("default options include scroll and taps",
  48. (tpg_options(&tp) & (TPG_TAPCLICK | TPG_RIGHT_TAP |
  49. TPG_RIGHT_CORNER | TPG_SCROLL)) ==
  50. (TPG_TAPCLICK | TPG_RIGHT_TAP | TPG_RIGHT_CORNER | TPG_SCROLL));
  51. /* One-finger tap -> left. */
  52. now = 1000;
  53. frame(&r, 1, 1000, 1000, 0, 0, 0, now);
  54. frame(&r, 1, 1000, 1000, 0, 0, 0, now + 50);
  55. frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
  56. check("one-finger tap emits left", r.buttons == 1);
  57. check("one-finger tap releases", tick(now + 250) == 0);
  58. /* Two-finger tap -> right. */
  59. now = 2000;
  60. frame(&r, 1, 1000, 1000, 0, 0, 0, now);
  61. frame(&r, 2, 1000, 1000, 1200, 1000, 0, now + 50);
  62. frame(&r, 2, 1000, 1000, 1200, 1000, 0, now + 100);
  63. frame(&r, 0, 0, 0, 0, 0, 0, now + 150);
  64. check("two-finger tap emits right", r.buttons == 2);
  65. check("two-finger tap releases", tick(now + 300) == 0);
  66. /* Corner tap -> right; normal tap stays left. */
  67. now = 3000;
  68. frame(&r, 1, 2800, 200, 0, 0, 0, now);
  69. frame(&r, 1, 2800, 200, 0, 0, 0, now + 50);
  70. frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
  71. check("corner tap emits right", r.buttons == 2);
  72. tick(now + 250);
  73. /* Physical corner press -> right and latched; outside -> left. */
  74. now = 4000;
  75. frame(&r, 1, 2800, 200, 0, 0, 1, now);
  76. check("physical corner press is right", r.buttons == 2);
  77. frame(&r, 1, 1000, 1000, 0, 0, 1, now + 50);
  78. check("physical corner latch survives movement", r.buttons == 2);
  79. frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
  80. check("physical corner press releases", r.buttons == 0);
  81. now = 5000;
  82. frame(&r, 1, 1000, 1000, 0, 0, 1, now);
  83. check("physical normal press is left", r.buttons == 1);
  84. frame(&r, 0, 0, 0, 0, 0, 0, now + 50);
  85. /* A moving one-finger contact is a pointer move, not a click. */
  86. now = 6000;
  87. frame(&r, 1, 1000, 1000, 0, 0, 0, now);
  88. frame(&r, 1, 1200, 1000, 0, 0, 0, now + 50);
  89. check("one-finger drag moves the pointer", r.dx > 0);
  90. frame(&r, 0, 0, 0, 0, 0, 0, now + 100);
  91. check("one-finger drag does not click", r.buttons == 0);
  92. /* Two-finger scroll: pointer frozen, positive pixel scroll, no click. */
  93. now = 7000;
  94. reset();
  95. frame(&r, 2, 1000, 1000, 1400, 1000, 0, now);
  96. scroll = 0;
  97. frame(&r, 2, 1000, 1020, 1400, 1020, 0, now + 25);
  98. frame(&r, 2, 1000, 1040, 1400, 1040, 0, now + 50);
  99. frame(&r, 2, 1000, 1060, 1400, 1060, 0, now + 75);
  100. scroll += r.scroll;
  101. check("two-finger scroll does not move the pointer", !r.dx && !r.dy);
  102. frame(&r, 2, 1000, 1100, 1400, 1100, 0, now + 100);
  103. scroll += r.scroll;
  104. frame(&r, 2, 1000, 1160, 1400, 1160, 0, now + 125);
  105. scroll += r.scroll;
  106. frame(&r, 0, 0, 0, 0, 0, 0, now + 150);
  107. check("two-finger scroll emits positive pixels", scroll > 0);
  108. check("two-finger scroll does not click", r.buttons == 0);
  109. check("two-finger scroll releases cleanly", tick(now + 300) == 0);
  110. /* Traditional direction inverts the output. */
  111. reset();
  112. tpg_set_options(&tp, tpg_options(&tp) | TPG_SCROLL_INVERT);
  113. now = 8000;
  114. frame(&r, 2, 1000, 1000, 1400, 1000, 0, now);
  115. frame(&r, 2, 1000, 1040, 1400, 1040, 0, now + 25);
  116. frame(&r, 2, 1000, 1120, 1400, 1120, 0, now + 50);
  117. check("traditional scroll inverts direction", r.scroll < 0);
  118. /* Two-finger tap with the first slot lifting early still right-clicks. */
  119. reset();
  120. now = 9000;
  121. frame(&r, 1, 1000, 1000, 0, 0, 0, now);
  122. frame(&r, 2, 1000, 1000, 1200, 1000, 0, now + 50);
  123. frame(&r, 1, 1200, 1000, 0, 0, 0, now + 100);
  124. frame(&r, 0, 0, 0, 0, 0, 0, now + 150);
  125. check("two-finger tap survives first-slot lift", r.buttons == 2);
  126. /* Pointer re-anchors after the second finger leaves. */
  127. reset();
  128. now = 10000;
  129. frame(&r, 1, 1000, 1000, 0, 0, 0, now);
  130. frame(&r, 1, 1100, 1000, 0, 0, 0, now + 25);
  131. frame(&r, 2, 1100, 1000, 1500, 1000, 0, now + 50);
  132. frame(&r, 1, 1500, 1000, 0, 0, 0, now + 75);
  133. check("returning to one finger re-anchors", r.dx == 0 && r.dy == 0);
  134. frame(&r, 1, 1600, 1000, 0, 0, 0, now + 100);
  135. check("pointer resumes after re-anchor", r.dx > 0);
  136. /* Scroll disabled: no output and no accidental right-click. */
  137. reset();
  138. tpg_set_options(&tp, tpg_options(&tp) & ~TPG_SCROLL);
  139. now = 11000;
  140. frame(&r, 2, 1000, 1000, 1400, 1000, 0, now);
  141. frame(&r, 2, 1000, 1040, 1400, 1040, 0, now + 25);
  142. frame(&r, 2, 1000, 1120, 1400, 1120, 0, now + 50);
  143. check("scroll off emits no scroll", r.scroll == 0);
  144. frame(&r, 0, 0, 0, 0, 0, 0, now + 75);
  145. check("scroll off still suppresses two-finger click", r.buttons == 0);
  146. /* A long press is not a tap. */
  147. reset();
  148. now = 12000;
  149. frame(&r, 1, 1000, 1000, 0, 0, 0, now);
  150. frame(&r, 0, 0, 0, 0, 0, 0, now + 400);
  151. check("long press does not click", r.buttons == 0);
  152. printf(failures ? "FAILURES: %d\n" : "all gesture checks passed\n",
  153. failures);
  154. return failures ? 1 : 0;
  155. }