Kevin Lubick | d2efe52 | 2019-01-04 15:59:06 -0500 | [diff] [blame] | 1 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 2 | |
| 3 | describe('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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 6 | const LoadPathKit = new Promise(function(resolve, reject) { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 7 | if (PathKit) { |
| 8 | resolve(); |
| 9 | } else { |
| 10 | PathKitInit({ |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 11 | locateFile: (file) => '/pathkit/'+file, |
Kevin Lubick | 275eaff | 2019-01-04 14:38:29 -0500 | [diff] [blame] | 12 | }).ready().then((_PathKit) => { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 13 | PathKit = _PathKit; |
| 14 | resolve(); |
| 15 | }); |
| 16 | } |
| 17 | }); |
| 18 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 19 | describe('Basic Path Features', function() { |
| 20 | function drawSimplePath() { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 21 | let path = PathKit.NewPath(); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 22 | 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 30 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 31 | 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 48 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 49 | }); |
| 50 | |
| 51 | it('has a copy constructor', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 52 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 53 | 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 62 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 63 | }); |
| 64 | |
| 65 | it('has a copy method', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 66 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 67 | 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 76 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 77 | }); |
| 78 | |
| 79 | it('can create a copy with MakePath', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 80 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 81 | 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 90 | })); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 91 | }); |
| 92 | }); |
| 93 | |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 94 | 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 Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 106 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 107 | function bits2float(str) { |
| 108 | return PathKit.SkBits2FloatUnsigned(parseInt(str)) |
| 109 | } |
| 110 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 111 | describe('bounds and rect', function(){ |
| 112 | it('dynamically updates getBounds()', function(done){ |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 113 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 114 | // Based on test_bounds_crbug_513799 |
| 115 | let path = PathKit.NewPath(); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 116 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(0, 0, 0, 0)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 117 | path.moveTo(-5, -8); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 118 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, -5, -8)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 119 | path.rect(1, 2, 2, 2); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 120 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 121 | path.moveTo(1, 2); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 122 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 123 | path.delete(); |
| 124 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 125 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 126 | }); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 127 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 128 | it('has getBounds() and computeTightBounds()', function(done){ |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 129 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 130 | // Based on PathOpsTightBoundsIllBehaved |
| 131 | let path = PathKit.NewPath(); |
| 132 | path.moveTo(1, 1); |
| 133 | path.quadraticCurveTo(4, 3, 2, 2); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 134 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(1, 1, 4, 3)); |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 135 | ExpectRectsToBeEqual(path.computeTightBounds(), |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 136 | PathKit.LTRBRect(1, 1, |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 137 | bits2float("0x40333334"), // 2.8 |
| 138 | bits2float("0x40155556"))); // 2.3333333 |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 139 | path.delete(); |
| 140 | |
| 141 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 142 | })); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 143 | }); |
| 144 | }); |
| 145 | |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 146 | 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 Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 161 | describe('Command arrays', function(){ |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame^] | 162 | it('does NOT approximates conics when dumping as toCmds', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 163 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 164 | 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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 168 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 169 | 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 Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 178 | ExpectCmdsToBeEqual(path.toCmds(), expectedCmds); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 179 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 180 | path.delete(); |
| 181 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 182 | })); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 183 | }); |
| 184 | }); |
| 185 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 186 | }); |