q5 2.9.22 → 2.9.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/.vscode/launch.json +26 -0
  2. package/bun.lockb +0 -0
  3. package/p5-tests/js/chai_helpers.js +20 -0
  4. package/p5-tests/js/mocha_setup.js +2 -0
  5. package/p5-tests/js/modernizr.js +5 -0
  6. package/p5-tests/js/p5_helpers.js +135 -0
  7. package/p5-tests/js/sinon.js +5949 -0
  8. package/p5-tests/mocha.css +289 -0
  9. package/p5-tests/test.html +71 -0
  10. package/p5-tests/unit/color/color_conversion.js +68 -0
  11. package/p5-tests/unit/color/creating_reading.js +217 -0
  12. package/p5-tests/unit/color/p5.Color.js +1000 -0
  13. package/p5-tests/unit/color/setting.js +289 -0
  14. package/p5-tests/unit/core/2d_primitives.js +490 -0
  15. package/p5-tests/unit/core/attributes.js +115 -0
  16. package/p5-tests/unit/core/curves.js +139 -0
  17. package/p5-tests/unit/core/environment.js +248 -0
  18. package/p5-tests/unit/core/error_helpers.js +1158 -0
  19. package/p5-tests/unit/core/main.js +340 -0
  20. package/p5-tests/unit/core/p5.Element.js +773 -0
  21. package/p5-tests/unit/core/p5.Graphics.js +179 -0
  22. package/p5-tests/unit/core/preload.js +285 -0
  23. package/p5-tests/unit/core/rendering.js +116 -0
  24. package/p5-tests/unit/core/structure.js +293 -0
  25. package/p5-tests/unit/core/transform.js +144 -0
  26. package/p5-tests/unit/core/version.js +28 -0
  27. package/p5-tests/unit/core/vertex.js +137 -0
  28. package/p5-tests/unit/dom/dom.js +2146 -0
  29. package/p5-tests/unit/events/acceleration.js +213 -0
  30. package/p5-tests/unit/events/keyboard.js +179 -0
  31. package/p5-tests/unit/events/mouse.js +487 -0
  32. package/p5-tests/unit/events/touch.js +180 -0
  33. package/p5-tests/unit/image/downloading.js +379 -0
  34. package/p5-tests/unit/image/filters.js +92 -0
  35. package/p5-tests/unit/image/loading.js +413 -0
  36. package/p5-tests/unit/image/p5.Image.js +201 -0
  37. package/p5-tests/unit/image/pixels.js +234 -0
  38. package/p5-tests/unit/io/files.js +378 -0
  39. package/p5-tests/unit/io/loadBytes.js +149 -0
  40. package/p5-tests/unit/io/loadImage.js +123 -0
  41. package/p5-tests/unit/io/loadJSON.js +185 -0
  42. package/p5-tests/unit/io/loadModel.js +215 -0
  43. package/p5-tests/unit/io/loadShader.js +176 -0
  44. package/p5-tests/unit/io/loadStrings.js +140 -0
  45. package/p5-tests/unit/io/loadTable.js +183 -0
  46. package/p5-tests/unit/io/loadXML.js +127 -0
  47. package/p5-tests/unit/io/saveModel.js +113 -0
  48. package/p5-tests/unit/io/saveTable.js +142 -0
  49. package/p5-tests/unit/math/calculation.js +452 -0
  50. package/p5-tests/unit/math/noise.js +66 -0
  51. package/p5-tests/unit/math/p5.Vector.js +1886 -0
  52. package/p5-tests/unit/math/random.js +177 -0
  53. package/p5-tests/unit/math/trigonometry.js +144 -0
  54. package/p5-tests/unit/spec.js +50 -0
  55. package/p5-tests/unit/typography/attributes.js +120 -0
  56. package/p5-tests/unit/typography/loadFont.js +162 -0
  57. package/p5-tests/unit/typography/p5.Font.js +63 -0
  58. package/p5-tests/unit/utilities/conversion.js +329 -0
  59. package/p5-tests/unit/utilities/time_date.js +133 -0
  60. package/package.json +1 -1
  61. package/q5.d.ts +172 -55
  62. package/q5.js +55 -40
  63. package/q5.min.js +1 -1
  64. package/src/q5-2d-image.js +7 -3
  65. package/src/q5-core.js +6 -2
  66. package/src/q5-math.js +1 -0
  67. package/src/q5-webgpu-canvas.js +8 -7
  68. package/src/q5-webgpu-drawing.js +15 -12
  69. package/src/q5-webgpu-image.js +1 -1
  70. package/src/q5-webgpu-text.js +17 -15
