blob: 80a779adb4967393114669b82a8194a507496f29 [file] [log] [blame]
Kevin Lubickd2efe522019-01-04 15:59:06 -05001jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
Kevin Lubick644d8e72018-08-09 13:58:04 -04002
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;
Kevin Lubick97d6d982018-08-10 15:53:16 -04006 const LoadPathKit = new Promise(function(resolve, reject) {
Kevin Lubick644d8e72018-08-09 13:58:04 -04007 if (PathKit) {
8 resolve();
9 } else {
10 PathKitInit({
Kevin Lubick97d6d982018-08-10 15:53:16 -040011 locateFile: (file) => '/pathkit/'+file,
Kevin Lubick275eaff2019-01-04 14:38:29 -050012 }).ready().then((_PathKit) => {
Kevin Lubick644d8e72018-08-09 13:58:04 -040013 PathKit = _PathKit;
14 resolve();
15 });
16 }
17 });
18
Kevin Lubick11194ab2018-08-17 13:52:56 -040019 describe('Basic Path Features', function() {
20 function drawSimplePath() {
Kevin Lubick644d8e72018-08-09 13:58:04 -040021 let path = PathKit.NewPath();
Kevin Lubick11194ab2018-08-17 13:52:56 -040022 path.moveTo(0, 0);
23 path.lineTo(10, 0);
24 path.lineTo(10, 10);
25 path.close();
26 return path;
27 }
28
29 it('supports.equals()', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050030 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040031 let path = drawSimplePath();
32 let otherPath = drawSimplePath();
33 let blank = PathKit.NewPath();
34
35 expect(path.equals(path)).toBe(true);
36 expect(otherPath.equals(path)).toBe(true);
37 expect(path.equals(otherPath)).toBe(true);
38
39 expect(path.equals(blank)).toBe(false);
40 expect(otherPath.equals(blank)).toBe(false);
41 expect(blank.equals(path)).toBe(false);
42 expect(blank.equals(otherPath)).toBe(false);
43
44 path.delete();
45 otherPath.delete();
46 blank.delete();
47 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050048 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040049 });
50
51 it('has a copy constructor', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050052 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040053 let orig = drawSimplePath();
54 let copy = new PathKit.SkPath(orig);
55
56 expect(orig.toSVGString()).toEqual(copy.toSVGString());
57 expect(orig.equals(copy)).toBe(true);
58
59 orig.delete();
60 copy.delete();
61 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050062 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040063 });
64
65 it('has a copy method', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050066 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040067 let orig = drawSimplePath();
68 let copy = orig.copy();
69
70 expect(orig.toSVGString()).toEqual(copy.toSVGString());
71 expect(orig.equals(copy)).toBe(true);
72
73 orig.delete();
74 copy.delete();
75 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050076 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040077 });
78
79 it('can create a copy with MakePath', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050080 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040081 let orig = drawSimplePath();
82 let copy = PathKit.NewPath(orig);
83
84 expect(orig.toSVGString()).toEqual(copy.toSVGString());
85 expect(orig.equals(copy)).toBe(true);
86
87 orig.delete();
88 copy.delete();
89 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050090 }));
Kevin Lubick644d8e72018-08-09 13:58:04 -040091 });
92 });
93
Kevin Lubickf14a3c02018-08-22 09:35:32 -040094 function ExpectRectsToBeEqual(actual, expected) {
95 if (PathKit.usingWasm) {
96 // exact match
97 expect(actual).toEqual(expected);
98 } else {
99 // floats get rounded a little bit
100 expect(actual.fLeft).toBeCloseTo(expected.fLeft, 4);
101 expect(actual.fTop).toBeCloseTo(expected.fTop, 4);
102 expect(actual.fRight).toBeCloseTo(expected.fRight, 4);
103 expect(actual.fBottom).toBeCloseTo(expected.fBottom, 4);
104 }
105 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400106
Kevin Lubick97d6d982018-08-10 15:53:16 -0400107 function bits2float(str) {
108 return PathKit.SkBits2FloatUnsigned(parseInt(str))
109 }
110
Kevin Lubick11194ab2018-08-17 13:52:56 -0400111 describe('bounds and rect', function(){
112 it('dynamically updates getBounds()', function(done){
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500113 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400114 // Based on test_bounds_crbug_513799
115 let path = PathKit.NewPath();
Kevin Lubickd9936482018-08-24 10:44:16 -0400116 expect(path.getBounds()).toEqual(PathKit.LTRBRect(0, 0, 0, 0));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400117 path.moveTo(-5, -8);
Kevin Lubickd9936482018-08-24 10:44:16 -0400118 expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, -5, -8));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400119 path.rect(1, 2, 2, 2);
Kevin Lubickd9936482018-08-24 10:44:16 -0400120 expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400121 path.moveTo(1, 2);
Kevin Lubickd9936482018-08-24 10:44:16 -0400122 expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400123 path.delete();
124 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500125 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400126 });
Kevin Lubick644d8e72018-08-09 13:58:04 -0400127
Kevin Lubick11194ab2018-08-17 13:52:56 -0400128 it('has getBounds() and computeTightBounds()', function(done){
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500129 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400130 // Based on PathOpsTightBoundsIllBehaved
131 let path = PathKit.NewPath();
132 path.moveTo(1, 1);
133 path.quadraticCurveTo(4, 3, 2, 2);
Kevin Lubickd9936482018-08-24 10:44:16 -0400134 expect(path.getBounds()).toEqual(PathKit.LTRBRect(1, 1, 4, 3));
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400135 ExpectRectsToBeEqual(path.computeTightBounds(),
Kevin Lubickd9936482018-08-24 10:44:16 -0400136 PathKit.LTRBRect(1, 1,
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400137 bits2float("0x40333334"), // 2.8
138 bits2float("0x40155556"))); // 2.3333333
Kevin Lubick11194ab2018-08-17 13:52:56 -0400139 path.delete();
140
141 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500142 }));
Kevin Lubick644d8e72018-08-09 13:58:04 -0400143 });
144 });
145
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400146 function ExpectCmdsToBeEqual(actual, expected) {
147 if (PathKit.usingWasm) {
148 // exact match
149 expect(actual).toEqual(expected);
150 } else {
151 // lossy match
152 actual.every((cmd, cmdIdx) => {
153 cmd.every((arg, argIdx) => {
154 // The asm.js code is close to the wasm/c++ output, but not quite.
155 expect(arg).toBeCloseTo(expected[cmdIdx][argIdx], 4)
156 });
157 });
158 }
159 }
160
Kevin Lubick11194ab2018-08-17 13:52:56 -0400161 describe('Command arrays', function(){
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500162 it('does NOT approximates conics when dumping as toCmds', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500163 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400164 let path = PathKit.NewPath();
165 path.moveTo(20, 120);
166 path.arc(20, 120, 18, 0, 1.75 * Math.PI);
167 path.lineTo(20, 120);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400168
Kevin Lubick11194ab2018-08-17 13:52:56 -0400169 let expectedCmds = [
170 [PathKit.MOVE_VERB, 20, 120],
171 [PathKit.LINE_VERB, 38, 120],
172 [PathKit.CONIC_VERB, 38, 138, 20, 138, bits2float("0x3f3504f3)")], // 0.707107f
173 [PathKit.CONIC_VERB, 2, 138, 2, 120, bits2float("0x3f3504f3)")], // 0.707107f
174 [PathKit.CONIC_VERB, 2, 102, 20, 102, bits2float("0x3f3504f3)")], // 0.707107f
175 [PathKit.CONIC_VERB, bits2float("0x41dba58e"), 102, bits2float("0x4202e962"), bits2float("0x42d68b4d"), bits2float("0x3f6c8361")], // 27.4558, 102, 32.7279, 107.272, 0.92388
176 [PathKit.LINE_VERB, 20, 120],
177 ];
Kevin Lubickf14a3c02018-08-22 09:35:32 -0400178 ExpectCmdsToBeEqual(path.toCmds(), expectedCmds);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400179
Kevin Lubick11194ab2018-08-17 13:52:56 -0400180 path.delete();
181 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500182 }));
Kevin Lubick97d6d982018-08-10 15:53:16 -0400183 });
184 });
185
Kevin Lubick644d8e72018-08-09 13:58:04 -0400186});