Skip to content

Commit

Permalink
Adding initial tests for vetted listeners, see #131
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Mar 1, 2018
1 parent e2e1bd3 commit c6c1a51
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
32 changes: 32 additions & 0 deletions js/listeners/DragListenerTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017, University of Colorado Boulder

/**
* DragListener tests
*
* @author Jonathan Olson <[email protected]>
*/
define( function( require ) {
'use strict';

// modules
var DragListener = require( 'SCENERY/listeners/DragListener' );
var ListenerTestUtils = require( 'SCENERY/listeners/ListenerTestUtils' );

QUnit.module( 'DragListener' );

QUnit.test( 'translateNode', function( assert ) {
ListenerTestUtils.simpleRectangleTest( function( display, rect, node ) {
var listener = new DragListener( {
translateNode: true
} );
rect.addInputListener( listener );

ListenerTestUtils.mouseMove( display, 10, 10 );
ListenerTestUtils.mouseDown( display, 10, 10 );
ListenerTestUtils.mouseMove( display, 20, 15 );
ListenerTestUtils.mouseUp( display, 20, 15 );
assert.equal( rect.x, 10, 'Drag with translateNode should have changed the x translation' );
assert.equal( rect.y, 5, 'Drag with translateNode should have changed the y translation' );
} );
} );
} );
65 changes: 65 additions & 0 deletions js/listeners/FireListenerTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2017, University of Colorado Boulder

/**
* FireListener tests
*
* @author Jonathan Olson <[email protected]>
*/
define( function( require ) {
'use strict';

// modules
var FireListener = require( 'SCENERY/listeners/FireListener' );
var ListenerTestUtils = require( 'SCENERY/listeners/ListenerTestUtils' );

QUnit.module( 'FireListener' );

QUnit.test( 'Basics', function( assert ) {
ListenerTestUtils.simpleRectangleTest( function( display, rect, node ) {
var fireCount = 0;
var listener = new FireListener( {
fire: function() {
fireCount++;
}
} );
rect.addInputListener( listener );

ListenerTestUtils.mouseMove( display, 10, 10 );
assert.equal( fireCount, 0, 'Not yet fired on move' );
ListenerTestUtils.mouseDown( display, 10, 10 );
assert.equal( fireCount, 0, 'Not yet fired on initial press' );
ListenerTestUtils.mouseUp( display, 10, 10 );
assert.equal( fireCount, 1, 'It fired on release' );

ListenerTestUtils.mouseMove( display, 50, 10 );
ListenerTestUtils.mouseDown( display, 50, 10 );
ListenerTestUtils.mouseUp( display, 50, 10 );
assert.equal( fireCount, 1, 'Should not fire when the mouse totally misses' );

ListenerTestUtils.mouseMove( display, 10, 10 );
ListenerTestUtils.mouseDown( display, 10, 10 );
ListenerTestUtils.mouseMove( display, 50, 10 );
ListenerTestUtils.mouseUp( display, 50, 10 );
assert.equal( fireCount, 1, 'Should NOT fire when pressed and then moved away' );

ListenerTestUtils.mouseMove( display, 50, 10 );
ListenerTestUtils.mouseDown( display, 50, 10 );
ListenerTestUtils.mouseMove( display, 10, 10 );
ListenerTestUtils.mouseUp( display, 10, 10 );
assert.equal( fireCount, 1, 'Should NOT fire when the press misses (even if the release is over)' );

ListenerTestUtils.mouseMove( display, 10, 10 );
ListenerTestUtils.mouseDown( display, 10, 10 );
listener.interrupt();
ListenerTestUtils.mouseUp( display, 10, 10 );
assert.equal( fireCount, 1, 'Should NOT fire on an interruption' );

ListenerTestUtils.mouseMove( display, 10, 10 );
ListenerTestUtils.mouseDown( display, 10, 10 );
ListenerTestUtils.mouseMove( display, 50, 10 );
ListenerTestUtils.mouseMove( display, 10, 10 );
ListenerTestUtils.mouseUp( display, 10, 10 );
assert.equal( fireCount, 2, 'Should fire if the mouse is moved away after press (but moved back before release)' );
} );
} );
} );
106 changes: 106 additions & 0 deletions js/listeners/ListenerTestUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2017, University of Colorado Boulder

