inputTest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "inputTest.h"
  2. using namespace dsr;
  3. #define TASK_IS(INDEX) context.taskIndex == INDEX
  4. #define EVENT_TYPE_IS(TYPE) event.mouseEventType == MouseEventType::TYPE
  5. void inputTests_populate(List<Test> &target, int buttonCount, bool relative, bool verticalScroll) {
  6. if (buttonCount >= 3) {
  7. target.pushConstruct(
  8. U"Mouse button test"
  9. ,
  10. [](AlignedImageRgbaU8 &canvas, TestContext &context) {
  11. image_fill(canvas, ColorRgbaI32(255, 255, 255, 255));
  12. context.drawAides(canvas);
  13. if (TASK_IS(0)) {
  14. font_printLine(canvas, font_getDefault(), U"Press down the left mouse button, .", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  15. } else if (TASK_IS(1)) {
  16. font_printLine(canvas, font_getDefault(), U"Release the left mouse button.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  17. } else if (TASK_IS(2)) {
  18. font_printLine(canvas, font_getDefault(), U"Press down the right mouse button.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  19. } else if (TASK_IS(3)) {
  20. font_printLine(canvas, font_getDefault(), U"Release the right mouse button.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  21. } else if (TASK_IS(4)) {
  22. font_printLine(canvas, font_getDefault(), U"Press down the middle mouse button.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  23. } else if (TASK_IS(5)) {
  24. font_printLine(canvas, font_getDefault(), U"Release the middle mouse button.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  25. }
  26. // TODO: Draw expanding circles from lowering and raising buttons together with names of the keys used.
  27. // TODO: Draw fading lines from move events.
  28. }
  29. ,
  30. [](const MouseEvent& event, TestContext &context) {
  31. if (EVENT_TYPE_IS(MouseDown)) {
  32. if (TASK_IS(0) && event.key == MouseKeyEnum::Left) {
  33. context.passTask();
  34. } else if (TASK_IS(2) && event.key == MouseKeyEnum::Right) {
  35. context.passTask();
  36. } else if (TASK_IS(4) && event.key == MouseKeyEnum::Middle) {
  37. context.passTask();
  38. } else {
  39. sendWarning(U"Detected a different key!\n");
  40. }
  41. } else if (EVENT_TYPE_IS(MouseUp)) {
  42. if (TASK_IS(1) && event.key == MouseKeyEnum::Left) {
  43. context.passTask();
  44. } else if (TASK_IS(3) && event.key == MouseKeyEnum::Right) {
  45. context.passTask();
  46. } else if (TASK_IS(5) && event.key == MouseKeyEnum::Middle) {
  47. context.finishTest(Grade::Passed);
  48. } else {
  49. sendWarning(U"Detected a different key!\n");
  50. }
  51. }
  52. }
  53. ,
  54. [](const KeyboardEvent& event, TestContext &context) {
  55. sendWarning(U"Detected a keyboard event with ", event.dsrKey, " instead of a mouse button!\n");
  56. }
  57. ,
  58. false
  59. );
  60. } else {
  61. sendWarning(U"Skipped the mouse button test due to settings.\n");
  62. }
  63. if (buttonCount >= 1) {
  64. target.pushConstruct(
  65. U"Mouse drag test"
  66. ,
  67. [](AlignedImageRgbaU8 &canvas, TestContext &context) {
  68. image_fill(canvas, ColorRgbaI32(255, 255, 255, 255));
  69. context.drawAides(canvas);
  70. if (TASK_IS(0)) {
  71. font_printLine(canvas, font_getDefault(), U"Hover the cursor over the window.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  72. } else if (TASK_IS(1)) {
  73. font_printLine(canvas, font_getDefault(), U"Press down the left mouse key.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  74. } else if (TASK_IS(2)) {
  75. font_printLine(canvas, font_getDefault(), U"Drag the mouse over the window with the left key pressed down.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  76. } else if (TASK_IS(3)) {
  77. font_printLine(canvas, font_getDefault(), U"Release the left key.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  78. }
  79. }
  80. ,
  81. [](const MouseEvent& event, TestContext &context) {
  82. if (TASK_IS(0) && EVENT_TYPE_IS(MouseMove) && !context.leftMouseDown && !context.middleMouseDown && !context.rightMouseDown) {
  83. context.passTask();
  84. } else if (TASK_IS(1) && EVENT_TYPE_IS(MouseDown)) {
  85. if (event.key == MouseKeyEnum::Left) {
  86. context.passTask();
  87. } else {
  88. // TODO: Say which key was triggered instead and suggest skipping with escape if it can not be found.
  89. }
  90. } else if (TASK_IS(2) && EVENT_TYPE_IS(MouseMove) && context.leftMouseDown) {
  91. context.passTask();
  92. } else if (TASK_IS(3) && EVENT_TYPE_IS(MouseUp)) {
  93. if (event.key == MouseKeyEnum::Left) {
  94. context.finishTest(Grade::Passed);
  95. } else {
  96. // TODO: Say which key was triggered instead and suggest skipping with escape if it can not be found.
  97. }
  98. }
  99. }
  100. ,
  101. [](const KeyboardEvent& event, TestContext &context) {}
  102. ,
  103. false
  104. );
  105. } else {
  106. sendWarning(U"Skipped the mouse button drag test due to settings.\n");
  107. }
  108. if (buttonCount >= 1 && verticalScroll) {
  109. target.pushConstruct(
  110. U"Mouse scroll test"
  111. ,
  112. [](AlignedImageRgbaU8 &canvas, TestContext &context) {
  113. image_fill(canvas, ColorRgbaI32(255, 255, 255, 255));
  114. context.drawAides(canvas);
  115. // TODO: Show something moving in the background while scrolling to show the direction. Only pass once the end has been reached.
  116. if (TASK_IS(0)) {
  117. font_printLine(canvas, font_getDefault(), U"Scroll in the direction used to reach the top of a document by moving content down.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  118. } else if (TASK_IS(1)) {
  119. font_printLine(canvas, font_getDefault(), U"Click when you are down scrolling up.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  120. } else if (TASK_IS(2)) {
  121. font_printLine(canvas, font_getDefault(), U"Scroll in the direction used to reach the bottom of a document by moving content up.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  122. } else if (TASK_IS(3)) {
  123. font_printLine(canvas, font_getDefault(), U"Click when you are down scrolling down.", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  124. }
  125. }
  126. ,
  127. [](const MouseEvent& event, TestContext &context) {
  128. // Due to many laptops having the scroll direction inverted by default so that draging down scrolls up
  129. // Comparing how scrolling works in external text editors would be a useful addition to this test,
  130. // because many users probably don't know what is scrolling up and what is scrolling down when the direction is inverted on a track-pad.
  131. if (TASK_IS(0) && EVENT_TYPE_IS(Scroll)) {
  132. if (event.key == MouseKeyEnum::ScrollUp) {
  133. context.passTask();
  134. } else if (event.key == MouseKeyEnum::ScrollDown) {
  135. // Just give a warning, because this test can be very
  136. sendWarning(U"Scroll down was detected when attempting to scroll up. Compare the scrolling direction of a textbox with an external text editor to ensure consistent behavior.\n");
  137. }
  138. } else if (TASK_IS(1) && EVENT_TYPE_IS(MouseDown)) {
  139. context.passTask();
  140. } else if (TASK_IS(2) && EVENT_TYPE_IS(Scroll)) {
  141. if (event.key == MouseKeyEnum::ScrollDown) {
  142. context.passTask();
  143. } else if (event.key == MouseKeyEnum::ScrollUp) {
  144. sendWarning(U"Scroll up was detected when attempting to scroll down. Compare the scrolling direction of a textbox with an external text editor to ensure consistent behavior.\n");
  145. }
  146. } else if (TASK_IS(3) && EVENT_TYPE_IS(MouseDown)) {
  147. context.finishTest(Grade::Passed);
  148. }
  149. }
  150. ,
  151. [](const KeyboardEvent& event, TestContext &context) {}
  152. ,
  153. false
  154. );
  155. } else {
  156. sendWarning(U"Skipped the vertical scroll test due to settings.\n");
  157. }
  158. target.pushConstruct(
  159. U"Keyboard test"
  160. ,
  161. [](AlignedImageRgbaU8 &canvas, TestContext &context) {
  162. image_fill(canvas, ColorRgbaI32(255, 255, 255, 255));
  163. // TODO: Draw a keyboard showing which key is expected and which key was detected.
  164. if (context.taskIndex < DsrKey::DsrKey_Unhandled) {
  165. font_printLine(canvas, font_getDefault(), string_combine(U"Press down ", (DsrKey)context.taskIndex, " on your physical or virtual keyboard."), IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  166. }
  167. }
  168. ,
  169. [](const MouseEvent& event, TestContext &context) {
  170. sendWarning(U"Detected a mouse event with ", event.key, " instead of a keyboard event!\n");
  171. }
  172. ,
  173. [](const KeyboardEvent& event, TestContext &context) {
  174. if (event.keyboardEventType == KeyboardEventType::KeyDown && event.dsrKey == context.taskIndex) {
  175. // TODO: Require both down and up events.
  176. if (context.taskIndex >= DsrKey::DsrKey_Unhandled - 1) {
  177. context.finishTest(Grade::Passed);
  178. } else {
  179. context.passTask();
  180. // Skip testing the escape key, because it is currently reserved for skipping tests.
  181. if (context.taskIndex == DsrKey::DsrKey_Escape) {
  182. context.passTask();
  183. }
  184. }
  185. }
  186. }
  187. ,
  188. false
  189. );
  190. }