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

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

Issue 1819783002: Blink Platform UnitBezier: Implement Slope and Range methods. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 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 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 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 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 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 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 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 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. 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24 24
25 #include "platform/animation/UnitBezier.h" 25 #include "platform/animation/UnitBezier.h"
26 26
27 #include "base/memory/scoped_ptr.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 29
29 namespace blink { 30 namespace blink {
30 31
31 TEST(UnitBezierTest, BasicUse) 32 TEST(UnitBezierTest, BasicUse)
32 { 33 {
33 UnitBezier bezier(0.5, 1.0, 0.5, 1.0); 34 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
34 EXPECT_EQ(0.875, bezier.solve(0.5, 0.005)); 35 EXPECT_EQ(0.875, bezier.solve(0.5));
35 } 36 }
36 37
37 TEST(UnitBezierTest, Overshoot) 38 TEST(UnitBezierTest, Overshoot)
38 { 39 {
39 UnitBezier bezier(0.5, 2.0, 0.5, 2.0); 40 UnitBezier bezier(0.5, 2.0, 0.5, 2.0);
40 EXPECT_EQ(1.625, bezier.solve(0.5, 0.005)); 41 EXPECT_EQ(1.625, bezier.solve(0.5));
41 } 42 }
42 43
43 TEST(UnitBezierTest, Undershoot) 44 TEST(UnitBezierTest, Undershoot)
44 { 45 {
45 UnitBezier bezier(0.5, -1.0, 0.5, -1.0); 46 UnitBezier bezier(0.5, -1.0, 0.5, -1.0);
46 EXPECT_EQ(-0.625, bezier.solve(0.5, 0.005)); 47 EXPECT_EQ(-0.625, bezier.solve(0.5));
47 } 48 }
48 49
49 TEST(UnitBezierTest, InputAtEdgeOfRange) 50 TEST(UnitBezierTest, InputAtEdgeOfRange)
50 { 51 {
51 UnitBezier bezier(0.5, 1.0, 0.5, 1.0); 52 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
52 EXPECT_EQ(0.0, bezier.solve(0.0, 0.005)); 53 EXPECT_EQ(0.0, bezier.solve(0.0));
53 EXPECT_EQ(1.0, bezier.solve(1.0, 0.005)); 54 EXPECT_EQ(1.0, bezier.solve(1.0));
54 } 55 }
55 56
56 TEST(UnitBezierTest, InputOutOfRange) 57 TEST(UnitBezierTest, InputOutOfRange)
57 { 58 {
58 UnitBezier bezier(0.5, 1.0, 0.5, 1.0); 59 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
59 EXPECT_EQ(-2.0, bezier.solve(-1.0, 0.005)); 60 EXPECT_EQ(-2.0, bezier.solve(-1.0));
60 EXPECT_EQ(1.0, bezier.solve(2.0, 0.005)); 61 EXPECT_EQ(1.0, bezier.solve(2.0));
61 } 62 }
62 63
63 TEST(UnitBezierTest, InputOutOfRangeLargeEpsilon) 64 TEST(UnitBezierTest, InputOutOfRangeLargeEpsilon)
64 { 65 {
65 UnitBezier bezier(0.5, 1.0, 0.5, 1.0); 66 UnitBezier bezier(0.5, 1.0, 0.5, 1.0);
66 EXPECT_EQ(-2.0, bezier.solve(-1.0, 1.0)); 67 EXPECT_EQ(-2.0, bezier.solveWithEpsilon(-1.0, 1.0));
67 EXPECT_EQ(1.0, bezier.solve(2.0, 1.0)); 68 EXPECT_EQ(1.0, bezier.solveWithEpsilon(2.0, 1.0));
68 } 69 }
69 70
70 TEST(UnitBezierTest, InputOutOfRangeCoincidentEndpoints) 71 TEST(UnitBezierTest, InputOutOfRangeCoincidentEndpoints)
71 { 72 {
72 UnitBezier bezier(0.0, 0.0, 1.0, 1.0); 73 UnitBezier bezier(0.0, 0.0, 1.0, 1.0);
73 EXPECT_EQ(-1.0, bezier.solve(-1.0, 0.005)); 74 EXPECT_EQ(-1.0, bezier.solve(-1.0));
74 EXPECT_EQ(2.0, bezier.solve(2.0, 0.005)); 75 EXPECT_EQ(2.0, bezier.solve(2.0));
75 } 76 }
76 77
77 TEST(UnitBezierTest, InputOutOfRangeVerticalGradient) 78 TEST(UnitBezierTest, InputOutOfRangeVerticalGradient)
78 { 79 {
79 UnitBezier bezier(0.0, 1.0, 1.0, 0.0); 80 UnitBezier bezier(0.0, 1.0, 1.0, 0.0);
80 EXPECT_EQ(0.0, bezier.solve(-1.0, 0.005)); 81 EXPECT_EQ(0.0, bezier.solve(-1.0));
81 EXPECT_EQ(1.0, bezier.solve(2.0, 0.005)); 82 EXPECT_EQ(1.0, bezier.solve(2.0));
82 } 83 }
83 84
84 TEST(UnitBezierTest, InputOutOfRangeDistinctEndpoints) 85 TEST(UnitBezierTest, InputOutOfRangeDistinctEndpoints)
85 { 86 {
86 UnitBezier bezier(0.1, 0.2, 0.8, 0.8); 87 UnitBezier bezier(0.1, 0.2, 0.8, 0.8);
87 EXPECT_EQ(-2.0, bezier.solve(-1.0, 0.005)); 88 EXPECT_EQ(-2.0, bezier.solve(-1.0));
88 EXPECT_EQ(2.0, bezier.solve(2.0, 0.005)); 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);
89 } 190 }
alancutter (OOO until 2018) 2016/03/21 07:12:56 Should the UnclampedYValues tests be ported over a
loyso (OOO) 2016/03/22 03:12:50 They test gfx::CubicBezier::Solve function. I don'
90 191
91 } // namespace blink 192 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698