summaryrefslogtreecommitdiffstats
path: root/cc/animation/scroll_offset_animation_curve_unittest.cc
blob: 1b8fdb5467c0ad4c4fc8a50f2d7c060f38fcaca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/animation/scroll_offset_animation_curve.h"

#include "cc/animation/timing_function.h"
#include "cc/test/geometry_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
namespace {

TEST(ScrollOffsetAnimationCurveTest, Duration) {
  gfx::Vector2dF target_value(100.f, 200.f);
  scoped_ptr<ScrollOffsetAnimationCurve> curve(
      ScrollOffsetAnimationCurve::Create(
          target_value,
          EaseInOutTimingFunction::Create().Pass()));

  curve->SetInitialValue(target_value);
  EXPECT_DOUBLE_EQ(0.0, curve->Duration());

  // x decreases, y stays the same.
  curve->SetInitialValue(gfx::Vector2dF(136.f, 200.f));
  EXPECT_DOUBLE_EQ(0.1, curve->Duration());

  // x increases, y stays the same.
  curve->SetInitialValue(gfx::Vector2dF(19.f, 200.f));
  EXPECT_DOUBLE_EQ(0.15, curve->Duration());

  // x stays the same, y decreases.
  curve->SetInitialValue(gfx::Vector2dF(100.f, 344.f));
  EXPECT_DOUBLE_EQ(0.2, curve->Duration());

  // x stays the same, y increases.
  curve->SetInitialValue(gfx::Vector2dF(100.f, 191.f));
  EXPECT_DOUBLE_EQ(0.05, curve->Duration());

  // x decreases, y decreases.
  curve->SetInitialValue(gfx::Vector2dF(32500.f, 500.f));
  EXPECT_DOUBLE_EQ(3.0, curve->Duration());

  // x decreases, y increases.
  curve->SetInitialValue(gfx::Vector2dF(150.f, 119.f));
  EXPECT_DOUBLE_EQ(0.15, curve->Duration());

  // x increases, y decreases.
  curve->SetInitialValue(gfx::Vector2dF(0.f, 14600.f));
  EXPECT_DOUBLE_EQ(2.0, curve->Duration());

  // x increases, y increases.
  curve->SetInitialValue(gfx::Vector2dF(95.f, 191.f));
  EXPECT_DOUBLE_EQ(0.05, curve->Duration());
}

TEST(ScrollOffsetAnimationCurveTest, GetValue) {
  gfx::Vector2dF initial_value(2.f, 40.f);
  gfx::Vector2dF target_value(10.f, 20.f);
  scoped_ptr<ScrollOffsetAnimationCurve> curve(
      ScrollOffsetAnimationCurve::Create(
          target_value,
          EaseInOutTimingFunction::Create().Pass()));
  curve->SetInitialValue(initial_value);

  double duration = curve->Duration();
  EXPECT_GT(curve->Duration(), 0);
  EXPECT_LT(curve->Duration(), 0.1);

  EXPECT_EQ(AnimationCurve::ScrollOffset, curve->Type());
  EXPECT_EQ(duration, curve->Duration());

  EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(-1.0));
  EXPECT_VECTOR2DF_EQ(initial_value, curve->GetValue(0.0));
  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(6.f, 30.f), curve->GetValue(duration/2.0));
  EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration));
  EXPECT_VECTOR2DF_EQ(target_value, curve->GetValue(duration+1.0));

  // Verify that GetValue takes the timing function into account.
  gfx::Vector2dF value = curve->GetValue(duration/4.0);
  EXPECT_NEAR(3.0333f, value.x(), 0.00015f);
  EXPECT_NEAR(37.4168f, value.y(), 0.00015f);
}

// Verify that a clone behaves exactly like the original.
TEST(ScrollOffsetAnimationCurveTest, Clone) {
  gfx::Vector2dF initial_value(2.f, 40.f);
  gfx::Vector2dF target_value(10.f, 20.f);
  scoped_ptr<ScrollOffsetAnimationCurve> curve(
      ScrollOffsetAnimationCurve::Create(
          target_value,
          EaseInOutTimingFunction::Create().Pass()));
  curve->SetInitialValue(initial_value);
  double duration = curve->Duration();

  scoped_ptr<AnimationCurve> clone(curve->Clone().Pass());

  EXPECT_EQ(AnimationCurve::ScrollOffset, clone->Type());
  EXPECT_EQ(duration, clone->Duration());

  EXPECT_VECTOR2DF_EQ(initial_value,
                      clone->ToScrollOffsetAnimationCurve()->GetValue(-1.0));
  EXPECT_VECTOR2DF_EQ(initial_value,
                      clone->ToScrollOffsetAnimationCurve()->GetValue(0.0));
  EXPECT_VECTOR2DF_EQ(
      gfx::Vector2dF(6.f, 30.f),
      clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 2.0));
  EXPECT_VECTOR2DF_EQ(
      target_value,
      clone->ToScrollOffsetAnimationCurve()->GetValue(duration));
  EXPECT_VECTOR2DF_EQ(
      target_value,
      clone->ToScrollOffsetAnimationCurve()->GetValue(duration + 1.0));

  // Verify that the timing function was cloned correctly.
  gfx::Vector2dF value =
      clone->ToScrollOffsetAnimationCurve()->GetValue(duration / 4.0);
  EXPECT_NEAR(3.0333f, value.x(), 0.00015f);
  EXPECT_NEAR(37.4168f, value.y(), 0.00015f);
}

TEST(ScrollOffsetAnimationCurveTest, UpdateTarget) {
  gfx::Vector2dF initial_value(0.f, 0.f);
  gfx::Vector2dF target_value(0.f, 3600.f);
  scoped_ptr<ScrollOffsetAnimationCurve> curve(
      ScrollOffsetAnimationCurve::Create(
          target_value, EaseInOutTimingFunction::Create().Pass()));
  curve->SetInitialValue(initial_value);
  EXPECT_EQ(1.0, curve->Duration());
  EXPECT_EQ(1800.0, curve->GetValue(0.5).y());
  EXPECT_EQ(3600.0, curve->GetValue(1.0).y());

  curve->UpdateTarget(0.5, gfx::Vector2dF(0.0, 9900.0));

  EXPECT_EQ(2.0, curve->Duration());
  EXPECT_EQ(1800.0, curve->GetValue(0.5).y());
  EXPECT_NEAR(5566.49, curve->GetValue(1.0).y(), 0.01);
  EXPECT_EQ(9900.0, curve->GetValue(2.0).y());

  curve->UpdateTarget(1.0, gfx::Vector2dF(0.0, 7200.0));

  EXPECT_NEAR(1.674, curve->Duration(), 0.01);
  EXPECT_NEAR(5566.49, curve->GetValue(1.0).y(), 0.01);
  EXPECT_EQ(7200.0, curve->GetValue(1.674).y());
}

}  // namespace
}  // namespace cc