fling_animation_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2017 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 "remoting/client/ui/fling_animation.h"
  5. #include <cmath>
  6. #include "base/bind.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "base/test/simple_test_tick_clock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace remoting {
  11. namespace {
  12. const float kFlingTimeConstant = 325.f;
  13. } // namespace
  14. class FlingAnimationTest : public testing::Test {
  15. public:
  16. void SetUp() override;
  17. void TearDown() override;
  18. protected:
  19. void TickAnimation(base::TimeDelta time_delta);
  20. // Must be called after the animation ticks.
  21. void AssertDeltaChanged();
  22. FlingAnimation fling_animation_{
  23. kFlingTimeConstant,
  24. base::BindRepeating(&FlingAnimationTest::OnDeltaChanged,
  25. base::Unretained(this))};
  26. float received_dx_ = 0.f;
  27. float received_dy_ = 0.f;
  28. private:
  29. void OnDeltaChanged(float dx, float dy);
  30. bool change_received_ = false;
  31. base::SimpleTestTickClock mock_clock_;
  32. };
  33. void FlingAnimationTest::SetUp() {
  34. fling_animation_.SetTickClockForTest(&mock_clock_);
  35. }
  36. void FlingAnimationTest::TearDown() {
  37. ASSERT_FALSE(change_received_);
  38. }
  39. void FlingAnimationTest::TickAnimation(base::TimeDelta time_delta) {
  40. mock_clock_.Advance(time_delta);
  41. fling_animation_.Tick();
  42. }
  43. void FlingAnimationTest::AssertDeltaChanged() {
  44. ASSERT_TRUE(change_received_);
  45. change_received_ = false;
  46. }
  47. void FlingAnimationTest::OnDeltaChanged(float dx, float dy) {
  48. received_dx_ = dx;
  49. received_dy_ = dy;
  50. change_received_ = true;
  51. }
  52. TEST_F(FlingAnimationTest, TestNoFling) {
  53. EXPECT_FALSE(fling_animation_.IsAnimationInProgress());
  54. // This should not change the delta.
  55. TickAnimation(base::Milliseconds(100));
  56. }
  57. TEST_F(FlingAnimationTest, TestFlingWillEventuallyStop) {
  58. fling_animation_.SetVelocity(1500.f, 1200.f);
  59. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  60. TickAnimation(base::Minutes(1));
  61. EXPECT_FALSE(fling_animation_.IsAnimationInProgress());
  62. }
  63. TEST_F(FlingAnimationTest, TestFlingDeltaIsDecreasing) {
  64. fling_animation_.SetVelocity(1500.f, 1200.f);
  65. float previous_dx = std::numeric_limits<float>::infinity();
  66. float previous_dy = std::numeric_limits<float>::infinity();
  67. while (true) {
  68. TickAnimation(base::Milliseconds(16));
  69. if (!fling_animation_.IsAnimationInProgress()) {
  70. break;
  71. }
  72. AssertDeltaChanged();
  73. float hyp =
  74. std::sqrt(received_dx_ * received_dx_ + received_dy_ * received_dy_);
  75. float prev_hyp =
  76. std::sqrt(previous_dx * previous_dx + previous_dy * previous_dy);
  77. EXPECT_LT(hyp, prev_hyp);
  78. previous_dx = received_dx_;
  79. previous_dy = received_dy_;
  80. }
  81. }
  82. TEST_F(FlingAnimationTest, TestIgnoreLowVelocity) {
  83. fling_animation_.SetVelocity(5.f, 5.f);
  84. EXPECT_FALSE(fling_animation_.IsAnimationInProgress());
  85. // This should not change the delta.
  86. TickAnimation(base::Milliseconds(5));
  87. }
  88. TEST_F(FlingAnimationTest, TestAbortAnimation) {
  89. fling_animation_.SetVelocity(1500.f, 1200.f);
  90. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  91. TickAnimation(base::Milliseconds(16));
  92. AssertDeltaChanged();
  93. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  94. fling_animation_.Abort();
  95. EXPECT_FALSE(fling_animation_.IsAnimationInProgress());
  96. }
  97. TEST_F(FlingAnimationTest, TestResetVelocity) {
  98. fling_animation_.SetVelocity(1000.f, -1000.f);
  99. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  100. TickAnimation(base::Milliseconds(16));
  101. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  102. AssertDeltaChanged();
  103. EXPECT_GT(received_dx_, 0);
  104. EXPECT_LT(received_dy_, 0);
  105. fling_animation_.SetVelocity(-1000.f, 1000.f);
  106. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  107. TickAnimation(base::Milliseconds(16));
  108. EXPECT_TRUE(fling_animation_.IsAnimationInProgress());
  109. AssertDeltaChanged();
  110. EXPECT_LT(received_dx_, 0);
  111. EXPECT_GT(received_dy_, 0);
  112. }
  113. } // namespace remoting