OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_sequence.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/layer_animation_element.h" | |
16 #include "ui/gfx/compositor/test/test_layer_animation_delegate.h" | |
17 #include "ui/gfx/compositor/test/test_layer_animation_observer.h" | |
18 #include "ui/gfx/compositor/test/test_utils.h" | |
19 | |
20 namespace ui { | |
21 | |
22 namespace { | |
23 | |
24 // Check that the sequence behaves sanely when it contains no elements. | |
25 TEST(LayerAnimationSequenceTest, NoElement) { | |
26 LayerAnimationSequence sequence; | |
27 EXPECT_EQ(sequence.duration(), base::TimeDelta()); | |
28 EXPECT_TRUE(sequence.properties().size() == 0); | |
29 LayerAnimationElement::AnimatableProperties properties; | |
30 EXPECT_FALSE(sequence.HasCommonProperty(properties)); | |
31 } | |
32 | |
33 // Check that the sequences progresses the delegate as expected when it contains | |
34 // a single element. | |
35 TEST(LayerAnimationSequenceTest, SingleElement) { | |
36 LayerAnimationSequence sequence; | |
37 TestLayerAnimationDelegate delegate; | |
38 float start = 0.0f; | |
39 float middle = 0.5f; | |
40 float target = 1.0f; | |
41 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
42 sequence.AddElement( | |
43 LayerAnimationElement::CreateOpacityElement(target, delta)); | |
44 | |
45 for (int i = 0; i < 2; ++i) { | |
46 delegate.SetOpacityFromAnimation(start); | |
47 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); | |
48 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | |
49 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); | |
50 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | |
51 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); | |
52 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | |
53 } | |
54 | |
55 EXPECT_TRUE(sequence.properties().size() == 1); | |
56 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | |
57 sequence.properties().end()); | |
58 EXPECT_EQ(delta, sequence.duration()); | |
59 } | |
60 | |
61 // Check that the sequences progresses the delegate as expected when it contains | |
62 // multiple elements. Note, see the layer animator tests for cyclic sequences. | |
63 TEST(LayerAnimationSequenceTest, MultipleElement) { | |
64 LayerAnimationSequence sequence; | |
65 TestLayerAnimationDelegate delegate; | |
66 float start_opacity = 0.0f; | |
67 float middle_opacity = 0.5f; | |
68 float target_opacity = 1.0f; | |
69 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
70 sequence.AddElement( | |
71 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | |
72 | |
73 // Pause bounds for a second. | |
74 LayerAnimationElement::AnimatableProperties properties; | |
75 properties.insert(LayerAnimationElement::BOUNDS); | |
76 | |
77 sequence.AddElement( | |
78 LayerAnimationElement::CreatePauseElement(properties, delta)); | |
79 | |
80 Transform start_transform, target_transform, middle_transform; | |
81 start_transform.SetRotate(-90); | |
82 target_transform.SetRotate(90); | |
83 | |
84 sequence.AddElement( | |
85 LayerAnimationElement::CreateTransformElement(target_transform, delta)); | |
86 | |
87 for (int i = 0; i < 2; ++i) { | |
88 delegate.SetOpacityFromAnimation(start_opacity); | |
89 delegate.SetTransformFromAnimation(start_transform); | |
90 | |
91 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); | |
92 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | |
93 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); | |
94 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); | |
95 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); | |
96 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | |
97 TestLayerAnimationDelegate copy = delegate; | |
98 | |
99 // In the middle of the pause -- nothing should have changed. | |
100 sequence.Progress(base::TimeDelta::FromMilliseconds(1500), &delegate); | |
101 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | |
102 copy.GetBoundsForAnimation()); | |
103 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | |
104 copy.GetTransformForAnimation()); | |
105 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | |
106 copy.GetOpacityForAnimation()); | |
107 | |
108 | |
109 sequence.Progress(base::TimeDelta::FromMilliseconds(2000), &delegate); | |
110 CheckApproximatelyEqual(start_transform, | |
111 delegate.GetTransformForAnimation()); | |
112 sequence.Progress(base::TimeDelta::FromMilliseconds(2500), &delegate); | |
113 CheckApproximatelyEqual(middle_transform, | |
114 delegate.GetTransformForAnimation()); | |
115 sequence.Progress(base::TimeDelta::FromMilliseconds(3000), &delegate); | |
116 CheckApproximatelyEqual(target_transform, | |
117 delegate.GetTransformForAnimation()); | |
118 } | |
119 | |
120 EXPECT_TRUE(sequence.properties().size() == 3); | |
121 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | |
122 sequence.properties().end()); | |
123 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != | |
124 sequence.properties().end()); | |
125 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != | |
126 sequence.properties().end()); | |
127 EXPECT_EQ(delta + delta + delta, sequence.duration()); | |
128 } | |
129 | |
130 // Check that a sequence can still be aborted if it has cycled many times. | |
131 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { | |
132 LayerAnimationSequence sequence; | |
133 TestLayerAnimationDelegate delegate; | |
134 float start_opacity = 0.0f; | |
135 float target_opacity = 1.0f; | |
136 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
137 sequence.AddElement( | |
138 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | |
139 | |
140 sequence.AddElement( | |
141 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); | |
142 | |
143 sequence.set_is_cyclic(true); | |
144 | |
145 delegate.SetOpacityFromAnimation(start_opacity); | |
146 | |
147 sequence.Progress(base::TimeDelta::FromMilliseconds(101000), &delegate); | |
148 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | |
149 sequence.Abort(); | |
150 | |
151 // Should be able to reuse the sequence after aborting. | |
152 delegate.SetOpacityFromAnimation(start_opacity); | |
153 sequence.Progress(base::TimeDelta::FromMilliseconds(100000), &delegate); | |
154 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | |
155 } | |
156 | |
157 // Check that a sequence can be 'fast-forwarded' to the end and the target set. | |
158 // Also check that this has no effect if the sequence is cyclic. | |
159 TEST(LayerAnimationSequenceTest, SetTarget) { | |
160 LayerAnimationSequence sequence; | |
161 TestLayerAnimationDelegate delegate; | |
162 float start_opacity = 0.0f; | |
163 float target_opacity = 1.0f; | |
164 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
165 sequence.AddElement( | |
166 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | |
167 | |
168 LayerAnimationElement::TargetValue target_value(&delegate); | |
169 target_value.opacity = start_opacity; | |
170 sequence.GetTargetValue(&target_value); | |
171 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); | |
172 | |
173 sequence.set_is_cyclic(true); | |
174 target_value.opacity = start_opacity; | |
175 sequence.GetTargetValue(&target_value); | |
176 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); | |
177 } | |
178 | |
179 TEST(LayerAnimationSequenceTest, AddObserver) { | |
180 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
181 LayerAnimationSequence sequence; | |
182 sequence.AddElement( | |
183 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
184 for (int i = 0; i < 2; ++i) { | |
185 TestLayerAnimationObserver observer; | |
186 TestLayerAnimationDelegate delegate; | |
187 sequence.AddObserver(&observer); | |
188 EXPECT_TRUE(!observer.last_ended_sequence()); | |
189 sequence.Progress(delta, &delegate); | |
190 EXPECT_EQ(observer.last_ended_sequence(), &sequence); | |
191 sequence.RemoveObserver(&observer); | |
192 } | |
193 } | |
194 | |
195 } // namespace | |
196 | |
197 } // namespace ui | |
OLD | NEW |