snap_scroll_controller_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/events/gesture_detection/snap_scroll_controller.h"
  5. #include "base/time/time.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "ui/events/test/motion_event_test_utils.h"
  8. using base::TimeTicks;
  9. using ui::test::MockMotionEvent;
  10. namespace ui {
  11. namespace {
  12. const float kSnapBound = 8.f;
  13. gfx::SizeF GetDisplayBounds() {
  14. return gfx::SizeF(640, 480);
  15. }
  16. } // namespace
  17. TEST(SnapScrollControllerTest, Basic) {
  18. SnapScrollController controller(kSnapBound, GetDisplayBounds());
  19. EXPECT_FALSE(controller.IsSnappingScrolls());
  20. EXPECT_FALSE(controller.IsSnapHorizontal());
  21. EXPECT_FALSE(controller.IsSnapVertical());
  22. // Test basic horizontal snapping.
  23. MockMotionEvent event;
  24. event.PressPoint(0, 0);
  25. controller.SetSnapScrollMode(event, false);
  26. EXPECT_FALSE(controller.IsSnappingScrolls());
  27. event.MovePoint(0, kSnapBound * 2, 0.f);
  28. controller.SetSnapScrollMode(event, false);
  29. EXPECT_TRUE(controller.IsSnappingScrolls());
  30. EXPECT_TRUE(controller.IsSnapHorizontal());
  31. EXPECT_FALSE(controller.IsSnapVertical());
  32. event.ReleasePoint();
  33. controller.SetSnapScrollMode(event, false);
  34. // Test basic vertical snapping.
  35. event.PressPoint(0, 0);
  36. controller.SetSnapScrollMode(event, false);
  37. EXPECT_FALSE(controller.IsSnappingScrolls());
  38. event.MovePoint(0, 0.f, kSnapBound * 2);
  39. controller.SetSnapScrollMode(event, false);
  40. EXPECT_TRUE(controller.IsSnappingScrolls());
  41. EXPECT_TRUE(controller.IsSnapVertical());
  42. EXPECT_FALSE(controller.IsSnapHorizontal());
  43. }
  44. TEST(SnapScrollControllerTest, VerticalScroll) {
  45. SnapScrollController controller(kSnapBound, GetDisplayBounds());
  46. EXPECT_FALSE(controller.IsSnappingScrolls());
  47. MockMotionEvent event;
  48. event.PressPoint(0, 0);
  49. controller.SetSnapScrollMode(event, false);
  50. EXPECT_FALSE(controller.IsSnappingScrolls());
  51. event.MovePoint(0, 0.f, -kSnapBound / 2.f);
  52. controller.SetSnapScrollMode(event, false);
  53. EXPECT_FALSE(controller.IsSnappingScrolls());
  54. event.MovePoint(0, kSnapBound / 2.f, -kSnapBound * 2.f);
  55. controller.SetSnapScrollMode(event, false);
  56. EXPECT_TRUE(controller.IsSnapVertical());
  57. EXPECT_FALSE(controller.IsSnapHorizontal());
  58. // Initial scrolling should be snapped.
  59. float delta_x = event.GetX(0);
  60. float delta_y = event.GetY(0);
  61. controller.UpdateSnapScrollMode(delta_x, delta_y);
  62. EXPECT_TRUE(controller.IsSnapVertical());
  63. EXPECT_FALSE(controller.IsSnapHorizontal());
  64. // Subsequent scrolling should be snapped as long as it's within the rails.
  65. delta_x = 5;
  66. delta_y = 10;
  67. controller.UpdateSnapScrollMode(delta_x, delta_y);
  68. EXPECT_TRUE(controller.IsSnapVertical());
  69. EXPECT_FALSE(controller.IsSnapHorizontal());
  70. // Large horizontal movement should end snapping.
  71. delta_x = 100;
  72. delta_y = 10;
  73. controller.UpdateSnapScrollMode(delta_x, delta_y);
  74. EXPECT_FALSE(controller.IsSnappingScrolls());
  75. }
  76. TEST(SnapScrollControllerTest, HorizontalScroll) {
  77. SnapScrollController controller(kSnapBound, GetDisplayBounds());
  78. EXPECT_FALSE(controller.IsSnappingScrolls());
  79. MockMotionEvent event;
  80. event.PressPoint(0, 0);
  81. controller.SetSnapScrollMode(event, false);
  82. EXPECT_FALSE(controller.IsSnappingScrolls());
  83. event.MovePoint(0, -kSnapBound / 2.f, 0.f);
  84. controller.SetSnapScrollMode(event, false);
  85. EXPECT_FALSE(controller.IsSnappingScrolls());
  86. event.MovePoint(0, kSnapBound * 2.f, kSnapBound / 2.f);
  87. controller.SetSnapScrollMode(event, false);
  88. EXPECT_TRUE(controller.IsSnapHorizontal());
  89. EXPECT_FALSE(controller.IsSnapVertical());
  90. // Initial scrolling should be snapped.
  91. float delta_x = event.GetX(0);
  92. float delta_y = event.GetY(0);
  93. controller.UpdateSnapScrollMode(delta_x, delta_y);
  94. EXPECT_TRUE(controller.IsSnapHorizontal());
  95. EXPECT_FALSE(controller.IsSnapVertical());
  96. // Subsequent scrolling should be snapped as long as it's within the rails.
  97. delta_x = 10;
  98. delta_y = 5;
  99. controller.UpdateSnapScrollMode(delta_x, delta_y);
  100. EXPECT_TRUE(controller.IsSnapHorizontal());
  101. EXPECT_FALSE(controller.IsSnapVertical());
  102. // Large vertical movement should end snapping.
  103. delta_x = 10;
  104. delta_y = 100;
  105. controller.UpdateSnapScrollMode(delta_x, delta_y);
  106. EXPECT_FALSE(controller.IsSnappingScrolls());
  107. }
  108. TEST(SnapScrollControllerTest, Diagonal) {
  109. SnapScrollController controller(kSnapBound, GetDisplayBounds());
  110. EXPECT_FALSE(controller.IsSnappingScrolls());
  111. MockMotionEvent event;
  112. event.PressPoint(0, 0);
  113. controller.SetSnapScrollMode(event, false);
  114. EXPECT_FALSE(controller.IsSnappingScrolls());
  115. // Sufficient initial diagonal motion will prevent any future snapping.
  116. event.MovePoint(0, kSnapBound * 3.f, -kSnapBound * 3.f);
  117. controller.SetSnapScrollMode(event, false);
  118. EXPECT_FALSE(controller.IsSnappingScrolls());
  119. float delta_x = event.GetX(0);
  120. float delta_y = event.GetY(0);
  121. controller.UpdateSnapScrollMode(delta_x, delta_y);
  122. EXPECT_FALSE(controller.IsSnappingScrolls());
  123. event.MovePoint(0, 0, 0);
  124. controller.SetSnapScrollMode(event, false);
  125. EXPECT_FALSE(controller.IsSnappingScrolls());
  126. event.MovePoint(0, kSnapBound * 5, 0);
  127. controller.SetSnapScrollMode(event, false);
  128. EXPECT_FALSE(controller.IsSnappingScrolls());
  129. event.MovePoint(0, 0, -kSnapBound * 5);
  130. controller.SetSnapScrollMode(event, false);
  131. EXPECT_FALSE(controller.IsSnappingScrolls());
  132. }
  133. } // namespace ui