snap_fling_curve_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2018 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 "cc/input/snap_fling_curve.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace cc {
  7. namespace test {
  8. TEST(SnapFlingCurveTest, CurveInitialization) {
  9. SnapFlingCurve active_curve(gfx::PointF(100, 100), gfx::PointF(500, 500),
  10. base::TimeTicks());
  11. EXPECT_FALSE(active_curve.IsFinished());
  12. SnapFlingCurve finished_curve(gfx::PointF(100, 100), gfx::PointF(100, 100),
  13. base::TimeTicks());
  14. EXPECT_TRUE(finished_curve.IsFinished());
  15. }
  16. TEST(SnapFlingCurveTest, AdvanceHalfwayThrough) {
  17. SnapFlingCurve curve(gfx::PointF(100, 100), gfx::PointF(500, 500),
  18. base::TimeTicks());
  19. base::TimeDelta duration = curve.duration();
  20. gfx::Vector2dF delta1 =
  21. curve.GetScrollDelta(base::TimeTicks() + duration / 2);
  22. EXPECT_LT(0, delta1.x());
  23. EXPECT_LT(0, delta1.y());
  24. EXPECT_FALSE(curve.IsFinished());
  25. // Repeated offset computations at the same timestamp before applying the
  26. // scrolled delta should yield identical results.
  27. gfx::Vector2dF delta2 =
  28. curve.GetScrollDelta(base::TimeTicks() + duration / 2);
  29. EXPECT_EQ(delta1, delta2);
  30. EXPECT_FALSE(curve.IsFinished());
  31. curve.UpdateCurrentOffset(gfx::PointF(100, 100) + delta1);
  32. EXPECT_FALSE(curve.IsFinished());
  33. }
  34. TEST(SnapFlingCurveTest, AdvanceFullyThrough) {
  35. SnapFlingCurve curve(gfx::PointF(100, 100), gfx::PointF(500, 500),
  36. base::TimeTicks());
  37. gfx::Vector2dF delta =
  38. curve.GetScrollDelta(base::TimeTicks() + curve.duration());
  39. EXPECT_EQ(gfx::Vector2dF(400, 400), delta);
  40. EXPECT_TRUE(curve.IsFinished());
  41. }
  42. TEST(SnapFlingCurveTest, ReturnsZeroAfterFinished) {
  43. SnapFlingCurve curve(gfx::PointF(100, 100), gfx::PointF(500, 500),
  44. base::TimeTicks());
  45. curve.UpdateCurrentOffset(gfx::PointF(500, 500));
  46. gfx::Vector2dF delta = curve.GetScrollDelta(base::TimeTicks());
  47. EXPECT_EQ(gfx::Vector2dF(), delta);
  48. EXPECT_TRUE(curve.IsFinished());
  49. delta = curve.GetScrollDelta(base::TimeTicks() + curve.duration());
  50. EXPECT_EQ(gfx::Vector2dF(), delta);
  51. EXPECT_TRUE(curve.IsFinished());
  52. }
  53. TEST(SnapFlingCurveTest, FlingFinishesWithinOnePixel) {
  54. SnapFlingCurve curve(gfx::PointF(0, 0), gfx::PointF(100.5, 99.5),
  55. base::TimeTicks());
  56. EXPECT_FALSE(curve.IsFinished());
  57. curve.UpdateCurrentOffset(gfx::PointF(99, 101));
  58. // IsFinished() is updated in GetScrollDelta().
  59. curve.GetScrollDelta(base::TimeTicks());
  60. EXPECT_FALSE(curve.IsFinished());
  61. curve.UpdateCurrentOffset(gfx::PointF(100, 100));
  62. curve.GetScrollDelta(base::TimeTicks());
  63. EXPECT_TRUE(curve.IsFinished());
  64. }
  65. } // namespace test
  66. } // namespace cc