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