@@ -127,51 +127,36 @@ suite('Core', function() {
127127 } ) ;
128128
129129 suite ( 'new p5() / global mode' , function ( ) {
130- var P5_SCRIPT_URL = '../../lib/p5.js' ;
131- var P5_SCRIPT_TAG = '<script src="' + P5_SCRIPT_URL + '"></script>' ;
132130 var iframe ;
133131
134- function createP5Iframe ( html ) {
135- html = html || P5_SCRIPT_TAG ;
136-
137- iframe = document . createElement ( 'iframe' ) ;
138-
139- document . body . appendChild ( iframe ) ;
140- iframe . setAttribute ( 'style' , 'visibility: hidden' ) ;
141-
142- iframe . contentDocument . open ( ) ;
143- iframe . contentDocument . write ( html ) ;
144- iframe . contentDocument . close ( ) ;
145- }
146-
147132 teardown ( function ( ) {
148133 if ( iframe ) {
149- iframe . parentNode . removeChild ( iframe ) ;
134+ iframe . teardown ( ) ;
150135 iframe = null ;
151136 }
152137 } ) ;
153138
154139 test ( 'is triggered when "setup" is in window' , function ( ) {
155140 return new Promise ( function ( resolve , reject ) {
156- createP5Iframe ( ) ;
157- iframe . contentWindow . setup = function ( ) {
141+ iframe = createP5Iframe ( ) ;
142+ iframe . elt . contentWindow . setup = function ( ) {
158143 resolve ( ) ;
159144 } ;
160145 } ) ;
161146 } ) ;
162147
163148 test ( 'is triggered when "draw" is in window' , function ( ) {
164149 return new Promise ( function ( resolve , reject ) {
165- createP5Iframe ( ) ;
166- iframe . contentWindow . draw = function ( ) {
150+ iframe = createP5Iframe ( ) ;
151+ iframe . elt . contentWindow . draw = function ( ) {
167152 resolve ( ) ;
168153 } ;
169154 } ) ;
170155 } ) ;
171156
172157 test ( 'works when p5.js is loaded asynchronously' , function ( ) {
173158 return new Promise ( function ( resolve , reject ) {
174- createP5Iframe ( `
159+ iframe = createP5Iframe ( `
175160 <script>
176161 window.onload = function() {
177162 var script = document.createElement('script');
@@ -181,13 +166,13 @@ suite('Core', function() {
181166 }
182167 </script>` ) ;
183168
184- iframe . contentWindow . setup = resolve ;
169+ iframe . elt . contentWindow . setup = resolve ;
185170 } ) ;
186171 } ) ;
187172
188173 test ( 'works on-demand' , function ( ) {
189174 return new Promise ( function ( resolve , reject ) {
190- createP5Iframe (
175+ iframe = createP5Iframe (
191176 [
192177 P5_SCRIPT_TAG ,
193178 '<script>' ,
@@ -199,9 +184,9 @@ suite('Core', function() {
199184 '</script>'
200185 ] . join ( '\n' )
201186 ) ;
202- iframe . contentWindow . onDoneLoading = resolve ;
187+ iframe . elt . contentWindow . onDoneLoading = resolve ;
203188 } ) . then ( function ( ) {
204- var win = iframe . contentWindow ;
189+ var win = iframe . elt . contentWindow ;
205190 assert . equal ( typeof win . myURL , 'string' ) ;
206191 assert . strictEqual ( win . setupCalled , true ) ;
207192 assert . strictEqual ( win . originalP5Instance , win . p5 . instance ) ;
@@ -212,7 +197,14 @@ suite('Core', function() {
212197 suite ( 'p5.prototype._createFriendlyGlobalFunctionBinder' , function ( ) {
213198 var noop = function ( ) { } ;
214199 var createBinder = p5 . prototype . _createFriendlyGlobalFunctionBinder ;
215- 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+ } ) ;
216208
217209 setup ( function ( ) {
218210 globalObject = { } ;
@@ -307,5 +299,33 @@ suite('Core', function() {
307299 assert . equal ( globalObject . mouseX , 50 ) ;
308300 assert . isUndefined ( logMsg ) ;
309301 } ) ;
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+ } ) ;
310330 } ) ;
311331} ) ;
0 commit comments