diff --git a/test/events.js b/test/events.js index 31fa9dd1..92f1cd81 100644 --- a/test/events.js +++ b/test/events.js @@ -5,12 +5,12 @@ module( 'Events', { setup: function () { - var example = $( '
' ).appendTo( "#qunit-fixture" ); + var example = $( '' ).appendTo( "#qunit-fixture" ); testLog.reset(); viewer = OpenSeadragon( { - id: 'example', + id: 'eventsexample', prefixUrl: '/build/openseadragon/images/', springStiffness: 100 // Faster animation = faster tests } ); @@ -60,4 +60,42 @@ viewer.open( '/test/data/testpattern.dzi' ); } ); + // ---------- + asyncTest( 'canvas-drag canvas-release canvas-click', function () { + var dragMoves = 10; + var dragMovesHandled = 0; + + var openHandler = function ( eventSender, eventData ) { + viewer.removeHandler( 'open', openHandler ); + + viewer.addHandler( 'canvas-drag', canvasDragHandler ); + viewer.addHandler( 'canvas-release', canvasReleaseHandler ); + viewer.addHandler( 'canvas-click', canvasClickHandler ); + + Util.simulateViewerDrag( viewer, 0.25, 0.25, 1, 1, dragMoves ); + }; + + var canvasDragHandler = function ( eventSender, eventData ) { + dragMovesHandled += 1; + ok( true, 'canvas-drag event handled' ); + }; + + var canvasReleaseHandler = function ( eventSender, eventData ) { + ok( true, 'canvas-release event handled' ); + }; + + var canvasClickHandler = function ( eventSender, eventData ) { + viewer.removeHandler( 'canvas-drag', canvasDragHandler ); + viewer.removeHandler( 'canvas-release', canvasReleaseHandler ); + viewer.removeHandler( 'canvas-click', canvasClickHandler ); + ok( true, 'canvas-click event handled' ); + equal( dragMovesHandled, dragMoves, 'canvas-drag event count matches mousemove count' ); + viewer.close(); + start(); + }; + + viewer.addHandler( 'open', openHandler ); + viewer.open( '/test/data/testpattern.dzi' ); + } ); + } )(); diff --git a/test/test.js b/test/test.js index 37b7a075..a3696998 100644 --- a/test/test.js +++ b/test/test.js @@ -1,67 +1,109 @@ /* global module, asyncTest, $, ok, equal, notEqual, start, test, Util */ -(function() { +(function () { // ---------- window.Util = { // ---------- - simulateViewerClick: function(viewer, widthFactor, heightFactor) { - if (widthFactor === undefined) { + simulateViewerClick: function ( viewer, widthFactor, heightFactor ) { + if ( widthFactor === undefined ) { widthFactor = 0.5; } //TODO Redefine to be the middle by default - if (heightFactor === undefined) { + if ( heightFactor === undefined ) { heightFactor = 0.5; } - widthFactor = Math.min(1, Math.max(0, widthFactor)); + widthFactor = Math.min( 1, Math.max( 0, widthFactor ) ); //TODO Fix this. The max height should be 1/AR - heightFactor = Math.min(1, Math.max(0, heightFactor)); + heightFactor = Math.min( 1, Math.max( 0, heightFactor ) ); - var $canvas = $(viewer.element).find('.openseadragon-canvas').not('.navigator .openseadragon-canvas'); + var $canvas = $( viewer.element ).find( '.openseadragon-canvas' ).not( '.navigator .openseadragon-canvas' ); var offset = $canvas.offset(); var event = { - clientX: offset.left + Math.floor($canvas.width() * widthFactor), - clientY: offset.top + Math.floor($canvas.height() * heightFactor) + clientX: offset.left + Math.floor( $canvas.width() * widthFactor ), + clientY: offset.top + Math.floor( $canvas.height() * heightFactor ) }; $canvas - .simulate('mouseover', event) - .simulate('mousedown', event) - .simulate('mouseup', event); + .simulate( 'mouseover', event ) + .simulate( 'mousedown', event ) + .simulate( 'mouseup', event ); + }, + + simulateViewerDrag: function ( viewer, widthFactor, heightFactor, dx, dy, movecount ) { + dx = dx || 1; + dy = dy || 1; + + movecount = movecount || 5; + if ( movecount < 1 ) { + movecount = 1; + } + + if ( widthFactor === undefined ) { + widthFactor = 0.5; + } + + //TODO Redefine to be the middle by default + if ( heightFactor === undefined ) { + heightFactor = 0.5; + } + + widthFactor = Math.min( 1, Math.max( 0, widthFactor ) ); + //TODO Fix this. The max height should be 1/AR + heightFactor = Math.min( 1, Math.max( 0, heightFactor ) ); + + var $canvas = $( viewer.element ).find( '.openseadragon-canvas' ).not( '.navigator .openseadragon-canvas' ); + var offset = $canvas.offset(); + var event = { + clientX: offset.left + Math.floor( $canvas.width() * widthFactor ), + clientY: offset.top + Math.floor( $canvas.height() * heightFactor ) + }; + + $canvas + .simulate( 'mouseover', event ) + .simulate( 'mousedown', event ); + for ( var i = 0; i < movecount; i++ ) { + event.clientX += dx; + event.clientY += dy; + $canvas + .simulate( "mousemove", event ); + } + $canvas + .simulate( 'mouseup', event ); }, initializeTestDOM: function () { - $("#qunit-fixture") - .append('