OLD | NEW |
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 are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 17 matching lines...) Expand all Loading... |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/platform/animation/TimingFunctionTestHelper.h" | 32 #include "core/platform/animation/TimingFunctionTestHelper.h" |
33 | 33 |
34 #include <ostream> // NOLINT | 34 #include <ostream> // NOLINT |
35 | 35 |
36 namespace WebCore { | 36 namespace WebCore { |
37 | 37 |
| 38 // This class exists so that ChainedTimingFunction only needs to friend one thin
g. |
| 39 class ChainedTimingFunctionTestHelper { |
| 40 static void PrintTo(const ChainedTimingFunction& timingFunction, ::std::ostr
eam* os) |
| 41 { |
| 42 // Forward declare the generic PrintTo function as ChainedTimingFunction
needs to call it. |
| 43 void PrintTo(const TimingFunction&, ::std::ostream*); |
| 44 |
| 45 *os << "ChainedTimingFunction@" << &timingFunction << "("; |
| 46 for (size_t i = 0; i < timingFunction.m_segments.size(); i++) { |
| 47 ChainedTimingFunction::Segment segment = timingFunction.m_segments[i
]; |
| 48 PrintTo(*(segment.m_timingFunction.get()), os); |
| 49 *os << "[" << segment.m_min << " -> " << segment.m_max << "]"; |
| 50 if (i+1 != timingFunction.m_segments.size()) { |
| 51 *os << ", "; |
| 52 } |
| 53 } |
| 54 *os << ")"; |
| 55 } |
| 56 |
| 57 static bool equals(const ChainedTimingFunction& lhs, const TimingFunction& r
hs) |
| 58 { |
| 59 if (rhs.type() != TimingFunction::ChainedFunction) |
| 60 return false; |
| 61 |
| 62 if (&lhs == &rhs) |
| 63 return true; |
| 64 |
| 65 const ChainedTimingFunction* ctf = static_cast<const ChainedTimingFuncti
on*>(&rhs); |
| 66 if (lhs.m_segments.size() != ctf->m_segments.size()) |
| 67 return false; |
| 68 |
| 69 for (size_t i = 0; i < lhs.m_segments.size(); i++) { |
| 70 if (!equals(lhs.m_segments[i], ctf->m_segments[i])) |
| 71 return false; |
| 72 } |
| 73 return true; |
| 74 } |
| 75 |
| 76 static bool equals(const ChainedTimingFunction::Segment& lhs, const ChainedT
imingFunction::Segment& rhs) |
| 77 { |
| 78 if (&lhs == &rhs) |
| 79 return true; |
| 80 |
| 81 if ((lhs.m_min != rhs.m_min) || (lhs.m_max != rhs.m_max)) |
| 82 return false; |
| 83 |
| 84 if (lhs.m_timingFunction == rhs.m_timingFunction) |
| 85 return true; |
| 86 |
| 87 ASSERT(lhs.m_timingFunction); |
| 88 ASSERT(rhs.m_timingFunction); |
| 89 |
| 90 return (*(lhs.m_timingFunction.get())) == (*(rhs.m_timingFunction.get())
); |
| 91 } |
| 92 |
| 93 friend void PrintTo(const ChainedTimingFunction&, ::std::ostream*); |
| 94 friend bool operator==(const ChainedTimingFunction& lhs, const TimingFunctio
n& rhs); |
| 95 }; |
| 96 |
38 void PrintTo(const LinearTimingFunction& timingFunction, ::std::ostream* os) | 97 void PrintTo(const LinearTimingFunction& timingFunction, ::std::ostream* os) |
39 { | 98 { |
40 *os << "LinearTimingFunction@" << &timingFunction; | 99 *os << "LinearTimingFunction@" << &timingFunction; |
41 } | 100 } |
42 | 101 |
43 void PrintTo(const CubicBezierTimingFunction& timingFunction, ::std::ostream* os
) | 102 void PrintTo(const CubicBezierTimingFunction& timingFunction, ::std::ostream* os
) |
44 { | 103 { |
45 *os << "CubicBezierTimingFunction@" << &timingFunction << "("; | 104 *os << "CubicBezierTimingFunction@" << &timingFunction << "("; |
46 switch (timingFunction.subType()) { | 105 switch (timingFunction.subType()) { |
47 case CubicBezierTimingFunction::Ease: | 106 case CubicBezierTimingFunction::Ease: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 *os << "Custom"; | 142 *os << "Custom"; |
84 break; | 143 break; |
85 default: | 144 default: |
86 ASSERT_NOT_REACHED(); | 145 ASSERT_NOT_REACHED(); |
87 } | 146 } |
88 *os << ", " << timingFunction.numberOfSteps(); | 147 *os << ", " << timingFunction.numberOfSteps(); |
89 *os << ", " << (timingFunction.stepAtStart() ? "true" : "false"); | 148 *os << ", " << (timingFunction.stepAtStart() ? "true" : "false"); |
90 *os << ")"; | 149 *os << ")"; |
91 } | 150 } |
92 | 151 |
93 class ChainedTimingFunctionPrinter { | |
94 private: | |
95 static void PrintTo(const ChainedTimingFunction& timingFunction, ::std::ostr
eam* os) | |
96 { | |
97 // Forward declare the generic PrintTo function as ChainedTimingFunction
needs to call it. | |
98 void PrintTo(const TimingFunction&, ::std::ostream*); | |
99 | |
100 *os << "ChainedTimingFunction@" << &timingFunction << "("; | |
101 for (size_t i = 0; i < timingFunction.m_segments.size(); i++) { | |
102 ChainedTimingFunction::Segment segment = timingFunction.m_segments[i
]; | |
103 PrintTo(*(segment.m_timingFunction.get()), os); | |
104 *os << "[" << segment.m_min << " -> " << segment.m_max << "]"; | |
105 if (i+1 != timingFunction.m_segments.size()) { | |
106 *os << ", "; | |
107 } | |
108 } | |
109 *os << ")"; | |
110 } | |
111 | |
112 friend void PrintTo(const ChainedTimingFunction&, ::std::ostream*); | |
113 }; | |
114 | |
115 void PrintTo(const ChainedTimingFunction& timingFunction, ::std::ostream* os) | 152 void PrintTo(const ChainedTimingFunction& timingFunction, ::std::ostream* os) |
116 { | 153 { |
117 ChainedTimingFunctionPrinter::PrintTo(timingFunction, os); | 154 ChainedTimingFunctionTestHelper::PrintTo(timingFunction, os); |
118 } | 155 } |
119 | 156 |
120 // The generic PrintTo *must* come after the non-generic PrintTo otherwise it | 157 // The generic PrintTo *must* come after the non-generic PrintTo otherwise it |
121 // will end up calling itself. | 158 // will end up calling itself. |
122 void PrintTo(const TimingFunction& timingFunction, ::std::ostream* os) | 159 void PrintTo(const TimingFunction& timingFunction, ::std::ostream* os) |
123 { | 160 { |
124 switch (timingFunction.type()) { | 161 switch (timingFunction.type()) { |
125 case TimingFunction::LinearFunction: { | 162 case TimingFunction::LinearFunction: { |
126 const LinearTimingFunction* linear = static_cast<const LinearTimingFunct
ion*>(&timingFunction); | 163 const LinearTimingFunction* linear = static_cast<const LinearTimingFunct
ion*>(&timingFunction); |
127 PrintTo(*linear, os); | 164 PrintTo(*linear, os); |
(...skipping 12 matching lines...) Expand all Loading... |
140 case TimingFunction::ChainedFunction: { | 177 case TimingFunction::ChainedFunction: { |
141 const ChainedTimingFunction* chained = static_cast<const ChainedTimingFu
nction*>(&timingFunction); | 178 const ChainedTimingFunction* chained = static_cast<const ChainedTimingFu
nction*>(&timingFunction); |
142 PrintTo(*chained, os); | 179 PrintTo(*chained, os); |
143 return; | 180 return; |
144 } | 181 } |
145 default: | 182 default: |
146 ASSERT_NOT_REACHED(); | 183 ASSERT_NOT_REACHED(); |
147 } | 184 } |
148 } | 185 } |
149 | 186 |
| 187 bool operator==(const LinearTimingFunction& lhs, const TimingFunction& rhs) |
| 188 { |
| 189 return rhs.type() == TimingFunction::LinearFunction; |
| 190 } |
| 191 |
| 192 bool operator==(const CubicBezierTimingFunction& lhs, const TimingFunction& rhs) |
| 193 { |
| 194 if (rhs.type() != TimingFunction::CubicBezierFunction) |
| 195 return false; |
| 196 |
| 197 const CubicBezierTimingFunction* ctf = static_cast<const CubicBezierTimingFu
nction*>(&rhs); |
| 198 if ((lhs.subType() == CubicBezierTimingFunction::Custom) && (ctf->subType()
== CubicBezierTimingFunction::Custom)) |
| 199 return (lhs.x1() == ctf->x1()) && (lhs.y1() == ctf->y1()) && (lhs.x2() =
= ctf->x2()) && (lhs.y2() == ctf->y2()); |
| 200 |
| 201 return lhs.subType() == ctf->subType(); |
| 202 } |
| 203 |
| 204 bool operator==(const StepsTimingFunction& lhs, const TimingFunction& rhs) |
| 205 { |
| 206 if (rhs.type() != TimingFunction::StepsFunction) |
| 207 return false; |
| 208 |
| 209 const StepsTimingFunction* stf = static_cast<const StepsTimingFunction*>(&rh
s); |
| 210 if ((lhs.subType() == StepsTimingFunction::Custom) && (stf->subType() == Ste
psTimingFunction::Custom)) |
| 211 return (lhs.numberOfSteps() == stf->numberOfSteps()) && (lhs.stepAtStart
() == stf->stepAtStart()); |
| 212 |
| 213 return lhs.subType() == stf->subType(); |
| 214 } |
| 215 |
| 216 bool operator==(const ChainedTimingFunction& lhs, const TimingFunction& rhs) |
| 217 { |
| 218 return ChainedTimingFunctionTestHelper::equals(lhs, rhs); |
| 219 } |
| 220 |
| 221 // Like in the PrintTo case, the generic operator== *must* come after the |
| 222 // non-generic operator== otherwise it will end up calling itself. |
| 223 bool operator==(const TimingFunction& lhs, const TimingFunction& rhs) |
| 224 { |
| 225 switch (lhs.type()) { |
| 226 case TimingFunction::LinearFunction: { |
| 227 const LinearTimingFunction* linear = static_cast<const LinearTimingFunct
ion*>(&lhs); |
| 228 return (*linear == rhs); |
| 229 } |
| 230 case TimingFunction::CubicBezierFunction: { |
| 231 const CubicBezierTimingFunction* cubic = static_cast<const CubicBezierTi
mingFunction*>(&lhs); |
| 232 return (*cubic == rhs); |
| 233 } |
| 234 case TimingFunction::StepsFunction: { |
| 235 const StepsTimingFunction* step = static_cast<const StepsTimingFunction*
>(&lhs); |
| 236 return (*step == rhs); |
| 237 } |
| 238 case TimingFunction::ChainedFunction: { |
| 239 const ChainedTimingFunction* chained = static_cast<const ChainedTimingFu
nction*>(&lhs); |
| 240 return (*chained == rhs); |
| 241 } |
| 242 default: |
| 243 ASSERT_NOT_REACHED(); |
| 244 } |
| 245 return false; |
| 246 } |
| 247 |
| 248 // No need to define specific operator!= as they can all come via this function. |
| 249 bool operator!=(const TimingFunction& lhs, const TimingFunction& rhs) |
| 250 { |
| 251 return !(lhs == rhs); |
| 252 } |
| 253 |
150 } // namespace WebCore | 254 } // namespace WebCore |
OLD | NEW |