Skip to content

Commit f13761e

Browse files
committed
Instance mode should not use global functions
See #2999 This is not an exhaustive test but these are the places we know of at this moment.
1 parent f9238ef commit f13761e

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

test/unit/core/core.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@ suite('Core', function() {
197197
suite('p5.prototype._createFriendlyGlobalFunctionBinder', function() {
198198
var noop = function() {};
199199
var createBinder = p5.prototype._createFriendlyGlobalFunctionBinder;
200-
var logMsg, globalObject, bind;
200+
var logMsg, globalObject, bind, iframe;
201+
202+
teardown(function() {
203+
if (iframe) {
204+
iframe.teardown();
205+
iframe = null;
206+
}
207+
});
201208

202209
setup(function() {
203210
globalObject = {};
@@ -292,5 +299,33 @@ suite('Core', function() {
292299
assert.equal(globalObject.mouseX, 50);
293300
assert.isUndefined(logMsg);
294301
});
302+
303+
test('instance preload is independent of window', function() {
304+
// callback for p5 instance mode.
305+
// It does not define a preload.
306+
// This tests that we don't call the global preload accidentally.
307+
function cb(s) {
308+
s.setup = function() {
309+
window.afterSetup();
310+
};
311+
}
312+
return new Promise(function(resolve) {
313+
iframe = createP5Iframe(
314+
[
315+
P5_SCRIPT_TAG,
316+
'<script>',
317+
'globalPreloads = 0;',
318+
'function setup() { }',
319+
'function preload() { window.globalPreloads++; }',
320+
'new p5(' + cb.toString() + ');',
321+
'</script>'
322+
].join('\n')
323+
);
324+
iframe.elt.contentWindow.afterSetup = resolve;
325+
}).then(function() {
326+
var win = iframe.elt.contentWindow;
327+
assert.strictEqual(win.globalPreloads, 1);
328+
});
329+
});
295330
});
296331
});

test/unit/core/structure.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,16 @@ suite('Structure', function() {
258258
});
259259
});
260260

261-
suite('p5.prototype.redraw', function() {
261+
suite.only('p5.prototype.redraw', function() {
262+
var iframe;
263+
264+
teardown(function() {
265+
if (iframe) {
266+
iframe.teardown();
267+
iframe = null;
268+
}
269+
});
270+
262271
test('resets the rendering matrix between frames', function() {
263272
return new Promise(function(resolve, reject) {
264273
myp5.draw = function() {
@@ -274,5 +283,33 @@ suite('Structure', function() {
274283
resolve();
275284
});
276285
});
286+
287+
test('instance redraw is independent of window', function() {
288+
// callback for p5 instance mode.
289+
// It does not call noLoop so redraw will be called many times.
290+
// Redraw is not supposed to call window.draw even though no draw is defined in cb
291+
function cb(s) {
292+
s.setup = function() {
293+
setTimeout(window.afterSetup, 1000);
294+
};
295+
}
296+
return new Promise(function(resolve) {
297+
iframe = createP5Iframe(
298+
[
299+
P5_SCRIPT_TAG,
300+
'<script>',
301+
'globalDraws = 0;',
302+
'function setup() { noLoop(); }',
303+
'function draw() { window.globalDraws++; }',
304+
'new p5(' + cb.toString() + ');',
305+
'</script>'
306+
].join('\n')
307+
);
308+
iframe.elt.contentWindow.afterSetup = resolve;
309+
}).then(function() {
310+
var win = iframe.elt.contentWindow;
311+
assert.strictEqual(win.globalDraws, 1);
312+
});
313+
});
277314
});
278315
});

0 commit comments

Comments
 (0)