blob: 29cdbd5e5d46ce2c7dec82427034075fc8a71139 [file] [log] [blame]
Kevin Lubickd2efe522019-01-04 15:59:06 -05001jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
Kevin Lubick11194ab2018-08-17 13:52:56 -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;
6 const LoadPathKit = new Promise(function(resolve, reject) {
7 if (PathKit) {
8 resolve();
9 } else {
10 PathKitInit({
11 locateFile: (file) => '/pathkit/'+file,
Kevin Lubick275eaff2019-01-04 14:38:29 -050012 }).ready().then((_PathKit) => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040013 PathKit = _PathKit;
14 resolve();
15 });
16 }
17 });
18
19 // see https://fiddle.skia.org/c/@discrete_path
20 function drawStar() {
21 let path = PathKit.NewPath();
22 let R = 115.2, C = 128.0;
23 path.moveTo(C + R + 22, C);
24 for (let i = 1; i < 8; i++) {
25 let a = 2.6927937 * i;
26 path.lineTo(C + R * Math.cos(a) + 22, C + R * Math.sin(a));
27 }
28 path.closePath();
29 return path;
30 }
31
32 describe('Dash Path Effect', function() {
33 it('performs dash in-place with start, stop, phase', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050034 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040035 let orig = drawStar();
36 let dashed = drawStar();
37 let notACopy = dashed.dash(10, 3, 0);
38 let phased = drawStar().dash(10, 3, 2);
39
40 expect(dashed === notACopy).toBe(true);
41 expect(dashed.equals(phased)).toBe(false);
42 expect(dashed.equals(orig)).toBe(false);
Kevin Lubick11194ab2018-08-17 13:52:56 -040043
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040044 reportPath(dashed, 'dashed_no_phase', () => {
45 reportPath(phased, 'dashed_with_phase', done);
46 orig.delete();
47 dashed.delete();
48 phased.delete();
49 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050050 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040051 });
52 });
53
54 describe('Trim Path Effect', function() {
55 it('performs trim in-place with start, stop, phase', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050056 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040057 let orig = drawStar();
58 let trimmed = drawStar();
59 let notACopy = trimmed.trim(0.25, .8);
60 let complement = drawStar().trim(.1, .9, true);
61
62 expect(trimmed === notACopy).toBe(true);
63 expect(trimmed.equals(complement)).toBe(false);
64 expect(trimmed.equals(orig)).toBe(false);
65 expect(complement.equals(orig)).toBe(false);
66
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040067 reportPath(trimmed, 'trimmed_non_complement', () => {
68 reportPath(complement, 'trimmed_complement', done);
69 orig.delete();
70 trimmed.delete();
71 complement.delete();
72 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050073 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040074 });
75 });
76
77 describe('Transform Path Effect', function() {
78 it('performs matrix transform in-place', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050079 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040080 let orig = drawStar();
81 let scaled = drawStar();
82 let notACopy = scaled.transform(3, 0, 0,
83 0, 3, 0,
84 0, 0, 1);
85
86 let scaled2 = drawStar().transform([3, 0, 0,
87 0, 3, 0,
88 0, 0, 1]);
89
90 expect(scaled === notACopy).toBe(true);
91 expect(scaled.equals(scaled2)).toBe(true);
92 expect(scaled.equals(orig)).toBe(false);
93
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040094 reportPath(scaled, 'transformed_scale', () => {
95 reportPath(scaled2, 'transformed_scale2', done);
96 orig.delete();
97 scaled.delete();
98 scaled2.delete();
99 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500100 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400101 });
102 });
103
104 describe('Stroke Path Effect', function() {
105 it('creates a stroked path in-place', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500106 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400107 let orig = drawStar();
108 let stroked = drawStar();
109 let notACopy = stroked.stroke({
110 width: 15,
111 join: PathKit.StrokeJoin.BEVEL,
112 cap: PathKit.StrokeCap.BUTT,
113 miter_limit: 2,
114 });
115
116 // Don't have to specify all of the fields, defaults will
117 // be used instead.
118 let rounded = drawStar().stroke({
119 width: 10,
120 join: PathKit.StrokeJoin.ROUND,
121 cap:PathKit.StrokeCap.SQUARE,
122 });
123
124 expect(stroked === notACopy).toBe(true);
125 expect(stroked.equals(rounded)).toBe(false);
126 expect(stroked.equals(orig)).toBe(false);
127
Kevin Lubickc6c48aa2018-08-17 15:00:43 -0400128 reportPath(stroked, 'stroke_bevel_butt', () => {
129 reportPath(rounded, 'stroke_round_square', done);
130 orig.delete();
131 stroked.delete();
132 rounded.delete();
133 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500134 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400135 });
136 });
137
138});