Remove randomness from test cases.

This commit is contained in:
nein09 2014-10-27 17:58:59 -07:00
parent c1ae3d08f7
commit 2d70ee32fc

View File

@ -23,15 +23,43 @@
viewer = null; viewer = null;
} }
}); });
// helper
var getRandom = function(min, max){ // helpers and constants
return min + Math.floor(Math.random() * (max - min + 1));
};
var ZOOM_FACTOR = 2; // the image will be twice as large as the viewer. var ZOOM_FACTOR = 2; // the image will be twice as large as the viewer.
var VIEWER_SIZE = 500; // We set up the viewer to be 500 px x 500 px. var VIEWER_SIZE = 500; // We set up the viewer to be 500 px x 500 px.
var VIEWER_PADDING = new OpenSeadragon.Point(20, 20); var VIEWER_PADDING = new OpenSeadragon.Point(20, 20);
var testZoomLevels = [-1, 0, 0.1, 0.5, 4, 10];
var testPoints = [
new OpenSeadragon.Point(0, 0),
new OpenSeadragon.Point(0.001, 0.001),
new OpenSeadragon.Point(0.25, 0.5),
new OpenSeadragon.Point(0.999, 0.999),
new OpenSeadragon.Point(1, 1)
];
var testRects = [
new OpenSeadragon.Rect(0, 0, 0, 0),
new OpenSeadragon.Rect(0.001, 0.005, 0.001, 0.003),
new OpenSeadragon.Rect(0.25, 0.25, 0.25, 0.25),
new OpenSeadragon.Rect(0.999, 0.999, 0.999, 0.999),
new OpenSeadragon.Rect(1, 1, 1, 1)
];
// If this is something we might want to add to the Rect class, I can do that.
// But I assumed that since it isn't there already, that's not functionality
// that we want to make broadly available.
OpenSeadragon.Rect.prototype.times = function( factor ) {
return new OpenSeadragon.Rect(
this.x * factor,
this.y * factor,
this.width * factor,
this.height * factor
);
}
// ---------- // ----------
/* /*
asyncTest('template', function() { asyncTest('template', function() {
@ -58,20 +86,18 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Rect( var orig, expected, actual;
getRandom(0, 1000), for (var i = 0; i < testRects.length; i++){
getRandom(0, 1000), orig = testRects[i].times(1000);
getRandom(0, 1000), expected = new OpenSeadragon.Rect(
getRandom(0, 1000)
);
var expected = new OpenSeadragon.Rect(
orig.x / 1000, orig.x / 1000,
orig.y / 1000, orig.y / 1000,
orig.width / 1000, orig.width / 1000,
orig.height / 1000 orig.height / 1000
); );
var actual = viewport.imageToViewportRectangle(orig); actual = viewport.imageToViewportRectangle(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -85,20 +111,18 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Rect( var orig, expected, actual;
getRandom(0, 500), for (var i = 0; i < testRects.length; i++){
getRandom(0, 500), orig = testRects[i].times(500);
getRandom(0, 500), expected = new OpenSeadragon.Rect(
getRandom(0, 500) orig.x * 1000,
); orig.y * 1000,
var expected = new OpenSeadragon.Rect( orig.width * 1000,
orig.x * 1000, orig.height * 1000
orig.y * 1000, );
orig.width * 1000, actual = viewport.viewportToImageRectangle(orig);
orig.height * 1000 propEqual(actual, expected, "Coordinates converted correctly for " + orig);
); }
var actual = viewport.viewportToImageRectangle(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig);
start(); start();
}; };
@ -112,12 +136,13 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Point( var orig, expected, actual;
getRandom(0, 500), getRandom(0, 500) for (var i = 0; i < testPoints.length; i++){
); orig = testPoints[i].times(500);
var expected = orig.times(ZOOM_FACTOR); expected = orig.times(ZOOM_FACTOR);
var actual = viewport.viewerElementToImageCoordinates(orig); actual = viewport.viewerElementToImageCoordinates(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -131,12 +156,13 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Point( var orig, expected, actual;
getRandom(0, 1000), getRandom(0, 1000) for (var i = 0; i < testPoints.length; i++){
); orig = testPoints[i].times(1000);
var expected = orig.divide(ZOOM_FACTOR); expected = orig.divide(ZOOM_FACTOR);
var actual = viewport.imageToViewerElementCoordinates(orig); actual = viewport.imageToViewerElementCoordinates(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -150,12 +176,13 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Point( var orig, expected, actual;
getRandom(100, 3000), getRandom(100, 3000) for (var i = 0; i < testPoints.length; i++){
); orig = testPoints[i].times(3000);
var expected = orig.divide(VIEWER_SIZE).plus(VIEWER_PADDING); expected = orig.divide(VIEWER_SIZE).plus(VIEWER_PADDING);
var actual = viewport.windowToViewportCoordinates(orig); actual = viewport.windowToViewportCoordinates(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -169,14 +196,15 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
// test image is 1000 x 1000 var orig, expected, actual;
var orig = new OpenSeadragon.Point( for (var i = 0; i < testPoints.length; i++){
getRandom(0, 1000), getRandom(0, 1000) // Test image is 1000 x 1000
); orig = testPoints[i].times(1000);
var position = viewer.element.getBoundingClientRect(); position = viewer.element.getBoundingClientRect();
var expected = orig.divide(ZOOM_FACTOR).plus( new OpenSeadragon.Point(position.top, position.left) ); expected = orig.divide(ZOOM_FACTOR).plus( new OpenSeadragon.Point(position.top, position.left) );
var actual = viewport.imageToWindowCoordinates(orig); actual = viewport.imageToWindowCoordinates(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -190,13 +218,13 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Point( var orig, expected, actual;
getRandom(100, 3000), getRandom(100, 3000) for (var i = 0; i < testPoints.length; i++){
); orig = testPoints[i].times(3000);
expected = orig.divide(VIEWER_SIZE).plus(VIEWER_PADDING);
var expected = orig.divide(VIEWER_SIZE).plus(VIEWER_PADDING); actual = viewport.windowToViewportCoordinates(orig);
var actual = viewport.windowToViewportCoordinates(orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); }
start(); start();
}; };
@ -210,13 +238,14 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = new OpenSeadragon.Point( var orig, expected, actual;
getRandom(0, 1000), getRandom(0, 1000) for (var i = 0; i < testPoints.length; i++){
); // Test image is 1000 x 1000
orig = testPoints[i].times(1000);
var expected = orig.minus(VIEWER_PADDING).times(VIEWER_SIZE); expected = orig.minus(VIEWER_PADDING).times(VIEWER_SIZE);
var actual = viewport.viewportToWindowCoordinates(orig); actual = viewport.viewportToWindowCoordinates(orig);
propEqual(actual, expected, "Coordinates converted correctly for " + orig); propEqual(actual, expected, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -230,10 +259,13 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = getRandom(0, 4); var orig, expected, actual;
var expected = orig / ZOOM_FACTOR; for (var i = 0; i < testPoints.length; i++){
var actual = viewport.viewportToImageZoom(orig); orig = testZoomLevels[i];
equal(expected, actual, "Coordinates converted correctly for " + orig); expected = orig / ZOOM_FACTOR;
actual = viewport.viewportToImageZoom(orig);
equal(expected, actual, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };
@ -247,10 +279,14 @@
var viewport = viewer.viewport; var viewport = viewer.viewport;
viewport.zoomTo(ZOOM_FACTOR, null, true); viewport.zoomTo(ZOOM_FACTOR, null, true);
var orig = getRandom(0, 4);
var expected = orig * ZOOM_FACTOR; var orig, expected, actual;
var actual = viewport.imageToViewportZoom(orig); for (var i = 0; i < testPoints.length; i++){
equal(expected, actual, "Coordinates converted correctly for " + orig); orig = testZoomLevels[i];
expected = orig * ZOOM_FACTOR;
actual = viewport.imageToViewportZoom(orig);
equal(expected, actual, "Coordinates converted correctly for " + orig);
}
start(); start();
}; };