Pass CreateDirectory errors up to IndexedDB.
[chromium-blink-merge.git] / cc / animation / transform_operations_unittest.cc
blob8fc9b949d7421d38cb48ca26c50087c55f066cb1
1 // Copyright 2013 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.
5 #include <limits>
7 #include "base/memory/scoped_vector.h"
8 #include "cc/animation/transform_operations.h"
9 #include "cc/test/geometry_test_utils.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/vector3d_f.h"
13 namespace cc {
14 namespace {
16 TEST(TransformOperationTest, TransformTypesAreUnique) {
17 ScopedVector<TransformOperations> transforms;
19 TransformOperations* to_add = new TransformOperations();
20 to_add->AppendTranslate(1, 0, 0);
21 transforms.push_back(to_add);
23 to_add = new TransformOperations();
24 to_add->AppendRotate(0, 0, 1, 2);
25 transforms.push_back(to_add);
27 to_add = new TransformOperations();
28 to_add->AppendScale(2, 2, 2);
29 transforms.push_back(to_add);
31 to_add = new TransformOperations();
32 to_add->AppendSkew(1, 0);
33 transforms.push_back(to_add);
35 to_add = new TransformOperations();
36 to_add->AppendPerspective(800);
37 transforms.push_back(to_add);
39 for (size_t i = 0; i < transforms.size(); ++i) {
40 for (size_t j = 0; j < transforms.size(); ++j) {
41 bool matches_type = transforms[i]->MatchesTypes(*transforms[j]);
42 EXPECT_TRUE((i == j && matches_type) || !matches_type);
47 TEST(TransformOperationTest, MatchTypesSameLength) {
48 TransformOperations translates;
49 translates.AppendTranslate(1, 0, 0);
50 translates.AppendTranslate(1, 0, 0);
51 translates.AppendTranslate(1, 0, 0);
53 TransformOperations skews;
54 skews.AppendSkew(0, 2);
55 skews.AppendSkew(0, 2);
56 skews.AppendSkew(0, 2);
58 TransformOperations translates2;
59 translates2.AppendTranslate(0, 2, 0);
60 translates2.AppendTranslate(0, 2, 0);
61 translates2.AppendTranslate(0, 2, 0);
63 TransformOperations translates3 = translates2;
65 EXPECT_FALSE(translates.MatchesTypes(skews));
66 EXPECT_TRUE(translates.MatchesTypes(translates2));
67 EXPECT_TRUE(translates.MatchesTypes(translates3));
70 TEST(TransformOperationTest, MatchTypesDifferentLength) {
71 TransformOperations translates;
72 translates.AppendTranslate(1, 0, 0);
73 translates.AppendTranslate(1, 0, 0);
74 translates.AppendTranslate(1, 0, 0);
76 TransformOperations skews;
77 skews.AppendSkew(2, 0);
78 skews.AppendSkew(2, 0);
80 TransformOperations translates2;
81 translates2.AppendTranslate(0, 2, 0);
82 translates2.AppendTranslate(0, 2, 0);
84 EXPECT_FALSE(translates.MatchesTypes(skews));
85 EXPECT_FALSE(translates.MatchesTypes(translates2));
88 void GetIdentityOperations(ScopedVector<TransformOperations>* operations) {
89 TransformOperations* to_add = new TransformOperations();
90 operations->push_back(to_add);
92 to_add = new TransformOperations();
93 to_add->AppendTranslate(0, 0, 0);
94 operations->push_back(to_add);
96 to_add = new TransformOperations();
97 to_add->AppendTranslate(0, 0, 0);
98 to_add->AppendTranslate(0, 0, 0);
99 operations->push_back(to_add);
101 to_add = new TransformOperations();
102 to_add->AppendScale(1, 1, 1);
103 operations->push_back(to_add);
105 to_add = new TransformOperations();
106 to_add->AppendScale(1, 1, 1);
107 to_add->AppendScale(1, 1, 1);
108 operations->push_back(to_add);
110 to_add = new TransformOperations();
111 to_add->AppendSkew(0, 0);
112 operations->push_back(to_add);
114 to_add = new TransformOperations();
115 to_add->AppendSkew(0, 0);
116 to_add->AppendSkew(0, 0);
117 operations->push_back(to_add);
119 to_add = new TransformOperations();
120 to_add->AppendRotate(0, 0, 1, 0);
121 operations->push_back(to_add);
123 to_add = new TransformOperations();
124 to_add->AppendRotate(0, 0, 1, 0);
125 to_add->AppendRotate(0, 0, 1, 0);
126 operations->push_back(to_add);
128 to_add = new TransformOperations();
129 to_add->AppendMatrix(gfx::Transform());
130 operations->push_back(to_add);
132 to_add = new TransformOperations();
133 to_add->AppendMatrix(gfx::Transform());
134 to_add->AppendMatrix(gfx::Transform());
135 operations->push_back(to_add);
138 TEST(TransformOperationTest, IdentityAlwaysMatches) {
139 ScopedVector<TransformOperations> operations;
140 GetIdentityOperations(&operations);
142 for (size_t i = 0; i < operations.size(); ++i) {
143 for (size_t j = 0; j < operations.size(); ++j)
144 EXPECT_TRUE(operations[i]->MatchesTypes(*operations[j]));
148 TEST(TransformOperationTest, ApplyTranslate) {
149 double x = 1;
150 double y = 2;
151 double z = 3;
152 TransformOperations operations;
153 operations.AppendTranslate(x, y, z);
154 gfx::Transform expected;
155 expected.Translate3d(x, y, z);
156 EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.Apply());
159 TEST(TransformOperationTest, ApplyRotate) {
160 double x = 1;
161 double y = 2;
162 double z = 3;
163 double degrees = 80;
164 TransformOperations operations;
165 operations.AppendRotate(x, y, z, degrees);
166 gfx::Transform expected;
167 expected.RotateAbout(gfx::Vector3dF(x, y, z), degrees);
168 EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.Apply());
171 TEST(TransformOperationTest, ApplyScale) {
172 double x = 1;
173 double y = 2;
174 double z = 3;
175 TransformOperations operations;
176 operations.AppendScale(x, y, z);
177 gfx::Transform expected;
178 expected.Scale3d(x, y, z);
179 EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.Apply());
182 TEST(TransformOperationTest, ApplySkew) {
183 double x = 1;
184 double y = 2;
185 TransformOperations operations;
186 operations.AppendSkew(x, y);
187 gfx::Transform expected;
188 expected.SkewX(x);
189 expected.SkewY(y);
190 EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.Apply());
193 TEST(TransformOperationTest, ApplyPerspective) {
194 double depth = 800;
195 TransformOperations operations;
196 operations.AppendPerspective(depth);
197 gfx::Transform expected;
198 expected.ApplyPerspectiveDepth(depth);
199 EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.Apply());
202 TEST(TransformOperationTest, ApplyMatrix) {
203 double dx = 1;
204 double dy = 2;
205 double dz = 3;
206 gfx::Transform expected_matrix;
207 expected_matrix.Translate3d(dx, dy, dz);
208 TransformOperations matrix_transform;
209 matrix_transform.AppendMatrix(expected_matrix);
210 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_matrix, matrix_transform.Apply());
213 TEST(TransformOperationTest, ApplyOrder) {
214 double sx = 2;
215 double sy = 4;
216 double sz = 8;
218 double dx = 1;
219 double dy = 2;
220 double dz = 3;
222 TransformOperations operations;
223 operations.AppendScale(sx, sy, sz);
224 operations.AppendTranslate(dx, dy, dz);
226 gfx::Transform expected_scale_matrix;
227 expected_scale_matrix.Scale3d(sx, sy, sz);
229 gfx::Transform expected_translate_matrix;
230 expected_translate_matrix.Translate3d(dx, dy, dz);
232 gfx::Transform expected_combined_matrix = expected_scale_matrix;
233 expected_combined_matrix.PreconcatTransform(expected_translate_matrix);
235 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_combined_matrix, operations.Apply());
238 TEST(TransformOperationTest, BlendOrder) {
239 double sx1 = 2;
240 double sy1 = 4;
241 double sz1 = 8;
243 double dx1 = 1;
244 double dy1 = 2;
245 double dz1 = 3;
247 double sx2 = 4;
248 double sy2 = 8;
249 double sz2 = 16;
251 double dx2 = 10;
252 double dy2 = 20;
253 double dz2 = 30;
255 TransformOperations operations_from;
256 operations_from.AppendScale(sx1, sy1, sz1);
257 operations_from.AppendTranslate(dx1, dy1, dz1);
259 TransformOperations operations_to;
260 operations_to.AppendScale(sx2, sy2, sz2);
261 operations_to.AppendTranslate(dx2, dy2, dz2);
263 gfx::Transform scale_from;
264 scale_from.Scale3d(sx1, sy1, sz1);
265 gfx::Transform translate_from;
266 translate_from.Translate3d(dx1, dy1, dz1);
268 gfx::Transform scale_to;
269 scale_to.Scale3d(sx2, sy2, sz2);
270 gfx::Transform translate_to;
271 translate_to.Translate3d(dx2, dy2, dz2);
273 double progress = 0.25;
275 gfx::Transform blended_scale = scale_to;
276 blended_scale.Blend(scale_from, progress);
278 gfx::Transform blended_translate = translate_to;
279 blended_translate.Blend(translate_from, progress);
281 gfx::Transform expected = blended_scale;
282 expected.PreconcatTransform(blended_translate);
284 EXPECT_TRANSFORMATION_MATRIX_EQ(
285 expected, operations_to.Blend(operations_from, progress));
288 static void CheckProgress(double progress,
289 const gfx::Transform& from_matrix,
290 const gfx::Transform& to_matrix,
291 const TransformOperations& from_transform,
292 const TransformOperations& to_transform) {
293 gfx::Transform expected_matrix = to_matrix;
294 expected_matrix.Blend(from_matrix, progress);
295 EXPECT_TRANSFORMATION_MATRIX_EQ(
296 expected_matrix, to_transform.Blend(from_transform, progress));
299 TEST(TransformOperationTest, BlendProgress) {
300 double sx = 2;
301 double sy = 4;
302 double sz = 8;
303 TransformOperations operations_from;
304 operations_from.AppendScale(sx, sy, sz);
306 gfx::Transform matrix_from;
307 matrix_from.Scale3d(sx, sy, sz);
309 sx = 4;
310 sy = 8;
311 sz = 16;
312 TransformOperations operations_to;
313 operations_to.AppendScale(sx, sy, sz);
315 gfx::Transform matrix_to;
316 matrix_to.Scale3d(sx, sy, sz);
318 CheckProgress(-1, matrix_from, matrix_to, operations_from, operations_to);
319 CheckProgress(0, matrix_from, matrix_to, operations_from, operations_to);
320 CheckProgress(0.25, matrix_from, matrix_to, operations_from, operations_to);
321 CheckProgress(0.5, matrix_from, matrix_to, operations_from, operations_to);
322 CheckProgress(1, matrix_from, matrix_to, operations_from, operations_to);
323 CheckProgress(2, matrix_from, matrix_to, operations_from, operations_to);
326 TEST(TransformOperationTest, BlendWhenTypesDoNotMatch) {
327 double sx1 = 2;
328 double sy1 = 4;
329 double sz1 = 8;
331 double dx1 = 1;
332 double dy1 = 2;
333 double dz1 = 3;
335 double sx2 = 4;
336 double sy2 = 8;
337 double sz2 = 16;
339 double dx2 = 10;
340 double dy2 = 20;
341 double dz2 = 30;
343 TransformOperations operations_from;
344 operations_from.AppendScale(sx1, sy1, sz1);
345 operations_from.AppendTranslate(dx1, dy1, dz1);
347 TransformOperations operations_to;
348 operations_to.AppendTranslate(dx2, dy2, dz2);
349 operations_to.AppendScale(sx2, sy2, sz2);
351 gfx::Transform from;
352 from.Scale3d(sx1, sy1, sz1);
353 from.Translate3d(dx1, dy1, dz1);
355 gfx::Transform to;
356 to.Translate3d(dx2, dy2, dz2);
357 to.Scale3d(sx2, sy2, sz2);
359 double progress = 0.25;
361 gfx::Transform expected = to;
362 expected.Blend(from, progress);
364 EXPECT_TRANSFORMATION_MATRIX_EQ(
365 expected, operations_to.Blend(operations_from, progress));
368 TEST(TransformOperationTest, LargeRotationsWithSameAxis) {
369 TransformOperations operations_from;
370 operations_from.AppendRotate(0, 0, 1, 0);
372 TransformOperations operations_to;
373 operations_to.AppendRotate(0, 0, 2, 360);
375 double progress = 0.5;
377 gfx::Transform expected;
378 expected.RotateAbout(gfx::Vector3dF(0, 0, 1), 180);
380 EXPECT_TRANSFORMATION_MATRIX_EQ(
381 expected, operations_to.Blend(operations_from, progress));
384 TEST(TransformOperationTest, LargeRotationsWithSameAxisInDifferentDirection) {
385 TransformOperations operations_from;
386 operations_from.AppendRotate(0, 0, 1, 180);
388 TransformOperations operations_to;
389 operations_to.AppendRotate(0, 0, -1, 180);
391 double progress = 0.5;
393 gfx::Transform expected;
395 EXPECT_TRANSFORMATION_MATRIX_EQ(
396 expected, operations_to.Blend(operations_from, progress));
399 TEST(TransformOperationTest, LargeRotationsWithDifferentAxes) {
400 TransformOperations operations_from;
401 operations_from.AppendRotate(0, 0, 1, 175);
403 TransformOperations operations_to;
404 operations_to.AppendRotate(0, 1, 0, 175);
406 double progress = 0.5;
407 gfx::Transform matrix_from;
408 matrix_from.RotateAbout(gfx::Vector3dF(0, 0, 1), 175);
410 gfx::Transform matrix_to;
411 matrix_to.RotateAbout(gfx::Vector3dF(0, 1, 0), 175);
413 gfx::Transform expected = matrix_to;
414 expected.Blend(matrix_from, progress);
416 EXPECT_TRANSFORMATION_MATRIX_EQ(
417 expected, operations_to.Blend(operations_from, progress));
420 TEST(TransformOperationTest, BlendRotationFromIdentity) {
421 ScopedVector<TransformOperations> identity_operations;
422 GetIdentityOperations(&identity_operations);
424 for (size_t i = 0; i < identity_operations.size(); ++i) {
425 TransformOperations operations;
426 operations.AppendRotate(0, 0, 1, 360);
428 double progress = 0.5;
430 gfx::Transform expected;
431 expected.RotateAbout(gfx::Vector3dF(0, 0, 1), 180);
433 EXPECT_TRANSFORMATION_MATRIX_EQ(
434 expected, operations.Blend(*identity_operations[i], progress));
436 progress = -0.5;
438 expected.MakeIdentity();
439 expected.RotateAbout(gfx::Vector3dF(0, 0, 1), -180);
441 EXPECT_TRANSFORMATION_MATRIX_EQ(
442 expected, operations.Blend(*identity_operations[i], progress));
444 progress = 1.5;
446 expected.MakeIdentity();
447 expected.RotateAbout(gfx::Vector3dF(0, 0, 1), 540);
449 EXPECT_TRANSFORMATION_MATRIX_EQ(
450 expected, operations.Blend(*identity_operations[i], progress));
454 TEST(TransformOperationTest, BlendTranslationFromIdentity) {
455 ScopedVector<TransformOperations> identity_operations;
456 GetIdentityOperations(&identity_operations);
458 for (size_t i = 0; i < identity_operations.size(); ++i) {
459 TransformOperations operations;
460 operations.AppendTranslate(2, 2, 2);
462 double progress = 0.5;
464 gfx::Transform expected;
465 expected.Translate3d(1, 1, 1);
467 EXPECT_TRANSFORMATION_MATRIX_EQ(
468 expected, operations.Blend(*identity_operations[i], progress));
470 progress = -0.5;
472 expected.MakeIdentity();
473 expected.Translate3d(-1, -1, -1);
475 EXPECT_TRANSFORMATION_MATRIX_EQ(
476 expected, operations.Blend(*identity_operations[i], progress));
478 progress = 1.5;
480 expected.MakeIdentity();
481 expected.Translate3d(3, 3, 3);
483 EXPECT_TRANSFORMATION_MATRIX_EQ(
484 expected, operations.Blend(*identity_operations[i], progress));
488 TEST(TransformOperationTest, BlendScaleFromIdentity) {
489 ScopedVector<TransformOperations> identity_operations;
490 GetIdentityOperations(&identity_operations);
492 for (size_t i = 0; i < identity_operations.size(); ++i) {
493 TransformOperations operations;
494 operations.AppendScale(3, 3, 3);
496 double progress = 0.5;
498 gfx::Transform expected;
499 expected.Scale3d(2, 2, 2);
501 EXPECT_TRANSFORMATION_MATRIX_EQ(
502 expected, operations.Blend(*identity_operations[i], progress));
504 progress = -0.5;
506 expected.MakeIdentity();
507 expected.Scale3d(0, 0, 0);
509 EXPECT_TRANSFORMATION_MATRIX_EQ(
510 expected, operations.Blend(*identity_operations[i], progress));
512 progress = 1.5;
514 expected.MakeIdentity();
515 expected.Scale3d(4, 4, 4);
517 EXPECT_TRANSFORMATION_MATRIX_EQ(
518 expected, operations.Blend(*identity_operations[i], progress));
522 TEST(TransformOperationTest, BlendSkewFromIdentity) {
523 ScopedVector<TransformOperations> identity_operations;
524 GetIdentityOperations(&identity_operations);
526 for (size_t i = 0; i < identity_operations.size(); ++i) {
527 TransformOperations operations;
528 operations.AppendSkew(2, 2);
530 double progress = 0.5;
532 gfx::Transform expected;
533 expected.SkewX(1);
534 expected.SkewY(1);
536 EXPECT_TRANSFORMATION_MATRIX_EQ(
537 expected, operations.Blend(*identity_operations[i], progress));
539 progress = -0.5;
541 expected.MakeIdentity();
542 expected.SkewX(-1);
543 expected.SkewY(-1);
545 EXPECT_TRANSFORMATION_MATRIX_EQ(
546 expected, operations.Blend(*identity_operations[i], progress));
548 progress = 1.5;
550 expected.MakeIdentity();
551 expected.SkewX(3);
552 expected.SkewY(3);
554 EXPECT_TRANSFORMATION_MATRIX_EQ(
555 expected, operations.Blend(*identity_operations[i], progress));
559 TEST(TransformOperationTest, BlendPerspectiveFromIdentity) {
560 ScopedVector<TransformOperations> identity_operations;
561 GetIdentityOperations(&identity_operations);
563 for (size_t i = 0; i < identity_operations.size(); ++i) {
564 TransformOperations operations;
565 operations.AppendPerspective(1000);
567 double progress = 0.5;
569 gfx::Transform expected;
570 expected.ApplyPerspectiveDepth(
571 500 + 0.5 * std::numeric_limits<double>::max());
573 EXPECT_TRANSFORMATION_MATRIX_EQ(
574 expected, operations.Blend(*identity_operations[i], progress));
578 TEST(TransformOperationTest, BlendRotationToIdentity) {
579 ScopedVector<TransformOperations> identity_operations;
580 GetIdentityOperations(&identity_operations);
582 for (size_t i = 0; i < identity_operations.size(); ++i) {
583 TransformOperations operations;
584 operations.AppendRotate(0, 0, 1, 360);
586 double progress = 0.5;
588 gfx::Transform expected;
589 expected.RotateAbout(gfx::Vector3dF(0, 0, 1), 180);
591 EXPECT_TRANSFORMATION_MATRIX_EQ(
592 expected, identity_operations[i]->Blend(operations, progress));
596 TEST(TransformOperationTest, BlendTranslationToIdentity) {
597 ScopedVector<TransformOperations> identity_operations;
598 GetIdentityOperations(&identity_operations);
600 for (size_t i = 0; i < identity_operations.size(); ++i) {
601 TransformOperations operations;
602 operations.AppendTranslate(2, 2, 2);
604 double progress = 0.5;
606 gfx::Transform expected;
607 expected.Translate3d(1, 1, 1);
609 EXPECT_TRANSFORMATION_MATRIX_EQ(
610 expected, identity_operations[i]->Blend(operations, progress));
614 TEST(TransformOperationTest, BlendScaleToIdentity) {
615 ScopedVector<TransformOperations> identity_operations;
616 GetIdentityOperations(&identity_operations);
618 for (size_t i = 0; i < identity_operations.size(); ++i) {
619 TransformOperations operations;
620 operations.AppendScale(3, 3, 3);
622 double progress = 0.5;
624 gfx::Transform expected;
625 expected.Scale3d(2, 2, 2);
627 EXPECT_TRANSFORMATION_MATRIX_EQ(
628 expected, identity_operations[i]->Blend(operations, progress));
632 TEST(TransformOperationTest, BlendSkewToIdentity) {
633 ScopedVector<TransformOperations> identity_operations;
634 GetIdentityOperations(&identity_operations);
636 for (size_t i = 0; i < identity_operations.size(); ++i) {
637 TransformOperations operations;
638 operations.AppendSkew(2, 2);
640 double progress = 0.5;
642 gfx::Transform expected;
643 expected.SkewX(1);
644 expected.SkewY(1);
646 EXPECT_TRANSFORMATION_MATRIX_EQ(
647 expected, identity_operations[i]->Blend(operations, progress));
651 TEST(TransformOperationTest, BlendPerspectiveToIdentity) {
652 ScopedVector<TransformOperations> identity_operations;
653 GetIdentityOperations(&identity_operations);
655 for (size_t i = 0; i < identity_operations.size(); ++i) {
656 TransformOperations operations;
657 operations.AppendPerspective(1000);
659 double progress = 0.5;
661 gfx::Transform expected;
662 expected.ApplyPerspectiveDepth(
663 500 + 0.5 * std::numeric_limits<double>::max());
665 EXPECT_TRANSFORMATION_MATRIX_EQ(
666 expected, identity_operations[i]->Blend(operations, progress));
670 TEST(TransformOperationTest, ExtrapolatePerspectiveBlending) {
671 TransformOperations operations1;
672 operations1.AppendPerspective(1000);
674 TransformOperations operations2;
675 operations2.AppendPerspective(500);
677 gfx::Transform expected;
678 expected.ApplyPerspectiveDepth(250);
680 EXPECT_TRANSFORMATION_MATRIX_EQ(
681 expected, operations1.Blend(operations2, -0.5));
683 expected.MakeIdentity();
684 expected.ApplyPerspectiveDepth(1250);
686 EXPECT_TRANSFORMATION_MATRIX_EQ(
687 expected, operations1.Blend(operations2, 1.5));
690 TEST(TransformOperationTest, ExtrapolateMatrixBlending) {
691 gfx::Transform transform1;
692 transform1.Translate3d(1, 1, 1);
693 TransformOperations operations1;
694 operations1.AppendMatrix(transform1);
696 gfx::Transform transform2;
697 transform2.Translate3d(3, 3, 3);
698 TransformOperations operations2;
699 operations2.AppendMatrix(transform2);
701 gfx::Transform expected;
702 EXPECT_TRANSFORMATION_MATRIX_EQ(
703 expected, operations1.Blend(operations2, 1.5));
705 expected.Translate3d(4, 4, 4);
706 EXPECT_TRANSFORMATION_MATRIX_EQ(
707 expected, operations1.Blend(operations2, -0.5));
710 } // namespace
711 } // namespace cc