blob: 3c55dfac83271997f93028615b0c3e23743bef6d [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 Lubick11194ab2018-08-17 13:52:56 -040093
Kevin Lubick97d6d982018-08-10 15:53:16 -040094 function bits2float(str) {
95 return PathKit.SkBits2FloatUnsigned(parseInt(str))
96 }
97
Kevin Lubick11194ab2018-08-17 13:52:56 -040098 describe('bounds and rect', function(){
99 it('dynamically updates getBounds()', function(done){
100 LoadPathKit.then(() => {
101 // Based on test_bounds_crbug_513799
102 let path = PathKit.NewPath();
103 expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(0, 0, 0, 0));
104 path.moveTo(-5, -8);
105 expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(-5, -8, -5, -8));
106 path.rect(1, 2, 2, 2);
107 expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(-5, -8, 3, 4));
108 path.moveTo(1, 2);
109 expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(-5, -8, 3, 4));
110 path.delete();
111 done();
112 });
113 });
Kevin Lubick644d8e72018-08-09 13:58:04 -0400114
Kevin Lubick11194ab2018-08-17 13:52:56 -0400115 it('has getBounds() and computeTightBounds()', function(done){
116 LoadPathKit.then(() => {
117 // Based on PathOpsTightBoundsIllBehaved
118 let path = PathKit.NewPath();
119 path.moveTo(1, 1);
120 path.quadraticCurveTo(4, 3, 2, 2);
121 expect(path.getBounds()).toEqual(PathKit.MakeLTRBRect(1, 1, 4, 3));
122 expect(path.computeTightBounds()).toEqual(PathKit.MakeLTRBRect(1, 1,
123 bits2float("0x40333334"), // 2.8
124 bits2float("0x40155556"))); // 2.3333333
125 path.delete();
126
127 done();
128 });
Kevin Lubick644d8e72018-08-09 13:58:04 -0400129 });
130 });
131
Kevin Lubick11194ab2018-08-17 13:52:56 -0400132 describe('Command arrays', function(){
133 it('does NOT approximates conics when dumping as toCmds', function(done){
134 LoadPathKit.then(() => {
135 let path = PathKit.NewPath();
136 path.moveTo(20, 120);
137 path.arc(20, 120, 18, 0, 1.75 * Math.PI);
138 path.lineTo(20, 120);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400139
Kevin Lubick11194ab2018-08-17 13:52:56 -0400140 let expectedCmds = [
141 [PathKit.MOVE_VERB, 20, 120],
142 [PathKit.LINE_VERB, 38, 120],
143 [PathKit.CONIC_VERB, 38, 138, 20, 138, bits2float("0x3f3504f3)")], // 0.707107f
144 [PathKit.CONIC_VERB, 2, 138, 2, 120, bits2float("0x3f3504f3)")], // 0.707107f
145 [PathKit.CONIC_VERB, 2, 102, 20, 102, bits2float("0x3f3504f3)")], // 0.707107f
146 [PathKit.CONIC_VERB, bits2float("0x41dba58e"), 102, bits2float("0x4202e962"), bits2float("0x42d68b4d"), bits2float("0x3f6c8361")], // 27.4558, 102, 32.7279, 107.272, 0.92388
147 [PathKit.LINE_VERB, 20, 120],
148 ];
149 let actual = path.toCmds();
150 expect(actual).toEqual(expectedCmds);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400151
Kevin Lubick11194ab2018-08-17 13:52:56 -0400152 path.delete();
153 done();
154 });
Kevin Lubick97d6d982018-08-10 15:53:16 -0400155 });
156 });
157
Kevin Lubick644d8e72018-08-09 13:58:04 -0400158});