blob: 852435b0838e413cfd38da2de0fe31dc9d7faf0d [file] [log] [blame]
Kevin Lubick644d8e72018-08-09 13:58:04 -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;
Kevin Lubick97d6d982018-08-10 15:53:16 -04005 const LoadPathKit = new Promise(function(resolve, reject) {
Kevin Lubick644d8e72018-08-09 13:58:04 -04006 if (PathKit) {
7 resolve();
8 } else {
9 PathKitInit({
Kevin Lubick97d6d982018-08-10 15:53:16 -040010 locateFile: (file) => '/pathkit/'+file,
Kevin Lubick644d8e72018-08-09 13:58:04 -040011 }).then((_PathKit) => {
12 PathKit = _PathKit;
13 resolve();
14 });
15 }
16 });
17
Kevin Lubick11194ab2018-08-17 13:52:56 -040018 describe('Basic Path Features', function() {
19 function drawSimplePath() {
Kevin Lubick644d8e72018-08-09 13:58:04 -040020 let path = PathKit.NewPath();
Kevin Lubick11194ab2018-08-17 13:52:56 -040021 path.moveTo(0, 0);
22 path.lineTo(10, 0);
23 path.lineTo(10, 10);
24 path.close();
25 return path;
26 }
27
28 it('supports.equals()', function(done) {
29 LoadPathKit.then(() => {
30 let path = drawSimplePath();
31 let otherPath = drawSimplePath();
32 let blank = PathKit.NewPath();
33
34 expect(path.equals(path)).toBe(true);
35 expect(otherPath.equals(path)).toBe(true);
36 expect(path.equals(otherPath)).toBe(true);
37
38 expect(path.equals(blank)).toBe(false);
39 expect(otherPath.equals(blank)).toBe(false);
40 expect(blank.equals(path)).toBe(false);
41 expect(blank.equals(otherPath)).toBe(false);
42
43 path.delete();
44 otherPath.delete();
45 blank.delete();
46 done();
47 });
48 });
49
50 it('has a copy constructor', function(done) {
51 LoadPathKit.then(() => {
52 let orig = drawSimplePath();
53 let copy = new PathKit.SkPath(orig);
54
55 expect(orig.toSVGString()).toEqual(copy.toSVGString());
56 expect(orig.equals(copy)).toBe(true);
57
58 orig.delete();
59 copy.delete();
60 done();
61 });
62 });
63
64 it('has a copy method', function(done) {
65 LoadPathKit.then(() => {
66 let orig = drawSimplePath();
67 let copy = orig.copy();
68
69 expect(orig.toSVGString()).toEqual(copy.toSVGString());
70 expect(orig.equals(copy)).toBe(true);
71
72 orig.delete();
73 copy.delete();
74 done();
75 });
76 });
77
78 it('can create a copy with MakePath', function(done) {
79 LoadPathKit.then(() => {
80 let orig = drawSimplePath();
81 let copy = PathKit.NewPath(orig);
82
83 expect(orig.toSVGString()).toEqual(copy.toSVGString());
84 expect(orig.equals(copy)).toBe(true);
85
86 orig.delete();
87 copy.delete();
88 done();
89 });
Kevin Lubick644d8e72018-08-09 13:58:04 -040090 });
91 });
92
Kevin Lubickf14a3c02018-08-22 09:35:32 -040093 function ExpectRectsToBeEqual(actual, expected) {
94 if (PathKit.usingWasm) {
95 // exact match
96 expect(actual).toEqual(expected);
97 } else {
98 // floats get rounded a little bit
99 expect(actual.fLeft).toBeCloseTo(expected.fLeft, 4);
100 expect(actual.fTop).toBeCloseTo(expected.fTop, 4);
101 expect(actual.fRight).toBeCloseTo(expected.fRight, 4);
102 expect(actual.fBottom).toBeCloseTo(expected.fBottom, 4);
103 }
104 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400105
Kevin Lubick97d6d982018-08-10 15:53:16 -0400106 function bits2float(str) {
107 return PathKit.SkBits2FloatUnsigned(parseInt(str))
108 }
109
Kevin Lubick11194ab2018-08-17 13:52:56 -0400110 describe('bounds and rect', function(){
111 it('dynamically updates getBounds()', function(done){
112 LoadPathKit.then(() => {
113 // Based on test_bounds_crbug_513799
114 let path = PathKit.NewPath();
Kevin Lubickd9936482018-08-24 10:44:16 -0400115 expect(path.getBounds()).toEqual(PathKit.LTRBRect(0, 0, 0, 0));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400116 path.moveTo(-5, -8);
Kevin Lubickd9936482018-08-24 10:44:16 -0400117 expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, -5, -8));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400118 path.rect(1, 2, 2, 2);
Kevin Lubickd9936482018-08-24 10:44:16 -0400119 expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400120 path.moveTo(1, 2);
Kevin Lubickd9936482018-08-24 10:44:16 -0400121 expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400122 path.delete();
123 done();
124 });
125 });
Kevin Lubick644d8e72018-08-09 13:58:04 -0400126
Kevin Lubick11194ab2018-08-17 13:52:56 -0400127 it('has getBounds() and computeTightBounds()', function(done){
128 LoadPathKit.then(() => {
129 // Based on PathOpsTightBoundsIllBehaved
130 let path = PathKit.NewPath();
131 path.moveTo(1, 1);
132 path.quadraticCurveTo(4, 3, 2, 2);
Kevin Lubickd9936482018-08-24 10:44:16 -0400133 expect(path.getBounds()).toEqual(PathKit.LTRBRect(1, 1, 4, 3));
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400134 ExpectRectsToBeEqual(path.computeTightBounds(),
Kevin Lubickd9936482018-08-24 10:44:16 -0400135 PathKit.LTRBRect(1, 1,
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400136 bits2float("0x40333334"), // 2.8
137 bits2float("0x40155556"))); // 2.3333333
Kevin Lubick11194ab2018-08-17 13:52:56 -0400138 path.delete();
139
140 done();
141 });
Kevin Lubick644d8e72018-08-09 13:58:04 -0400142 });
143 });
144
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400145 function ExpectCmdsToBeEqual(actual, expected) {
146 if (PathKit.usingWasm) {
147 // exact match
148 expect(actual).toEqual(expected);
149 } else {
150 // lossy match
151 actual.every((cmd, cmdIdx) => {
152 cmd.every((arg, argIdx) => {
153 // The asm.js code is close to the wasm/c++ output, but not quite.
154 expect(arg).toBeCloseTo(expected[cmdIdx][argIdx], 4)
155 });
156 });
157 }
158 }
159
Kevin Lubick11194ab2018-08-17 13:52:56 -0400160 describe('Command arrays', function(){
161 it('does NOT approximates conics when dumping as toCmds', function(done){
162 LoadPathKit.then(() => {
163 let path = PathKit.NewPath();
164 path.moveTo(20, 120);
165 path.arc(20, 120, 18, 0, 1.75 * Math.PI);
166 path.lineTo(20, 120);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400167
Kevin Lubick11194ab2018-08-17 13:52:56 -0400168 let expectedCmds = [
169 [PathKit.MOVE_VERB, 20, 120],
170 [PathKit.LINE_VERB, 38, 120],
171 [PathKit.CONIC_VERB, 38, 138, 20, 138, bits2float("0x3f3504f3)")], // 0.707107f
172 [PathKit.CONIC_VERB, 2, 138, 2, 120, bits2float("0x3f3504f3)")], // 0.707107f
173 [PathKit.CONIC_VERB, 2, 102, 20, 102, bits2float("0x3f3504f3)")], // 0.707107f
174 [PathKit.CONIC_VERB, bits2float("0x41dba58e"), 102, bits2float("0x4202e962"), bits2float("0x42d68b4d"), bits2float("0x3f6c8361")], // 27.4558, 102, 32.7279, 107.272, 0.92388
175 [PathKit.LINE_VERB, 20, 120],
176 ];
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400177 ExpectCmdsToBeEqual(path.toCmds(), expectedCmds);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400178
Kevin Lubick11194ab2018-08-17 13:52:56 -0400179 path.delete();
180 done();
181 });
Kevin Lubick97d6d982018-08-10 15:53:16 -0400182 });
183 });
184
Kevin Lubick644d8e72018-08-09 13:58:04 -0400185});