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