From fd380afe7f81dd2b1caf3709753b9cd4c52f3129 Mon Sep 17 00:00:00 2001 From: nein09 Date: Wed, 29 Oct 2014 21:10:15 -0700 Subject: [PATCH 01/10] Add tests for pixel <-> Point and viewport coordinates <-> image coordinates. Still working out the fine points of the target location. --- test/viewport.js | 113 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 2daabc68..fbf63fe9 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -57,10 +57,13 @@ viewport.zoomTo(ZOOM_FACTOR, null, true); // do stuff here - var orig = ; - var expected = ; - var actual = ; - equal(expected, actual, "what are you testing"); + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = ; + expected = ; + actual = ; + propEqual(actual, expected, "message " + orig); + } start(); }; @@ -68,6 +71,108 @@ viewer.open(DZI_PATH); }); */ + asyncTest('pixelFromPoint', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = testPoints[i].times(viewer.source.dimensions.x); + // todo: wtf is up with this magic point? + expected = orig.plus(new OpenSeadragon.Point(0.25, 0.25)).times(viewport.getContainerSize().x * ZOOM_FACTOR).minus(viewport.getContainerSize()); + actual = viewport.pixelFromPoint(orig, false); + propEqual( + actual, + expected, + "Correctly converted coordinates with current = false " + orig + ); + expected_current = orig.times(viewport.getContainerSize().x); + actual_current = viewport.pixelFromPoint(orig, true); + propEqual( + actual_current, + expected_current, + "Correctly converted coordinates with current = true " + orig + ); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + + asyncTest('pointFromPixel', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = testPoints[i].times(viewport.getContainerSize().x); + // todo: what is up with this magic point? + expected = orig.divide(viewer.source.dimensions.x).plus(new OpenSeadragon.Point(0.25, 0.25)); + actual = viewport.pointFromPixel(orig, false); + propEqual( + actual, + expected, + "Correctly converted coordinates with current = false " + orig + ); + expected_current = orig.divide(viewer.source.dimensions.x / ZOOM_FACTOR); + actual_current = viewport.pointFromPixel(orig, true); + propEqual( + actual_current, + expected_current, + "Correctly converted coordinates with current = true " + orig + ); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + asyncTest('viewportToImageCoordinates', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = testPoints[i].times(viewport.getContainerSize().x); + expected = orig.divide(viewer.source.dimensions.x); + actual = viewport.imageToViewportCoordinates(orig); + propEqual(actual, expected, "Coordinates converted correctly for " + orig); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + + asyncTest('imageToViewportCoordinates', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = testPoints[i].times(viewer.source.dimensions.x); + expected = orig.divide(ZOOM_FACTOR * viewport.getContainerSize().x); + actual = viewport.imageToViewportCoordinates(orig); + propEqual(actual, expected, "Coordinates converted correctly for " + orig); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); asyncTest('imageToViewportRectangle', function() { var openHandler = function(event) { viewer.removeHandler('open', openHandler); From 06e970bbc3952a566995cd1b0d6c439ca023ed65 Mon Sep 17 00:00:00 2001 From: nein09 Date: Thu, 6 Nov 2014 18:30:24 -0800 Subject: [PATCH 02/10] Last of the tests for pixel/point/rect conversion. --- test/viewport.js | 53 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index fbf63fe9..0253e112 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -56,7 +56,6 @@ var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - // do stuff here var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = ; @@ -71,6 +70,54 @@ viewer.open(DZI_PATH); }); */ + asyncTest('deltaPixelsFromPoints', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = testPoints[i].times(viewer.source.dimensions.x); + expected = orig.times(viewport.getContainerSize().x * ZOOM_FACTOR); + actual = viewport.deltaPixelsFromPoints(orig); + propEqual(actual, expected, "Correctly got delta pixels from points with current = false " + orig); + + expected_current = orig.times(viewport.getContainerSize().x); + actual_current = viewport.deltaPixelsFromPoints(orig, true); + propEqual(actual_current, expected_current, "Correctly got delta pixels from points with current = true " + orig); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + + asyncTest('deltaPointsFromPixels', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + var orig, expected, actual; + for (var i = 0; i < testPoints.length; i++){ + orig = testPoints[i].times(viewport.getContainerSize().x); + + expected = orig.divide(viewport.getContainerSize().x * ZOOM_FACTOR); + actual = viewport.deltaPointsFromPixels(orig); + propEqual(actual, expected, "Correctly got delta points from pixels with current = false " + orig); + + expected_current = orig.divide(viewport.getContainerSize().x); + actual_current = viewport.deltaPointsFromPixels(orig, true); + propEqual(actual_current, expected_current, "Correctly got delta points from pixels with current = true " + orig); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); asyncTest('pixelFromPoint', function() { var openHandler = function(event) { viewer.removeHandler('open', openHandler); @@ -80,7 +127,7 @@ var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); - // todo: wtf is up with this magic point? + // todo: this magic point is something to do with the target location but I don't entirely understand why it's here expected = orig.plus(new OpenSeadragon.Point(0.25, 0.25)).times(viewport.getContainerSize().x * ZOOM_FACTOR).minus(viewport.getContainerSize()); actual = viewport.pixelFromPoint(orig, false); propEqual( @@ -112,7 +159,7 @@ var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewport.getContainerSize().x); - // todo: what is up with this magic point? + // todo: this magic point is something to do with the target location but I don't entirely understand why it's here expected = orig.divide(viewer.source.dimensions.x).plus(new OpenSeadragon.Point(0.25, 0.25)); actual = viewport.pointFromPixel(orig, false); propEqual( From f765ab9933ea9b75ac0faf924f9d480d10ddf171 Mon Sep 17 00:00:00 2001 From: nein09 Date: Thu, 13 Nov 2014 08:55:15 -0800 Subject: [PATCH 03/10] min and max zoom level tests --- test/viewport.js | 217 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 208 insertions(+), 9 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 0253e112..643a9841 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -29,6 +29,7 @@ var ZOOM_FACTOR = 2; // the image will be twice as large as the viewer. var VIEWER_PADDING = new OpenSeadragon.Point(20, 20); var DZI_PATH = '/test/data/testpattern.dzi' + var TALL_PATH = '/test/data/tall.dzi' var testZoomLevels = [-1, 0, 0.1, 0.5, 4, 10]; @@ -56,21 +57,219 @@ var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = ; - expected = ; - actual = ; - propEqual(actual, expected, "message " + orig); - } - start(); }; viewer.addHandler('open', openHandler); viewer.open(DZI_PATH); }); */ - asyncTest('deltaPixelsFromPoints', function() { + asyncTest('getMinZoomDefault', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + equal(viewport.getMinZoom(), .9, "Test min zoom level") + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + + asyncTest('getMaxZoomDefault', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + equal(viewport.getMaxZoom(), 2.2, "Test max zoom level") + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + + asyncTest('getMinZoom', function() { + var expected, level, i = 0; + var openHandler = function(event) { + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + if(level == 0) { // 0 means use the default + expected = 0.9; + } + else if(level > 1){ + expected = 1; // min zoom won't go bigger than 1. + } + + equal( + viewport.getMinZoom(), + expected, + "Test getMinZoom with zoom level of " + level + ); + i++; + if(i < testZoomLevels.length){ + level = testZoomLevels[i]; + viewer.minZoomLevel = level; + expected = level; + viewer.open(DZI_PATH); + } + else { + viewer.removeHandler('open', openHandler); + start(); + } + }; + + viewer.addHandler('open', openHandler); + level = testZoomLevels[i]; + viewer.minZoomLevel = level; + expected = level; + viewer.open(DZI_PATH); + }); + + asyncTest('getMaxZoom', function() { + var expected, level, i = 0; + var openHandler = function(event) { + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + if(level == 0){ // 0 means use the default + expected = 2.2; + } + else if(level < 1){ + expected = 1; // max zoom won't go smaller than 1. + } + + equal( + viewport.getMaxZoom(), + expected, + "Test getMaxZoom with zoom level of " + level + ); + i++; + if(i < testZoomLevels.length){ + level = testZoomLevels[i]; + viewer.maxZoomLevel = level; + expected = level; + viewer.open(DZI_PATH); + } + else { + viewer.removeHandler('open', openHandler); + start(); + } + }; + + viewer.addHandler('open', openHandler); + level = testZoomLevels[i]; + viewer.maxZoomLevel = level; + expected = level; + viewer.open(DZI_PATH); + }); + + asyncTest('getHomeBounds', function() { + var expected, i = 0; + var openHandler = function(event) { + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + // Have to special case this to avoid dividing by 0 + if(testZoomLevels[i] == 0){ + expected = new OpenSeadragon.Rect(0, 0, 1, 1); + } + else{ + var sideLength = 1.0 / viewer.defaultZoomLevel; // it's a square in this case + var position = 0.5 - (sideLength / 2.0); + expected = new OpenSeadragon.Rect(position, position, sideLength, sideLength); + } + propEqual( + viewport.getHomeBounds(), + expected, + "Test getHomeBounds with default zoom level of " + viewer.defaultZoomLevel + ); + i++; + if(i < testZoomLevels.length){ + viewer.defaultZoomLevel = testZoomLevels[i]; + viewer.open(DZI_PATH); + } + else { + viewer.removeHandler('open', openHandler); + start(); + } + }; + + viewer.addHandler('open', openHandler); + viewer.defaultZoomLevel = testZoomLevels[i]; + viewer.open(DZI_PATH); + }); + + asyncTest('getHomeZoom', function() { + var expected, i = 0; + var openHandler = function(event) { + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + // If the default zoom level is set to 0, then we expect the home zoom to be 1. + if(expected == 0){ + expected = 1; + } + equal( + viewport.getHomeZoom(), + expected, + "Test getHomeZoom with default zoom level of " + expected + ); + i++; + if(i < testZoomLevels.length){ + expected = testZoomLevels[i]; + viewer.defaultZoomLevel = expected; + viewer.open(DZI_PATH); + } + else { + viewer.removeHandler('open', openHandler); + start(); + } + }; + + viewer.addHandler('open', openHandler); + expected = testZoomLevels[i]; + viewer.defaultZoomLevel = expected; + viewer.open(DZI_PATH); + }); + + asyncTest('getHomeZoomWithHomeFillsViewer', function() { + var expected, i = 0; + var openHandler = function(event) { + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + // If the default zoom level is set to 0, then we expect the home zoom to be 1. + if(expected == 0){ + expected = 1; + } + + equal( + viewport.getHomeZoom(), + expected, + "Test getHomeZoom with homeFillsViewer = true and default zoom level of " + expected + ); + i++; + if(i < testZoomLevels.length){ + expected = testZoomLevels[i]; + viewer.defaultZoomLevel = expected; + viewer.open(TALL_PATH); // use a different image for homeFillsViewer + } + else { + viewer.removeHandler('open', openHandler); + start(); + } + }; + + viewer.addHandler('open', openHandler); + expected = testZoomLevels[i]; + viewer.homeFillsViewer = true; + viewer.defaultZoomLevel = expected; + viewer.open(TALL_PATH); // use a different image for homeFillsViewer + }); + + asyncTest('deltaPixelsFromPoints', function() { var openHandler = function(event) { viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; From ca7f79c896f07e232babce6de4af041fb877fa67 Mon Sep 17 00:00:00 2001 From: nein09 Date: Thu, 13 Nov 2014 09:04:11 -0800 Subject: [PATCH 04/10] Aspect ratio test --- test/viewport.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 643a9841..679ac3cf 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -63,13 +63,26 @@ viewer.open(DZI_PATH); }); */ + asyncTest('getAspectRatio', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + equal(viewport.getAspectRatio(), 1, "Test aspect ratio") + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + asyncTest('getMinZoomDefault', function() { var openHandler = function(event) { viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - equal(viewport.getMinZoom(), .9, "Test min zoom level") + equal(viewport.getMinZoom(), .9, "Test default min zoom level") start(); }; viewer.addHandler('open', openHandler); @@ -82,7 +95,7 @@ var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - equal(viewport.getMaxZoom(), 2.2, "Test max zoom level") + equal(viewport.getMaxZoom(), 2.2, "Test default max zoom level") start(); }; viewer.addHandler('open', openHandler); From 20711c485b1c96d7c848023b66b68a7d8ed79d59 Mon Sep 17 00:00:00 2001 From: nein09 Date: Thu, 13 Nov 2014 17:19:36 -0800 Subject: [PATCH 05/10] getContainerSize test --- test/viewport.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/viewport.js b/test/viewport.js index 679ac3cf..4df19bbf 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -63,6 +63,19 @@ viewer.open(DZI_PATH); }); */ + asyncTest('getContainerSize', function() { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + + propEqual(viewport.getContainerSize(), new OpenSeadragon.Point(500, 500), "Test container size") + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }); + asyncTest('getAspectRatio', function() { var openHandler = function(event) { viewer.removeHandler('open', openHandler); From 614261876d16f56164fa5a1a7ef6f68607e42273 Mon Sep 17 00:00:00 2001 From: nein09 Date: Tue, 18 Nov 2014 13:05:58 -0800 Subject: [PATCH 06/10] Incorporate code review feedback - instantiate the viewer for looping ZoomLevel tests, change a few little syntax things --- test/viewport.js | 113 ++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 50 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 4df19bbf..8602cfd4 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -2,6 +2,9 @@ (function () { var viewer; + var VIEWER_ID = "example"; + var PREFIX_URL = "/build/openseadragon/images"; + var SPRING_STIFFNESS = 100; // Faster animation = faster tests module("viewport", { setup: function () { @@ -10,9 +13,9 @@ testLog.reset(); viewer = OpenSeadragon({ - id: 'example', - prefixUrl: '/build/openseadragon/images/', - springStiffness: 100 // Faster animation = faster tests + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS }); }, teardown: function () { @@ -93,7 +96,6 @@ var openHandler = function(event) { viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); equal(viewport.getMinZoom(), .9, "Test default min zoom level") start(); @@ -106,7 +108,6 @@ var openHandler = function(event) { viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); equal(viewport.getMaxZoom(), 2.2, "Test default max zoom level") start(); @@ -118,13 +119,12 @@ asyncTest('getMinZoom', function() { var expected, level, i = 0; var openHandler = function(event) { + viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); if(level == 0) { // 0 means use the default expected = 0.9; - } - else if(level > 1){ + } else if(level > 1){ expected = 1; // min zoom won't go bigger than 1. } @@ -135,34 +135,34 @@ ); i++; if(i < testZoomLevels.length){ - level = testZoomLevels[i]; - viewer.minZoomLevel = level; - expected = level; + level = expected = testZoomLevels[i]; + viewer = OpenSeadragon({ + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS, + minZoomLevel: level + }); + viewer.addHandler('open', openHandler); viewer.open(DZI_PATH); - } - else { - viewer.removeHandler('open', openHandler); + } else { start(); } }; - viewer.addHandler('open', openHandler); - level = testZoomLevels[i]; + level = expected = testZoomLevels[i]; viewer.minZoomLevel = level; - expected = level; viewer.open(DZI_PATH); }); asyncTest('getMaxZoom', function() { var expected, level, i = 0; var openHandler = function(event) { + viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); if(level == 0){ // 0 means use the default expected = 2.2; - } - else if(level < 1){ + } else if(level < 1){ expected = 1; // max zoom won't go smaller than 1. } @@ -173,35 +173,36 @@ ); i++; if(i < testZoomLevels.length){ - level = testZoomLevels[i]; - viewer.maxZoomLevel = level; - expected = level; + level = expected = testZoomLevels[i]; + viewer = OpenSeadragon({ + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS, + maxZoomLevel: level + }); + viewer.addHandler('open', openHandler); viewer.open(DZI_PATH); - } - else { - viewer.removeHandler('open', openHandler); + } else { start(); } }; - viewer.addHandler('open', openHandler); - level = testZoomLevels[i]; + level = expected = testZoomLevels[i]; viewer.maxZoomLevel = level; - expected = level; viewer.open(DZI_PATH); }); asyncTest('getHomeBounds', function() { var expected, i = 0; var openHandler = function(event) { + viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); // Have to special case this to avoid dividing by 0 if(testZoomLevels[i] == 0){ expected = new OpenSeadragon.Rect(0, 0, 1, 1); - } - else{ + } else { var sideLength = 1.0 / viewer.defaultZoomLevel; // it's a square in this case var position = 0.5 - (sideLength / 2.0); expected = new OpenSeadragon.Rect(position, position, sideLength, sideLength); @@ -213,15 +214,18 @@ ); i++; if(i < testZoomLevels.length){ - viewer.defaultZoomLevel = testZoomLevels[i]; + viewer = OpenSeadragon({ + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS, + defaultZoomLevel: testZoomLevels[i] + }); + viewer.addHandler('open', openHandler); viewer.open(DZI_PATH); - } - else { - viewer.removeHandler('open', openHandler); + } else { start(); } }; - viewer.addHandler('open', openHandler); viewer.defaultZoomLevel = testZoomLevels[i]; viewer.open(DZI_PATH); @@ -230,6 +234,7 @@ asyncTest('getHomeZoom', function() { var expected, i = 0; var openHandler = function(event) { + viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); @@ -245,15 +250,18 @@ i++; if(i < testZoomLevels.length){ expected = testZoomLevels[i]; - viewer.defaultZoomLevel = expected; + viewer = OpenSeadragon({ + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS, + defaultZoomLevel: expected + }); + viewer.addHandler('open', openHandler); viewer.open(DZI_PATH); - } - else { - viewer.removeHandler('open', openHandler); + } else { start(); } }; - viewer.addHandler('open', openHandler); expected = testZoomLevels[i]; viewer.defaultZoomLevel = expected; @@ -261,13 +269,14 @@ }); asyncTest('getHomeZoomWithHomeFillsViewer', function() { - var expected, i = 0; + var expected, level, i = 0; var openHandler = function(event) { + viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); // If the default zoom level is set to 0, then we expect the home zoom to be 1. - if(expected == 0){ + if(level == 0){ expected = 1; } @@ -278,18 +287,22 @@ ); i++; if(i < testZoomLevels.length){ - expected = testZoomLevels[i]; - viewer.defaultZoomLevel = expected; + level = expected = testZoomLevels[i]; + viewer = OpenSeadragon({ + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS, + defaultZoomLevel: level, + homeFillsViewer: true + }); + viewer.addHandler('open', openHandler); viewer.open(TALL_PATH); // use a different image for homeFillsViewer - } - else { - viewer.removeHandler('open', openHandler); + } else { start(); } }; - viewer.addHandler('open', openHandler); - expected = testZoomLevels[i]; + level = expected = testZoomLevels[i]; viewer.homeFillsViewer = true; viewer.defaultZoomLevel = expected; viewer.open(TALL_PATH); // use a different image for homeFillsViewer From c4fac8248905f2d0487c271390d6f6d29b950b77 Mon Sep 17 00:00:00 2001 From: nein09 Date: Fri, 12 Dec 2014 12:51:50 -0800 Subject: [PATCH 07/10] Clean up errors found by SublimeLinter. --- test/viewport.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 8602cfd4..02b662dc 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -1,4 +1,4 @@ -/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util, testLog */ +/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util, testLog, propEqual */ (function () { var viewer; @@ -31,8 +31,8 @@ var ZOOM_FACTOR = 2; // the image will be twice as large as the viewer. var VIEWER_PADDING = new OpenSeadragon.Point(20, 20); - var DZI_PATH = '/test/data/testpattern.dzi' - var TALL_PATH = '/test/data/tall.dzi' + var DZI_PATH = '/test/data/testpattern.dzi'; + var TALL_PATH = '/test/data/tall.dzi'; var testZoomLevels = [-1, 0, 0.1, 0.5, 4, 10]; @@ -72,7 +72,7 @@ var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - propEqual(viewport.getContainerSize(), new OpenSeadragon.Point(500, 500), "Test container size") + propEqual(viewport.getContainerSize(), new OpenSeadragon.Point(500, 500), "Test container size"); start(); }; viewer.addHandler('open', openHandler); @@ -85,7 +85,7 @@ var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - equal(viewport.getAspectRatio(), 1, "Test aspect ratio") + equal(viewport.getAspectRatio(), 1, "Test aspect ratio"); start(); }; viewer.addHandler('open', openHandler); @@ -97,7 +97,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - equal(viewport.getMinZoom(), .9, "Test default min zoom level") + equal(viewport.getMinZoom(), .9, "Test default min zoom level"); start(); }; viewer.addHandler('open', openHandler); @@ -109,7 +109,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - equal(viewport.getMaxZoom(), 2.2, "Test default max zoom level") + equal(viewport.getMaxZoom(), 2.2, "Test default max zoom level"); start(); }; viewer.addHandler('open', openHandler); @@ -122,7 +122,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - if(level == 0) { // 0 means use the default + if(level === 0) { // 0 means use the default expected = 0.9; } else if(level > 1){ expected = 1; // min zoom won't go bigger than 1. @@ -160,7 +160,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - if(level == 0){ // 0 means use the default + if(level === 0){ // 0 means use the default expected = 2.2; } else if(level < 1){ expected = 1; // max zoom won't go smaller than 1. @@ -200,7 +200,7 @@ viewport.zoomTo(ZOOM_FACTOR, null, true); // Have to special case this to avoid dividing by 0 - if(testZoomLevels[i] == 0){ + if(testZoomLevels[i] === 0){ expected = new OpenSeadragon.Rect(0, 0, 1, 1); } else { var sideLength = 1.0 / viewer.defaultZoomLevel; // it's a square in this case @@ -239,7 +239,7 @@ viewport.zoomTo(ZOOM_FACTOR, null, true); // If the default zoom level is set to 0, then we expect the home zoom to be 1. - if(expected == 0){ + if(expected === 0){ expected = 1; } equal( @@ -276,7 +276,7 @@ viewport.zoomTo(ZOOM_FACTOR, null, true); // If the default zoom level is set to 0, then we expect the home zoom to be 1. - if(level == 0){ + if(level === 0){ expected = 1; } @@ -321,6 +321,7 @@ actual = viewport.deltaPixelsFromPoints(orig); propEqual(actual, expected, "Correctly got delta pixels from points with current = false " + orig); + var expected_current, actual_current; expected_current = orig.times(viewport.getContainerSize().x); actual_current = viewport.deltaPixelsFromPoints(orig, true); propEqual(actual_current, expected_current, "Correctly got delta pixels from points with current = true " + orig); @@ -346,6 +347,7 @@ actual = viewport.deltaPointsFromPixels(orig); propEqual(actual, expected, "Correctly got delta points from pixels with current = false " + orig); + var expected_current, actual_current; expected_current = orig.divide(viewport.getContainerSize().x); actual_current = viewport.deltaPointsFromPixels(orig, true); propEqual(actual_current, expected_current, "Correctly got delta points from pixels with current = true " + orig); @@ -373,6 +375,8 @@ expected, "Correctly converted coordinates with current = false " + orig ); + + var expected_current, actual_current; expected_current = orig.times(viewport.getContainerSize().x); actual_current = viewport.pixelFromPoint(orig, true); propEqual( @@ -405,6 +409,8 @@ expected, "Correctly converted coordinates with current = false " + orig ); + + var expected_current, actual_current; expected_current = orig.divide(viewer.source.dimensions.x / ZOOM_FACTOR); actual_current = viewport.pointFromPixel(orig, true); propEqual( @@ -575,7 +581,7 @@ var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - var orig, expected, actual; + var position, orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); position = viewer.element.getBoundingClientRect(); From e9012488169d29a24000fcf01f49ed1efe6531d7 Mon Sep 17 00:00:00 2001 From: nein09 Date: Sun, 14 Dec 2014 11:08:54 -0800 Subject: [PATCH 08/10] Explicitly call viewport.update() after zooming and make the test expected values reflect that action. --- test/viewport.js | 97 +++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 02b662dc..43485d28 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -30,7 +30,7 @@ // helpers and constants var ZOOM_FACTOR = 2; // the image will be twice as large as the viewer. - var VIEWER_PADDING = new OpenSeadragon.Point(20, 20); + var VIEWER_PADDING = new OpenSeadragon.Point(0.25, 0.25); var DZI_PATH = '/test/data/testpattern.dzi'; var TALL_PATH = '/test/data/tall.dzi'; @@ -71,6 +71,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true propEqual(viewport.getContainerSize(), new OpenSeadragon.Point(500, 500), "Test container size"); start(); @@ -84,6 +85,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true equal(viewport.getAspectRatio(), 1, "Test aspect ratio"); start(); @@ -97,7 +99,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - equal(viewport.getMinZoom(), .9, "Test default min zoom level"); + equal(viewport.getMinZoom(), 0.9, "Test default min zoom level"); start(); }; viewer.addHandler('open', openHandler); @@ -198,6 +200,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true // Have to special case this to avoid dividing by 0 if(testZoomLevels[i] === 0){ @@ -237,6 +240,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true // If the default zoom level is set to 0, then we expect the home zoom to be 1. if(expected === 0){ @@ -274,6 +278,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true // If the default zoom level is set to 0, then we expect the home zoom to be 1. if(level === 0){ @@ -313,18 +318,14 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); expected = orig.times(viewport.getContainerSize().x * ZOOM_FACTOR); actual = viewport.deltaPixelsFromPoints(orig); - propEqual(actual, expected, "Correctly got delta pixels from points with current = false " + orig); - - var expected_current, actual_current; - expected_current = orig.times(viewport.getContainerSize().x); - actual_current = viewport.deltaPixelsFromPoints(orig, true); - propEqual(actual_current, expected_current, "Correctly got delta pixels from points with current = true " + orig); + propEqual(actual, expected, "Correctly got delta pixels from points" + orig); } start(); @@ -338,6 +339,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ @@ -345,12 +347,7 @@ expected = orig.divide(viewport.getContainerSize().x * ZOOM_FACTOR); actual = viewport.deltaPointsFromPixels(orig); - propEqual(actual, expected, "Correctly got delta points from pixels with current = false " + orig); - - var expected_current, actual_current; - expected_current = orig.divide(viewport.getContainerSize().x); - actual_current = viewport.deltaPointsFromPixels(orig, true); - propEqual(actual_current, expected_current, "Correctly got delta points from pixels with current = true " + orig); + propEqual(actual, expected, "Correctly got delta points from pixels " + orig); } start(); @@ -363,26 +360,17 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); - // todo: this magic point is something to do with the target location but I don't entirely understand why it's here - expected = orig.plus(new OpenSeadragon.Point(0.25, 0.25)).times(viewport.getContainerSize().x * ZOOM_FACTOR).minus(viewport.getContainerSize()); + expected = orig.plus(VIEWER_PADDING).times(viewport.getContainerSize().x * ZOOM_FACTOR).minus(viewport.getContainerSize()); actual = viewport.pixelFromPoint(orig, false); propEqual( actual, expected, - "Correctly converted coordinates with current = false " + orig - ); - - var expected_current, actual_current; - expected_current = orig.times(viewport.getContainerSize().x); - actual_current = viewport.pixelFromPoint(orig, true); - propEqual( - actual_current, - expected_current, - "Correctly converted coordinates with current = true " + orig + "Correctly converted coordinates " + orig ); } @@ -397,26 +385,17 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewport.getContainerSize().x); - // todo: this magic point is something to do with the target location but I don't entirely understand why it's here - expected = orig.divide(viewer.source.dimensions.x).plus(new OpenSeadragon.Point(0.25, 0.25)); + expected = orig.divide(viewer.source.dimensions.x).plus(VIEWER_PADDING); actual = viewport.pointFromPixel(orig, false); propEqual( actual, expected, - "Correctly converted coordinates with current = false " + orig - ); - - var expected_current, actual_current; - expected_current = orig.divide(viewer.source.dimensions.x / ZOOM_FACTOR); - actual_current = viewport.pointFromPixel(orig, true); - propEqual( - actual_current, - expected_current, - "Correctly converted coordinates with current = true " + orig + "Correctly converted coordinates " + orig ); } @@ -430,6 +409,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ @@ -450,6 +430,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ @@ -469,6 +450,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testRects.length; i++){ @@ -494,6 +476,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testRects.length; i++){ @@ -519,11 +502,12 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewport.getContainerSize().x); - expected = orig.times(ZOOM_FACTOR); + expected = orig.plus(viewport.getContainerSize().divide(2)); actual = viewport.viewerElementToImageCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig); } @@ -539,11 +523,12 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.divide(ZOOM_FACTOR); + expected = orig.minus(viewport.getContainerSize().divide(2)); actual = viewport.imageToViewerElementCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig); } @@ -559,12 +544,15 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var window_boundary = Math.min(window.innerWidth, window.innerHeight); - var orig, expected, actual; + var orig, expected, actual, position, pos_point; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(window_boundary); - expected = orig.divide(viewport.getContainerSize().x).plus(VIEWER_PADDING); + expected = orig.minus(pos_point).divide(viewport.getContainerSize().x * ZOOM_FACTOR).plus(VIEWER_PADDING); actual = viewport.windowToViewportCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig); } @@ -580,12 +568,14 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true - var position, orig, expected, actual; + var position, orig, expected, actual, pos_point; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); - position = viewer.element.getBoundingClientRect(); - expected = orig.divide(ZOOM_FACTOR).plus( new OpenSeadragon.Point(position.top, position.left) ); + expected = orig.plus(pos_point).minus(VIEWER_PADDING.times(viewport.getContainerSize().x * ZOOM_FACTOR)); actual = viewport.imageToWindowCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig); } @@ -601,12 +591,15 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var window_boundary = Math.min(window.innerWidth, window.innerHeight); - var orig, expected, actual; + var orig, expected, actual, position; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(window_boundary); - expected = orig.divide(viewport.getContainerSize().x).plus(VIEWER_PADDING); + expected = orig.minus(pos_point).divide(viewport.getContainerSize().x * ZOOM_FACTOR).plus(VIEWER_PADDING); actual = viewport.windowToViewportCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig); } @@ -622,11 +615,14 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true - var orig, expected, actual; + var orig, expected, actual, position; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); for (var i = 0; i < testPoints.length; i++){ orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.minus(VIEWER_PADDING).times(viewport.getContainerSize().x); + expected = orig.minus(VIEWER_PADDING).times(viewport.getContainerSize().x * ZOOM_FACTOR).plus(pos_point); actual = viewport.viewportToWindowCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig); } @@ -642,6 +638,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ @@ -662,7 +659,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; viewport.zoomTo(ZOOM_FACTOR, null, true); - + viewport.update(); // need to call this even with immediately=true var orig, expected, actual; for (var i = 0; i < testPoints.length; i++){ From aebfecb4e8b484c6a9207d2f6675658fd2b71a07 Mon Sep 17 00:00:00 2001 From: nein09 Date: Sun, 14 Dec 2014 13:15:50 -0800 Subject: [PATCH 09/10] Factor out looping and reopening viewer into a helper method, based on Ian's example. --- test/viewport.js | 224 +++++++++++++++++------------------------------ 1 file changed, 80 insertions(+), 144 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 43485d28..1c97a481 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -52,20 +52,45 @@ new OpenSeadragon.Rect(1, 1, 1, 1) ]; - // ---------- -/* - asyncTest('template', function() { + // Test helper - a lot of these tests loop through a few possible + // values for zoom levels, and reopen the viewer for each iteration. + var reopenViewerHelper = function(config) { + var expected, level, actual, i = 0; var openHandler = function(event) { viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); + expected = config.processExpected(level, expected); + actual = viewport[config.method](); - start(); + propEqual( + actual, + expected, + "Test " + config.method + " with zoom level of " + level + ". Expected : " + expected + ", got " + actual + ); + i++; + if(i < testZoomLevels.length){ + level = expected = testZoomLevels[i]; + var viewerConfig = { + id: VIEWER_ID, + prefixUrl: PREFIX_URL, + springStiffness: SPRING_STIFFNESS + }; + + viewerConfig[config.property] = level; + viewer = OpenSeadragon(viewerConfig); + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + } else { + start(); + } }; viewer.addHandler('open', openHandler); + level = expected = testZoomLevels[i]; + viewer[config.property] = level; viewer.open(DZI_PATH); - }); -*/ + }; + +// Tests start here. asyncTest('getContainerSize', function() { var openHandler = function(event) { viewer.removeHandler('open', openHandler); @@ -119,159 +144,70 @@ }); asyncTest('getMinZoom', function() { - var expected, level, i = 0; - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - - if(level === 0) { // 0 means use the default - expected = 0.9; - } else if(level > 1){ - expected = 1; // min zoom won't go bigger than 1. + reopenViewerHelper({ + property: 'minZoomLevel', + method: 'getMinZoom', + processExpected: function(level, expected){ + if(level === 0) { // 0 means use the default + expected = 0.9; + } else if(level > 1) { + expected = 1; // min zoom won't go bigger than 1. + } + return expected; } - - equal( - viewport.getMinZoom(), - expected, - "Test getMinZoom with zoom level of " + level - ); - i++; - if(i < testZoomLevels.length){ - level = expected = testZoomLevels[i]; - viewer = OpenSeadragon({ - id: VIEWER_ID, - prefixUrl: PREFIX_URL, - springStiffness: SPRING_STIFFNESS, - minZoomLevel: level - }); - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); - } else { - start(); - } - }; - viewer.addHandler('open', openHandler); - level = expected = testZoomLevels[i]; - viewer.minZoomLevel = level; - viewer.open(DZI_PATH); + }); }); asyncTest('getMaxZoom', function() { - var expected, level, i = 0; - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - - if(level === 0){ // 0 means use the default - expected = 2.2; - } else if(level < 1){ - expected = 1; // max zoom won't go smaller than 1. + reopenViewerHelper({ + property: 'maxZoomLevel', + method: 'getMaxZoom', + processExpected: function(level, expected) { + if(level === 0){ // 0 means use the default + expected = 2.2; + } else if(level < 1){ + expected = 1; // max zoom won't go smaller than 1. + } + return expected; } - equal( - viewport.getMaxZoom(), - expected, - "Test getMaxZoom with zoom level of " + level - ); - i++; - if(i < testZoomLevels.length){ - level = expected = testZoomLevels[i]; - viewer = OpenSeadragon({ - id: VIEWER_ID, - prefixUrl: PREFIX_URL, - springStiffness: SPRING_STIFFNESS, - maxZoomLevel: level - }); - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); - } else { - start(); - } - }; - viewer.addHandler('open', openHandler); - level = expected = testZoomLevels[i]; - viewer.maxZoomLevel = level; - viewer.open(DZI_PATH); + }); }); asyncTest('getHomeBounds', function() { - var expected, i = 0; - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - // Have to special case this to avoid dividing by 0 - if(testZoomLevels[i] === 0){ - expected = new OpenSeadragon.Rect(0, 0, 1, 1); - } else { - var sideLength = 1.0 / viewer.defaultZoomLevel; // it's a square in this case - var position = 0.5 - (sideLength / 2.0); - expected = new OpenSeadragon.Rect(position, position, sideLength, sideLength); + reopenViewerHelper({ + property: 'defaultZoomLevel', + method: 'getHomeBounds', + processExpected: function(level, expected) { + // Have to special case this to avoid dividing by 0 + if(level === 0){ + expected = new OpenSeadragon.Rect(0, 0, 1, 1); + } else { + var sideLength = 1.0 / viewer.defaultZoomLevel; // it's a square in this case + var position = 0.5 - (sideLength / 2.0); + expected = new OpenSeadragon.Rect(position, position, sideLength, sideLength); + } + return expected; } - propEqual( - viewport.getHomeBounds(), - expected, - "Test getHomeBounds with default zoom level of " + viewer.defaultZoomLevel - ); - i++; - if(i < testZoomLevels.length){ - viewer = OpenSeadragon({ - id: VIEWER_ID, - prefixUrl: PREFIX_URL, - springStiffness: SPRING_STIFFNESS, - defaultZoomLevel: testZoomLevels[i] - }); - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); - } else { - start(); - } - }; - viewer.addHandler('open', openHandler); - viewer.defaultZoomLevel = testZoomLevels[i]; - viewer.open(DZI_PATH); + }); }); asyncTest('getHomeZoom', function() { - var expected, i = 0; - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - // If the default zoom level is set to 0, then we expect the home zoom to be 1. - if(expected === 0){ - expected = 1; + reopenViewerHelper({ + property: 'defaultZoomLevel', + method: 'getHomeZoom', + processExpected: function(level, expected){ + // If the default zoom level is set to 0, then we expect the home zoom to be 1. + if(expected === 0){ + expected = 1; + } + return expected; } - equal( - viewport.getHomeZoom(), - expected, - "Test getHomeZoom with default zoom level of " + expected - ); - i++; - if(i < testZoomLevels.length){ - expected = testZoomLevels[i]; - viewer = OpenSeadragon({ - id: VIEWER_ID, - prefixUrl: PREFIX_URL, - springStiffness: SPRING_STIFFNESS, - defaultZoomLevel: expected - }); - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); - } else { - start(); - } - }; - viewer.addHandler('open', openHandler); - expected = testZoomLevels[i]; - viewer.defaultZoomLevel = expected; - viewer.open(DZI_PATH); + }); }); + // I don't use the helper for this one because it sets a couple more + // properties that would need a lot of special casing. asyncTest('getHomeZoomWithHomeFillsViewer', function() { var expected, level, i = 0; var openHandler = function(event) { From 00510c6cc2b9002fb5a71ccbeae587cda7aa89d1 Mon Sep 17 00:00:00 2001 From: nein09 Date: Mon, 15 Dec 2014 09:41:32 -0800 Subject: [PATCH 10/10] Refactor other looping tests that don't reopen the viewer in a similar manner. --- test/viewport.js | 512 +++++++++++++++++++---------------------------- 1 file changed, 204 insertions(+), 308 deletions(-) diff --git a/test/viewport.js b/test/viewport.js index 1c97a481..35402d87 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -90,6 +90,33 @@ viewer.open(DZI_PATH); }; + // Test helper - a lot of these tests loop through a test data + // array and test different values. This helper does not reopen the viewer. + var loopingTestHelper = function(config) { + var openHandler = function(event) { + viewer.removeHandler('open', openHandler); + var viewport = viewer.viewport; + viewport.zoomTo(ZOOM_FACTOR, null, true); + viewport.update(); // need to call this even with immediately=true + + var orig, expected, actual; + for (var i = 0; i < config.testArray.length; i++){ + orig = config.getOrig(config.testArray[i], viewport); + expected = config.getExpected(orig, viewport); + actual = viewport[config.method](orig); + propEqual( + actual, + expected, + "Correctly converted coordinates " + orig + ); + } + + start(); + }; + viewer.addHandler('open', openHandler); + viewer.open(DZI_PATH); + }; + // Tests start here. asyncTest('getContainerSize', function() { var openHandler = function(event) { @@ -207,7 +234,7 @@ }); // I don't use the helper for this one because it sets a couple more - // properties that would need a lot of special casing. + // properties that would need special casing. asyncTest('getHomeZoomWithHomeFillsViewer', function() { var expected, level, i = 0; var openHandler = function(event) { @@ -250,365 +277,234 @@ }); asyncTest('deltaPixelsFromPoints', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.times(viewport.getContainerSize().x * ZOOM_FACTOR); - actual = viewport.deltaPixelsFromPoints(orig); - propEqual(actual, expected, "Correctly got delta pixels from points" + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport){ + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport){ + return orig.times(viewport.getContainerSize().x * ZOOM_FACTOR); + }, + method: 'deltaPixelsFromPoints' + }); }); asyncTest('deltaPointsFromPixels', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewport.getContainerSize().x); - - expected = orig.divide(viewport.getContainerSize().x * ZOOM_FACTOR); - actual = viewport.deltaPointsFromPixels(orig); - propEqual(actual, expected, "Correctly got delta points from pixels " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewport.getContainerSize().x); + }, + getExpected: function(orig, viewport) { + return orig.divide(viewport.getContainerSize().x * ZOOM_FACTOR); + }, + method: 'deltaPointsFromPixels' + }); }); + asyncTest('pixelFromPoint', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.plus(VIEWER_PADDING).times(viewport.getContainerSize().x * ZOOM_FACTOR).minus(viewport.getContainerSize()); - actual = viewport.pixelFromPoint(orig, false); - propEqual( - actual, - expected, - "Correctly converted coordinates " + orig - ); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport) { + return orig.plus(VIEWER_PADDING).times(viewport.getContainerSize().x * ZOOM_FACTOR).minus(viewport.getContainerSize()); + }, + method: 'pixelFromPoint' + }); }); asyncTest('pointFromPixel', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewport.getContainerSize().x); - expected = orig.divide(viewer.source.dimensions.x).plus(VIEWER_PADDING); - actual = viewport.pointFromPixel(orig, false); - propEqual( - actual, - expected, - "Correctly converted coordinates " + orig - ); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewport.getContainerSize().x); + }, + getExpected: function(orig, viewport) { + return orig.divide(viewer.source.dimensions.x).plus(VIEWER_PADDING); + }, + method: 'pointFromPixel' + }); }); + asyncTest('viewportToImageCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewport.getContainerSize().x); - expected = orig.divide(viewer.source.dimensions.x); - actual = viewport.imageToViewportCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewport.getContainerSize().x); + }, + getExpected: function(orig, viewport) { + return orig.divide(viewer.source.dimensions.x); + }, + method: 'imageToViewportCoordinates' + }); }); asyncTest('imageToViewportCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.divide(ZOOM_FACTOR * viewport.getContainerSize().x); - actual = viewport.imageToViewportCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport) { + return orig.divide(ZOOM_FACTOR * viewport.getContainerSize().x); + }, + method: 'imageToViewportCoordinates' + }); }); asyncTest('imageToViewportRectangle', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testRects.length; i++){ - orig = testRects[i].times(viewer.source.dimensions.x); - expected = new OpenSeadragon.Rect( + loopingTestHelper({ + testArray: testRects, + getOrig: function(el, viewport) { + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport) { + return new OpenSeadragon.Rect( orig.x / viewer.source.dimensions.x, orig.y / viewer.source.dimensions.x, orig.width / viewer.source.dimensions.x, orig.height / viewer.source.dimensions.x ); - actual = viewport.imageToViewportRectangle(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + }, + method: 'imageToViewportRectangle' + }); }); asyncTest('viewportToImageRectangle', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testRects.length; i++){ - orig = testRects[i].times(viewport.getContainerSize().x); - expected = new OpenSeadragon.Rect( + loopingTestHelper({ + testArray: testRects, + getOrig: function(el, viewport) { + return el.times(viewport.getContainerSize().x); + }, + getExpected: function(orig, viewport) { + return new OpenSeadragon.Rect( orig.x * viewer.source.dimensions.x, orig.y * viewer.source.dimensions.x, orig.width * viewer.source.dimensions.x, orig.height * viewer.source.dimensions.x ); - actual = viewport.viewportToImageRectangle(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + }, + method: 'viewportToImageRectangle' + }); }); asyncTest('viewerElementToImageCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewport.getContainerSize().x); - expected = orig.plus(viewport.getContainerSize().divide(2)); - actual = viewport.viewerElementToImageCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewport.getContainerSize().x); + }, + getExpected: function(orig, viewport) { + return orig.plus(viewport.getContainerSize().divide(2)); + }, + method: 'viewerElementToImageCoordinates' + }); }); asyncTest('imageToViewerElementCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.minus(viewport.getContainerSize().divide(2)); - actual = viewport.imageToViewerElementCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport) { + return orig.minus(viewport.getContainerSize().divide(2)); + }, + method: 'imageToViewerElementCoordinates' + }); }); asyncTest('windowToImageCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var window_boundary = Math.min(window.innerWidth, window.innerHeight); - var orig, expected, actual, position, pos_point; - position = viewer.element.getBoundingClientRect(); - pos_point = new OpenSeadragon.Point(position.top, position.left); - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(window_boundary); - expected = orig.minus(pos_point).divide(viewport.getContainerSize().x * ZOOM_FACTOR).plus(VIEWER_PADDING); - actual = viewport.windowToViewportCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + var window_boundary = Math.min(window.innerWidth, window.innerHeight); + return el.times(window_boundary); + }, + getExpected: function(orig, viewport) { + var position, pos_point; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); + return orig.minus(pos_point).divide(viewport.getContainerSize().x * ZOOM_FACTOR).plus(VIEWER_PADDING); + }, + method: 'windowToViewportCoordinates' + }); }); asyncTest('imageToWindowCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var position, orig, expected, actual, pos_point; - position = viewer.element.getBoundingClientRect(); - pos_point = new OpenSeadragon.Point(position.top, position.left); - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.plus(pos_point).minus(VIEWER_PADDING.times(viewport.getContainerSize().x * ZOOM_FACTOR)); - actual = viewport.imageToWindowCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport) { + var position, pos_point; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); + return orig.plus(pos_point).minus(VIEWER_PADDING.times(viewport.getContainerSize().x * ZOOM_FACTOR)); + }, + method: 'imageToWindowCoordinates' + }); }); asyncTest('windowToViewportCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var window_boundary = Math.min(window.innerWidth, window.innerHeight); - var orig, expected, actual, position; - position = viewer.element.getBoundingClientRect(); - pos_point = new OpenSeadragon.Point(position.top, position.left); - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(window_boundary); - expected = orig.minus(pos_point).divide(viewport.getContainerSize().x * ZOOM_FACTOR).plus(VIEWER_PADDING); - actual = viewport.windowToViewportCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + var window_boundary = Math.min(window.innerWidth, window.innerHeight); + return el.times(window_boundary); + }, + getExpected: function(orig, viewport) { + var position, pos_point; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); + return orig.minus(pos_point).divide(viewport.getContainerSize().x * ZOOM_FACTOR).plus(VIEWER_PADDING); + }, + method: 'windowToViewportCoordinates' + }); }); asyncTest('viewportToWindowCoordinates', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual, position; - position = viewer.element.getBoundingClientRect(); - pos_point = new OpenSeadragon.Point(position.top, position.left); - for (var i = 0; i < testPoints.length; i++){ - orig = testPoints[i].times(viewer.source.dimensions.x); - expected = orig.minus(VIEWER_PADDING).times(viewport.getContainerSize().x * ZOOM_FACTOR).plus(pos_point); - actual = viewport.viewportToWindowCoordinates(orig); - propEqual(actual, expected, "Coordinates converted correctly for " + orig); - } - - start(); - }; - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el.times(viewer.source.dimensions.x); + }, + getExpected: function(orig, viewport) { + var position, pos_point; + position = viewer.element.getBoundingClientRect(); + pos_point = new OpenSeadragon.Point(position.top, position.left); + return orig.minus(VIEWER_PADDING).times(viewport.getContainerSize().x * ZOOM_FACTOR).plus(pos_point); + }, + method: 'viewportToWindowCoordinates' + }); }); asyncTest('viewportToImageZoom', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testZoomLevels[i]; - expected = orig / ZOOM_FACTOR; - actual = viewport.viewportToImageZoom(orig); - equal(expected, actual, "Coordinates converted correctly for " + orig); - } - start(); - }; - - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el; + }, + getExpected: function(orig, viewport) { + return orig / ZOOM_FACTOR; + }, + method: 'viewportToImageZoom' + }); }); asyncTest('imageToViewportZoom', function() { - var openHandler = function(event) { - viewer.removeHandler('open', openHandler); - var viewport = viewer.viewport; - viewport.zoomTo(ZOOM_FACTOR, null, true); - viewport.update(); // need to call this even with immediately=true - - var orig, expected, actual; - for (var i = 0; i < testPoints.length; i++){ - orig = testZoomLevels[i]; - expected = orig * ZOOM_FACTOR; - actual = viewport.imageToViewportZoom(orig); - equal(expected, actual, "Coordinates converted correctly for " + orig); - } - start(); - }; - - viewer.addHandler('open', openHandler); - viewer.open(DZI_PATH); + loopingTestHelper({ + testArray: testPoints, + getOrig: function(el, viewport) { + return el; + }, + getExpected: function(orig, viewport) { + return orig * ZOOM_FACTOR; + }, + method: 'imageToViewportZoom' + }); }); })();