Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 1 | |
| 2 | describe('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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 5 | const LoadPathKit = new Promise(function(resolve, reject) { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 6 | if (PathKit) { |
| 7 | resolve(); |
| 8 | } else { |
| 9 | PathKitInit({ |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 10 | locateFile: (file) => '/pathkit/'+file, |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 11 | }).then((_PathKit) => { |
| 12 | PathKit = _PathKit; |
| 13 | resolve(); |
| 14 | }); |
| 15 | } |
| 16 | }); |
| 17 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 18 | describe('Basic Path Features', function() { |
| 19 | function drawSimplePath() { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 20 | let path = PathKit.NewPath(); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 21 | 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 Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 90 | }); |
| 91 | }); |
| 92 | |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame^] | 93 | 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 Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 105 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 106 | function bits2float(str) { |
| 107 | return PathKit.SkBits2FloatUnsigned(parseInt(str)) |
| 108 | } |
| 109 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 110 | 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(); |
| 115 | expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(0, 0, 0, 0)); |
| 116 | path.moveTo(-5, -8); |
| 117 | expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(-5, -8, -5, -8)); |
| 118 | path.rect(1, 2, 2, 2); |
| 119 | expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(-5, -8, 3, 4)); |
| 120 | path.moveTo(1, 2); |
| 121 | expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(-5, -8, 3, 4)); |
| 122 | path.delete(); |
| 123 | done(); |
| 124 | }); |
| 125 | }); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 126 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 127 | 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); |
| 133 | expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(1, 1, 4, 3)); |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame^] | 134 | ExpectRectsToBeEqual(path.computeTightBounds(), |
| 135 | PathKit.MakeLTRBRect(1, 1, |
| 136 | bits2float("0x40333334"), // 2.8 |
| 137 | bits2float("0x40155556"))); // 2.3333333 |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 138 | path.delete(); |
| 139 | |
| 140 | done(); |
| 141 | }); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 142 | }); |
| 143 | }); |
| 144 | |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame^] | 145 | 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 Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 160 | 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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 167 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 168 | 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 Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame^] | 177 | ExpectCmdsToBeEqual(path.toCmds(), expectedCmds); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 178 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 179 | path.delete(); |
| 180 | done(); |
| 181 | }); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 182 | }); |
| 183 | }); |
| 184 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 185 | }); |