From e8d4c00c5fab72cbccc9718ca6b0adf1aaf5faa3 Mon Sep 17 00:00:00 2001 From: nein09 Date: Thu, 9 Oct 2014 16:58:45 -0700 Subject: [PATCH] Add viewport.js to test.html so tests get run. Use built-in Point functions. Add a helper function to get a random number. --- test/test.html | 1 + test/viewport.js | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/test/test.html b/test/test.html index 592847bd..b837d733 100644 --- a/test/test.html +++ b/test/test.html @@ -31,5 +31,6 @@ + diff --git a/test/viewport.js b/test/viewport.js index 352a18c8..d9788b3c 100644 --- a/test/viewport.js +++ b/test/viewport.js @@ -23,6 +23,10 @@ viewer = null; } }); + // helper + var getRandom = function(min, max){ + return min + Math.floor(Math.random() * (max - min + 1)); + } // ---------- /* @@ -47,14 +51,14 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - var orig_x = Math.floor(Math.random() * 10000); - var orig_y = Math.floor(Math.random() * 10000); + var orig_x = getRandom(500, 10000); + var orig_y = getRandom(500, 10000); var orig = new OpenSeadragon.Point(orig_x, orig_y); - // why does the math work this way? - var expected = new OpenSeadragon.Point((orig_x / 500) + 20, (orig_x / 500) + 20); + + // 500 is the viewport container size; there's 20 px of padding (I think) + var expected = orig.divide(500).plus( new OpenSeadragon.Point(20, 20) ); var actual = viewport.windowToViewportCoordinates(orig); - equal(actual.x, expected.x, "x coordinate for " + orig_x); - equal(actual.y, expected.y, "y coordinate for " + orig_y); + ok(actual.equals(expected), "Coordinates converted correctly for " + orig); start(); }; @@ -67,14 +71,14 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - var orig_x = Math.floor(Math.random() * 1000); - var orig_y = Math.floor(Math.random() * 1000); + var orig_x = getRandom(0, 1000); + var orig_y = getRandom(0, 1000); var orig = new OpenSeadragon.Point(orig_x, orig_y); - // why does the math work this way? - var expected = new OpenSeadragon.Point((orig_x - 20) * 500, (orig_y -20) * 500); + + // 500 is the viewport container size; there's 20 px of padding (I think) + var expected = orig.minus( new OpenSeadragon.Point(20, 20) ).times(500); var actual = viewport.viewportToWindowCoordinates(orig); - equal(actual.x, expected.x, "x coordinate for " + orig_x); - equal(actual.y, expected.y, "y coordinate for " + orig_y); + ok(actual.equals(expected), "Coordinates converted correctly for " + orig); start(); }; @@ -87,7 +91,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - var orig = Math.floor(Math.random() * 4); + var orig = getRandom(0, 4); var expected = orig / 2; // because the container is 500 x 500 var actual = viewport.viewportToImageZoom(orig); equal(expected, actual, "Expected " + expected + ", got " + actual); @@ -103,7 +107,7 @@ viewer.removeHandler('open', openHandler); var viewport = viewer.viewport; - var orig = Math.floor(Math.random() * 4); + var orig = getRandom(0, 4); var expected = orig * 2; // because the container is 500 x 500 var actual = viewport.imageToViewportZoom(orig); equal(expected, actual, "Expected " + expected + ", got " + actual);