-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathstructure.js
293 lines (262 loc) · 7.46 KB
/
structure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
suite('Structure', function() {
var myp5;
setup(function(done) {
new p5(function(p) {
p.setup = function() {
myp5 = p;
done();
};
});
});
teardown(function() {
myp5.remove();
});
suite('p5.prototype.loop and p5.prototype.noLoop', function() {
test('noLoop should stop', function() {
return new Promise(function(resolve, reject) {
var c0 = myp5.frameCount;
myp5.noLoop();
myp5.draw = function() {
var c1 = myp5.frameCount;
// Allow one final draw to run
if (c1 > c0 + 1) {
reject('Entered draw');
}
};
setTimeout(resolve, 100);
});
});
test('loop should restart', function() {
return new Promise(function(resolve, reject) {
var c0 = myp5.frameCount;
myp5.noLoop();
myp5.draw = function() {
var c1 = myp5.frameCount;
// Allow one final draw to run
if (c1 > c0 + 1) {
reject('Entered draw');
}
};
setTimeout(resolve, 100);
}).then(function() {
return new Promise(function(resolve, reject) {
myp5.draw = resolve;
myp5.loop();
setTimeout(function() {
reject('Failed to restart draw.');
}, 100);
});
});
});
});
suite('p5.prototype.push and p5.prototype.pop', function() {
function getRenderState() {
var state = {};
for (var key in myp5._renderer) {
var value = myp5._renderer[key];
if (
typeof value !== 'function' &&
key !== '_cachedFillStyle' &&
key !== '_cachedStrokeStyle'
) {
state[key] = value;
}
}
return state;
}
function assertCanPreserveRenderState(work) {
var originalState = getRenderState();
myp5.push();
work();
myp5.pop();
assert.deepEqual(getRenderState(), originalState);
}
test('leak no state after fill()', function() {
myp5.noFill();
assertCanPreserveRenderState(function() {
myp5.fill('red');
});
});
test('leak no state after noFill()', function() {
myp5.fill('red');
assertCanPreserveRenderState(function() {
myp5.noFill();
});
});
test('leak no state after stroke()', function() {
myp5.noStroke();
assertCanPreserveRenderState(function() {
myp5.stroke('red');
});
});
test('leak no state after noStroke()', function() {
myp5.stroke('red');
assertCanPreserveRenderState(function() {
myp5.noStroke();
});
});
test('leak no state after tint()', function() {
myp5.noTint();
assertCanPreserveRenderState(function() {
myp5.tint(255, 0, 0);
});
});
test('leak no state after noTint()', function() {
myp5.tint(255, 0, 0);
assertCanPreserveRenderState(function() {
myp5.noTint();
});
});
test('leak no state after strokeWeight()', function() {
myp5.strokeWeight(1);
assertCanPreserveRenderState(function() {
myp5.strokeWeight(10);
});
});
test('leak no state after strokeCap()', function() {
myp5.strokeCap(myp5.ROUND);
assertCanPreserveRenderState(function() {
myp5.strokeCap(myp5.SQUARE);
});
});
test('leak no state after strokeJoin()', function() {
myp5.strokeJoin(myp5.BEVEL);
assertCanPreserveRenderState(function() {
myp5.strokeJoin(myp5.MITER);
});
});
test('leak no state after imageMode()', function() {
myp5.imageMode(myp5.CORNER);
assertCanPreserveRenderState(function() {
myp5.imageMode(myp5.CENTER);
});
});
test('leak no state after rectMode()', function() {
myp5.rectMode(myp5.CORNER);
assertCanPreserveRenderState(function() {
myp5.rectMode(myp5.CENTER);
});
});
test('leak no state after ellipseMode()', function() {
myp5.ellipseMode(myp5.CORNER);
assertCanPreserveRenderState(function() {
myp5.ellipseMode(myp5.CENTER);
});
});
test('leak no state after colorMode()', function() {
myp5.colorMode(myp5.HSB);
assertCanPreserveRenderState(function() {
myp5.colorMode(myp5.RGB);
});
});
test('leak no state after textAlign()', function() {
myp5.textAlign(myp5.RIGHT, myp5.BOTTOM);
assertCanPreserveRenderState(function() {
myp5.textAlign(myp5.CENTER, myp5.CENTER);
});
});
test('leak no state after textFont()', function() {
myp5.textFont('Georgia');
assertCanPreserveRenderState(function() {
myp5.textFont('Helvetica');
});
});
test('leak no state after textStyle()', function() {
myp5.textStyle(myp5.ITALIC);
assertCanPreserveRenderState(function() {
myp5.textStyle(myp5.BOLD);
});
});
test('leak no state after textSize()', function() {
myp5.textSize(12);
assertCanPreserveRenderState(function() {
myp5.textSize(16);
});
});
test('leak no state after textLeading()', function() {
myp5.textLeading(20);
assertCanPreserveRenderState(function() {
myp5.textLeading(30);
});
});
});
suite('p5.prototype.redraw', function() {
var iframe;
teardown(function() {
if (iframe) {
iframe.teardown();
iframe = null;
}
});
test('resets the rendering matrix between frames', function() {
return new Promise(function(resolve, reject) {
myp5.draw = function() {
myp5.background(0);
myp5.stroke(255);
myp5.point(10, 10);
if (myp5.get(10, 10)[0] === 0) {
reject(new Error("Drawing matrix doesn't appear to be reset"));
}
myp5.rotate(10);
};
myp5.redraw(10);
resolve();
});
});
test('instance redraw is independent of window', function() {
// callback for p5 instance mode.
// It does not call noLoop so redraw will be called many times.
// Redraw is not supposed to call window.draw even though no draw is defined in cb
function cb(s) {
s.setup = function() {
setTimeout(window.afterSetup, 1000);
};
}
return new Promise(function(resolve) {
iframe = createP5Iframe(
[
P5_SCRIPT_TAG,
'<script>',
'globalDraws = 0;',
'function setup() { noLoop(); }',
'function draw() { window.globalDraws++; }',
'new p5(' + cb.toString() + ');',
'</script>'
].join('\n')
);
iframe.elt.contentWindow.afterSetup = resolve;
}).then(function() {
var win = iframe.elt.contentWindow;
assert.strictEqual(win.globalDraws, 1);
});
});
});
suite('loop', function() {
testSketchWithPromise('loop in setup does not call draw', function(
sketch,
resolve,
reject
) {
sketch.setup = function() {
sketch.loop();
resolve();
};
sketch.draw = function() {
reject(new Error('Entered draw during loop()'));
};
});
testSketchWithPromise('loop in draw does not call draw', function(
sketch,
resolve,
reject
) {
sketch.draw = function() {
if (sketch.frameCount > 1) {
reject(new Error('re-entered draw during loop() call'));
}
sketch.loop();
resolve();
};
});
});
});