[chromium] Clean up includes in compositor unit tests
[WebKit-https.git] / Source / WebKit / chromium / tests / CCLayerAnimationControllerTest.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #include "cc/CCLayerAnimationController.h"
28
29 #include "CCAnimationTestCommon.h"
30 #include "cc/CCActiveAnimation.h"
31 #include "cc/CCAnimationCurve.h"
32
33 #include <gmock/gmock.h>
34 #include <gtest/gtest.h>
35 #include <public/WebTransformationMatrix.h>
36 #include <wtf/Vector.h>
37
38 using namespace WebCore;
39 using namespace WebKitTests;
40 using WebKit::WebTransformationMatrix;
41
42 namespace {
43
44 void expectTranslateX(double translateX, const WebTransformationMatrix& matrix)
45 {
46     EXPECT_FLOAT_EQ(translateX, matrix.m41());
47 }
48
49 PassOwnPtr<CCActiveAnimation> createActiveAnimation(PassOwnPtr<CCAnimationCurve> curve, int id, CCActiveAnimation::TargetProperty property)
50 {
51     return CCActiveAnimation::create(curve, 0, id, property);
52 }
53
54 TEST(CCLayerAnimationControllerTest, syncNewAnimation)
55 {
56     FakeLayerAnimationControllerClient dummyImpl;
57     OwnPtr<CCLayerAnimationController> controllerImpl(CCLayerAnimationController::create(&dummyImpl));
58     FakeLayerAnimationControllerClient dummy;
59     OwnPtr<CCLayerAnimationController> controller(CCLayerAnimationController::create(&dummy));
60
61     EXPECT_FALSE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
62
63     addOpacityTransitionToController(*controller, 1, 0, 1, false);
64
65     controller->pushAnimationUpdatesTo(controllerImpl.get());
66
67     EXPECT_TRUE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
68     EXPECT_EQ(CCActiveAnimation::WaitingForTargetAvailability, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
69 }
70
71 // If an animation is started on the impl thread before it is ticked on the main
72 // thread, we must be sure to respect the synchronized start time.
73 TEST(CCLayerAnimationControllerTest, doNotClobberStartTimes)
74 {
75     FakeLayerAnimationControllerClient dummyImpl;
76     OwnPtr<CCLayerAnimationController> controllerImpl(CCLayerAnimationController::create(&dummyImpl));
77     FakeLayerAnimationControllerClient dummy;
78     OwnPtr<CCLayerAnimationController> controller(CCLayerAnimationController::create(&dummy));
79
80     EXPECT_FALSE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
81
82     addOpacityTransitionToController(*controller, 1, 0, 1, false);
83
84     controller->pushAnimationUpdatesTo(controllerImpl.get());
85
86     EXPECT_TRUE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
87     EXPECT_EQ(CCActiveAnimation::WaitingForTargetAvailability, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
88
89     CCAnimationEventsVector events;
90     controllerImpl->animate(1, &events);
91
92     // Synchronize the start times.
93     EXPECT_EQ(1u, events.size());
94     controller->notifyAnimationStarted(events[0]);
95     EXPECT_EQ(controller->getActiveAnimation(0, CCActiveAnimation::Opacity)->startTime(), controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->startTime());
96
97     // Start the animation on the main thread. Should not affect the start time.
98     controller->animate(1.5, 0);
99     EXPECT_EQ(controller->getActiveAnimation(0, CCActiveAnimation::Opacity)->startTime(), controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->startTime());
100 }
101
102 TEST(CCLayerAnimationControllerTest, syncPauseAndResume)
103 {
104     FakeLayerAnimationControllerClient dummyImpl;
105     OwnPtr<CCLayerAnimationController> controllerImpl(CCLayerAnimationController::create(&dummyImpl));
106     FakeLayerAnimationControllerClient dummy;
107     OwnPtr<CCLayerAnimationController> controller(CCLayerAnimationController::create(&dummy));
108
109     EXPECT_FALSE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
110
111     addOpacityTransitionToController(*controller, 1, 0, 1, false);
112
113     controller->pushAnimationUpdatesTo(controllerImpl.get());
114
115     EXPECT_TRUE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
116     EXPECT_EQ(CCActiveAnimation::WaitingForTargetAvailability, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
117
118     // Start the animations on each controller.
119     CCAnimationEventsVector events;
120     controllerImpl->animate(0, &events);
121     controller->animate(0, 0);
122     EXPECT_EQ(CCActiveAnimation::Running, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
123     EXPECT_EQ(CCActiveAnimation::Running, controller->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
124
125     // Pause the main-thread animation.
126     controller->suspendAnimations(1);
127     EXPECT_EQ(CCActiveAnimation::Paused, controller->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
128
129     // The pause run state change should make it to the impl thread controller.
130     controller->pushAnimationUpdatesTo(controllerImpl.get());
131     EXPECT_EQ(CCActiveAnimation::Paused, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
132
133     // Resume the main-thread animation.
134     controller->resumeAnimations(2);
135     EXPECT_EQ(CCActiveAnimation::Running, controller->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
136
137     // The pause run state change should make it to the impl thread controller.
138     controller->pushAnimationUpdatesTo(controllerImpl.get());
139     EXPECT_EQ(CCActiveAnimation::Running, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
140 }
141
142 TEST(CCLayerAnimationControllerTest, doNotSyncFinishedAnimation)
143 {
144     FakeLayerAnimationControllerClient dummyImpl;
145     OwnPtr<CCLayerAnimationController> controllerImpl(CCLayerAnimationController::create(&dummyImpl));
146     FakeLayerAnimationControllerClient dummy;
147     OwnPtr<CCLayerAnimationController> controller(CCLayerAnimationController::create(&dummy));
148
149     EXPECT_FALSE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
150
151     addOpacityTransitionToController(*controller, 1, 0, 1, false);
152
153     controller->pushAnimationUpdatesTo(controllerImpl.get());
154
155     EXPECT_TRUE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
156     EXPECT_EQ(CCActiveAnimation::WaitingForTargetAvailability, controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity)->runState());
157
158     // Notify main thread controller that the animation has started.
159     CCAnimationEvent animationStartedEvent(CCAnimationEvent::Started, 0, 0, CCActiveAnimation::Opacity, 0);
160     controller->notifyAnimationStarted(animationStartedEvent);
161
162     // Force animation to complete on impl thread.
163     controllerImpl->removeAnimation(0);
164
165     EXPECT_FALSE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
166
167     controller->pushAnimationUpdatesTo(controllerImpl.get());
168
169     // Even though the main thread has a 'new' animation, it should not be pushed because the animation has already completed on the impl thread.
170     EXPECT_FALSE(controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity));
171 }
172
173 // Tests that transitioning opacity from 0 to 1 works as expected.
174 TEST(CCLayerAnimationControllerTest, TrivialTransition)
175 {
176     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
177     FakeLayerAnimationControllerClient dummy;
178     OwnPtr<CCLayerAnimationController> controller(
179         CCLayerAnimationController::create(&dummy));
180
181     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
182
183     controller->addAnimation(toAdd.release());
184     controller->animate(0, events.get());
185     EXPECT_TRUE(controller->hasActiveAnimation());
186     EXPECT_EQ(0, dummy.opacity());
187     controller->animate(1, events.get());
188     EXPECT_EQ(1, dummy.opacity());
189     EXPECT_FALSE(controller->hasActiveAnimation());
190 }
191
192 // Tests animations that are waiting for a synchronized start time do not finish.
193 TEST(CCLayerAnimationControllerTest, AnimationsWaitingForStartTimeDoNotFinishIfTheyWaitLongerToStartThanTheirDuration)
194 {
195     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
196     FakeLayerAnimationControllerClient dummy;
197     OwnPtr<CCLayerAnimationController> controller(
198         CCLayerAnimationController::create(&dummy));
199
200     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
201     toAdd->setNeedsSynchronizedStartTime(true);
202
203     // We should pause at the first keyframe indefinitely waiting for that animation to start.
204     controller->addAnimation(toAdd.release());
205     controller->animate(0, events.get());
206     EXPECT_TRUE(controller->hasActiveAnimation());
207     EXPECT_EQ(0, dummy.opacity());
208     controller->animate(1, events.get());
209     EXPECT_TRUE(controller->hasActiveAnimation());
210     EXPECT_EQ(0, dummy.opacity());
211     controller->animate(2, events.get());
212     EXPECT_TRUE(controller->hasActiveAnimation());
213     EXPECT_EQ(0, dummy.opacity());
214
215     // Send the synchronized start time.
216     controller->notifyAnimationStarted(CCAnimationEvent(CCAnimationEvent::Started, 0, 1, CCActiveAnimation::Opacity, 2));
217     controller->animate(5, events.get());
218     EXPECT_EQ(1, dummy.opacity());
219     EXPECT_FALSE(controller->hasActiveAnimation());
220 }
221
222 // Tests that two queued animations affecting the same property run in sequence.
223 TEST(CCLayerAnimationControllerTest, TrivialQueuing)
224 {
225     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
226     FakeLayerAnimationControllerClient dummy;
227     OwnPtr<CCLayerAnimationController> controller(
228         CCLayerAnimationController::create(&dummy));
229
230     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
231     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 1, 0.5)), 2, CCActiveAnimation::Opacity));
232
233     controller->animate(0, events.get());
234     EXPECT_TRUE(controller->hasActiveAnimation());
235     EXPECT_EQ(0, dummy.opacity());
236     controller->animate(1, events.get());
237     EXPECT_TRUE(controller->hasActiveAnimation());
238     EXPECT_EQ(1, dummy.opacity());
239     controller->animate(2, events.get());
240     EXPECT_EQ(0.5, dummy.opacity());
241     EXPECT_FALSE(controller->hasActiveAnimation());
242 }
243
244 // Tests interrupting a transition with another transition.
245 TEST(CCLayerAnimationControllerTest, Interrupt)
246 {
247     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
248     FakeLayerAnimationControllerClient dummy;
249     OwnPtr<CCLayerAnimationController> controller(
250         CCLayerAnimationController::create(&dummy));
251     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
252     controller->animate(0, events.get());
253     EXPECT_TRUE(controller->hasActiveAnimation());
254     EXPECT_EQ(0, dummy.opacity());
255
256     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 1, 0.5)), 2, CCActiveAnimation::Opacity));
257     toAdd->setRunState(CCActiveAnimation::WaitingForNextTick, 0);
258     controller->addAnimation(toAdd.release());
259
260     // Since the animation was in the WaitingForNextTick state, it should start right in
261     // this call to animate.
262     controller->animate(0.5, events.get());
263     EXPECT_TRUE(controller->hasActiveAnimation());
264     EXPECT_EQ(1, dummy.opacity());
265     controller->animate(1.5, events.get());
266     EXPECT_EQ(0.5, dummy.opacity());
267     EXPECT_FALSE(controller->hasActiveAnimation());
268 }
269
270 // Tests scheduling two animations to run together when only one property is free.
271 TEST(CCLayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked)
272 {
273     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
274     FakeLayerAnimationControllerClient dummy;
275     OwnPtr<CCLayerAnimationController> controller(
276         CCLayerAnimationController::create(&dummy));
277
278     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeTransformTransition(1)), 1, CCActiveAnimation::Transform));
279     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeTransformTransition(1)), 2, CCActiveAnimation::Transform));
280     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 2, CCActiveAnimation::Opacity));
281
282     controller->animate(0, events.get());
283     EXPECT_EQ(0, dummy.opacity());
284     EXPECT_TRUE(controller->hasActiveAnimation());
285     controller->animate(1, events.get());
286     // Should not have started the float transition yet.
287     EXPECT_TRUE(controller->hasActiveAnimation());
288     EXPECT_EQ(0, dummy.opacity());
289     // The float animation should have started at time 1 and should be done.
290     controller->animate(2, events.get());
291     EXPECT_EQ(1, dummy.opacity());
292     EXPECT_FALSE(controller->hasActiveAnimation());
293 }
294
295 // Tests scheduling two animations to run together with different lengths and another
296 // animation queued to start when the shorter animation finishes (should wait
297 // for both to finish).
298 TEST(CCLayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting)
299 {
300     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
301     FakeLayerAnimationControllerClient dummy;
302     OwnPtr<CCLayerAnimationController> controller(
303         CCLayerAnimationController::create(&dummy));
304
305     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeTransformTransition(2)), 1, CCActiveAnimation::Transform));
306     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
307     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 1, 0.5)), 2, CCActiveAnimation::Opacity));
308
309     // Animations with id 1 should both start now.
310     controller->animate(0, events.get());
311     EXPECT_TRUE(controller->hasActiveAnimation());
312     EXPECT_EQ(0, dummy.opacity());
313     // The opacity animation should have finished at time 1, but the group
314     // of animations with id 1 don't finish until time 2 because of the length
315     // of the transform animation.
316     controller->animate(2, events.get());
317     // Should not have started the float transition yet.
318     EXPECT_TRUE(controller->hasActiveAnimation());
319     EXPECT_EQ(1, dummy.opacity());
320
321     // The second opacity animation should start at time 2 and should be done by time 3
322     controller->animate(3, events.get());
323     EXPECT_EQ(0.5, dummy.opacity());
324     EXPECT_FALSE(controller->hasActiveAnimation());
325 }
326
327 // Tests scheduling an animation to start in the future.
328 TEST(CCLayerAnimationControllerTest, ScheduleAnimation)
329 {
330     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
331     FakeLayerAnimationControllerClient dummy;
332     OwnPtr<CCLayerAnimationController> controller(
333         CCLayerAnimationController::create(&dummy));
334
335     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
336     toAdd->setRunState(CCActiveAnimation::WaitingForStartTime, 0);
337     toAdd->setStartTime(1);
338     controller->addAnimation(toAdd.release());
339
340     controller->animate(0, events.get());
341     EXPECT_TRUE(controller->hasActiveAnimation());
342     EXPECT_EQ(0, dummy.opacity());
343     controller->animate(1, events.get());
344     EXPECT_TRUE(controller->hasActiveAnimation());
345     EXPECT_EQ(0, dummy.opacity());
346     controller->animate(2, events.get());
347     EXPECT_EQ(1, dummy.opacity());
348     EXPECT_FALSE(controller->hasActiveAnimation());
349 }
350
351 // Tests scheduling an animation to start in the future that's interrupting a running animation.
352 TEST(CCLayerAnimationControllerTest, ScheduledAnimationInterruptsRunningAnimation)
353 {
354     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
355     FakeLayerAnimationControllerClient dummy;
356     OwnPtr<CCLayerAnimationController> controller(
357         CCLayerAnimationController::create(&dummy));
358
359     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(2, 0, 1)), 1, CCActiveAnimation::Opacity));
360
361     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0.5, 0)), 2, CCActiveAnimation::Opacity));
362     toAdd->setRunState(CCActiveAnimation::WaitingForStartTime, 0);
363     toAdd->setStartTime(1);
364     controller->addAnimation(toAdd.release());
365
366     // First 2s opacity transition should start immediately.
367     controller->animate(0, events.get());
368     EXPECT_TRUE(controller->hasActiveAnimation());
369     EXPECT_EQ(0, dummy.opacity());
370     controller->animate(0.5, events.get());
371     EXPECT_TRUE(controller->hasActiveAnimation());
372     EXPECT_EQ(0.25, dummy.opacity());
373     controller->animate(1, events.get());
374     EXPECT_TRUE(controller->hasActiveAnimation());
375     EXPECT_EQ(0.5, dummy.opacity());
376     controller->animate(2, events.get());
377     EXPECT_EQ(0, dummy.opacity());
378     EXPECT_FALSE(controller->hasActiveAnimation());
379 }
380
381 // Tests scheduling an animation to start in the future that interrupts a running animation
382 // and there is yet another animation queued to start later.
383 TEST(CCLayerAnimationControllerTest, ScheduledAnimationInterruptsRunningAnimationWithAnimInQueue)
384 {
385     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
386     FakeLayerAnimationControllerClient dummy;
387     OwnPtr<CCLayerAnimationController> controller(
388         CCLayerAnimationController::create(&dummy));
389
390     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(2, 0, 1)), 1, CCActiveAnimation::Opacity));
391
392     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(2, 0.5, 0)), 2, CCActiveAnimation::Opacity));
393     toAdd->setRunState(CCActiveAnimation::WaitingForStartTime, 0);
394     toAdd->setStartTime(1);
395     controller->addAnimation(toAdd.release());
396
397     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 0.75)), 3, CCActiveAnimation::Opacity));
398
399     // First 2s opacity transition should start immediately.
400     controller->animate(0, events.get());
401     EXPECT_TRUE(controller->hasActiveAnimation());
402     EXPECT_EQ(0, dummy.opacity());
403     controller->animate(0.5, events.get());
404     EXPECT_TRUE(controller->hasActiveAnimation());
405     EXPECT_EQ(0.25, dummy.opacity());
406     EXPECT_TRUE(controller->hasActiveAnimation());
407     controller->animate(1, events.get());
408     EXPECT_TRUE(controller->hasActiveAnimation());
409     EXPECT_EQ(0.5, dummy.opacity());
410     controller->animate(3, events.get());
411     EXPECT_TRUE(controller->hasActiveAnimation());
412     EXPECT_EQ(0, dummy.opacity());
413     controller->animate(4, events.get());
414     EXPECT_EQ(0.75, dummy.opacity());
415     EXPECT_FALSE(controller->hasActiveAnimation());
416 }
417
418 // Test that a looping animation loops and for the correct number of iterations.
419 TEST(CCLayerAnimationControllerTest, TrivialLooping)
420 {
421     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
422     FakeLayerAnimationControllerClient dummy;
423     OwnPtr<CCLayerAnimationController> controller(
424         CCLayerAnimationController::create(&dummy));
425
426     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), 1, CCActiveAnimation::Opacity));
427     toAdd->setIterations(3);
428     controller->addAnimation(toAdd.release());
429
430     controller->animate(0, events.get());
431     EXPECT_TRUE(controller->hasActiveAnimation());
432     EXPECT_EQ(0, dummy.opacity());
433     controller->animate(1.25, events.get());
434     EXPECT_TRUE(controller->hasActiveAnimation());
435     EXPECT_EQ(0.25, dummy.opacity());
436     controller->animate(1.75, events.get());
437     EXPECT_TRUE(controller->hasActiveAnimation());
438     EXPECT_EQ(0.75, dummy.opacity());
439     controller->animate(2.25, events.get());
440     EXPECT_TRUE(controller->hasActiveAnimation());
441     EXPECT_EQ(0.25, dummy.opacity());
442     controller->animate(2.75, events.get());
443     EXPECT_TRUE(controller->hasActiveAnimation());
444     EXPECT_EQ(0.75, dummy.opacity());
445     controller->animate(3, events.get());
446     EXPECT_FALSE(controller->hasActiveAnimation());
447     EXPECT_EQ(1, dummy.opacity());
448
449     // Just be extra sure.
450     controller->animate(4, events.get());
451     EXPECT_EQ(1, dummy.opacity());
452 }
453
454 // Test that an infinitely looping animation does indeed go until aborted.
455 TEST(CCLayerAnimationControllerTest, InfiniteLooping)
456 {
457     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
458     FakeLayerAnimationControllerClient dummy;
459     OwnPtr<CCLayerAnimationController> controller(
460         CCLayerAnimationController::create(&dummy));
461
462     const int id = 1;
463     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), id, CCActiveAnimation::Opacity));
464     toAdd->setIterations(-1);
465     controller->addAnimation(toAdd.release());
466
467     controller->animate(0, events.get());
468     EXPECT_TRUE(controller->hasActiveAnimation());
469     EXPECT_EQ(0, dummy.opacity());
470     controller->animate(1.25, events.get());
471     EXPECT_TRUE(controller->hasActiveAnimation());
472     EXPECT_EQ(0.25, dummy.opacity());
473     controller->animate(1.75, events.get());
474     EXPECT_TRUE(controller->hasActiveAnimation());
475     EXPECT_EQ(0.75, dummy.opacity());
476
477     controller->animate(1073741824.25, events.get());
478     EXPECT_TRUE(controller->hasActiveAnimation());
479     EXPECT_EQ(0.25, dummy.opacity());
480     controller->animate(1073741824.75, events.get());
481     EXPECT_TRUE(controller->hasActiveAnimation());
482     EXPECT_EQ(0.75, dummy.opacity());
483
484     EXPECT_TRUE(controller->getActiveAnimation(id, CCActiveAnimation::Opacity));
485     controller->getActiveAnimation(id, CCActiveAnimation::Opacity)->setRunState(CCActiveAnimation::Aborted, 0.75);
486     EXPECT_FALSE(controller->hasActiveAnimation());
487     EXPECT_EQ(0.75, dummy.opacity());
488 }
489
490 // Test that pausing and resuming work as expected.
491 TEST(CCLayerAnimationControllerTest, PauseResume)
492 {
493     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
494     FakeLayerAnimationControllerClient dummy;
495     OwnPtr<CCLayerAnimationController> controller(
496         CCLayerAnimationController::create(&dummy));
497
498     const int id = 1;
499     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 0, 1)), id, CCActiveAnimation::Opacity));
500
501     controller->animate(0, events.get());
502     EXPECT_TRUE(controller->hasActiveAnimation());
503     EXPECT_EQ(0, dummy.opacity());
504     controller->animate(0.5, events.get());
505     EXPECT_TRUE(controller->hasActiveAnimation());
506     EXPECT_EQ(0.5, dummy.opacity());
507
508     EXPECT_TRUE(controller->getActiveAnimation(id, CCActiveAnimation::Opacity));
509     controller->getActiveAnimation(id, CCActiveAnimation::Opacity)->setRunState(CCActiveAnimation::Paused, 0.5);
510
511     controller->animate(1024, events.get());
512     EXPECT_TRUE(controller->hasActiveAnimation());
513     EXPECT_EQ(0.5, dummy.opacity());
514
515     EXPECT_TRUE(controller->getActiveAnimation(id, CCActiveAnimation::Opacity));
516     controller->getActiveAnimation(id, CCActiveAnimation::Opacity)->setRunState(CCActiveAnimation::Running, 1024);
517
518     controller->animate(1024.25, events.get());
519     EXPECT_TRUE(controller->hasActiveAnimation());
520     EXPECT_EQ(0.75, dummy.opacity());
521     controller->animate(1024.5, events.get());
522     EXPECT_FALSE(controller->hasActiveAnimation());
523     EXPECT_EQ(1, dummy.opacity());
524 }
525
526 TEST(CCLayerAnimationControllerTest, AbortAGroupedAnimation)
527 {
528     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
529     FakeLayerAnimationControllerClient dummy;
530     OwnPtr<CCLayerAnimationController> controller(
531         CCLayerAnimationController::create(&dummy));
532
533     const int id = 1;
534     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeTransformTransition(1)), id, CCActiveAnimation::Transform));
535     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(2, 0, 1)), id, CCActiveAnimation::Opacity));
536     controller->addAnimation(createActiveAnimation(adoptPtr(new FakeFloatTransition(1, 1, 0.75)), 2, CCActiveAnimation::Opacity));
537
538     controller->animate(0, events.get());
539     EXPECT_TRUE(controller->hasActiveAnimation());
540     EXPECT_EQ(0, dummy.opacity());
541     controller->animate(1, events.get());
542     EXPECT_TRUE(controller->hasActiveAnimation());
543     EXPECT_EQ(0.5, dummy.opacity());
544
545     EXPECT_TRUE(controller->getActiveAnimation(id, CCActiveAnimation::Opacity));
546     controller->getActiveAnimation(id, CCActiveAnimation::Opacity)->setRunState(CCActiveAnimation::Aborted, 1);
547     controller->animate(1, events.get());
548     EXPECT_TRUE(controller->hasActiveAnimation());
549     EXPECT_EQ(1, dummy.opacity());
550     controller->animate(2, events.get());
551     EXPECT_TRUE(!controller->hasActiveAnimation());
552     EXPECT_EQ(0.75, dummy.opacity());
553 }
554
555 TEST(CCLayerAnimationControllerTest, ForceSyncWhenSynchronizedStartTimeNeeded)
556 {
557     FakeLayerAnimationControllerClient dummyImpl;
558     OwnPtr<CCLayerAnimationController> controllerImpl(CCLayerAnimationController::create(&dummyImpl));
559     OwnPtr<CCAnimationEventsVector> events(adoptPtr(new CCAnimationEventsVector));
560     FakeLayerAnimationControllerClient dummy;
561     OwnPtr<CCLayerAnimationController> controller(
562         CCLayerAnimationController::create(&dummy));
563
564     OwnPtr<CCActiveAnimation> toAdd(createActiveAnimation(adoptPtr(new FakeFloatTransition(2, 0, 1)), 0, CCActiveAnimation::Opacity));
565     toAdd->setNeedsSynchronizedStartTime(true);
566     controller->addAnimation(toAdd.release());
567
568     controller->animate(0, 0);
569     EXPECT_TRUE(controller->hasActiveAnimation());
570     CCActiveAnimation* activeAnimation = controller->getActiveAnimation(0, CCActiveAnimation::Opacity);
571     EXPECT_TRUE(activeAnimation);
572     EXPECT_TRUE(activeAnimation->needsSynchronizedStartTime());
573
574     controller->setForceSync();
575
576     controller->pushAnimationUpdatesTo(controllerImpl.get());
577
578     activeAnimation = controllerImpl->getActiveAnimation(0, CCActiveAnimation::Opacity);
579     EXPECT_TRUE(activeAnimation);
580     EXPECT_EQ(CCActiveAnimation::WaitingForTargetAvailability, activeAnimation->runState());
581 }
582
583 } // namespace