@@ -32,6 +32,13 @@ suite('Touch Events', function() {
3232 myp5 . remove ( ) ;
3333 } ) ;
3434
35+ beforeEach ( function ( ) {
36+ // Reset pointer state so tests don't leak active pointers into each other.
37+ myp5 . _activePointers . clear ( ) ;
38+ myp5 . touches = [ ] ;
39+ myp5 . mouseIsPressed = false ;
40+ } ) ;
41+
3542 suite ( 'p5.prototype.touches' , function ( ) {
3643 test ( 'should be an empty array' , function ( ) {
3744 assert . deepEqual ( myp5 . touches , [ ] ) ;
@@ -44,7 +51,45 @@ suite('Touch Events', function() {
4451 } ) ;
4552
4653 test ( 'should contain the touch registered' , function ( ) {
54+ window . dispatchEvent ( touchEvent1 ) ;
4755 assert . strictEqual ( myp5 . touches [ 0 ] . id , 1 ) ;
4856 } ) ;
4957 } ) ;
58+
59+ suite ( 'p5.prototype._onpointercancel' , function ( ) {
60+ test ( 'should remove the cancelled touch from touches' , function ( ) {
61+ window . dispatchEvent ( touchEvent1 ) ;
62+ window . dispatchEvent ( touchEvent2 ) ;
63+ assert . strictEqual ( myp5 . touches . length , 2 ) ;
64+
65+ // A cancelled pointer must be cleaned up even though no
66+ // 'pointerup' event is dispatched.
67+ const cancelEvent = new PointerEvent ( 'pointercancel' , {
68+ pointerId : 1 ,
69+ clientX : 100 ,
70+ clientY : 100 ,
71+ pointerType : 'touch'
72+ } ) ;
73+ window . dispatchEvent ( cancelEvent ) ;
74+
75+ assert . strictEqual ( myp5 . touches . length , 1 ) ;
76+ assert . strictEqual ( myp5 . touches [ 0 ] . id , 2 ) ;
77+ } ) ;
78+
79+ test ( 'should reset mouseIsPressed once all pointers are cancelled' , function ( ) {
80+ window . dispatchEvent ( touchEvent1 ) ;
81+ assert . strictEqual ( myp5 . mouseIsPressed , true ) ;
82+
83+ const cancelEvent = new PointerEvent ( 'pointercancel' , {
84+ pointerId : 1 ,
85+ clientX : 100 ,
86+ clientY : 100 ,
87+ pointerType : 'touch'
88+ } ) ;
89+ window . dispatchEvent ( cancelEvent ) ;
90+
91+ assert . strictEqual ( myp5 . touches . length , 0 ) ;
92+ assert . strictEqual ( myp5 . mouseIsPressed , false ) ;
93+ } ) ;
94+ } ) ;
5095} ) ;
0 commit comments