/**
* Utilities for listener tests
*
* @author Jonathan Olson <[email protected]>
*/
define( function( require ) {
'use strict';

// modules
var Display = require( 'SCENERY/display/Display' );
var Node = require( 'SCENERY/nodes/Node' );
var Rectangle = require( 'SCENERY/nodes/Rectangle' );
var Vector2 = require( 'DOT/Vector2' );

return {
/**
* Sends a synthesized mouseDown event at the given coordinates.
* @public
*
* @param {Display} display
* @param {number} x
* @param {number} y
*/
mouseDown: function( display, x, y ) {
var domEvent = document.createEvent( 'MouseEvent' );

// technically deprecated, but DOM4 event constructors not out yet. people on #whatwg said to use it
domEvent.initMouseEvent( 'mousedown', true, true, window, 1, // click count
x, y, x, y,
false, false, false, false,
0, // button
null );

display._input.validatePointers();
display._input.mouseDown( new Vector2( x, y ), domEvent );
},

/**
* Sends a synthesized mouseUp event at the given coordinates.
* @public
*
* @param {Display} display
* @param {number} x
* @param {number} y
*/
mouseUp: function( display, x, y ) {
var domEvent = document.createEvent( 'MouseEvent' );

// technically deprecated, but DOM4 event constructors not out yet. people on #whatwg said to use it
domEvent.initMouseEvent( 'mouseup', true, true, window, 1, // click count
x, y, x, y,
false, false, false, false,
0, // button
null );

display._input.validatePointers();
display._input.mouseUp( new Vector2( x, y ), domEvent );
},

/**
* Sends a synthesized mouseMove event at the given coordinates.
* @public
*
* @param {Display} display
* @param {number} x
* @param {number} y
*/
mouseMove: function( display, x, y ) {
var domEvent = document.createEvent( 'MouseEvent' );

// technically deprecated, but DOM4 event constructors not out yet. people on #whatwg said to use it
domEvent.initMouseEvent( 'mousemove', true, true, window, 0, // click count
x, y, x, y,
false, false, false, false,
0, // button
null );


display._input.validatePointers();
display._input.mouseMove( new Vector2( x, y ), domEvent );
},

/**
* Runs a simple test with a 20x20 rectangle in a 640x480 display.
* @public
*
* @param {Function} callback - Called with callback( {Display}, {Node}, {Node} ) - First node is the draggable rect
*/
simpleRectangleTest: function( callback ) {
var node = new Node();
var display = new Display( node, { width: 640, height: 480 } );
display.initializeEvents();
display.updateDisplay();

var rect = new Rectangle( 0, 0, 20, 20, { fill: 'red' } );
node.addChild( rect );

callback( display, rect, node );

// Cleanup, so we don't leak listeners/memory
display.dispose();
}
};
} );
105 changes: 105 additions & 0 deletions js/listeners/PressListenerTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2017, University of Colorado Boulder

