openseadragon/test/helpers/test.js

212 lines
7.5 KiB
JavaScript
Raw Normal View History

/* global $, QUnit, Util */
(function () {
2013-03-22 00:44:22 +04:00
// ----------
window.Util = {
// ----------
simulateViewerClickWithDrag: function ( args ) {
// args = { viewer, widthFactor, heightFactor, dragCount, dragDx, dragDy }
if ( args.hasOwnProperty( 'dragCount' ) ) {
args.dragDx = args.dragDx || 1;
args.dragDy = args.dragDy || 1;
2013-03-22 00:44:22 +04:00
}
else {
args.dragCount = 0;
}
if ( args.widthFactor === undefined ) {
args.widthFactor = 0.5;
}
//TODO Redefine to be the middle by default
if ( args.heightFactor === undefined ) {
args.heightFactor = 0.5;
}
args.widthFactor = Math.min( 1, Math.max( 0, args.widthFactor ) );
//TODO Fix this. The max height should be 1/AR
args.heightFactor = Math.min( 1, Math.max( 0, args.heightFactor ) );
var $canvas = $( args.viewer.element ).find( '.openseadragon-canvas' ).not( '.navigator .openseadragon-canvas' );
var offset = $canvas.offset();
var event = {
clientX: offset.left + Math.floor( $canvas.width() * args.widthFactor ),
clientY: offset.top + Math.floor( $canvas.height() * args.heightFactor )
};
$canvas
.simulate( 'mouseenter', event )
.simulate( 'mousedown', event );
for ( var i = 0; i < args.dragCount; i++ ) {
event.clientX += args.dragDx;
event.clientY += args.dragDy;
$canvas
.simulate( "mousemove", event );
}
$canvas
.simulate( 'mouseup', event )
.simulate( 'mouseleave', event );
},
2014-11-21 01:25:17 +03:00
// ----------
initializeTestDOM: function () {
$( "#qunit-fixture" )
.append( '<div><div id="example"></div><div id="exampleNavigator"></div></div>' )
.append( '<div id="wideexample"></div>' )
.append( '<div id="tallexample"></div>' );
},
2014-11-21 01:25:17 +03:00
// ----------
equalsWithVariance: function ( value1, value2, variance ) {
return Math.abs( value1 - value2 ) <= variance;
},
2014-11-21 01:25:17 +03:00
// ----------
2017-12-05 04:31:14 +03:00
assessNumericValue: function ( assert, value1, value2, variance, message ) {
assert.ok( Util.equalsWithVariance( value1, value2, variance ), message + " Expected:" + value1 + " Found: " + value2 + " Variance: " + variance );
},
// ----------
2017-12-05 04:31:14 +03:00
assertPointsEquals: function (assert, pointA, pointB, precision, message) {
Util.assessNumericValue(assert, pointA.x, pointB.x, precision, message + " x: ");
Util.assessNumericValue(assert, pointA.y, pointB.y, precision, message + " y: ");
},
// ----------
2017-12-05 04:31:14 +03:00
assertRectangleEquals: function (assert, rectA, rectB, precision, message) {
Util.assessNumericValue(assert, rectA.x, rectB.x, precision, message + " x: ");
Util.assessNumericValue(assert, rectA.y, rectB.y, precision, message + " y: ");
Util.assessNumericValue(assert, rectA.width, rectB.width, precision,
message + " width: ");
2017-12-05 04:31:14 +03:00
Util.assessNumericValue(assert, rectA.height, rectB.height, precision,
message + " height: ");
2017-12-05 04:31:14 +03:00
Util.assessNumericValue(assert, rectA.degrees, rectB.degrees, precision,
message + " degrees: ");
},
2014-11-21 01:25:17 +03:00
// ----------
timeWatcher: function ( assert, time ) {
var done = assert.async();
2013-05-10 22:53:49 +04:00
time = time || 2000;
var finished = false;
setTimeout( function () {
if ( !finished ) {
2013-05-10 22:53:49 +04:00
finished = true;
assert.ok( false, 'finishes in ' + time + 'ms' );
done();
2013-05-10 22:53:49 +04:00
}
}, time );
2013-05-10 22:53:49 +04:00
return {
done: function () {
if ( !finished ) {
2013-05-10 22:53:49 +04:00
finished = true;
done();
2013-05-10 22:53:49 +04:00
}
}
};
2014-11-21 01:25:17 +03:00
},
2014-11-21 01:25:17 +03:00
// ----------
spyOnce: function(obj, functionName, callback) {
var original = obj[functionName];
obj[functionName] = function() {
obj[functionName] = original;
var result = callback.apply(this, arguments);
if (result === undefined) {
result = original.apply(this, arguments);
}
return result;
};
2014-11-21 03:02:02 +03:00
},
// ----------
2017-12-07 04:20:39 +03:00
testDeprecation: function(assert, obj0, member0, obj1, member1) {
2014-11-21 03:02:02 +03:00
var called = false;
var errored = false;
if (obj1 && member1) {
this.spyOnce(obj1, member1, function() {
called = true;
return false;
});
} else {
called = true;
}
this.spyOnce(OpenSeadragon.console, 'error', function(message) {
if (/deprecated/.test(message)) {
errored = true;
}
});
obj0[member0]();
assert.equal(called, true, 'called through for ' + member0);
assert.equal(errored, true, 'errored for ' + member0);
2024-01-18 23:44:29 +03:00
},
2013-03-22 00:44:22 +04:00
};
// Log the name of the currently running test when it starts. Uses console.log rather than
// $.console.log so that the message is printed even after the $.console is diverted (see below).
QUnit.testStart((details) => {
console.log(`Starting test ${details.module}.${details.name}`);
});
2013-07-02 23:12:16 +04:00
/*
Test console log capture
1. Only the OpenSeadragon.console logger is touched
2. All log messages are stored in window.testLog in arrays keyed on the logger name (e.g. log,
warning, error, etc.) as JSON-serialized strings to simplify comparisons
3. The captured log arrays have a custom contains() method for ease of testing
4. testLog.reset() will clear all of the message arrays, intended for use in test setup routines
*/
2013-07-02 23:12:16 +04:00
var testConsole = window.testConsole = {},
testLog = window.testLog = {
log: [],
debug: [],
info: [],
warn: [],
error: [],
2013-07-02 23:12:16 +04:00
reset: function () {
for ( var i in testLog ) {
if ( testLog.hasOwnProperty( i ) && 'length' in testLog[i] && 'push' in testLog[i] ) {
2013-07-02 23:12:16 +04:00
testLog[i].length = 0;
}
}
}
};
for ( var i in testLog ) {
if ( testLog.hasOwnProperty( i ) && testLog[i].push ) {
testConsole[i] = ( function ( arr ) {
2013-07-02 23:12:16 +04:00
return function () {
var args = Array.prototype.slice.call( arguments, 0 ); // Coerce to true Array
arr.push( JSON.stringify( args ) ); // Store as JSON to avoid tedious array-equality tests
2013-07-02 23:12:16 +04:00
};
} )( testLog[i] );
2013-07-02 23:12:16 +04:00
testLog[i].contains = function ( needle ) {
for ( var i = 0; i < this.length; i++ ) {
if ( this[i] == needle ) {
return true;
}
}
return false;
2013-07-02 23:12:16 +04:00
};
}
}
2014-08-19 03:32:21 +04:00
testConsole.assert = function(condition, message) {
if (condition) {
testConsole.error(message);
}
};
2013-07-02 23:12:16 +04:00
OpenSeadragon.console = testConsole;
} )();