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

Side by Side Diff: ui/compositor/layer_animation_sequence_unittest.cc

Issue 11896017: Thread ui opacity animations (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Use cc layer's opacity instead of ui::Layer::opacity_ Created 7 years, 10 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 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/compositor/layer_animation_sequence.h" 5 #include "ui/compositor/layer_animation_sequence.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h" 10 #include "base/time.h"
(...skipping 16 matching lines...) Expand all
27 base::TimeTicks start_time; 27 base::TimeTicks start_time;
28 start_time += base::TimeDelta::FromSeconds(1); 28 start_time += base::TimeDelta::FromSeconds(1);
29 sequence.set_start_time(start_time); 29 sequence.set_start_time(start_time);
30 EXPECT_TRUE(sequence.IsFinished(start_time)); 30 EXPECT_TRUE(sequence.IsFinished(start_time));
31 EXPECT_TRUE(sequence.properties().size() == 0); 31 EXPECT_TRUE(sequence.properties().size() == 0);
32 LayerAnimationElement::AnimatableProperties properties; 32 LayerAnimationElement::AnimatableProperties properties;
33 EXPECT_FALSE(sequence.HasCommonProperty(properties)); 33 EXPECT_FALSE(sequence.HasCommonProperty(properties));
34 } 34 }
35 35
36 // Check that the sequences progresses the delegate as expected when it contains 36 // Check that the sequences progresses the delegate as expected when it contains
37 // a single element. 37 // a single non-threaded element.
38 TEST(LayerAnimationSequenceTest, SingleElement) { 38 TEST(LayerAnimationSequenceTest, SingleElement) {
39 LayerAnimationSequence sequence; 39 LayerAnimationSequence sequence;
40 TestLayerAnimationDelegate delegate; 40 TestLayerAnimationDelegate delegate;
41 float start = 0.0f; 41 float start = 0.0f;
42 float middle = 0.5f; 42 float middle = 0.5f;
43 float target = 1.0f; 43 float target = 1.0f;
44 base::TimeTicks start_time; 44 base::TimeTicks start_time;
45 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 45 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
46 sequence.AddElement( 46 sequence.AddElement(
47 LayerAnimationElement::CreateOpacityElement(target, delta)); 47 LayerAnimationElement::CreateBrightnessElement(target, delta));
48 48
49 for (int i = 0; i < 2; ++i) { 49 for (int i = 0; i < 2; ++i) {
50 start_time += delta; 50 start_time += delta;
51 sequence.set_start_time(start_time); 51 sequence.set_start_time(start_time);
52 delegate.SetBrightnessFromAnimation(start);
53 sequence.Progress(start_time, &delegate);
54 EXPECT_FLOAT_EQ(start, delegate.GetBrightnessForAnimation());
55 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500),
56 &delegate);
57 EXPECT_FLOAT_EQ(middle, delegate.GetBrightnessForAnimation());
58 EXPECT_TRUE(sequence.IsFinished(start_time + delta));
59 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000),
60 &delegate);
61 EXPECT_FLOAT_EQ(target, delegate.GetBrightnessForAnimation());
62 }
63
64 EXPECT_TRUE(sequence.properties().size() == 1);
65 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BRIGHTNESS) !=
66 sequence.properties().end());
67 }
68
69 // Check that the sequences progresses the delegate as expected when it contains
70 // a single threaded element.
71 TEST(LayerAnimationSequenceTest, SingleThreadedElement) {
72 LayerAnimationSequence sequence;
73 TestLayerAnimationDelegate delegate;
74 float start = 0.0f;
75 float middle = 0.5f;
76 float target = 1.0f;
77 base::TimeTicks start_time;
78 base::TimeTicks effective_start;
79 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
80 sequence.AddElement(
81 LayerAnimationElement::CreateOpacityElement(target, delta));
82 int group_id = 1;
83 sequence.set_animation_group_id(group_id);
84
85 for (int i = 0; i < 2; ++i) {
86 start_time = effective_start + delta;
87 sequence.set_start_time(start_time);
52 delegate.SetOpacityFromAnimation(start); 88 delegate.SetOpacityFromAnimation(start);
53 sequence.Progress(start_time, &delegate); 89 sequence.Progress(start_time, &delegate);
54 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); 90 EXPECT_FLOAT_EQ(start, sequence.last_progressed_fraction());
55 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), 91 effective_start = start_time + delta;
56 &delegate); 92 sequence.OnThreadedAnimationStarted(cc::AnimationEvent(
57 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); 93 cc::AnimationEvent::Started,
58 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), 94 0,
59 &delegate); 95 group_id,
96 cc::Animation::Opacity,
97 (effective_start - base::TimeTicks()).InSecondsF()));
98 sequence.Progress(effective_start + delta/2, &delegate);
99 EXPECT_FLOAT_EQ(middle, sequence.last_progressed_fraction());
100 EXPECT_TRUE(sequence.IsFinished(effective_start + delta));
101 sequence.Progress(effective_start + delta, &delegate);
102 EXPECT_FLOAT_EQ(target, sequence.last_progressed_fraction());
60 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); 103 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation());
61 } 104 }
62 105
63 EXPECT_TRUE(sequence.properties().size() == 1); 106 EXPECT_TRUE(sequence.properties().size() == 1);
64 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != 107 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) !=
65 sequence.properties().end()); 108 sequence.properties().end());
66 EXPECT_TRUE(sequence.IsFinished(start_time + delta));
67 } 109 }
68 110
69 // Check that the sequences progresses the delegate as expected when it contains 111 // Check that the sequences progresses the delegate as expected when it contains
70 // multiple elements. Note, see the layer animator tests for cyclic sequences. 112 // multiple elements. Note, see the layer animator tests for cyclic sequences.
71 TEST(LayerAnimationSequenceTest, MultipleElement) { 113 TEST(LayerAnimationSequenceTest, MultipleElement) {
72 LayerAnimationSequence sequence; 114 LayerAnimationSequence sequence;
73 TestLayerAnimationDelegate delegate; 115 TestLayerAnimationDelegate delegate;
74 float start_opacity = 0.0f; 116 float start_opacity = 0.0f;
75 float middle_opacity = 0.5f;
76 float target_opacity = 1.0f; 117 float target_opacity = 1.0f;
77 base::TimeTicks start_time; 118 base::TimeTicks start_time;
119 base::TimeTicks effective_start;
78 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 120 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
79 sequence.AddElement( 121 sequence.AddElement(
80 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); 122 LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
81 123
82 // Pause bounds for a second. 124 // Pause bounds for a second.
83 LayerAnimationElement::AnimatableProperties properties; 125 LayerAnimationElement::AnimatableProperties properties;
84 properties.insert(LayerAnimationElement::BOUNDS); 126 properties.insert(LayerAnimationElement::BOUNDS);
85 127
86 sequence.AddElement( 128 sequence.AddElement(
87 LayerAnimationElement::CreatePauseElement(properties, delta)); 129 LayerAnimationElement::CreatePauseElement(properties, delta));
88 130
89 gfx::Transform start_transform, target_transform, middle_transform; 131 gfx::Transform start_transform, target_transform, middle_transform;
90 start_transform.Rotate(-30.0); 132 start_transform.Rotate(-30.0);
91 target_transform.Rotate(30.0); 133 target_transform.Rotate(30.0);
92 134
93 sequence.AddElement( 135 sequence.AddElement(
94 LayerAnimationElement::CreateTransformElement(target_transform, delta)); 136 LayerAnimationElement::CreateTransformElement(target_transform, delta));
95 137
138 int group_id = 1;
139 sequence.set_animation_group_id(group_id);
140
96 for (int i = 0; i < 2; ++i) { 141 for (int i = 0; i < 2; ++i) {
97 start_time += delta + delta + delta; 142 start_time = effective_start + 3 * delta;
98 sequence.set_start_time(start_time); 143 sequence.set_start_time(start_time);
99 delegate.SetOpacityFromAnimation(start_opacity); 144 delegate.SetOpacityFromAnimation(start_opacity);
100 delegate.SetTransformFromAnimation(start_transform); 145 delegate.SetTransformFromAnimation(start_transform);
101 146
102 sequence.Progress(start_time, &delegate); 147 sequence.Progress(start_time, &delegate);
103 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); 148 EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction());
104 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), 149 effective_start = start_time + delta;
105 &delegate); 150 sequence.OnThreadedAnimationStarted(cc::AnimationEvent(
106 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); 151 cc::AnimationEvent::Started,
107 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), 152 0,
108 &delegate); 153 group_id,
154 cc::Animation::Opacity,
155 (effective_start - base::TimeTicks()).InSecondsF()));
156 sequence.Progress(effective_start + delta/2, &delegate);
157 EXPECT_FLOAT_EQ(0.5, sequence.last_progressed_fraction());
158 sequence.Progress(effective_start + delta, &delegate);
109 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); 159 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation());
160
161 // Now at the start of the pause.
162 EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction());
110 TestLayerAnimationDelegate copy = delegate; 163 TestLayerAnimationDelegate copy = delegate;
111 164
112 // In the middle of the pause -- nothing should have changed. 165 // In the middle of the pause -- nothing should have changed.
113 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1500), 166 sequence.Progress(effective_start + delta + delta/2,
114 &delegate); 167 &delegate);
115 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), 168 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(),
116 copy.GetBoundsForAnimation()); 169 copy.GetBoundsForAnimation());
117 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), 170 CheckApproximatelyEqual(delegate.GetTransformForAnimation(),
118 copy.GetTransformForAnimation()); 171 copy.GetTransformForAnimation());
119 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 172 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(),
120 copy.GetOpacityForAnimation()); 173 copy.GetOpacityForAnimation());
121 174
122 175
123 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(2000), 176 sequence.Progress(effective_start + 2 * delta, &delegate);
124 &delegate);
125 CheckApproximatelyEqual(start_transform, 177 CheckApproximatelyEqual(start_transform,
126 delegate.GetTransformForAnimation()); 178 delegate.GetTransformForAnimation());
127 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(2500), 179 sequence.Progress(effective_start + 2 * delta + delta/2, &delegate);
128 &delegate);
129 CheckApproximatelyEqual(middle_transform, 180 CheckApproximatelyEqual(middle_transform,
130 delegate.GetTransformForAnimation()); 181 delegate.GetTransformForAnimation());
131 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(3000), 182 EXPECT_TRUE(sequence.IsFinished(effective_start + 3 * delta));
132 &delegate); 183 sequence.Progress(effective_start + 3 * delta, &delegate);
133 CheckApproximatelyEqual(target_transform, 184 CheckApproximatelyEqual(target_transform,
134 delegate.GetTransformForAnimation()); 185 delegate.GetTransformForAnimation());
135 } 186 }
136 187
137 EXPECT_TRUE(sequence.properties().size() == 3); 188 EXPECT_TRUE(sequence.properties().size() == 3);
138 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != 189 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) !=
139 sequence.properties().end()); 190 sequence.properties().end());
140 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != 191 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) !=
141 sequence.properties().end()); 192 sequence.properties().end());
142 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != 193 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) !=
143 sequence.properties().end()); 194 sequence.properties().end());
144 EXPECT_TRUE(sequence.IsFinished(start_time + delta + delta + delta));
145 } 195 }
146 196
147 // Check that a sequence can still be aborted if it has cycled many times. 197 // Check that a sequence can still be aborted if it has cycled many times.
148 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { 198 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) {
149 LayerAnimationSequence sequence; 199 LayerAnimationSequence sequence;
150 TestLayerAnimationDelegate delegate; 200 TestLayerAnimationDelegate delegate;
151 float start_opacity = 0.0f; 201 float start_brightness = 0.0f;
152 float target_opacity = 1.0f; 202 float target_brightness = 1.0f;
153 base::TimeTicks start_time; 203 base::TimeTicks start_time;
154 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 204 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
155 sequence.AddElement( 205 sequence.AddElement(
156 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); 206 LayerAnimationElement::CreateBrightnessElement(target_brightness, delta));
157 207
158 sequence.AddElement( 208 sequence.AddElement(
159 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); 209 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
160 210
161 sequence.set_is_cyclic(true); 211 sequence.set_is_cyclic(true);
162 212
163 delegate.SetOpacityFromAnimation(start_opacity); 213 delegate.SetBrightnessFromAnimation(start_brightness);
164 214
165 start_time += delta; 215 start_time += delta;
166 sequence.set_start_time(start_time); 216 sequence.set_start_time(start_time);
167 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000), 217 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000),
168 &delegate); 218 &delegate);
169 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); 219 EXPECT_FLOAT_EQ(target_brightness, delegate.GetBrightnessForAnimation());
170 sequence.Abort(); 220 sequence.Abort(&delegate);
171 221
172 // Should be able to reuse the sequence after aborting. 222 // Should be able to reuse the sequence after aborting.
173 delegate.SetOpacityFromAnimation(start_opacity); 223 delegate.SetBrightnessFromAnimation(start_brightness);
174 start_time += base::TimeDelta::FromMilliseconds(101000); 224 start_time += base::TimeDelta::FromMilliseconds(101000);
175 sequence.set_start_time(start_time); 225 sequence.set_start_time(start_time);
176 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(100000), 226 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(100000),
177 &delegate); 227 &delegate);
178 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); 228 EXPECT_FLOAT_EQ(start_brightness, delegate.GetBrightnessForAnimation());
179 } 229 }
180 230
181 // Check that a sequence can be 'fast-forwarded' to the end and the target set. 231 // Check that a sequence can be 'fast-forwarded' to the end and the target set.
182 // Also check that this has no effect if the sequence is cyclic. 232 // Also check that this has no effect if the sequence is cyclic.
183 TEST(LayerAnimationSequenceTest, SetTarget) { 233 TEST(LayerAnimationSequenceTest, SetTarget) {
184 LayerAnimationSequence sequence; 234 LayerAnimationSequence sequence;
185 TestLayerAnimationDelegate delegate; 235 TestLayerAnimationDelegate delegate;
186 float start_opacity = 0.0f; 236 float start_opacity = 0.0f;
187 float target_opacity = 1.0f; 237 float target_opacity = 1.0f;
188 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 238 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
189 sequence.AddElement( 239 sequence.AddElement(
190 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); 240 LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
191 241
192 LayerAnimationElement::TargetValue target_value(&delegate); 242 LayerAnimationElement::TargetValue target_value(&delegate);
193 target_value.opacity = start_opacity; 243 target_value.opacity = start_opacity;
194 sequence.GetTargetValue(&target_value); 244 sequence.GetTargetValue(&target_value);
195 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); 245 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity);
196 246
197 sequence.set_is_cyclic(true); 247 sequence.set_is_cyclic(true);
198 target_value.opacity = start_opacity; 248 target_value.opacity = start_opacity;
199 sequence.GetTargetValue(&target_value); 249 sequence.GetTargetValue(&target_value);
200 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); 250 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity);
201 } 251 }
202 252
203 TEST(LayerAnimationSequenceTest, AddObserver) { 253 TEST(LayerAnimationSequenceTest, AddObserver) {
204 base::TimeTicks start_time; 254 base::TimeTicks start_time;
205 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 255 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
206 LayerAnimationSequence sequence; 256 LayerAnimationSequence sequence;
207 sequence.AddElement( 257 sequence.AddElement(
208 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); 258 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
209 for (int i = 0; i < 2; ++i) { 259 for (int i = 0; i < 2; ++i) {
210 start_time += delta; 260 start_time += delta;
211 sequence.set_start_time(start_time); 261 sequence.set_start_time(start_time);
212 TestLayerAnimationObserver observer; 262 TestLayerAnimationObserver observer;
213 TestLayerAnimationDelegate delegate; 263 TestLayerAnimationDelegate delegate;
214 sequence.AddObserver(&observer); 264 sequence.AddObserver(&observer);
215 EXPECT_TRUE(!observer.last_ended_sequence()); 265 EXPECT_TRUE(!observer.last_ended_sequence());
216 sequence.Progress(start_time + delta, &delegate); 266 sequence.Progress(start_time + delta, &delegate);
217 EXPECT_EQ(observer.last_ended_sequence(), &sequence); 267 EXPECT_EQ(observer.last_ended_sequence(), &sequence);
218 sequence.RemoveObserver(&observer); 268 sequence.RemoveObserver(&observer);
219 } 269 }
220 } 270 }
221 271
222 } // namespace 272 } // namespace
223 273
224 } // namespace ui 274 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698