Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1139)

Side by Side Diff: third_party/WebKit/Source/platform/animation/UnitBezierTest.cpp

Issue 1846733003: UI GFX Geometry: Make UnitBezier a wrapper for gfx::CubicBezier (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add one more TODO for Range. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include "platform/animation/UnitBezier.h"
26
27 #include "base/memory/scoped_ptr.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29
30 namespace blink {
31
32 TEST(UnitBezierTest, BasicUse)
33 {
34 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
35 EXPECT_EQ(0.875, bezier.solve(0.5));
36 }
37
38 TEST(UnitBezierTest, Overshoot)
39 {
40 UnitBezier bezier(0.5, 2.0, 0.5, 2.0);
41 EXPECT_EQ(1.625, bezier.solve(0.5));
42 }
43
44 TEST(UnitBezierTest, Undershoot)
45 {
46 UnitBezier bezier(0.5, -1.0, 0.5, -1.0);
47 EXPECT_EQ(-0.625, bezier.solve(0.5));
48 }
49
50 TEST(UnitBezierTest, InputAtEdgeOfRange)
51 {
52 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
53 EXPECT_EQ(0.0, bezier.solve(0.0));
54 EXPECT_EQ(1.0, bezier.solve(1.0));
55 }
56
57 TEST(UnitBezierTest, InputOutOfRange)
58 {
59 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
60 EXPECT_EQ(-2.0, bezier.solve(-1.0));
61 EXPECT_EQ(1.0, bezier.solve(2.0));
62 }
63
64 TEST(UnitBezierTest, InputOutOfRangeLargeEpsilon)
65 {
66 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
67 EXPECT_EQ(-2.0, bezier.solveWithEpsilon(-1.0, 1.0));
68 EXPECT_EQ(1.0, bezier.solveWithEpsilon(2.0, 1.0));
69 }
70
71 TEST(UnitBezierTest, InputOutOfRangeCoincidentEndpoints)
72 {
73 UnitBezier bezier(0.0, 0.0, 1.0, 1.0);
74 EXPECT_EQ(-1.0, bezier.solve(-1.0));
75 EXPECT_EQ(2.0, bezier.solve(2.0));
76 }
77
78 TEST(UnitBezierTest, InputOutOfRangeVerticalGradient)
79 {
80 UnitBezier bezier(0.0, 1.0, 1.0, 0.0);
81 EXPECT_EQ(0.0, bezier.solve(-1.0));
82 EXPECT_EQ(1.0, bezier.solve(2.0));
83 }
84
85 TEST(UnitBezierTest, InputOutOfRangeDistinctEndpoints)
86 {
87 UnitBezier bezier(0.1, 0.2, 0.8, 0.8);
88 EXPECT_EQ(-2.0, bezier.solve(-1.0));
89 EXPECT_EQ(2.0, bezier.solve(2.0));
90 }
91
92 TEST(UnitBezierTest, Range)
93 {
94 double epsilon = 0.00015;
95 double min, max;
96
97 // Derivative is a constant.
98 scoped_ptr<UnitBezier> bezier(
99 new UnitBezier(0.25, (1.0 / 3.0), 0.75, (2.0 / 3.0)));
100 bezier->range(&min, &max);
101 EXPECT_EQ(0, min);
102 EXPECT_EQ(1, max);
103
104 // Derivative is linear.
105 bezier.reset(new UnitBezier(0.25, -0.5, 0.75, (-1.0 / 6.0)));
106 bezier->range(&min, &max);
107 EXPECT_NEAR(min, -0.225, epsilon);
108 EXPECT_EQ(1, max);
109
110 // Derivative has no real roots.
111 bezier.reset(new UnitBezier(0.25, 0.25, 0.75, 0.5));
112 bezier->range(&min, &max);
113 EXPECT_EQ(0, min);
114 EXPECT_EQ(1, max);
115
116 // Derivative has exactly one real root.
117 bezier.reset(new UnitBezier(0.0, 1.0, 1.0, 0.0));
118 bezier->range(&min, &max);
119 EXPECT_EQ(0, min);
120 EXPECT_EQ(1, max);
121
122 // Derivative has one root < 0 and one root > 1.
123 bezier.reset(new UnitBezier(0.25, 0.1, 0.75, 0.9));
124 bezier->range(&min, &max);
125 EXPECT_EQ(0, min);
126 EXPECT_EQ(1, max);
127
128 // Derivative has two roots in [0,1].
129 bezier.reset(new UnitBezier(0.25, 2.5, 0.75, 0.5));
130 bezier->range(&min, &max);
131 EXPECT_EQ(0, min);
132 EXPECT_NEAR(max, 1.28818, epsilon);
133 bezier.reset(new UnitBezier(0.25, 0.5, 0.75, -1.5));
134 bezier->range(&min, &max);
135 EXPECT_NEAR(min, -0.28818, epsilon);
136 EXPECT_EQ(1, max);
137
138 // Derivative has one root < 0 and one root in [0,1].
139 bezier.reset(new UnitBezier(0.25, 0.1, 0.75, 1.5));
140 bezier->range(&min, &max);
141 EXPECT_EQ(0, min);
142 EXPECT_NEAR(max, 1.10755, epsilon);
143
144 // Derivative has one root in [0,1] and one root > 1.
145 bezier.reset(new UnitBezier(0.25, -0.5, 0.75, 0.9));
146 bezier->range(&min, &max);
147 EXPECT_NEAR(min, -0.10755, epsilon);
148 EXPECT_EQ(1, max);
149
150 // Derivative has two roots < 0.
151 bezier.reset(new UnitBezier(0.25, 0.3, 0.75, 0.633));
152 bezier->range(&min, &max);
153 EXPECT_EQ(0, min);
154 EXPECT_EQ(1, max);
155
156 // Derivative has two roots > 1.
157 bezier.reset(new UnitBezier(0.25, 0.367, 0.75, 0.7));
158 bezier->range(&min, &max);
159 EXPECT_EQ(0.f, min);
160 EXPECT_EQ(1.f, max);
161 }
162
163 TEST(UnitBezierTest, Slope)
164 {
165 UnitBezier bezier(0.25, 0.0, 0.75, 1.0);
166
167 double epsilon = 0.00015;
168
169 EXPECT_NEAR(bezier.slope(0), 0, epsilon);
170 EXPECT_NEAR(bezier.slope(0.05), 0.42170, epsilon);
171 EXPECT_NEAR(bezier.slope(0.1), 0.69778, epsilon);
172 EXPECT_NEAR(bezier.slope(0.15), 0.89121, epsilon);
173 EXPECT_NEAR(bezier.slope(0.2), 1.03184, epsilon);
174 EXPECT_NEAR(bezier.slope(0.25), 1.13576, epsilon);
175 EXPECT_NEAR(bezier.slope(0.3), 1.21239, epsilon);
176 EXPECT_NEAR(bezier.slope(0.35), 1.26751, epsilon);
177 EXPECT_NEAR(bezier.slope(0.4), 1.30474, epsilon);
178 EXPECT_NEAR(bezier.slope(0.45), 1.32628, epsilon);
179 EXPECT_NEAR(bezier.slope(0.5), 1.33333, epsilon);
180 EXPECT_NEAR(bezier.slope(0.55), 1.32628, epsilon);
181 EXPECT_NEAR(bezier.slope(0.6), 1.30474, epsilon);
182 EXPECT_NEAR(bezier.slope(0.65), 1.26751, epsilon);
183 EXPECT_NEAR(bezier.slope(0.7), 1.21239, epsilon);
184 EXPECT_NEAR(bezier.slope(0.75), 1.13576, epsilon);
185 EXPECT_NEAR(bezier.slope(0.8), 1.03184, epsilon);
186 EXPECT_NEAR(bezier.slope(0.85), 0.89121, epsilon);
187 EXPECT_NEAR(bezier.slope(0.9), 0.69778, epsilon);
188 EXPECT_NEAR(bezier.slope(0.95), 0.42170, epsilon);
189 EXPECT_NEAR(bezier.slope(1), 0, epsilon);
190 }
191
192 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/animation/UnitBezier.cpp ('k') | third_party/WebKit/Source/platform/blink_platform.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698