Update units

This commit is contained in:
nein09 2017-12-04 17:45:48 -08:00
parent 9f39f7bf03
commit 76669f8a65
2 changed files with 61 additions and 59 deletions

View File

@ -1,12 +1,12 @@
/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util, testLog */ /* global QUnit, $, Util, testLog */
(function () { (function () {
var viewer; var viewer;
var precision = 0.00000001; var precision = 0.00000001;
module('Units', { QUnit.module('Units', {
setup: function () { beforeEach: function () {
var example = $('<div id="unitsexample"></div>').appendTo("#qunit-fixture"); $('<div id="unitsexample"></div>').appendTo("#qunit-fixture");
testLog.reset(); testLog.reset();
@ -16,7 +16,7 @@
springStiffness: 100 // Faster animation = faster tests springStiffness: 100 // Faster animation = faster tests
}); });
}, },
teardown: function () { afterEach: function () {
if (viewer && viewer.close) { if (viewer && viewer.close) {
viewer.close(); viewer.close();
} }
@ -26,62 +26,62 @@
}); });
function pointEqual(a, b, message) { function pointEqual(assert, a, b, message) {
Util.assessNumericValue(a.x, b.x, precision, message); Util.assessNumericValue(assert, a.x, b.x, precision, message);
Util.assessNumericValue(a.y, b.y, precision, message); Util.assessNumericValue(assert, a.y, b.y, precision, message);
} }
// Check that f^-1 ( f(x) ) = x // Check that f^-1 ( f(x) ) = x
function checkPoint(context) { function checkPoint(assert, context) {
var viewport = viewer.viewport; var viewport = viewer.viewport;
var point = new OpenSeadragon.Point(15, 12); var point = new OpenSeadragon.Point(15, 12);
var result = viewport.viewerElementToImageCoordinates( var result = viewport.viewerElementToImageCoordinates(
viewport.imageToViewerElementCoordinates(point)); viewport.imageToViewerElementCoordinates(point));
pointEqual(result, point, 'viewerElement and image ' + context); pointEqual(assert, result, point, 'viewerElement and image ' + context);
result = viewport.windowToImageCoordinates( result = viewport.windowToImageCoordinates(
viewport.imageToWindowCoordinates(point)); viewport.imageToWindowCoordinates(point));
pointEqual(result, point, 'window and image ' + context); pointEqual(assert, result, point, 'window and image ' + context);
result = viewport.viewerElementToViewportCoordinates( result = viewport.viewerElementToViewportCoordinates(
viewport.viewportToViewerElementCoordinates(point)); viewport.viewportToViewerElementCoordinates(point));
pointEqual(result, point, 'viewerElement and viewport ' + context); pointEqual(assert, result, point, 'viewerElement and viewport ' + context);
result = viewport.windowToViewportCoordinates( result = viewport.windowToViewportCoordinates(
viewport.viewportToWindowCoordinates(point)); viewport.viewportToWindowCoordinates(point));
pointEqual(result, point, 'window and viewport ' + context); pointEqual(assert, result, point, 'window and viewport ' + context);
for (var i = 0; i < viewer.world.getItemCount(); i++) { for (var i = 0; i < viewer.world.getItemCount(); i++) {
var tiledImage = viewer.world.getItemAt(i); var tiledImage = viewer.world.getItemAt(i);
result = tiledImage.viewportToImageCoordinates( result = tiledImage.viewportToImageCoordinates(
tiledImage.imageToViewportCoordinates(point)); tiledImage.imageToViewportCoordinates(point));
pointEqual(result, point, 'viewport and tiled image ' + i + context); pointEqual(assert, result, point, 'viewport and tiled image ' + i + context);
result = tiledImage.viewerElementToImageCoordinates( result = tiledImage.viewerElementToImageCoordinates(
tiledImage.imageToViewerElementCoordinates(point)); tiledImage.imageToViewerElementCoordinates(point));
pointEqual(result, point, 'viewerElement and tiled image ' + i + context); pointEqual(assert, result, point, 'viewerElement and tiled image ' + i + context);
result = tiledImage.windowToImageCoordinates( result = tiledImage.windowToImageCoordinates(
tiledImage.imageToWindowCoordinates(point)); tiledImage.imageToWindowCoordinates(point));
pointEqual(result, point, 'window and tiled image ' + i + context); pointEqual(assert, result, point, 'window and tiled image ' + i + context);
} }
} }
// ---------- // ----------
asyncTest('Single image coordinates conversions', function () { QUnit.test('Single image coordinates conversions', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () { viewer.addHandler("open", function () {
var viewport = viewer.viewport; var viewport = viewer.viewport;
var tiledImage = viewer.world.getItemAt(0); var tiledImage = viewer.world.getItemAt(0);
var point0_0 = new OpenSeadragon.Point(0, 0); var point0_0 = new OpenSeadragon.Point(0, 0);
var point = viewport.viewerElementToViewportCoordinates(point0_0); var point = viewport.viewerElementToViewportCoordinates(point0_0);
pointEqual(point, point0_0, 'When opening, viewer coordinate 0,0 is also point 0,0'); pointEqual(assert, point, point0_0, 'When opening, viewer coordinate 0,0 is also point 0,0');
var viewportPixel = viewport.viewerElementToImageCoordinates(point0_0); var viewportPixel = viewport.viewerElementToImageCoordinates(point0_0);
pointEqual(viewportPixel, point0_0, 'When opening, viewer coordinate 0,0 is also viewport pixel 0,0'); pointEqual(assert, viewportPixel, point0_0, 'When opening, viewer coordinate 0,0 is also viewport pixel 0,0');
var imagePixel = tiledImage.viewerElementToImageCoordinates(point0_0); var imagePixel = tiledImage.viewerElementToImageCoordinates(point0_0);
pointEqual(imagePixel, point0_0, 'When opening, viewer coordinate 0,0 is also image pixel 0,0'); pointEqual(assert, imagePixel, point0_0, 'When opening, viewer coordinate 0,0 is also image pixel 0,0');
var viewerWidth = $(viewer.element).width(); var viewerWidth = $(viewer.element).width();
var imageWidth = viewer.source.dimensions.x; var imageWidth = viewer.source.dimensions.x;
@ -90,17 +90,17 @@
var imageTopRight = new OpenSeadragon.Point(imageWidth, 0); var imageTopRight = new OpenSeadragon.Point(imageWidth, 0);
point = viewport.viewerElementToViewportCoordinates(viewerTopRight); point = viewport.viewerElementToViewportCoordinates(viewerTopRight);
pointEqual(point, point1_0, 'Viewer top right has viewport coordinates 1,0.'); pointEqual(assert, point, point1_0, 'Viewer top right has viewport coordinates 1,0.');
viewportPixel = viewport.viewerElementToImageCoordinates(viewerTopRight); viewportPixel = viewport.viewerElementToImageCoordinates(viewerTopRight);
pointEqual(viewportPixel, imageTopRight, 'Viewer top right has viewport pixel coordinates imageWidth,0.'); pointEqual(assert, viewportPixel, imageTopRight, 'Viewer top right has viewport pixel coordinates imageWidth,0.');
imagePixel = tiledImage.viewerElementToImageCoordinates(viewerTopRight); imagePixel = tiledImage.viewerElementToImageCoordinates(viewerTopRight);
pointEqual(imagePixel, imageTopRight, 'Viewer top right has image pixel coordinates imageWidth,0.'); pointEqual(assert, imagePixel, imageTopRight, 'Viewer top right has image pixel coordinates imageWidth,0.');
checkPoint(' after opening'); checkPoint(assert, ' after opening');
viewer.addHandler('animation-finish', function animationHandler() { viewer.addHandler('animation-finish', function animationHandler() {
viewer.removeHandler('animation-finish', animationHandler); viewer.removeHandler('animation-finish', animationHandler);
checkPoint(' after zoom and pan'); checkPoint(assert, ' after zoom and pan');
start(); done();
}); });
viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2)); viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2));
}); });
@ -109,8 +109,8 @@
// --------- // ---------
asyncTest('Multiple images coordinates conversion', function () { QUnit.test('Multiple images coordinates conversion', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () { viewer.addHandler("open", function () {
var viewport = viewer.viewport; var viewport = viewer.viewport;
var tiledImage1 = viewer.world.getItemAt(0); var tiledImage1 = viewer.world.getItemAt(0);
@ -120,11 +120,11 @@
var point0_0 = new OpenSeadragon.Point(0, 0); var point0_0 = new OpenSeadragon.Point(0, 0);
var point = viewport.viewerElementToViewportCoordinates(point0_0); var point = viewport.viewerElementToViewportCoordinates(point0_0);
pointEqual(point, point0_0, 'When opening, viewer coordinate 0,0 is also point 0,0'); pointEqual(assert, point, point0_0, 'When opening, viewer coordinate 0,0 is also point 0,0');
var image1Pixel = tiledImage1.viewerElementToImageCoordinates(point0_0); var image1Pixel = tiledImage1.viewerElementToImageCoordinates(point0_0);
pointEqual(image1Pixel, point0_0, 'When opening, viewer coordinate 0,0 is also image 1 pixel 0,0'); pointEqual(assert, image1Pixel, point0_0, 'When opening, viewer coordinate 0,0 is also image 1 pixel 0,0');
var image2Pixel = tiledImage2.viewerElementToImageCoordinates(point0_0); var image2Pixel = tiledImage2.viewerElementToImageCoordinates(point0_0);
pointEqual(image2Pixel, pointEqual(assert, image2Pixel,
new OpenSeadragon.Point(-2 * imageWidth, -2 * imageHeight), new OpenSeadragon.Point(-2 * imageWidth, -2 * imageHeight),
'When opening, viewer coordinates 0,0 is also image 2 pixel -2*imageWidth, -2*imageHeight'); 'When opening, viewer coordinates 0,0 is also image 2 pixel -2*imageWidth, -2*imageHeight');
@ -133,23 +133,23 @@
var viewerBottomRight = new OpenSeadragon.Point(viewerWidth, viewerHeight); var viewerBottomRight = new OpenSeadragon.Point(viewerWidth, viewerHeight);
point = viewport.viewerElementToViewportCoordinates(viewerBottomRight); point = viewport.viewerElementToViewportCoordinates(viewerBottomRight);
pointEqual(point, new OpenSeadragon.Point(1.5, 1.5), pointEqual(assert, point, new OpenSeadragon.Point(1.5, 1.5),
'Viewer bottom right has viewport coordinates 1.5,1.5.'); 'Viewer bottom right has viewport coordinates 1.5,1.5.');
image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerBottomRight); image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerBottomRight);
pointEqual(image1Pixel, pointEqual(assert, image1Pixel,
new OpenSeadragon.Point(imageWidth * 1.5, imageHeight * 1.5), new OpenSeadragon.Point(imageWidth * 1.5, imageHeight * 1.5),
'Viewer bottom right has image 1 pixel coordinates imageWidth * 1.5, imageHeight * 1.5'); 'Viewer bottom right has image 1 pixel coordinates imageWidth * 1.5, imageHeight * 1.5');
image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerBottomRight); image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerBottomRight);
pointEqual(image2Pixel, pointEqual(assert, image2Pixel,
new OpenSeadragon.Point(imageWidth, imageHeight), new OpenSeadragon.Point(imageWidth, imageHeight),
'Viewer bottom right has image 2 pixel coordinates imageWidth,imageHeight.'); 'Viewer bottom right has image 2 pixel coordinates imageWidth,imageHeight.');
checkPoint(' after opening'); checkPoint(assert, ' after opening');
viewer.addHandler('animation-finish', function animationHandler() { viewer.addHandler('animation-finish', function animationHandler() {
viewer.removeHandler('animation-finish', animationHandler); viewer.removeHandler('animation-finish', animationHandler);
checkPoint(' after zoom and pan'); checkPoint(assert, ' after zoom and pan');
start(); done();
}); });
viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2)); viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2));
}); });
@ -167,8 +167,8 @@
// --------- // ---------
asyncTest('Multiple images coordinates conversion with viewport rotation', function () { QUnit.test('Multiple images coordinates conversion with viewport rotation', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () { viewer.addHandler("open", function () {
var viewport = viewer.viewport; var viewport = viewer.viewport;
var tiledImage1 = viewer.world.getItemAt(0); var tiledImage1 = viewer.world.getItemAt(0);
@ -183,35 +183,35 @@
var point0_0 = new OpenSeadragon.Point(0, 0); var point0_0 = new OpenSeadragon.Point(0, 0);
var point = viewport.viewerElementToViewportCoordinates(viewerMiddleTop); var point = viewport.viewerElementToViewportCoordinates(viewerMiddleTop);
pointEqual(point, point0_0, 'When opening, viewer middle top is also viewport 0,0'); pointEqual(assert, point, point0_0, 'When opening, viewer middle top is also viewport 0,0');
var image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerMiddleTop); var image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerMiddleTop);
pointEqual(image1Pixel, point0_0, 'When opening, viewer middle top is also image 1 pixel 0,0'); pointEqual(assert, image1Pixel, point0_0, 'When opening, viewer middle top is also image 1 pixel 0,0');
var image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerMiddleTop); var image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerMiddleTop);
pointEqual(image2Pixel, pointEqual(assert, image2Pixel,
new OpenSeadragon.Point(-2 * imageWidth, -2 * imageHeight), new OpenSeadragon.Point(-2 * imageWidth, -2 * imageHeight),
'When opening, viewer middle top is also image 2 pixel -2*imageWidth, -2*imageHeight'); 'When opening, viewer middle top is also image 2 pixel -2*imageWidth, -2*imageHeight');
point = viewport.viewerElementToViewportCoordinates(viewerMiddleBottom); point = viewport.viewerElementToViewportCoordinates(viewerMiddleBottom);
pointEqual(point, new OpenSeadragon.Point(1.5, 1.5), pointEqual(assert, point, new OpenSeadragon.Point(1.5, 1.5),
'Viewer middle bottom has viewport coordinates 1.5,1.5.'); 'Viewer middle bottom has viewport coordinates 1.5,1.5.');
image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerMiddleBottom); image1Pixel = tiledImage1.viewerElementToImageCoordinates(viewerMiddleBottom);
pointEqual(image1Pixel, pointEqual(assert, image1Pixel,
new OpenSeadragon.Point(imageWidth * 1.5, imageHeight * 1.5), new OpenSeadragon.Point(imageWidth * 1.5, imageHeight * 1.5),
'Viewer middle bottom has image 1 pixel coordinates imageWidth * 1.5, imageHeight * 1.5'); 'Viewer middle bottom has image 1 pixel coordinates imageWidth * 1.5, imageHeight * 1.5');
image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerMiddleBottom); image2Pixel = tiledImage2.viewerElementToImageCoordinates(viewerMiddleBottom);
pointEqual(image2Pixel, pointEqual(assert, image2Pixel,
new OpenSeadragon.Point(imageWidth, imageHeight), new OpenSeadragon.Point(imageWidth, imageHeight),
'Viewer middle bottom has image 2 pixel coordinates imageWidth,imageHeight.'); 'Viewer middle bottom has image 2 pixel coordinates imageWidth,imageHeight.');
checkPoint(' after opening'); checkPoint(assert, ' after opening');
viewer.addHandler('animation-finish', function animationHandler() { viewer.addHandler('animation-finish', function animationHandler() {
viewer.removeHandler('animation-finish', animationHandler); viewer.removeHandler('animation-finish', animationHandler);
checkPoint(' after zoom and pan'); checkPoint(assert, ' after zoom and pan');
//Restore rotation //Restore rotation
viewer.viewport.setRotation(0); viewer.viewport.setRotation(0);
start(); done();
}); });
viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2)); viewer.viewport.zoomTo(0.8).panTo(new OpenSeadragon.Point(0.1, 0.2));
}); });
@ -229,7 +229,8 @@
}); });
// ---------- // ----------
asyncTest('ZoomRatio 1 image', function () { QUnit.test('ZoomRatio 1 image', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () { viewer.addHandler("open", function () {
var viewport = viewer.viewport; var viewport = viewer.viewport;
@ -249,10 +250,10 @@
var expectedViewportZoom = viewport.getZoom(true); var expectedViewportZoom = viewport.getZoom(true);
var actualImageZoom = viewport.viewportToImageZoom( var actualImageZoom = viewport.viewportToImageZoom(
expectedViewportZoom); expectedViewportZoom);
Util.assessNumericValue(actualImageZoom, expectedImageZoom, precision); Util.assessNumericValue(assert, actualImageZoom, expectedImageZoom, precision);
var actualViewportZoom = viewport.imageToViewportZoom(actualImageZoom); var actualViewportZoom = viewport.imageToViewportZoom(actualImageZoom);
Util.assessNumericValue(actualViewportZoom, expectedViewportZoom, precision); Util.assessNumericValue(assert, actualViewportZoom, expectedViewportZoom, precision);
} }
checkZoom(); checkZoom();
@ -260,7 +261,7 @@
var zoomHandler = function () { var zoomHandler = function () {
viewer.removeHandler('animation-finish', zoomHandler); viewer.removeHandler('animation-finish', zoomHandler);
checkZoom(); checkZoom();
start(); done();
}; };
viewer.addHandler('animation-finish', zoomHandler); viewer.addHandler('animation-finish', zoomHandler);
@ -271,7 +272,8 @@
}); });
// ---------- // ----------
asyncTest('ZoomRatio 2 images', function () { QUnit.test('ZoomRatio 2 images', function (assert) {
var done = assert.async();
viewer.addHandler("open", function () { viewer.addHandler("open", function () {
var viewport = viewer.viewport; var viewport = viewer.viewport;
@ -294,12 +296,12 @@
var expectedViewportZoom = viewport.getZoom(true); var expectedViewportZoom = viewport.getZoom(true);
var actualImageZoom = image.viewportToImageZoom( var actualImageZoom = image.viewportToImageZoom(
expectedViewportZoom); expectedViewportZoom);
Util.assessNumericValue(actualImageZoom, expectedImageZoom, Util.assessNumericValue(assert, actualImageZoom, expectedImageZoom,
precision); precision);
var actualViewportImage1Zoom = image.imageToViewportZoom(actualImageZoom); var actualViewportImage1Zoom = image.imageToViewportZoom(actualImageZoom);
Util.assessNumericValue( Util.assessNumericValue(
actualViewportImage1Zoom, expectedViewportZoom, precision); assert, actualViewportImage1Zoom, expectedViewportZoom, precision);
} }
checkZoom(image1); checkZoom(image1);
@ -309,7 +311,7 @@
viewer.removeHandler('animation-finish', zoomHandler); viewer.removeHandler('animation-finish', zoomHandler);
checkZoom(image1); checkZoom(image1);
checkZoom(image2); checkZoom(image2);
start(); done();
}; };
viewer.addHandler('animation-finish', zoomHandler); viewer.addHandler('animation-finish', zoomHandler);

View File

@ -26,9 +26,9 @@
<script src="/test/modules/strings.js"></script> <script src="/test/modules/strings.js"></script>
<script src="/test/modules/formats.js"></script> <script src="/test/modules/formats.js"></script>
<script src="/test/modules/utils.js"></script> <script src="/test/modules/utils.js"></script>
<!--<script src="/test/modules/events.js"></script> <script src="/test/modules/events.js"></script>
<script src="/test/modules/units.js"></script> <script src="/test/modules/units.js"></script>
<script src="/test/modules/multi-image.js"></script> <!--<script src="/test/modules/multi-image.js"></script>
<script src="/test/modules/overlays.js"></script> <script src="/test/modules/overlays.js"></script>
<script src="/test/modules/controls.js"></script> <script src="/test/modules/controls.js"></script>
<script src="/test/modules/viewport.js"></script> <script src="/test/modules/viewport.js"></script>