@@ -0,0 +1,773 @@
1
+ suite('p5.Element', function() {
2
+ var myp5 = new p5(function(sketch) {
3
+ sketch.setup = function() {};
4
+ sketch.draw = function() {};
5
+ });
6
+
7
+ var elt;
8
+
9
+ teardown(function() {
10
+ if (elt && elt.parentNode) {
11
+ elt.parentNode.removeChild(elt);
12
+ elt = null;
13
+ }
14
+ myp5.remove();
15
+ });
16
+
17
+ suite('p5.Element.prototype.parent', function() {
18
+ test('attaches child to parent', function() {
19
+ let div0 = myp5.createDiv('this is the parent');
20
+ let div1 = myp5.createDiv('this is the child');
21
+ div1.attribute('id', 'child');
22
+ div1.parent(div0); //attaches div1 to div0
23
+ assert.equal(document.getElementById('child').parentElement, div0.elt);
24
+ });
25
+
26
+ test('attaches child to parent using classname', function() {
27
+ let div0 = myp5.createDiv('this is the parent');
28
+ let div1 = myp5.createDiv('this is the child');
29
+ div0.attribute('id', 'parent');
30
+ div1.parent('parent'); //attaches div1 to div0 using classname
31
+ assert.equal(div1.parent(), div0.elt); //returns parent of div1
32
+ });
33
+
34
+ test('attaches child to parent using classname', function() {
35
+ let div0 = myp5.createDiv('this is the parent');
36
+ let div1 = myp5.createDiv('this is the child');
37
+ div0.attribute('id', 'parent');
38
+ div1.parent('#parent'); //attaches div1 to div0
39
+ assert.equal(div1.parent(), div0.elt); //returns parent of div1 using id
40
+ });
41
+
42
+ test('returns the parent', function() {
43
+ let div0 = document.createElement('div');
44
+ let div1 = document.createElement('div');
45
+ div1.setAttribute('id', 'child');
46
+ div0.appendChild(div1);
47
+ document.body.appendChild(div0);
48
+ assert.equal(myp5.select('#child').parent(), div0);
49
+ });
50
+ });
51
+
52
+ suite('p5.Element.prototype.id', function() {
53
+ test('attaches child to parent', function() {
54
+ elt = myp5.createDiv();
55
+ elt.id('test');
56
+ assert.equal(document.getElementById('test'), elt.elt);
57
+ });
58
+
59
+ test('returns the id', function() {
60
+ elt = document.createElement('div');
61
+ elt.setAttribute('id', 'test');
62
+ document.body.appendChild(elt);
63
+ assert.equal(myp5.select('#child').id(), 'child');
64
+ });
65
+ });
66
+
67
+ suite('p5.Element.prototype.mousePressed', function() {
68
+ test('attaches and gets events', function() {
69
+ // setup
70
+ elt = myp5.createDiv('hello');
71
+ var myFnCounter = 0;
72
+ var myFn = function() {
73
+ myFnCounter++;
74
+ };
75
+
76
+ elt.mousePressed(myFn);
77
+ assert.isFunction(elt._events.mousedown);
78
+ elt.elt.dispatchEvent(new Event('mousedown'));
79
+ assert.equal(myFnCounter, 1);
80
+ });
81
+
82
+ test('attaches multiple handlers and only latest gets events', function() {
83
+ // setup
84
+ elt = myp5.createDiv('hello');
85
+ var myFnCounter = 0;
86
+ var myFn = function() {
87
+ myFnCounter++;
88
+ };
89
+ var myFnCounterOther = 0;
90
+ var myFnOther = function() {
91
+ myFnCounterOther++;
92
+ };
93
+
94
+ elt.mousePressed(myFn);
95
+ elt.mousePressed(myFnOther);
96
+ assert.isFunction(elt._events.mousedown);
97
+ elt.elt.dispatchEvent(new Event('mousedown'));
98
+ assert.equal(myFnCounter, 0);
99
+ assert.equal(myFnCounterOther, 1);
100
+ });
101
+ });
102
+
103
+ suite('p5.Element.prototype.mouseClicked', function() {
104
+ test('attaches and gets events', function() {
105
+ // setup
106
+ elt = myp5.createDiv('hello');
107
+ var myFnCounter = 0;
108
+ var myFn = function() {
109
+ myFnCounter++;
110
+ };
111
+
112
+ elt.mouseClicked(myFn);
113
+ assert.isFunction(elt._events.click);
114
+ elt.elt.dispatchEvent(new Event('click'));
115
+ assert.equal(myFnCounter, 1);
116
+ });
117
+
118
+ test('attaches multiple handlers and only latest gets events', function() {
119
+ // setup
120
+ elt = myp5.createDiv('hello');
121
+ var myFnCounter = 0;
122
+ var myFn = function() {
123
+ myFnCounter++;
124
+ };
125
+ var myFnCounterOther = 0;
126
+ var myFnOther = function() {
127
+ myFnCounterOther++;
128
+ };
129
+
130
+ elt.mouseClicked(myFn);
131
+ elt.mouseClicked(myFnOther);
132
+ assert.isFunction(elt._events.click);
133
+ elt.elt.dispatchEvent(new Event('click'));
134
+ assert.equal(myFnCounter, 0);
135
+ assert.equal(myFnCounterOther, 1);
136
+ });
137
+
138
+ test('detaches and does not get events', function() {
139
+ // setup
140
+ elt = myp5.createDiv('hello');
141
+ var myFnCounter = 0;
142
+ var myFn = function() {
143
+ myFnCounter++;
144
+ };
145
+
146
+ elt.mouseClicked(myFn);
147
+ elt.mouseClicked(false);
148
+ assert.isNull(elt._events.click);
149
+ elt.elt.dispatchEvent(new Event('click'));
150
+ assert.equal(myFnCounter, 0);
151
+ });
152
+ });
153
+
154
+ suite('p5.Element.prototype.doubleClicked', function() {
155
+ test('attaches and gets events', function() {
156
+ // setup
157
+ elt = myp5.createDiv('hello');
158
+ var myFnCounter = 0;
159
+ var myFn = function() {
160
+ myFnCounter++;
161
+ };
162
+
163
+ elt.doubleClicked(myFn);
164
+ assert.isFunction(elt._events.dblclick);
165
+ elt.elt.dispatchEvent(new Event('dblclick'));
166
+ assert.equal(myFnCounter, 1);
167
+ });
168
+
169
+ test('attaches multiple handlers and only latest gets events', function() {
170
+ // setup
171
+ elt = myp5.createDiv('hello');
172
+ var myFnCounter = 0;
173
+ var myFn = function() {
174
+ myFnCounter++;
175
+ };
176
+ var myFnCounterOther = 0;
177
+ var myFnOther = function() {
178
+ myFnCounterOther++;
179
+ };
180
+
181
+ elt.doubleClicked(myFn);
182
+ elt.doubleClicked(myFnOther);
183
+ assert.isFunction(elt._events.dblclick);
184
+ elt.elt.dispatchEvent(new Event('dblclick'));
185
+ assert.equal(myFnCounter, 0);
186
+ assert.equal(myFnCounterOther, 1);
187
+ });
188
+
189
+ test('detaches and does not get events', function() {
190
+ // setup
191
+ elt = myp5.createDiv('hello');
192
+ var myFnCounter = 0;
193
+ var myFn = function() {
194
+ myFnCounter++;
195
+ };
196
+
197
+ elt.doubleClicked(myFn);
198
+ elt.doubleClicked(false);
199
+ assert.isNull(elt._events.dblclick);
200
+ elt.elt.dispatchEvent(new Event('dblclick'));
201
+ assert.equal(myFnCounter, 0);
202
+ });
203
+ });
204
+
205
+ suite('p5.Element.prototype.mouseWheel', function() {
206
+ test('attaches and gets events', function() {
207
+ // setup
208
+ elt = myp5.createDiv('hello');
209
+ var myFnCounter = 0;
210
+ var myFn = function(event) {
211
+ if (event.deltaX > 0) {
212
+ myFnCounter++;
213
+ }
214
+ };
215
+
216
+ elt.mouseWheel(myFn);
217
+ assert.isFunction(elt._events.wheel);
218
+ elt.elt.dispatchEvent(new WheelEvent('wheel', { deltaX: 10 }));
219
+ assert.equal(myFnCounter, 1);
220
+ });
221
+
222
+ test('attaches multiple handlers and only latest gets events', function() {
223
+ // setup
224
+ elt = myp5.createDiv('hello');
225
+ var myFnCounter = 0;
226
+ var myFn = function() {
227
+ myFnCounter++;
228
+ };
229
+ var myFnCounterOther = 0;
230
+ var myFnOther = function() {
231
+ myFnCounterOther++;
232
+ };
233
+
234
+ elt.mouseWheel(myFn);
235
+ elt.mouseWheel(myFnOther);
236
+ assert.isFunction(elt._events.wheel);
237
+ elt.elt.dispatchEvent(new Event('wheel'));
238
+ assert.equal(myFnCounter, 0);
239
+ assert.equal(myFnCounterOther, 1);
240
+ });
241
+ });
242
+
243
+ suite('p5.Element.prototype.touchStarted', function() {
244
+ test('attaches and gets events', function() {
245
+ // setup
246
+ elt = myp5.createDiv('hello');
247
+ var myFnCounter = 0;
248
+ var myFn = function(event) {
249
+ myFnCounter++;
250
+ };
251
+
252
+ elt.touchStarted(myFn);
253
+ assert.isFunction(elt._events.touchstart);
254
+ elt.elt.dispatchEvent(new Event('touchstart'));
255
+ assert.equal(myFnCounter, 1);
256
+ });
257
+
258
+ test('attaches multiple handlers and only latest gets events', function() {
259
+ // setup
260
+ elt = myp5.createDiv('hello');
261
+ var myFnCounter = 0;
262
+ var myFn = function() {
263
+ myFnCounter++;
264
+ };
265
+ var myFnCounterOther = 0;
266
+ var myFnOther = function() {
267
+ myFnCounterOther++;
268
+ };
269
+
270
+ elt.touchStarted(myFn);
271
+ elt.touchStarted(myFnOther);
272
+ assert.isFunction(elt._events.touchstart);
273
+ elt.elt.dispatchEvent(new Event('touchstart'));
274
+ assert.equal(myFnCounter, 0);
275
+ assert.equal(myFnCounterOther, 1);
276
+ });
277
+
278
+ test('detaches and does not get events', function() {
279
+ // setup
280
+ elt = myp5.createDiv('hello');
281
+ var myFnCounter = 0;
282
+ var myFn = function() {
283
+ myFnCounter++;
284
+ };
285
+
286
+ elt.touchStarted(myFn);
287
+ elt.touchStarted(false);
288
+ assert.isNull(elt._events.touchstart);
289
+ elt.elt.dispatchEvent(new Event('touchstart'));
290
+ assert.equal(myFnCounter, 0);
291
+ });
292
+ });
293
+
294
+ suite('p5.Element.prototype.touchMoved', function() {
295
+ test('attaches and gets events', function() {
296
+ // setup
297
+ elt = myp5.createDiv('hello');
298
+ var myFnCounter = 0;
299
+ var myFn = function(event) {
300
+ myFnCounter++;
301
+ };
302
+
303
+ elt.touchMoved(myFn);
304
+ assert.isFunction(elt._events.touchmove);
305
+ elt.elt.dispatchEvent(new Event('touchmove'));
306
+ assert.equal(myFnCounter, 1);
307
+ });
308
+
309
+ test('attaches multiple handlers and only latest gets events', function() {
310
+ // setup
311
+ elt = myp5.createDiv('hello');
312
+ var myFnCounter = 0;
313
+ var myFn = function() {
314
+ myFnCounter++;
315
+ };
316
+ var myFnCounterOther = 0;
317
+ var myFnOther = function() {
318
+ myFnCounterOther++;
319
+ };
320
+
321
+ elt.touchMoved(myFn);
322
+ elt.touchMoved(myFnOther);
323
+ assert.isFunction(elt._events.touchmove);
324
+ elt.elt.dispatchEvent(new Event('touchmove'));
325
+ assert.equal(myFnCounter, 0);
326
+ assert.equal(myFnCounterOther, 1);
327
+ });
328
+
329
+ test('detaches and does not get events', function() {
330
+ // setup
331
+ elt = myp5.createDiv('hello');
332
+ var myFnCounter = 0;
333
+ var myFn = function() {
334
+ myFnCounter++;
335
+ };
336
+
337
+ elt.touchMoved(myFn);
338
+ elt.touchMoved(false);
339
+ assert.isNull(elt._events.touchmove);
340
+ elt.elt.dispatchEvent(new Event('touchmove'));
341
+ assert.equal(myFnCounter, 0);
342
+ });
343
+ });
344
+
345
+ suite('p5.Element.prototype.touchEnded', function() {
346
+ test('attaches and gets events', function() {
347
+ // setup
348
+ elt = myp5.createDiv('hello');
349
+ var myFnCounter = 0;
350
+ var myFn = function(event) {
351
+ myFnCounter++;
352
+ };
353
+
354
+ elt.touchEnded(myFn);
355
+ assert.isFunction(elt._events.touchend);
356
+ elt.elt.dispatchEvent(new Event('touchend'));
357
+ assert.equal(myFnCounter, 1);
358
+ });
359
+
360
+ test('attaches multiple handlers and only latest gets events', function() {
361
+ // setup
362
+ elt = myp5.createDiv('hello');
363
+ var myFnCounter = 0;
364
+ var myFn = function() {
365
+ myFnCounter++;
366
+ };
367
+ var myFnCounterOther = 0;
368
+ var myFnOther = function() {
369
+ myFnCounterOther++;
370
+ };
371
+
372
+ elt.touchEnded(myFn);
373
+ elt.touchEnded(myFnOther);
374
+ assert.isFunction(elt._events.touchend);
375
+ elt.elt.dispatchEvent(new Event('touchend'));
376
+ assert.equal(myFnCounter, 0);
377
+ assert.equal(myFnCounterOther, 1);
378
+ });
379
+
380
+ test('detaches and does not get events', function() {
381
+ // setup
382
+ elt = myp5.createDiv('hello');
383
+ var myFnCounter = 0;
384
+ var myFn = function() {
385
+ myFnCounter++;
386
+ };
387
+
388
+ elt.touchEnded(myFn);
389
+ elt.touchEnded(false);
390
+ assert.isNull(elt._events.touchend);
391
+ elt.elt.dispatchEvent(new Event('touchend'));
392
+ assert.equal(myFnCounter, 0);
393
+ });
394
+ });
395
+
396
+ suite('p5.Element.prototype.mouseReleased', function() {
397
+ test('attaches and gets events', function() {
398
+ // setup
399
+ elt = myp5.createDiv('hello');
400
+ var myFnCounter = 0;
401
+ var myFn = function() {
402
+ myFnCounter++;
403
+ };
404
+
405
+ elt.mouseReleased(myFn);
406
+ assert.isFunction(elt._events.mouseup);
407
+ elt.elt.dispatchEvent(new Event('mouseup'));
408
+ assert.equal(myFnCounter, 1);
409
+ });
410
+
411
+ test('attaches multiple handlers and only latest gets events', function() {
412
+ // setup
413
+ elt = myp5.createDiv('hello');
414
+ var myFnCounter = 0;
415
+ var myFn = function() {
416
+ myFnCounter++;
417
+ };
418
+ var myFnCounterOther = 0;
419
+ var myFnOther = function() {
420
+ myFnCounterOther++;
421
+ };
422
+
423
+ elt.mouseReleased(myFn);
424
+ elt.mouseReleased(myFnOther);
425
+ assert.isFunction(elt._events.mouseup);
426
+ elt.elt.dispatchEvent(new Event('mouseup'));
427
+ assert.equal(myFnCounter, 0);
428
+ assert.equal(myFnCounterOther, 1);
429
+ });
430
+
431
+ test('detaches and does not get events', function() {
432
+ // setup
433
+ elt = myp5.createDiv('hello');
434
+ var myFnCounter = 0;
435
+ var myFn = function() {
436
+ myFnCounter++;
437
+ };
438
+
439
+ elt.mouseReleased(myFn);
440
+ elt.mouseReleased(false);
441
+ assert.isNull(elt._events.mouseup);
442
+ elt.elt.dispatchEvent(new Event('mouseup'));
443
+ assert.equal(myFnCounter, 0);
444
+ });
445
+ });
446
+
447
+ suite('p5.Element.prototype.mouseMoved', function() {
448
+ test('attaches and gets events', function() {
449
+ // setup
450
+ elt = myp5.createDiv('hello');
451
+ var myFnCounter = 0;
452
+ var myFn = function() {
453
+ myFnCounter++;
454
+ };
455
+
456
+ elt.mouseMoved(myFn);
457
+ assert.isFunction(elt._events.mousemove);
458
+ elt.elt.dispatchEvent(new Event('mousemove'));
459
+ assert.equal(myFnCounter, 1);
460
+ });
461
+
462
+ test('attaches multiple handlers and only latest gets events', function() {
463
+ // setup
464
+ elt = myp5.createDiv('hello');
465
+ var myFnCounter = 0;
466
+ var myFn = function() {
467
+ myFnCounter++;
468
+ };
469
+ var myFnCounterOther = 0;
470
+ var myFnOther = function() {
471
+ myFnCounterOther++;
472
+ };
473
+
474
+ elt.mouseMoved(myFn);
475
+ elt.mouseMoved(myFnOther);
476
+ assert.isFunction(elt._events.mousemove);
477
+ elt.elt.dispatchEvent(new Event('mousemove'));
478
+ assert.equal(myFnCounter, 0);
479
+ assert.equal(myFnCounterOther, 1);
480
+ });
481
+
482
+ test('detaches and does not get events', function() {
483
+ // setup
484
+ elt = myp5.createDiv('hello');
485
+ var myFnCounter = 0;
486
+ var myFn = function() {
487
+ myFnCounter++;
488
+ };
489
+
490
+ elt.mouseMoved(myFn);
491
+ elt.mouseMoved(false);
492
+ assert.isNull(elt._events.mousemove);
493
+ elt.elt.dispatchEvent(new Event('mousemove'));
494
+ assert.equal(myFnCounter, 0);
495
+ });
496
+ });
497
+
498
+ suite('p5.Element.prototype.mouseOver', function() {
499
+ test('attaches and gets events', function() {
500
+ // setup
501
+ elt = myp5.createDiv('hello');
502
+ var myFnCounter = 0;
503
+ var myFn = function() {
504
+ myFnCounter++;
505
+ };
506
+
507
+ elt.mouseOver(myFn);
508
+ assert.isFunction(elt._events.mouseover);
509
+ elt.elt.dispatchEvent(new Event('mouseover'));
510
+ assert.equal(myFnCounter, 1);
511
+ });
512
+
513
+ test('attaches multiple handlers and only latest gets events', function() {
514
+ // setup
515
+ elt = myp5.createDiv('hello');
516
+ var myFnCounter = 0;
517
+ var myFn = function() {
518
+ myFnCounter++;
519
+ };
520
+ var myFnCounterOther = 0;
521
+ var myFnOther = function() {
522
+ myFnCounterOther++;
523
+ };
524
+
525
+ elt.mouseOver(myFn);
526
+ elt.mouseOver(myFnOther);
527
+ assert.isFunction(elt._events.mouseover);
528
+ elt.elt.dispatchEvent(new Event('mouseover'));
529
+ assert.equal(myFnCounter, 0);
530
+ assert.equal(myFnCounterOther, 1);
531
+ });
532
+
533
+ test('detaches and does not get events', function() {
534
+ // setup
535
+ elt = myp5.createDiv('hello');
536
+ var myFnCounter = 0;
537
+ var myFn = function() {
538
+ myFnCounter++;
539
+ };
540
+
541
+ elt.mouseOver(myFn);
542
+ elt.mouseOver(false);
543
+ assert.isNull(elt._events.mouseover);
544
+ elt.elt.dispatchEvent(new Event('mouseover'));
545
+ assert.equal(myFnCounter, 0);
546
+ });
547
+ });
548
+
549
+ suite('p5.Element.prototype.mouseOut', function() {
550
+ test('attaches and gets events', function() {
551
+ // setup
552
+ elt = myp5.createDiv('hello');
553
+ var myFnCounter = 0;
554
+ var myFn = function() {
555
+ myFnCounter++;
556
+ };
557
+
558
+ elt.mouseOut(myFn);
559
+ assert.isFunction(elt._events.mouseout);
560
+ elt.elt.dispatchEvent(new Event('mouseout'));
561
+ assert.equal(myFnCounter, 1);
562
+ });
563
+
564
+ test('attaches multiple handlers and only latest gets events', function() {
565
+ // setup
566
+ elt = myp5.createDiv('hello');
567
+ var myFnCounter = 0;
568
+ var myFn = function() {
569
+ myFnCounter++;
570
+ };
571
+ var myFnCounterOther = 0;
572
+ var myFnOther = function() {
573
+ myFnCounterOther++;
574
+ };
575
+
576
+ elt.mouseOut(myFn);
577
+ elt.mouseOut(myFnOther);
578
+ assert.isFunction(elt._events.mouseout);
579
+ elt.elt.dispatchEvent(new Event('mouseout'));
580
+ assert.equal(myFnCounter, 0);
581
+ assert.equal(myFnCounterOther, 1);
582
+ });
583
+
584
+ test('detaches and does not get events', function() {
585
+ // setup
586
+ elt = myp5.createDiv('hello');
587
+ var myFnCounter = 0;
588
+ var myFn = function() {
589
+ myFnCounter++;
590
+ };
591
+
592
+ elt.mouseOut(myFn);
593
+ elt.mouseOut(false);
594
+ assert.isNull(elt._events.mouseout);
595
+ elt.elt.dispatchEvent(new Event('mouseout'));
596
+ assert.equal(myFnCounter, 0);
597
+ });
598
+ });
599
+
600
+ suite('p5.Element.prototype.dragOver', function() {
601
+ test('attaches and gets events', function() {
602
+ elt = myp5.createDiv('hello');
603
+ var myFnCounter = 0;
604
+ var myFn = function() {
605
+ myFnCounter++;
606
+ };
607
+
608
+ elt.dragOver(myFn);
609
+ assert.isFunction(elt._events.dragover);
610
+ elt.elt.dispatchEvent(new Event('dragover'));
611
+ assert.equal(myFnCounter, 1);
612
+ });
613
+
614
+ test('attaches multiple handlers and only latest gets events', function() {
615
+ // setup
616
+ elt = myp5.createDiv('hello');
617
+ var myFnCounter = 0;
618
+ var myFn = function() {
619
+ myFnCounter++;
620
+ };
621
+ var myFnCounterOther = 0;
622
+ var myFnOther = function() {
623
+ myFnCounterOther++;
624
+ };
625
+
626
+ elt.dragOver(myFn);
627
+ elt.dragOver(myFnOther);
628
+ assert.isFunction(elt._events.dragover);
629
+ elt.elt.dispatchEvent(new Event('dragover'));
630
+ assert.equal(myFnCounter, 0);
631
+ assert.equal(myFnCounterOther, 1);
632
+ });
633
+
634
+ test('detaches and does not get events', function() {
635
+ // setup
636
+ elt = myp5.createDiv('hello');
637
+ var myFnCounter = 0;
638
+ var myFn = function() {
639
+ myFnCounter++;
640
+ };
641
+
642
+ elt.dragOver(myFn);
643
+ elt.dragOver(false);
644
+ assert.isNull(elt._events.dragover);
645
+ elt.elt.dispatchEvent(new Event('dragover'));
646
+ assert.equal(myFnCounter, 0);
647
+ });
648
+ });
649
+
650
+ suite('p5.Element.prototype.dragLeave', function() {
651
+ test('attaches and gets events', function() {
652
+ elt = myp5.createDiv('hello');
653
+ var myFnCounter = 0;
654
+ var myFn = function() {
655
+ myFnCounter++;
656
+ };
657
+
658
+ elt.dragLeave(myFn);
659
+ assert.isFunction(elt._events.dragleave);
660
+ elt.elt.dispatchEvent(new Event('dragleave'));
661
+ assert.equal(myFnCounter, 1);
662
+ });
663
+
664
+ test('attaches multiple handlers and only latest gets events', function() {
665
+ // setup
666
+ elt = myp5.createDiv('hello');
667
+ var myFnCounter = 0;
668
+ var myFn = function() {
669
+ myFnCounter++;
670
+ };
671
+ var myFnCounterOther = 0;
672
+ var myFnOther = function() {
673
+ myFnCounterOther++;
674
+ };
675
+
676
+ elt.dragLeave(myFn);
677
+ elt.dragLeave(myFnOther);
678
+ assert.isFunction(elt._events.dragleave);
679
+ elt.elt.dispatchEvent(new Event('dragleave'));
680
+ assert.equal(myFnCounter, 0);
681
+ assert.equal(myFnCounterOther, 1);
682
+ });
683
+
684
+ test('detaches and does not get events', function() {
685
+ // setup
686
+ elt = myp5.createDiv('hello');
687
+ var myFnCounter = 0;
688
+ var myFn = function() {
689
+ myFnCounter++;
690
+ };
691
+
692
+ elt.dragLeave(myFn);
693
+ elt.dragLeave(false);
694
+ assert.isNull(elt._events.dragleave);
695
+ elt.elt.dispatchEvent(new Event('dragleave'));
696
+ assert.equal(myFnCounter, 0);
697
+ });
698
+ });
699
+
700
+ suite('operating with element classes', function() {
701
+ test('should add class to element', function() {
702
+ elt = document.createElement('div');
703
+ elt.setAttribute('id', 'testdiv');
704
+ document.body.appendChild(elt);
705
+
706
+ myp5.select('#testdiv').addClass('testclass');
707
+ assert.strictEqual(elt.getAttribute('class'), 'testclass');
708
+ });
709
+
710
+ test('should remove class from element with only one class', function() {
711
+ elt = document.createElement('div');
712
+ elt.setAttribute('id', 'testdiv');
713
+ elt.setAttribute('class', 'testclass');
714
+ document.body.appendChild(elt);
715
+
716
+ myp5.select('#testdiv').removeClass('testclass');
717
+ assert.strictEqual(elt.getAttribute('class'), '');
718
+ });
719
+
720
+ test('should remove class from element with several classes', function() {
721
+ elt = document.createElement('div');
722
+ elt.setAttribute('id', 'testdiv');
723
+ elt.setAttribute('class', 'testclass1 testclass2 testclass3');
724
+ document.body.appendChild(elt);
725
+
726
+ myp5.select('#testdiv').removeClass('testclass2');
727
+ assert.strictEqual(elt.getAttribute('class'), 'testclass1 testclass3');
728
+ });
729
+
730
+ test('should return true if element has specified class', function() {
731
+ elt = document.createElement('div');
732
+ elt.setAttribute('id', 'testdiv');
733
+ elt.setAttribute('class', 'testclass1 testclass2 testclass3');
734
+ document.body.appendChild(elt);
735
+
736
+ assert.strictEqual(myp5.select('#testdiv').hasClass('testclass2'), true);
737
+ });
738
+
739
+ test('should return false if element has not specified class', function() {
740
+ elt = document.createElement('div');
741
+ elt.setAttribute('id', 'testdiv');
742
+ elt.setAttribute('class', 'testclass1 testclass3');
743
+ document.body.appendChild(elt);
744
+
745
+ assert.strictEqual(myp5.select('#testdiv').hasClass('testclass2'), false);
746
+ });
747
+
748
+ test('should return false if element has class that is partially similar as specified class', function() {
749
+ elt = document.createElement('div');
750
+ elt.setAttribute('id', 'testdiv');
751
+ elt.setAttribute('class', 'testclass slideshow newtestsclas');
752
+ document.body.appendChild(elt);
753
+
754
+ assert.strictEqual(myp5.select('#testdiv').hasClass('show'), false);
755
+ assert.strictEqual(myp5.select('#testdiv').hasClass('slide'), false);
756
+ assert.strictEqual(myp5.select('#testdiv').hasClass('test'), false);
757
+ assert.strictEqual(myp5.select('#testdiv').hasClass('class'), false);
758
+ });
759
+
760
+ test('should toggle specified class on element', function() {
761
+ elt = document.createElement('div');
762
+ elt.setAttribute('id', 'testdiv');
763
+ elt.setAttribute('class', 'testclass1 testclass2');
764
+ document.body.appendChild(elt);
765
+
766
+ myp5.select('#testdiv').toggleClass('testclass2');
767
+ assert.strictEqual(elt.getAttribute('class'), 'testclass1');
768
+
769
+ myp5.select('#testdiv').toggleClass('testclass2');
770
+ assert.strictEqual(elt.getAttribute('class'), 'testclass1 testclass2');
771
+ });
772
+ });
773
+ });