/**
* PressListener tests
*
* @author Jonathan Olson <[email protected]>
*/
define( function( require ) {
'use strict';

// modules
var ListenerTestUtils = require( 'SCENERY/listeners/ListenerTestUtils' );
var PressListener = require( 'SCENERY/listeners/PressListener' );

QUnit.module( 'PressListener' );

QUnit.test( 'Basics', function( assert ) {
ListenerTestUtils.simpleRectangleTest( function( display, rect, node ) {
var pressCount = 0;
var releaseCount = 0;
var dragCount = 0;
var listener = new PressListener( {
press: function( event, listener ) {
pressCount++;
},
release: function( event, listener ) {
releaseCount++;
},
drag: function( event, listener ) {
dragCount++;
}
} );
rect.addInputListener( listener );

assert.equal( pressCount, 0, '[1] Has not been pressed yet' );
assert.equal( releaseCount, 0, '[1] Has not been released yet' );
assert.equal( dragCount, 0, '[1] Has not been dragged yet' );
assert.equal( listener.isPressedProperty.value, false, '[1] Is not pressed' );
assert.equal( listener.isOverProperty.value, false, '[1] Is not over' );
assert.equal( listener.isHoveringProperty.value, false, '[1] Is not hovering' );
assert.equal( listener.isHighlightedProperty.value, false, '[1] Is not highlighted' );
assert.equal( listener.interrupted, false, '[1] Is not interrupted' );

ListenerTestUtils.mouseMove( display, 10, 10 );

assert.equal( pressCount, 0, '[2] Has not been pressed yet' );
assert.equal( releaseCount, 0, '[2] Has not been released yet' );
assert.equal( dragCount, 0, '[2] Has not been dragged yet' );
assert.equal( listener.isPressedProperty.value, false, '[2] Is not pressed' );
assert.equal( listener.isOverProperty.value, true, '[2] Is over' );
assert.equal( listener.isHoveringProperty.value, true, '[2] Is hovering' );
assert.equal( listener.isHighlightedProperty.value, true, '[2] Is highlighted' );
assert.equal( listener.interrupted, false, '[2] Is not interrupted' );

ListenerTestUtils.mouseDown( display, 10, 10 );

assert.equal( pressCount, 1, '[3] Pressed once' );
assert.equal( releaseCount, 0, '[3] Has not been released yet' );
assert.equal( dragCount, 0, '[3] Has not been dragged yet' );
assert.equal( listener.isPressedProperty.value, true, '[3] Is pressed' );
assert.equal( listener.isOverProperty.value, true, '[3] Is over' );
assert.equal( listener.isHoveringProperty.value, true, '[3] Is hovering' );
assert.equal( listener.isHighlightedProperty.value, true, '[3] Is highlighted' );
assert.equal( listener.interrupted, false, '[3] Is not interrupted' );

assert.ok( listener.pressedTrail.lastNode() === rect, '[3] Dragging the proper rectangle' );

// A move that goes "outside" the node
ListenerTestUtils.mouseMove( display, 50, 10 );

assert.equal( pressCount, 1, '[4] Pressed once' );
assert.equal( releaseCount, 0, '[4] Has not been released yet' );
assert.equal( dragCount, 1, '[4] Dragged once' );
assert.equal( listener.isPressedProperty.value, true, '[4] Is pressed' );
assert.equal( listener.isOverProperty.value, false, '[4] Is NOT over anymore' );
assert.equal( listener.isHoveringProperty.value, false, '[4] Is NOT hovering' );
assert.equal( listener.isHighlightedProperty.value, true, '[4] Is highlighted' );
assert.equal( listener.interrupted, false, '[4] Is not interrupted' );

ListenerTestUtils.mouseUp( display, 50, 10 );

assert.equal( pressCount, 1, '[5] Pressed once' );
assert.equal( releaseCount, 1, '[5] Released once' );
assert.equal( dragCount, 1, '[5] Dragged once' );
assert.equal( listener.isPressedProperty.value, false, '[5] Is NOT pressed' );
assert.equal( listener.isOverProperty.value, false, '[5] Is NOT over anymore' );
assert.equal( listener.isHoveringProperty.value, false, '[5] Is NOT hovering' );
assert.equal( listener.isHighlightedProperty.value, false, '[5] Is NOT highlighted' );
assert.equal( listener.interrupted, false, '[5] Is not interrupted' );
} );
} );

QUnit.test( 'Interruption', function( assert ) {
ListenerTestUtils.simpleRectangleTest( function( display, rect, node ) {
var listener = new PressListener();
rect.addInputListener( listener );

ListenerTestUtils.mouseDown( display, 10, 10 );

assert.equal( listener.isPressedProperty.value, true, 'Is pressed before the interruption' );
listener.interrupt();
assert.equal( listener.isPressedProperty.value, false, 'Is NOT pressed after the interruption' );
} );
} );
} );
4 changes: 4 additions & 0 deletions js/scenery-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ define( function( require ) {
require( 'SCENERY/util/ColorTests' );
require( 'SCENERY/display/DisplayTests' );
require( 'SCENERY/display/FuzzTests' );
require( 'SCENERY/listeners/DragListenerTests' );
require( 'SCENERY/listeners/FireListenerTests' );
require( 'SCENERY/listeners/PressListenerTests' );
require( 'SCENERY/listeners/DragListenerTests' );
require( 'SCENERY/nodes/NodeTests' );
require( 'SCENERY/nodes/ShapeTests' );
require( 'SCENERY/util/FontTests' );
Expand Down

0 comments on commit c6c1a51

Please sign in to comment.