/
LayerAnimationTest.coffee
868 lines (662 loc) · 18.8 KB
/
LayerAnimationTest.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
assert = require "assert"
AnimationTime = 0.05
AnimationProperties = ["x", "y", "midY", "rotation"]
describe "LayerAnimation", ->
describe "Defaults", ->
it "should use defaults", ->
Framer.Defaults.Animation =
curve: "spring(1, 2, 3)"
animation = new Animation new Layer(),
x: 50
animation.options.curve.should.equal "spring(1, 2, 3)"
Framer.resetDefaults()
it "should use linear", ->
# We shouldn't change the linear default, people rely on it
animation = new Animation new Layer(),
x: 50
animation.options.curve.should.equal "ease"
animation.options.time.should.equal 1
describe "Properties", ->
AnimationProperties.map (p) ->
it "should still support the deprecated API for property #{p}", (done) ->
layer = new Layer()
properties = {}
properties[p] = 100
layer.animate
properties: properties
curve: "linear"
time: AnimationTime
layer.on "end", ->
layer[p].should.equal 100
done()
it "should animate property #{p}", (done) ->
layer = new Layer()
properties = {}
properties[p] = 100
options =
curve: "linear"
time: AnimationTime
animation = layer.animate properties, options
animation.options.curve.should.equal "linear"
layer.on "end", ->
layer[p].should.equal 100
done()
it "should support options key for property #{p}", (done) ->
layer = new Layer()
properties = {}
properties[p] = 100
properties.options =
curve: "linear"
time: AnimationTime
animation = layer.animate properties
animation.options.curve.should.equal "linear"
layer.on "end", ->
layer[p].should.equal 100
done()
it "should animate property #{p} with positive offset from current value", (done) ->
layer = new Layer()
layer[p] = 50
properties = {}
properties[p] = "+=50"
layer.animate properties,
curve: "linear"
time: AnimationTime
layer.on "end", ->
layer[p].should.equal 100
done()
it "should animate property #{p} with negative offset from current value", (done) ->
layer = new Layer()
layer[p] = 50
properties = {}
properties[p] = "+=50"
layer.animate properties,
curve: "linear"
time: AnimationTime
layer.on "end", ->
layer[p].should.equal 100
done()
it "should animate dynamic properties", (done) ->
layer = new Layer()
layer.animate
scale: -> layer.scale + 1
options:
curve: "linear"
time: AnimationTime
layer.on "end", ->
layer.scale.should.equal 2
done()
it "should animate colors", (done) ->
color = "red"
layer = new Layer()
layer.animate
backgroundColor: color
options:
curve: "linear"
time: AnimationTime
layer.on "end", ->
layer.backgroundColor.toName().should.eql color
done()
it "should not animate non-animatable properties that are set to null", ->
layerA = new Layer
layerB = new Layer parent: layerA
layerB.animate
parent: null
options:
instant: true
assert.equal(layerB.parent, layerA)
describe "Basic", ->
it "should stop", (done) ->
layer = new Layer()
animation = new Animation layer,
x: 50
options:
curve: "linear"
time: 0.5
animation.start()
Utils.delay animation.options.time / 2.0, ->
animation.stop()
layer.x.should.be.within(10, 45)
done()
it "should cancel previous animation for the same property", ->
layer = new Layer()
animationA = new Animation layer,
x: 50
options:
curve: "linear"
time: 0.5
animationB = new Animation layer,
x: 50
options:
curve: "linear"
time: 0.5
stopped = false
animationA.on "stop", -> stopped = true
animationA.start().should.equal true
animationB.start().should.equal true
stopped.should.equal true
it "should work, even with MobileScrollFix enabled", (done) ->
layer = new Layer()
Framer.Extras.MobileScrollFix.enable()
layer.animationOptions = time: AnimationTime
animation = layer.animate x: 100
animation.start()
Utils.delay animation.options.time, ->
done()
it "copy should work", (done) ->
layer = new Layer
x: 50
animation = new Animation layer, {x: 100}, {time: AnimationTime}
copy = animation.copy()
copy.onAnimationEnd ->
layer.x.should.equal 100
done()
copy.start()
it "reverse should work", (done) ->
layer = new Layer
x: 50
animation = new Animation layer, {x: 100}, {time: AnimationTime}
animation.onAnimationEnd ->
layer.x.should.equal 100
a = animation.reverse()
a.once Events.AnimationEnd, ->
layer.x.should.equal 50
done()
a.start()
animation.start()
it "it should set the noop property", ->
layer = new Layer
animation = layer.animate
x: 50
animation.isNoop.should.be.equal false
animation.finish()
layer.x.should.be.equal 50
animation2 = layer.animate
x: 50
animation2.isNoop.should.be.equal true
it "In shouldn't crash when finish on delayed animation", ->
layer = new Layer
animation = layer.animate
x: 50
options:
delay: 1
animation.finish.should.not.throw()
it "should work with derived properties like size", (done) ->
layer = new Layer
animation = layer.animate
x: 300
size:
width: 400
height: 500
animation.onAnimationEnd ->
layer.x.should.equal 300
layer.width.should.equal 400
layer.height.should.equal 500
done()
it "should work with using objects that have more properties", (done) ->
layer = new Layer
animation = layer.animate
x: 300
size: Screen
animation.onAnimationEnd ->
layer.x.should.equal 300
layer.width.should.equal Screen.width
layer.height.should.equal Screen.height
done()
describe "Context", ->
it "should list running animations", ->
layer = new Layer()
animation = layer.animate
x: 100
options:
time: 0.5
(animation in layer.animations()).should.be.true
layer.animateStop()
(animation in layer.animations()).should.be.false
it "should list running animations correctly", (done) ->
layer = new Layer()
animation = layer.animate
x: 100
options:
time: 0.5
count = 0
test = ->
layer.animations().length.should.equal 0
count++
if count is 2
done()
animation.on "end", test
animation.on "stop", test
it "shouldn't return delayed animations from layer.animations()", (done) ->
layer = new Layer()
animation = layer.animate
x: 100
options:
time: 0.1
delay: 0.1
count = 0
test = ->
layer.animations().length.should.equal 0
count++
if count is 3
done()
test()
animation.on "end", test
animation.on "stop", test
it "should return delayed animations when calling layer.animations(true)", (done) ->
layer = new Layer
animation = layer.animate
x: 100
options:
time: 0.1
delay: 0.1
count = 0
test = ->
(animation in layer.animations(true)).should.equal true
count++
if count is 2
done()
test()
animation.on "start", test
it "should tell you if animations are running", ->
layer = new Layer()
animation = layer.animate
x: 100
options:
time: 0.5
layer.isAnimating.should.equal(true)
layer.animateStop()
layer.isAnimating.should.equal(false)
it "should tell you if delayed animations are running", (done) ->
layer = new Layer()
animation = layer.animate
x: 100
options:
time: 0.3
delay: 0.1
animation.isAnimating.should.equal(false)
Utils.delay 0.2, ->
animation.isAnimating.should.equal(true)
done()
describe "Events", ->
it "should throw start", (done) ->
layer = new Layer()
animation = new Animation layer,
x: 50
options:
curve: "linear"
time: AnimationTime
count = 0
animation.on "start", -> count++
layer.on "start", -> count++
animation.start()
layer.on "end", ->
count.should.equal 2
done()
it "should throw end", (done) ->
layer = new Layer()
animation = new Animation layer,
x: 50
options:
curve: "linear"
time: AnimationTime
count = 0
test = -> count++; done() if count is 2
animation.on "end", test
layer.on "end", test
animation.start()
it "should throw stop", (done) ->
layer = new Layer()
animation = new Animation layer,
x: 50
options:
curve: "linear"
time: AnimationTime * 2
count = 0
test = -> count++; done() if count is 2
animation.on "stop", test
layer.on "stop", test
animation.start()
Utils.delay AnimationTime, ->
animation.stop()
describe "Delay", ->
it "should start after a while", (done) ->
layer = new Layer()
animation = new Animation layer,
x: 50
options:
curve: "linear"
time: AnimationTime
delay: AnimationTime
animation.start()
Utils.delay AnimationTime, ->
layer.x.should.be.within(0, 1)
Utils.delay AnimationTime, ->
layer.x.should.be.within(30, 50)
done()
it "should stop when stopped before delay ends", (done) ->
layer = new Layer()
animation = new Animation layer,
properties: {x: 50}
curve: "linear"
time: 0.5
delay: 0.3
animation.start()
animation.stop()
Utils.delay 0.3, ->
layer.x.should.equal 0
Utils.delay 0.5, ->
layer.x.should.equal 0
done()
it "pending flag should be false by default", ->
layer = new Layer
a = layer.animate
x: 100
a.isPending.should.equal false
a.stop()
it "should add a pending flag for delayed animations", (done) ->
layer = new Layer
a = layer.animate
x: 100
options:
delay: 0.3
a.isPending.should.equal true
Utils.delay 0.3, ->
a.isPending.should.equal false
done()
it "should add pending animations to the context", ->
layer = new Layer
a = layer.animate
x: 100
options:
delay: 0.3
(a in layer.context.animations).should.equal true
describe "Repeat", ->
it "should start repeatedly", (done) ->
layer = new Layer()
animation = new Animation layer,
x: -> layer.x + 100
options:
curve: "linear"
time: AnimationTime
repeat: 5
animation.start()
count = 0
layer.on "end", ->
count++
if count is animation.options.repeat
done()
describe "AnimationLoop", ->
it "should only stop when all animations are done", (done) ->
layerA = new Layer width: 80, height: 80
layerA.name = "layerA"
layerA.animate
y: 300
options:
time: 2 * AnimationTime
layerB = new Layer width: 80, height: 80, x: 100, backgroundColor: "red"
layerB.name = "layerB"
layerB.animate
y: 300
options:
time: 5 * AnimationTime
layerC = new Layer width: 80, height: 80, x: 200, backgroundColor: "orange"
layerC.name = "layerC"
layerC.animate
y: 300
options:
time: 2 * AnimationTime
curve: "cubic-bezier"
readyLayers = []
ready = (animation, layer) ->
(layer in readyLayers).should.equal false
readyLayers.push layer
if readyLayers.length is 3
layerA.y.should.equal 300
layerB.y.should.equal 300
layerC.y.should.equal 300
done()
layerA.on "end", ready
layerB.on "end", ready
layerC.on "end", ready
describe "Animation", ->
beforeEach ->
@layer = new Layer x: 0, y: 0, width: 80, height: 80
describe "Parsing Animation Options", ->
describe "BezierCurveAnimator", ->
it "should create animation with bezier curve defined by values array and time in curveOptions", ->
animation = new Animation @layer,
x: 100
options:
curve: "cubic-bezier"
curveOptions:
time: 2
values: [0, 0, 0.58, 1]
animation.start()
animation._animator.options.time.should.equal 2
animation._animator.options.values.should.eql [0, 0, .58, 1]
it "should create animation with bezier curve defined by named bezier curve in values and time in curveOptions", ->
animation = new Animation @layer,
x: 100
options:
curve: "cubic-bezier"
curveOptions:
time: 2
values: "ease-out"
animation.start()
animation._animator.options.time.should.equal 2
animation._animator.options.values.should.eql [0, 0, .58, 1]
it "should create animation with named bezier curve", ->
animation = new Animation @layer,
x: 100
options:
curve: "cubic-bezier"
curveOptions: "ease-out"
animation.start()
animation._animator.options.time.should.equal 1
animation._animator.options.values.should.eql [0, 0, .58, 1]
it "should create animation with named bezier curve and time", ->
animation = new Animation @layer,
x: 100
options:
time: 2
curve: "cubic-bezier"
curveOptions: "ease-out"
animation.start()
animation._animator.options.time.should.equal 2
animation._animator.options.values.should.eql [0, 0, .58, 1]
it "should create animation with bezier curve function passed in as a string and time", ->
animation = new Animation @layer,
x: 100
options:
time: 2
curve: "cubic-bezier(0, 0, 0.58, 1)"
animation.start()
animation._animator.options.time.should.equal 2
animation._animator.options.values.should.eql [0, 0, .58, 1]
it "should create animation with bezier curve defined by an array and time", ->
animation = new Animation @layer,
x: 100
options:
time: 2
curve: "cubic-bezier"
curveOptions: [0, 0, 0.58, 1]
animation.start()
animation._animator.options.time.should.equal 2
animation._animator.options.values.should.eql [0, 0, .58, 1]
describe "LinearAnimator", ->
it "should create linear animation with time defined outside of curveOptions", ->
animation = new Animation @layer,
x: 100
options:
curve: "linear"
time: 2
animation.start()
animation._animator.options.time.should.equal 2
it "should create linear animation with time defined inside curveOptions", ->
animation = new Animation @layer,
x: 100
options:
curve: "linear"
curveOptions:
time: 2
animation.start()
animation._animator.options.time.should.equal 2
describe "Instant", ->
it "should not animate if animate is disabled", ->
animation = new Animation @layer,
x: 100
options:
curve: "spring"
time: 2
animate: false
calledEvents = []
["start", "stop", "end"].map (eventName) ->
animation.on eventName, ->
calledEvents.push(eventName)
animation.start()
@layer.x.should.equal 100
calledEvents.should.eql(["start", "stop", "end"])
it "should listen to instant: true to disable animation", ->
animation = @layer.animate
x: 100
options:
curve: "spring"
time: 2
instant: true
start: false
calledEvents = []
["start", "stop", "end"].map (eventName) ->
animation.on eventName, ->
calledEvents.push(eventName)
animation.start()
@layer.x.should.equal 100
calledEvents.should.eql(["start", "stop", "end"])
describe "Callbacks", ->
it "should call start", (done) ->
layer = new Layer
layer.animate
x: 100
options:
time: AnimationTime
onStart: ->
layer.x.should.eql 0
done()
it "should call halt", (done) ->
layer = new Layer
layer.animate
x: 100
options:
time: AnimationTime
onHalt: ->
done()
layer.animateStop()
it "should call stop", (done) ->
layer = new Layer
layer.animate
x: 100
options:
time: AnimationTime
onStop: ->
layer.x.should.eql 100
done()
it "should call end", (done) ->
layer = new Layer
layer.animate
x: 100
options:
time: AnimationTime
onEnd: ->
layer.x.should.eql 100
done()
describe "Backwards compatibility", ->
it "should support the original api", ->
layer = new Layer()
animation = new Animation
layer: layer
properties:
x: 50
curve: "linear"
time: 0.1
animation.layer.should.equal layer
animation.properties.should.eql {x: 50}
animation.options.curve.should.equal "linear"
animation.options.time.should.equal 0.1
it "should support the original api variation 1", ->
layer = new Layer()
animation = new Animation layer,
properties:
x: 50
options:
curve: "linear"
time: 0.1
animation.layer.should.equal layer
animation.properties.should.eql {x: 50}
animation.options.curve.should.equal "linear"
animation.options.time.should.equal 0.1
describe "API Variations", ->
it "should support properties", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
animation = layer.animate x: 10
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()
it "should support properties with options", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
animation = layer.animate({x: 10}, {curve: "linear"})
animation.options.curve.should.equal "linear"
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()
it "should support properties with options as object", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
animation = layer.animate
x: 10
options:
curve: "linear"
animation.options.curve.should.equal "linear"
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()
it "should support properties with options that have undefined curveOptions as object", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
animation = layer.animate
x: 10
options:
curve: "linear"
curveOptions: undefined
animation.options.curve.should.equal "linear"
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()
it "should support states", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
layer.states.test = {x: 10}
animation = layer.animate "test"
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()
it "should support state with options", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
layer.states.test = {x: 10}
animation = layer.animate("test", {curve: "linear"})
animation.options.curve.should.equal "linear"
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()
it "should support state with options as object", (done) ->
layer = new Layer
layer.animationOptions = time: AnimationTime
layer.states.test = {x: 10}
animation = layer.animate "test",
options:
curve: "linear"
animation.options.curve.should.equal "linear"
animation.on Events.AnimationEnd, ->
layer.x.should.equal 10
done()