This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
unittests.js
882 lines (829 loc) · 47.9 KB
/
unittests.js
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, it, expect, beforeEach, afterEach, runs, $, brackets, waitsForDone */
define(function (require, exports, module) {
"use strict";
// Modules from the SpecRunner window
var SpecRunnerUtils = brackets.getModule("spec/SpecRunnerUtils"),
KeyEvent = brackets.getModule("utils/KeyEvent"),
testContentCSS = require("text!unittest-files/unittests.css"),
provider = require("main").inlineTimingFunctionEditorProvider,
TimingFunctionUtils = require("TimingFunctionUtils"),
BezierCurveEditor = require("BezierCurveEditor").BezierCurveEditor,
StepEditor = require("StepEditor").StepEditor;
describe("Inline Timing Function Editor", function () {
var testDocument, testEditor, inline;
/**
* Creates an inline timing function editor connected to the given cursor position in the test editor.
* Note that this does *not* actually open it as an inline editor in the test editor.
* Tests that use this must wrap their contents in a runs() block.
* @param {!{line:number, ch: number}} cursor Position for which to open the inline editor.
* if the provider did not create an inline editor.
*/
function makeTimingFunctionEditor(cursor) {
runs(function () {
var promise = provider(testEditor, cursor);
if (promise) {
promise.done(function (inlineResult) {
inlineResult.onAdded();
inline = inlineResult;
});
waitsForDone(promise, "open timing function editor");
}
});
}
/**
* Expects arrays to be of specified length and equal.
* @param {Array} a1 Result to test
* @param {Array} a2 Expected values.
* @param {number} len Expected length.
*/
function expectArraysToBeEqual(a1, a2, len) {
expect(a2.length).toEqual(a1.length);
a2.forEach(function (entry, index) {
expect(entry).toEqual(a1[index]);
});
}
describe("TimingFunctionUtils for bezier curve functions", function () {
var match;
/**
* Expects an invalid steps() function to be corrected the right way, with the right match
* and originalString given a string to match and an expectation of the output match.
* @param {string} str The string to match
* @param {Array} expectedArray The array that should equal the output match.
*/
function testInvalidBezier(str, expectedArray) {
var match = TimingFunctionUtils.timingFunctionMatch(str, false);
runs(function () {
expectArraysToBeEqual(match, expectedArray);
expect(match.originalString).toEqual(str);
});
}
// Valid cubic-bezier function cases
it("should match bezier curve function in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier(.1, .2, .3, .4)", false);
expect(match).toBeTruthy();
expectArraysToBeEqual(match, ["cubic-bezier(.1, .2, .3, .4)", ".1", ".2", ".3", ".4"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier(.1, .2, .3, .4)", true);
expect(match).toBeTruthy();
expectArraysToBeEqual(match, ["cubic-bezier(.1, .2, .3, .4)", ".1", ".2", ".3", ".4"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function with negative value", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier(0, -.2, 1, 1.2)", false);
expectArraysToBeEqual(match, ["cubic-bezier(0, -.2, 1, 1.2)", "0", "-.2", "1", "1.2"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function in full line of longhand css", function () {
match = TimingFunctionUtils.timingFunctionMatch(" transition-timing-function: cubic-bezier(.37, .28, .83, .94);", false);
expectArraysToBeEqual(match, ["cubic-bezier(.37, .28, .83, .94)", ".37", ".28", ".83", ".94"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function in full line of shorthand css", function () {
match = TimingFunctionUtils.timingFunctionMatch(" transition: top 100ms cubic-bezier(.37, .28, .83, .94) 0;", false);
expectArraysToBeEqual(match, ["cubic-bezier(.37, .28, .83, .94)", ".37", ".28", ".83", ".94"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function with leading zeros", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier(0.1, 0.2, 0.3, 0.4)", false);
expectArraysToBeEqual(match, ["cubic-bezier(0.1, 0.2, 0.3, 0.4)", "0.1", "0.2", "0.3", "0.4"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function with no optional whitespace", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier(.1,.2,.3,.4)", false);
expectArraysToBeEqual(match, ["cubic-bezier(.1,.2,.3,.4)", ".1", ".2", ".3", ".4"]);
expect(match.originalString).toBeFalsy();
});
it("should match bezier curve function with extra optional whitespace", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier( .1 , .2 , .3 , .4 )", false);
expectArraysToBeEqual(match, ["cubic-bezier( .1 , .2 , .3 , .4 )", ".1", ".2", ".3", ".4"]);
expect(match.originalString).toBeFalsy();
});
// Valid other functions
it("should match linear function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: linear;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("linear");
expect(match.originalString).toBeFalsy();
});
it("should match linear function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("linear", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("linear");
expect(match.originalString).toBeFalsy();
});
it("should match ease function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: ease;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease");
expect(match.originalString).toBeFalsy();
});
it("should match ease function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("ease", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease");
expect(match.originalString).toBeFalsy();
});
it("should match ease-in function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: ease-in;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease-in");
expect(match.originalString).toBeFalsy();
});
it("should match ease-in function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("ease-in", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease-in");
expect(match.originalString).toBeFalsy();
});
it("should match ease-out function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: ease-out;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease-out");
expect(match.originalString).toBeFalsy();
});
it("should match ease-out function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("ease-out", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease-out");
expect(match.originalString).toBeFalsy();
});
it("should match ease-in-out function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: ease-in-out;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease-in-out");
expect(match.originalString).toBeFalsy();
});
it("should match ease-in-out function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("ease-in-out", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("ease-in-out");
expect(match.originalString).toBeFalsy();
});
// Invalid cubic-beziers - they should be corrected automatically
it("should correct cubic-bezier function with out-of-range X parameters", function () {
testInvalidBezier("cubic-bezier(-.2, 0, 1.2, 1)", ["cubic-bezier(0, 0, 1, 1)", "0", "0", "1", "1"]);
});
it("should correct cubic-bezier function with Infinity parameters", function () {
testInvalidBezier("cubic-bezier(0, Infinity, 1, -Infinity)", ["cubic-bezier(0, 0, 1, 1)", "0", "0", "1", "1"]);
});
it("should correct cubic-bezier function with non-numeric parameters", function () {
testInvalidBezier("cubic-bezier(x1, y1, x2, y2)", ["cubic-bezier(.42, 0, .58, 1)", ".42", "0", ".58", "1"]);
});
it("should correct cubic-bezier function with no parameters", function () {
testInvalidBezier("cubic-bezier()", ["cubic-bezier(.42, 0, .58, 1)", ".42", "0", ".58", "1"]);
});
it("should correct cubic-bezier function with 3 parameters", function () {
testInvalidBezier("cubic-bezier(0, 0, 1)", ["cubic-bezier(0, 0, 1, 1)", "0", "0", "1", "1"]);
});
it("should correct cubic-bezier function with 5 parameters", function () {
testInvalidBezier("cubic-bezier(0, 0, 1, 1, 1)", ["cubic-bezier(0, 0, 1, 1)", "0", "0", "1", "1"]);
});
it("should correct cubic-bezier function with trailing comma", function () {
testInvalidBezier("cubic-bezier(.42, 0, .58, .5,)", ["cubic-bezier(.42, 0, .58, .5)", ".42", "0", ".58", ".5"]);
});
// Real invalid cubic-beziers - they should NOT be corrected automatically
it("should not match cubic-bezier function with invalid whitespace", function () {
match = TimingFunctionUtils.timingFunctionMatch("cubic-bezier (0, 0, 1, 1)", false);
expect(match).toBeFalsy();
});
it("should not match cubic-bezier function with UPPER-CASE", function () {
match = TimingFunctionUtils.timingFunctionMatch("CUBIC-BEZIER(0, 0, 1, 1)", false);
expect(match).toBeFalsy();
});
it("should not match unknown timing function", function () {
match = TimingFunctionUtils.timingFunctionMatch("ease-out-in", false);
expect(match).toBeFalsy();
});
it("should not match linear when not a timing function", function () {
match = TimingFunctionUtils.timingFunctionMatch("background: linear-gradient(to bottom, blue, white);", false);
expect(match).toBeFalsy();
});
});
describe("TimingFunctionUtils for step functions", function () {
var match;
/**
* Expects an invalid steps() function to be corrected the right way, with the right match
* and originalString given a string to match and an expectation of the output match.
* @param {string} str The string to match
* @param {Array} expectedArray The array that should equal the output match.
*/
function testInvalidStep(str, expectedArray) {
var match = TimingFunctionUtils.timingFunctionMatch(str, false);
runs(function () {
expectArraysToBeEqual(match, expectedArray);
expect(match.originalString).toEqual(str);
});
}
// Valid steps function cases
it("should match steps function in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(3, start)", false);
expect(match).toBeTruthy();
expectArraysToBeEqual(match, ["steps(3, start)", "3", "start"]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(3, start)", true);
expect(match).toBeTruthy();
expectArraysToBeEqual(match, ["steps(3, start)", "3", "start"]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with second parameter of end", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(12, end)", false);
expectArraysToBeEqual(match, ["steps(12, end)", "12", "end"]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with only 1 parameter", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(8)", false);
expectArraysToBeEqual(match, ["steps(8)", "8", undefined]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function in full line of longhand css", function () {
match = TimingFunctionUtils.timingFunctionMatch(" transition-timing-function: steps(5, start);", false);
expectArraysToBeEqual(match, ["steps(5, start)", "5", "start"]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function in full line of shorthand css", function () {
match = TimingFunctionUtils.timingFunctionMatch(" transition: top 100ms steps(10) 0;", false);
expectArraysToBeEqual(match, ["steps(10)", "10", undefined]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with leading zeros", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(04, end)", false);
expectArraysToBeEqual(match, ["steps(04, end)", "04", "end"]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with no optional whitespace with 1 param", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(3)", false);
expectArraysToBeEqual(match, ["steps(3)", "3", undefined]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with no optional whitespace with 2 params", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps(3,end)", false);
expectArraysToBeEqual(match, ["steps(3,end)", "3", "end"]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with extra optional whitespace with 1 param", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps( 7 )", false);
expectArraysToBeEqual(match, ["steps( 7 )", "7", undefined]);
expect(match.originalString).toBeFalsy();
});
it("should match steps function with extra optional whitespace with 2 params", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps( 8 , start )", false);
expectArraysToBeEqual(match, ["steps( 8 , start )", "8", "start"]);
expect(match.originalString).toBeFalsy();
});
// Valid other functions
it("should match step-start function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: step-start;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("step-start");
expect(match.originalString).toBeFalsy();
});
it("should match step-start function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("step-start", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("step-start");
expect(match.originalString).toBeFalsy();
});
it("should match step-end function in declaration in strict mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("transition-timing-function: step-end;", false);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("step-end");
expect(match.originalString).toBeFalsy();
});
it("should match step-end function value in lax mode", function () {
match = TimingFunctionUtils.timingFunctionMatch("step-end", true);
expect(match.length).toEqual(1);
expect(match[0]).toEqual("step-end");
expect(match.originalString).toBeFalsy();
});
// Invalid steps - they should be corrected automatically
it("should correct steps function with zero steps", function () {
testInvalidStep("steps(0)", ["steps(5, end)", "5", "end"]);
});
it("should correct steps function with a non-integer number of steps", function () {
testInvalidStep("steps(3.0)", ["steps(3, end)", "3", "end"]);
});
it("should correct steps function with a negative number of steps", function () {
testInvalidStep("steps(-2)", ["steps(5, end)", "5", "end"]);
});
it("should correct steps function with an infinite number of steps", function () {
testInvalidStep("steps(Infinity,)", ["steps(5, end)", "5", "end"]);
});
it("should correct steps function with NaN number of steps", function () {
testInvalidStep("steps(NaN,)", ["steps(5, end)", "5", "end"]);
});
it("should correct steps function with non-numeric number of steps", function () {
testInvalidStep("steps(x)", ["steps(5, end)", "5", "end"]);
});
it("should correct steps function with a string-value number of steps", function () {
testInvalidStep("steps('3')", ["steps(3, end)", "3", "end"]);
});
it("should correct steps function with no parameters", function () {
testInvalidStep("steps()", ["steps(5, end)", "5", "end"]);
});
it("should correct steps function with empty second parameter", function () {
testInvalidStep("steps(1,)", ["steps(1, end)", "1", "end"]);
});
it("should correct steps function with undefined second parameter", function () {
testInvalidStep("steps(1, middle)", ["steps(1, end)", "1", "end"]);
});
it("should correct steps function with typo in second parameter", function () {
testInvalidStep("steps(1, satrt)", ["steps(1, start)", "1", "start"]);
});
it("should correct steps function with a string as second parameter", function () {
testInvalidStep("steps(1, 'start')", ["steps(1, start)", "1", "start"]);
});
it("should correct steps function with 3 parameters", function () {
testInvalidStep("steps(1, start, end)", ["steps(1, start)", "1", "start"]);
});
// Real invalid cubic-beziers - they should NOT be corrected automatically
it("should not match steps function with no parens", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps", false);
expect(match).toBeFalsy();
});
it("should not match steps function with invalid whitespace", function () {
match = TimingFunctionUtils.timingFunctionMatch("steps (1, end)", false);
expect(match).toBeFalsy();
});
it("should not match steps function with UPPER-CASE", function () {
match = TimingFunctionUtils.timingFunctionMatch("STEPS(12)", false);
expect(match).toBeFalsy();
});
it("should not match unknown timing function", function () {
match = TimingFunctionUtils.timingFunctionMatch("step", false);
expect(match).toBeFalsy();
});
});
describe("Bookmark Timing Function", function () {
beforeEach(function () {
var mock = SpecRunnerUtils.createMockEditor(testContentCSS, "css");
testDocument = mock.doc;
testEditor = mock.editor;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(testDocument);
testEditor = null;
testDocument = null;
inline = null;
});
/**
* Expects an inline editor to be opened at the given cursor position and to have the
* given initial timing function (which should match the timing function at that position).
* @param {!{line:number, ch:number}} cursor The cursor position to try opening the inline at.
* @param {number} start The expected start of timing function.
* @param {number} end The expected end of timing function.
*/
function testOpenTimingFunction(cursor, start, end) {
makeTimingFunctionEditor(cursor);
runs(function () {
expect(inline).toBeTruthy();
expect(inline._startBookmark.find().ch).toBe(start);
expect(inline._endBookmark.find().ch).toBe(end);
});
}
it("should bookmark cubic-bezier() function when opened in inline editor", function () {
testOpenTimingFunction({line: 3, ch: 34}, 32, 60);
});
it("should bookmark linear function when opened in inline editor", function () {
testOpenTimingFunction({line: 5, ch: 35}, 32, 38);
});
it("should bookmark second cubic-bezier() function when opened in inline editor", function () {
testOpenTimingFunction({line: 13, ch: 80}, 75, 107);
});
it("should bookmark steps() function when opened in inline editor", function () {
testOpenTimingFunction({line: 17, ch: 37}, 32, 45);
});
it("should bookmark step-start function when opened in inline editor", function () {
testOpenTimingFunction({line: 20, ch: 40}, 32, 42);
});
it("should bookmark long, invalid cubic-bezier() function when opened in inline editor", function () {
testOpenTimingFunction({line: 25, ch: 52}, 32, 74);
});
it("should bookmark empty, invalid cubic-bezier() function when opened in inline editor", function () {
testOpenTimingFunction({line: 26, ch: 47}, 32, 46);
});
it("should bookmark long, invalid steps() function when opened in inline editor", function () {
testOpenTimingFunction({line: 30, ch: 44}, 32, 50);
});
it("should bookmark empty, invalid steps() function when opened in inline editor", function () {
testOpenTimingFunction({line: 31, ch: 45}, 32, 39);
});
});
describe("TimingFunction editor UI", function () {
var timingFuncEditor;
/**
* Creates a hidden BezierCurveEditor and appends it to the body. Note that this is a
* standalone BezierCurveEditor, not inside an InlineTimingFunctionEditor.
* @param {string} initialTimingFunction The timingFunction that should be initially set
* in the BezierCurveEditor.
* @param {?function} callback An optional callback to be passed as the BezierCurveEditor's
* callback. If none is supplied, a dummy function is passed.
*/
function makeTimingFuncUI(initialTimingFunction, callback) {
var parent = $(document.body),
match = TimingFunctionUtils.timingFunctionMatch(initialTimingFunction, true),
cb = callback || function () { };
if (match.isBezier) {
timingFuncEditor = new BezierCurveEditor(parent, match, cb);
} else if (match.isStep) {
timingFuncEditor = new StepEditor(parent, match, cb);
}
// Hide it
timingFuncEditor.getRootElement().css("display", "none");
}
afterEach(function () {
timingFuncEditor.getRootElement().remove();
timingFuncEditor = null;
});
describe("Initial Load and External Update", function () {
it("should load the initial cubic-bezier function correctly", function () {
runs(function () {
makeTimingFuncUI("cubic-bezier(.2, .3, .4, .5)");
expect(timingFuncEditor).toBeTruthy();
expect(timingFuncEditor._cubicBezierCoords).toBeTruthy();
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".2", ".3", ".4", ".5"]);
});
});
it("should load externally updated cubic-bezier function correctly", function () {
runs(function () {
makeTimingFuncUI("cubic-bezier(.1, .3, .5, .7)");
var matchUpdate = TimingFunctionUtils.timingFunctionMatch("cubic-bezier(.2, .4, .6, .8)", true);
timingFuncEditor.handleExternalUpdate(matchUpdate);
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".2", ".4", ".6", ".8"]);
});
});
it("should load the initial steps function correctly", function () {
runs(function () {
makeTimingFuncUI("steps(5, start)");
expect(timingFuncEditor).toBeTruthy();
expect(timingFuncEditor._stepParams).toBeTruthy();
expect(timingFuncEditor._stepParams.count).toEqual(5);
expect(timingFuncEditor._stepParams.timing).toEqual("start");
});
});
it("should load externally updated steps function correctly", function () {
runs(function () {
makeTimingFuncUI("steps(5, start)");
var matchUpdate = TimingFunctionUtils.timingFunctionMatch("steps(6, end)", true);
timingFuncEditor.handleExternalUpdate(matchUpdate);
expect(timingFuncEditor._stepParams.count).toEqual(6);
expect(timingFuncEditor._stepParams.timing).toEqual("end");
});
});
});
describe("Conversions", function () {
it("should convert linear function to cubic-bezier function parameters", function () {
runs(function () {
makeTimingFuncUI("linear");
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, ["0", "0", "1", "1"]);
});
});
it("should convert ease function to cubic-bezier function parameters", function () {
runs(function () {
makeTimingFuncUI("ease");
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".25", ".1", ".25", "1"]);
});
});
it("should convert ease-in function to cubic-bezier function parameters", function () {
runs(function () {
makeTimingFuncUI("ease-in");
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".42", "0", "1", "1"]);
});
});
it("should convert ease-out function to cubic-bezier function parameters", function () {
runs(function () {
makeTimingFuncUI("ease-out");
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, ["0", "0", ".58", "1"]);
});
});
it("should convert ease-in-out function to cubic-bezier function parameters", function () {
runs(function () {
makeTimingFuncUI("ease-in-out");
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".42", "0", ".58", "1"]);
});
});
it("should convert step-start function to steps function parameters", function () {
runs(function () {
makeTimingFuncUI("step-start");
expect(timingFuncEditor).toBeTruthy();
expect(timingFuncEditor._stepParams).toBeTruthy();
expect(timingFuncEditor._stepParams.count).toEqual(1);
expect(timingFuncEditor._stepParams.timing).toEqual("start");
});
});
it("should convert step-end function to steps function parameters", function () {
runs(function () {
makeTimingFuncUI("step-end");
expect(timingFuncEditor._stepParams.count).toEqual(1);
expect(timingFuncEditor._stepParams.timing).toEqual("end");
});
});
});
describe("Editing with Mouse", function () {
/**
* Translate from a bezier-curve point (1.0 x 1.0 grid)
* to a canvas element point (150px x 150px grid).
* @param {Array} bezierPoint The bezier point [x, y].
* @return {Array} canvas element point in [x, y]
*/
function translatePointFromBezierToCanvas(bezierPoint) {
return [
Math.round(bezierPoint[0] * 150),
Math.round(((1 - bezierPoint[1]) * 150) + 75)
];
}
/**
* Simulate the given event with clientX/clientY specified by the given
* offsets by the left/top of the item.
* @param {string} event The name of the event to simulate.
* @param {object} $item A jQuery object to trigger the event on.
* @param {Array.<number>} offsets Numbers the x and y positions of the
* event relative to the item's top and left.
*/
function eventAtOffset(event, $item, offsets) {
$item.trigger($.Event(event, {
pageX: $item.offset().left + offsets[0],
pageY: $item.offset().top + offsets[1],
which: 1
}));
}
/**
* Test a mouse down event on the given UI element in a cubic-bezier function.
* @param {object} opts The parameters to test:
* item: The (string) name of the member of BezierCurveEditor that
* references the element to test.
* clickAt: An [x, y] array specifying the simulated x/y mouse position as
* an offset of the item's width/height.
* expected: The expected array of values for _cubicBezierCoords.
*/
function testCubicBezierClick(opts) {
makeTimingFuncUI("cubic-bezier(.42, 0, .58 ,1)");
var $item = $(timingFuncEditor[opts.item]);
eventAtOffset("click", $item, opts.clickAt);
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, opts.expected);
}
/**
* Test a drag event on the given UI element.
* @param {object} opts The parameters to test:
* downItem: The (string) name of the member of BezierCurveEditor
* that references the element to mousedown on to drag.
* clickAt: An [x, y] array specifying the simulated x/y mouse position as an offset of the
* item's width/height.
* dragItem: The (string) name of the member of BezierCurveEditor
* that references the element to drag item to.
* dragTo: An [x, y] array specifying the location to drag to, using the same convention as clickAt.
* expected: The expected array of values for _cubicBezierCoords.
*/
function testCubicBezierDrag(opts) {
makeTimingFuncUI("cubic-bezier(.42, 0, .58 ,1)");
var $downItem = $(timingFuncEditor[opts.downItem]),
$dragItem = $(timingFuncEditor[opts.dragItem]);
eventAtOffset("mousedown", $downItem, opts.clickAt);
eventAtOffset("mousemove", $dragItem, opts.dragTo);
$downItem.trigger("mouseup");
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, opts.expected);
}
it("should move point P1 on mousedown in curve", function () {
testCubicBezierClick({
item: "curve",
clickAt: translatePointFromBezierToCanvas([0.5, 0.1]),
expected: [".5", ".1", ".58", "1"]
});
});
it("should move point P2 on mousedown in curve", function () {
testCubicBezierClick({
item: "curve",
clickAt: translatePointFromBezierToCanvas([0.6, 1.2]),
expected: [".42", "0", ".6", "1.2"]
});
});
it("should move point P1 on drag", function () {
testCubicBezierDrag({
downItem: "P1", // mouse down on this element
clickAt: [5, 5],
dragItem: "curve", // drag over this element
dragTo: translatePointFromBezierToCanvas([0.6, -0.1]),
expected: [".6", "-0.1", ".58", "1"]
});
});
it("should move point P2 on drag", function () {
testCubicBezierDrag({
downItem: "P2", // mouse down on this element
clickAt: [5, 5],
dragItem: "curve", // drag over this element
dragTo: translatePointFromBezierToCanvas([0.8, 0.9]),
expected: [".42", "0", ".8", ".9"]
});
});
it("should not move point P2 x-value out-of-range on drag", function () {
testCubicBezierDrag({
downItem: "P2", // mouse down on this element
clickAt: [5, 5],
dragItem: "curve", // drag over this element
dragTo: translatePointFromBezierToCanvas([1.1, 1]),
expected: [".42", "0", "1", "1"]
});
});
});
describe("Editing with Keyboard", function () {
function makeKeyEvent(opts) {
return $.Event("keydown", { keyCode: opts.key, shiftKey: !!opts.shift });
}
/**
* Create a timing function editor and trigger a key event on it.
* @param {object} opts The parameters to test:
* func: The initial timing function
* item: The (string) name of the member of BezierCurveEditor
* that references the element to test.
* key: The KeyEvent key code to simulate.
* shift: Optional boolean specifying whether to simulate the shift
* key being down (default false).
* expected: The expected array of values for _cubicBezierCoords.
*/
function triggerTimingFunctionEditorKey(opts) {
makeTimingFuncUI(opts.func, opts.callback);
var $item = $(timingFuncEditor[opts.item]);
$item.focus();
$item.trigger(makeKeyEvent(opts));
}
// cubic-bezier() tests
it("should increase P1 x-value by .02 on right arrow in cubic-bezier()", function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(.42, 0, .58, 1)",
item: "P1",
key: KeyEvent.DOM_VK_RIGHT,
shift: false
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".44", "0", ".58", "1"]);
});
it("should increase P1 y-value by .1 on shift up arrow in cubic-bezier()", function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(.42, 0, .58, 1)",
item: "P1",
key: KeyEvent.DOM_VK_UP,
shift: true
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".42", ".1", ".58", "1"]);
});
it("should decrease P2 x-value by .02 on left arrow in cubic-bezier()", function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(.42, 0, .58, 1)",
item: "P2",
key: KeyEvent.DOM_VK_LEFT,
shift: false
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".42", "0", ".56", "1"]);
});
it("should decrease P2 y-value by .1 on shift down arrow in cubic-bezier()", function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(.42, 0, .58 ,1)",
item: "P2",
key: KeyEvent.DOM_VK_DOWN,
shift: true
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".42", "0", ".58", ".9"]);
});
it("should not decrease P1 x-value below 0 on left arrow in cubic-bezier()", function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(0, 0, 1, 1)",
item: "P1",
key: KeyEvent.DOM_VK_LEFT,
shift: false
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, ["0", "0", "1", "1"]);
});
it("should not increase P2 x-value above 0 on shift right arrow in cubic-bezier()", function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(0, 0, 1, 1)",
item: "P2",
key: KeyEvent.DOM_VK_RIGHT,
shift: true
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, ["0", "0", "1", "1"]);
});
it("should call callback function after cubic-bezier edit in cubic-bezier()", function () {
var calledBack = false;
var _callback = function (timingFunctionString) {
calledBack = true;
expect(timingFunctionString).toBe("cubic-bezier(.42, .1, .58, 1)");
};
runs(function () {
triggerTimingFunctionEditorKey({
func: "cubic-bezier(.42, 0, .58 ,1)",
item: "P1",
key: KeyEvent.DOM_VK_UP,
shift: true,
callback: _callback
});
expectArraysToBeEqual(timingFuncEditor._cubicBezierCoords, [".42", ".1", ".58", "1"]);
});
runs(function () {
expect(calledBack).toBeTruthy();
});
});
// steps() tests
it("should increase count by 1 on up arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(5)",
item: "canvas",
key: KeyEvent.DOM_VK_UP
});
expect(timingFuncEditor._stepParams.count).toEqual(6);
});
it("should decrease count by 1 on down arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(5)",
item: "canvas",
key: KeyEvent.DOM_VK_DOWN
});
expect(timingFuncEditor._stepParams.count).toEqual(4);
});
it("should change start to end on right arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(5, start)",
item: "canvas",
key: KeyEvent.DOM_VK_RIGHT
});
expect(timingFuncEditor._stepParams.timing).toEqual("end");
});
it("should change end to start on left arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(5, end)",
item: "canvas",
key: KeyEvent.DOM_VK_LEFT
});
expect(timingFuncEditor._stepParams.timing).toEqual("start");
});
it("should not decrease count to be less than 1 on down arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(1)",
item: "canvas",
key: KeyEvent.DOM_VK_DOWN
});
expect(timingFuncEditor._stepParams.count).toEqual(1);
});
it("should not change start to end on left arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(5, start)",
item: "canvas",
key: KeyEvent.DOM_VK_LEFT
});
expect(timingFuncEditor._stepParams.timing).toEqual("start");
});
it("should not change end to start on right arrow in steps()", function () {
triggerTimingFunctionEditorKey({
func: "steps(5, end)",
item: "canvas",
key: KeyEvent.DOM_VK_RIGHT
});
expect(timingFuncEditor._stepParams.timing).toEqual("end");
});
it("should call callback function after steps function edit", function () {
var calledBack = false;
var _callback = function (timingFunctionString) {
calledBack = true;
expect(timingFunctionString).toBe("steps(5, start)");
};
runs(function () {
triggerTimingFunctionEditorKey({
func: "steps(4, start)",
item: "canvas",
key: KeyEvent.DOM_VK_UP,
callback: _callback
});
expect(timingFuncEditor._stepParams.count).toEqual(5);
});
runs(function () {
expect(calledBack).toBeTruthy();
});
});
});
});
});
});