1jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
2
3describe('PathKit\'s Path Behavior', function() {
4    // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
5    var PathKit = null;
6    const LoadPathKit = new Promise(function(resolve, reject) {
7        if (PathKit) {
8            resolve();
9        } else {
10            PathKitInit({
11                locateFile: (file) => '/pathkit/'+file,
12            }).ready().then((_PathKit) => {
13                PathKit = _PathKit;
14                resolve();
15            });
16        }
17    });
18
19    // see https://fiddle.skia.org/c/@discrete_path
20    function drawStar() {
21        let path = PathKit.NewPath();
22        let R = 115.2, C = 128.0;
23        path.moveTo(C + R + 22, C);
24        for (let i = 1; i < 8; i++) {
25            let a = 2.6927937 * i;
26            path.lineTo(C + R * Math.cos(a) + 22, C + R * Math.sin(a));
27        }
28        path.closePath();
29        return path;
30    }
31
32    describe('Dash Path Effect', function() {
33        it('performs dash in-place with start, stop, phase', function(done) {
34            LoadPathKit.then(catchException(done, () => {
35                let orig = drawStar();
36                let dashed = drawStar();
37                let notACopy = dashed.dash(10, 3, 0);
38                let phased = drawStar().dash(10, 3, 2);
39
40                expect(dashed === notACopy).toBe(true);
41                expect(dashed.equals(phased)).toBe(false);
42                expect(dashed.equals(orig)).toBe(false);
43
44                reportPath(dashed, 'dashed_no_phase', () => {
45                    reportPath(phased, 'dashed_with_phase', done);
46                    orig.delete();
47                    dashed.delete();
48                    phased.delete();
49                });
50            }));
51        });
52    });
53
54    describe('Trim Path Effect', function() {
55        it('performs trim in-place with start, stop, phase', function(done) {
56            LoadPathKit.then(catchException(done, () => {
57                let orig = drawStar();
58                let trimmed = drawStar();
59                let notACopy = trimmed.trim(0.25, .8);
60                let complement = drawStar().trim(.1, .9, true);
61
62                expect(trimmed === notACopy).toBe(true);
63                expect(trimmed.equals(complement)).toBe(false);
64                expect(trimmed.equals(orig)).toBe(false);
65                expect(complement.equals(orig)).toBe(false);
66
67                reportPath(trimmed, 'trimmed_non_complement', () => {
68                    reportPath(complement, 'trimmed_complement', done);
69                    orig.delete();
70                    trimmed.delete();
71                    complement.delete();
72                });
73            }));
74        });
75    });
76
77    describe('Transform Path Effect', function() {
78        it('performs matrix transform in-place', function(done) {
79            LoadPathKit.then(catchException(done, () => {
80                let orig = drawStar();
81                let scaled = drawStar();
82                let notACopy = scaled.transform(3, 0, 0,
83                                                0, 3, 0,
84                                                0, 0, 1);
85
86                let scaled2 = drawStar().transform([3, 0, 0,
87                                                    0, 3, 0,
88                                                    0, 0, 1]);
89
90                expect(scaled === notACopy).toBe(true);
91                expect(scaled.equals(scaled2)).toBe(true);
92                expect(scaled.equals(orig)).toBe(false);
93
94                reportPath(scaled, 'transformed_scale', () => {
95                    reportPath(scaled2, 'transformed_scale2', done);
96                    orig.delete();
97                    scaled.delete();
98                    scaled2.delete();
99                });
100            }));
101        });
102    });
103
104    describe('Stroke Path Effect', function() {
105        it('creates a stroked path in-place', function(done) {
106            LoadPathKit.then(catchException(done, () => {
107                let orig = drawStar();
108                let stroked = drawStar();
109                let notACopy = stroked.stroke({
110                    width: 15,
111                    join: PathKit.StrokeJoin.BEVEL,
112                    cap: PathKit.StrokeCap.BUTT,
113                    miter_limit: 2,
114                });
115
116                // Don't have to specify all of the fields, defaults will
117                // be used instead.
118                let rounded = drawStar().stroke({
119                    width: 10,
120                    join: PathKit.StrokeJoin.ROUND,
121                    cap:PathKit.StrokeCap.SQUARE,
122                });
123
124                expect(stroked === notACopy).toBe(true);
125                expect(stroked.equals(rounded)).toBe(false);
126                expect(stroked.equals(orig)).toBe(false);
127
128                reportPath(stroked, 'stroke_bevel_butt', () => {
129                    reportPath(rounded, 'stroke_round_square', done);
130                    orig.delete();
131                    stroked.delete();
132                    rounded.delete();
133                });
134            }));
135        });
136    });
137
138});
139