OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "ui/gfx/compositor/layer_animation_element.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/time.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 #include "ui/gfx/rect.h" | |
13 #include "ui/gfx/transform.h" | |
14 #include "ui/gfx/compositor/layer_animation_delegate.h" | |
15 #include "ui/gfx/compositor/test/test_layer_animation_delegate.h" | |
16 #include "ui/gfx/compositor/test/test_utils.h" | |
17 | |
18 namespace ui { | |
19 | |
20 namespace { | |
21 | |
22 // Check that the transformation element progresses the delegate as expected and | |
23 // that the element can be reused after it completes. | |
24 TEST(LayerAnimationElementTest, TransformElement) { | |
25 TestLayerAnimationDelegate delegate; | |
26 Transform start_transform, target_transform, middle_transform; | |
27 start_transform.SetRotate(-90); | |
28 target_transform.SetRotate(90); | |
29 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
30 | |
31 scoped_ptr<LayerAnimationElement> element( | |
32 LayerAnimationElement::CreateTransformElement(target_transform, delta)); | |
33 | |
34 for (int i = 0; i < 2; ++i) { | |
35 delegate.SetTransformFromAnimation(start_transform); | |
36 element->Progress(0.0, &delegate); | |
37 CheckApproximatelyEqual(start_transform, | |
38 delegate.GetTransformForAnimation()); | |
39 element->Progress(0.5, &delegate); | |
40 CheckApproximatelyEqual(middle_transform, | |
41 delegate.GetTransformForAnimation()); | |
42 element->Progress(1.0, &delegate); | |
43 CheckApproximatelyEqual(target_transform, | |
44 delegate.GetTransformForAnimation()); | |
45 } | |
46 | |
47 LayerAnimationElement::TargetValue target_value(&delegate); | |
48 element->GetTargetValue(&target_value); | |
49 CheckApproximatelyEqual(target_transform, target_value.transform); | |
50 | |
51 EXPECT_EQ(delta, element->duration()); | |
52 } | |
53 | |
54 // Check that the bounds element progresses the delegate as expected and | |
55 // that the element can be reused after it completes. | |
56 TEST(LayerAnimationElementTest, BoundsElement) { | |
57 TestLayerAnimationDelegate delegate; | |
58 gfx::Rect start, target, middle; | |
59 start = target = middle = gfx::Rect(0, 0, 50, 50); | |
60 start.set_x(-90); | |
61 target.set_x(90); | |
62 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
63 | |
64 scoped_ptr<LayerAnimationElement> element( | |
65 LayerAnimationElement::CreateBoundsElement(target, delta)); | |
66 | |
67 for (int i = 0; i < 2; ++i) { | |
68 delegate.SetBoundsFromAnimation(start); | |
69 element->Progress(0.0, &delegate); | |
70 CheckApproximatelyEqual(start, delegate.GetBoundsForAnimation()); | |
71 element->Progress(0.5, &delegate); | |
72 CheckApproximatelyEqual(middle, delegate.GetBoundsForAnimation()); | |
73 element->Progress(1.0, &delegate); | |
74 CheckApproximatelyEqual(target, delegate.GetBoundsForAnimation()); | |
75 } | |
76 | |
77 LayerAnimationElement::TargetValue target_value(&delegate); | |
78 element->GetTargetValue(&target_value); | |
79 CheckApproximatelyEqual(target, target_value.bounds); | |
80 | |
81 EXPECT_EQ(delta, element->duration()); | |
82 } | |
83 | |
84 // Check that the opacity element progresses the delegate as expected and | |
85 // that the element can be reused after it completes. | |
86 TEST(LayerAnimationElementTest, OpacityElement) { | |
87 TestLayerAnimationDelegate delegate; | |
88 float start = 0.0; | |
89 float middle = 0.5; | |
90 float target = 1.0; | |
91 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
92 scoped_ptr<LayerAnimationElement> element( | |
93 LayerAnimationElement::CreateOpacityElement(target, delta)); | |
94 | |
95 for (int i = 0; i < 2; ++i) { | |
96 delegate.SetOpacityFromAnimation(start); | |
97 element->Progress(0.0, &delegate); | |
98 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | |
99 element->Progress(0.5, &delegate); | |
100 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | |
101 element->Progress(1.0, &delegate); | |
102 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | |
103 } | |
104 | |
105 LayerAnimationElement::TargetValue target_value(&delegate); | |
106 element->GetTargetValue(&target_value); | |
107 EXPECT_FLOAT_EQ(target, target_value.opacity); | |
108 | |
109 EXPECT_EQ(delta, element->duration()); | |
110 } | |
111 | |
112 // Check that the visibility element progresses the delegate as expected and | |
113 // that the element can be reused after it completes. | |
114 TEST(LayerAnimationElementTest, VisibilityElement) { | |
115 TestLayerAnimationDelegate delegate; | |
116 bool start = true; | |
117 bool target = false; | |
118 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
119 scoped_ptr<LayerAnimationElement> element( | |
120 LayerAnimationElement::CreateVisibilityElement(target, delta)); | |
121 | |
122 for (int i = 0; i < 2; ++i) { | |
123 delegate.SetVisibilityFromAnimation(start); | |
124 element->Progress(0.0, &delegate); | |
125 EXPECT_TRUE(delegate.GetVisibilityForAnimation()); | |
126 element->Progress(0.5, &delegate); | |
127 EXPECT_TRUE(delegate.GetVisibilityForAnimation()); | |
128 element->Progress(1.0, &delegate); | |
129 EXPECT_FALSE(delegate.GetVisibilityForAnimation()); | |
130 } | |
131 | |
132 LayerAnimationElement::TargetValue target_value(&delegate); | |
133 element->GetTargetValue(&target_value); | |
134 EXPECT_FALSE(target_value.visibility); | |
135 | |
136 EXPECT_EQ(delta, element->duration()); | |
137 } | |
138 | |
139 // Check that the pause element progresses the delegate as expected and | |
140 // that the element can be reused after it completes. | |
141 TEST(LayerAnimationElementTest, PauseElement) { | |
142 LayerAnimationElement::AnimatableProperties properties; | |
143 properties.insert(LayerAnimationElement::TRANSFORM); | |
144 properties.insert(LayerAnimationElement::BOUNDS); | |
145 properties.insert(LayerAnimationElement::OPACITY); | |
146 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
147 | |
148 scoped_ptr<LayerAnimationElement> element( | |
149 LayerAnimationElement::CreatePauseElement(properties, delta)); | |
150 | |
151 TestLayerAnimationDelegate delegate; | |
152 TestLayerAnimationDelegate copy = delegate; | |
153 | |
154 element->Progress(1.0, &delegate); | |
155 | |
156 // Nothing should have changed. | |
157 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | |
158 copy.GetBoundsForAnimation()); | |
159 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | |
160 copy.GetTransformForAnimation()); | |
161 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | |
162 copy.GetOpacityForAnimation()); | |
163 | |
164 // Pause should last for |delta|. | |
165 EXPECT_EQ(delta, element->duration()); | |
166 } | |
167 | |
168 } // namespace | |
169 | |
170 } // namespace ui | |
OLD | NEW |