Tests: add open failure tests, overhaul test framework

* Add tests for open failures
* Refactor tests to avoid tests depending on implied status from
  previous tests:
  1. The viewer is now created and destroyed for each
     test to avoid pollution and simplify error handling: nothing
     starts until you request it.
  2. Some tests like Basic: Homepage depended on the Zoom & Pan tests;
     now this is handled explicitly as part of the test setup
  3. All basic tests are now properly async tests (since they needed
     the viewer to load, they really were in the past except that
     they were relying on the viewer state left behind from previous
     tests)
* All tests now run inside the qunit-fixture to prevent masking
  failures. Util.resetDom() has been refactored to use the
  qunit-fixture and the teardown logic only used in the navigator
  tests has been moved into the navigator test teardown method
* Fixed undeclared mainViewerElement variable in optional path in
  the navigator tests
* JSHint cleanup
This commit is contained in:
Chris Adams 2013-07-01 18:17:45 -04:00
parent 0ed4703bd3
commit 59a254bea6
5 changed files with 195 additions and 128 deletions

View File

@ -1,144 +1,207 @@
/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util */ /* global module, asyncTest, $, ok, equal, notEqual, start, test, Util */
(function() { (function() {
var viewer;
module('Basic'); module('Basic', {
setup: function () {
var example = $('<div id="example"></div>').appendTo("#qunit-fixture");
// TODO: Test drag
var viewer = null;
// ----------
asyncTest('Open', function() {
$(document).ready(function() {
viewer = OpenSeadragon({ viewer = OpenSeadragon({
id: 'example', id: 'example',
prefixUrl: '/build/openseadragon/images/', prefixUrl: '/build/openseadragon/images/',
tileSources: '/test/data/testpattern.dzi',
springStiffness: 100 // Faster animation = faster tests springStiffness: 100 // Faster animation = faster tests
}); });
},
teardown: function () {
if (!!viewer && viewer.close) {
viewer.close();
}
ok(viewer, 'Viewer exists'); viewer = null;
}
});
var openHandler = function(eventSender, eventData) { // ----------
viewer.removeHandler('open', openHandler); asyncTest('Open', function() {
ok(true, 'Open event was sent'); ok(viewer, 'Viewer exists');
equal(eventSender, viewer, 'Sender of open event was viewer');
ok(eventData, 'Handler also received event data');
ok(viewer.viewport, 'Viewport exists');
ok(viewer.source, 'source exists');
ok(viewer._updateRequestId, 'timer is on');
start();
};
viewer.addHandler('open', openHandler); var openHandler = function(eventSender, eventData) {
viewer.removeHandler('open', openHandler);
ok(true, 'Open event was sent');
equal(eventSender, viewer, 'Sender of open event was viewer');
ok(eventData, 'Handler also received event data');
ok(viewer.viewport, 'Viewport exists');
ok(viewer.source, 'source exists');
ok(viewer._updateRequestId, 'timer is on');
start();
};
viewer.addHandler('open', openHandler);
viewer.open('/test/data/testpattern.dzi');
});
asyncTest('Open Error Handling', function() {
ok(viewer, 'Viewer exists');
viewer.addHandler('open', function(eventSender, eventData) {
ok(false, "The open event should not fire for failed opens");
start();
}); });
viewer.addHandler('open-failed', function(eventSender, eventData) {
ok(true, "The open-failed event should be fired when the source 404s");
equal($(".openseadragon-message").length, 1, "Open failures should display a message");
start();
});
viewer.open('/test/data/not-a-real-file');
}); });
// ---------- // ----------
asyncTest('Zoom', function() { asyncTest('Zoom', function() {
var viewport = viewer.viewport; viewer.addHandler("open", function () {
equal(viewport.getZoom(), 1, 'We start out unzoomed'); var viewport = viewer.viewport;
var zoomHandler = function() { equal(viewport.getZoom(), 1, 'We start out unzoomed');
viewer.removeHandler('animationfinish', zoomHandler);
equal(viewport.getZoom(), 2, 'Zoomed correctly');
start();
};
viewer.addHandler('animationfinish', zoomHandler); var zoomHandler = function() {
viewport.zoomTo(2); viewer.removeHandler('animationfinish', zoomHandler);
equal(viewport.getZoom(), 2, 'Zoomed correctly');
start();
};
viewer.addHandler('animationfinish', zoomHandler);
viewport.zoomTo(2);
});
viewer.open('/test/data/testpattern.dzi');
}); });
// ---------- // ----------
asyncTest('Pan', function() { asyncTest('Pan', function() {
var viewport = viewer.viewport; viewer.addHandler("open", function () {
var center = viewport.getCenter(); var viewport = viewer.viewport,
ok(center.x === 0.5 && center.y === 0.5, 'We start out unpanned'); center = viewport.getCenter();
var panHandler = function() { ok(center.x === 0.5 && center.y === 0.5, 'We start out unpanned');
viewer.removeHandler('animationfinish', panHandler);
center = viewport.getCenter();
ok(center.x === 0.1 && center.y === 0.1, 'Panned correctly');
start();
};
viewer.addHandler('animationfinish', panHandler); var panHandler = function() {
viewport.panTo(new OpenSeadragon.Point(0.1, 0.1)); viewer.removeHandler('animationfinish', panHandler);
center = viewport.getCenter();
ok(center.x === 0.1 && center.y === 0.1, 'Panned correctly');
start();
};
viewer.addHandler('animationfinish', panHandler);
viewport.panTo(new OpenSeadragon.Point(0.1, 0.1));
});
viewer.open('/test/data/testpattern.dzi');
}); });
// ---------- // ----------
asyncTest('Home', function() { asyncTest('Home', function() {
var viewport = viewer.viewport; // Test setup:
var center = viewport.getCenter(); function opener() {
ok(center.x !== 0.5 && center.y !== 0.5, 'We start out panned'); var viewport = viewer.viewport;
notEqual(viewport.getZoom(), 1, 'We start out zoomed'); viewport.panTo(new OpenSeadragon.Point(0.1, 0.1));
viewport.zoomTo(2);
}
var homeHandler = function() { function stage1() {
viewer.removeHandler('animationfinish', homeHandler); var viewport = viewer.viewport,
center = viewport.getCenter(); center = viewport.getCenter();
ok(center.x === 0.5 && center.y === 0.5, 'We end up unpanned');
equal(viewport.getZoom(), 1, 'We end up unzoomed');
start();
};
viewer.addHandler('animationfinish', homeHandler); viewer.removeHandler('animationfinish', stage1);
viewport.goHome(true);
ok(center.x !== 0.5 && center.y !== 0.5, 'We start out panned');
notEqual(viewport.getZoom(), 1, 'We start out zoomed');
var homeHandler = function() {
viewer.removeHandler('animationfinish', homeHandler);
center = viewport.getCenter();
ok(center.x === 0.5 && center.y === 0.5, 'We end up unpanned');
equal(viewport.getZoom(), 1, 'We end up unzoomed');
start();
};
viewer.addHandler('animationfinish', homeHandler);
viewport.goHome(true);
}
viewer.addHandler("open", opener);
viewer.addHandler("animationfinish", stage1);
viewer.open('/test/data/testpattern.dzi');
}); });
// ---------- // ----------
asyncTest('Click', function() { asyncTest('Click', function() {
var viewport = viewer.viewport, viewer.addHandler("open", function () {
var viewport = viewer.viewport,
center = viewport.getCenter(); center = viewport.getCenter();
ok(center.x === 0.5 && center.y === 0.5, 'We start out unpanned'); ok(center.x === 0.5 && center.y === 0.5, 'We start out unpanned');
equal(viewport.getZoom(), 1, 'We start out unzoomed'); equal(viewport.getZoom(), 1, 'We start out unzoomed');
var clickHandler = function() { var clickHandler = function() {
viewer.removeHandler('animationfinish', clickHandler); viewer.removeHandler('animationfinish', clickHandler);
center = viewport.getCenter(); center = viewport.getCenter();
ok(center.x > 0.37 && center.x < 0.38 && center.y > 0.37 && center.y < 0.38, 'Panned correctly'); ok(center.x > 0.37 && center.x < 0.38 && center.y > 0.37 && center.y < 0.38, 'Panned correctly');
equal(viewport.getZoom(), 2, 'Zoomed correctly'); equal(viewport.getZoom(), 2, 'Zoomed correctly');
start(); start();
}; };
viewer.addHandler('animationfinish', clickHandler); viewer.addHandler('animationfinish', clickHandler);
Util.simulateViewerClick(viewer, 0.25, 0.25); Util.simulateViewerClick(viewer, 0.25, 0.25);
});
viewer.open('/test/data/testpattern.dzi');
}); });
// ---------- // ----------
test('Fullscreen', function() { asyncTest('Fullscreen', function() {
ok(!viewer.isFullPage(), 'Started out not fullpage'); viewer.addHandler("open", function () {
ok(!$(viewer.element).hasClass('fullpage'), ok(!viewer.isFullPage(), 'Started out not fullpage');
'No fullpage class on div'); ok(!$(viewer.element).hasClass('fullpage'),
'No fullpage class on div');
viewer.setFullPage(true); viewer.setFullPage(true);
ok(viewer.isFullPage(), 'Enabled fullpage'); ok(viewer.isFullPage(), 'Enabled fullpage');
ok($(viewer.element).hasClass('fullpage'), ok($(viewer.element).hasClass('fullpage'),
'Fullpage class added to div'); 'Fullpage class added to div');
viewer.setFullPage(false); viewer.setFullPage(false);
ok(!viewer.isFullPage(), 'Disabled fullpage'); ok(!viewer.isFullPage(), 'Disabled fullpage');
ok(!$(viewer.element).hasClass('fullpage'), ok(!$(viewer.element).hasClass('fullpage'),
'Fullpage class removed from div'); 'Fullpage class removed from div');
start();
});
viewer.open('/test/data/testpattern.dzi');
}); });
// ---------- // ----------
asyncTest('Close', function() { asyncTest('Close', function() {
var closeHandler = function() { viewer.addHandler("open", function () {
viewer.removeHandler('close', closeHandler); var closeHandler = function() {
ok(!viewer.source, 'no source'); viewer.removeHandler('close', closeHandler);
$('#example').empty(); ok(!viewer.source, 'no source');
ok(true, 'Close event was sent'); $('#example').empty();
ok(!viewer._updateRequestId, 'timer is off'); ok(true, 'Close event was sent');
setTimeout(function() { ok(!viewer._updateRequestId, 'timer is off');
ok(!viewer._updateRequestId, 'timer is still off'); setTimeout(function() {
start(); ok(!viewer._updateRequestId, 'timer is still off');
}, 100); start();
}; }, 100);
};
viewer.addHandler('close', closeHandler); viewer.addHandler('close', closeHandler);
viewer.close(); viewer.close();
});
viewer.open('/test/data/testpattern.dzi');
}); });
})(); })();

View File

@ -3,7 +3,13 @@
// This module tests whether our various file formats can be opened. // This module tests whether our various file formats can be opened.
// TODO: Add more file formats (with corresponding test data). // TODO: Add more file formats (with corresponding test data).
module('Formats'); module('Formats', {
setup: function () {
var example = document.createElement("div");
example.id = "example";
document.getElementById("qunit-fixture").appendChild(example);
}
});
var viewer = null; var viewer = null;

View File

@ -1,3 +1,5 @@
/* global QUnit, module, Util, $, console, test, asyncTest, start, ok, equal */
QUnit.config.autostart = false; QUnit.config.autostart = false;
(function () { (function () {
@ -14,14 +16,18 @@ QUnit.config.autostart = false;
topNavigatorClickAdjustment; topNavigatorClickAdjustment;
module("navigator", { module("navigator", {
setup:function () { setup: function () {
Util.resetDom(); Util.initializeTestDOM();
resetTestVariables(); resetTestVariables();
$(document).scrollTop(0); $(document).scrollTop(0);
$(document).scrollLeft(0); $(document).scrollLeft(0);
}, },
teardown:function () { teardown: function () {
Util.resetDom(); // jQuery UI creates its controls outside the normal DOM hierarchy which QUnit cleans up:
if ($('#exampleNavigator').is(':ui-dialog')) {
$('#exampleNavigator').dialog('destroy');
}
resetTestVariables(); resetTestVariables();
} }
}); });
@ -31,7 +37,7 @@ QUnit.config.autostart = false;
}); });
var resetTestVariables = function () { var resetTestVariables = function () {
if (viewer != null) { if (viewer) {
viewer.close(); viewer.close();
} }
displayRegion = null; displayRegion = null;
@ -125,11 +131,11 @@ QUnit.config.autostart = false;
viewerAndNavigatorDisplayReady = viewer.drawer !== null && viewerAndNavigatorDisplayReady = viewer.drawer !== null &&
!viewer.drawer.needsUpdate() && !viewer.drawer.needsUpdate() &&
currentDisplayWidth > 0 && currentDisplayWidth > 0 &&
Util.equalsWithVariance(lastDisplayRegionLeft, currentDisplayRegionLeft, .0001) && Util.equalsWithVariance(lastDisplayRegionLeft, currentDisplayRegionLeft, 0.0001) &&
Util.equalsWithVariance(lastDisplayWidth, currentDisplayWidth, .0001) && Util.equalsWithVariance(lastDisplayWidth, currentDisplayWidth, 0.0001) &&
Util.equalsWithVariance(viewer.viewport.getBounds(true).x, viewer.viewport.getBounds().x, .0001) && Util.equalsWithVariance(viewer.viewport.getBounds(true).x, viewer.viewport.getBounds().x, 0.0001) &&
Util.equalsWithVariance(viewer.viewport.getBounds(true).y, viewer.viewport.getBounds().y, .0001) && Util.equalsWithVariance(viewer.viewport.getBounds(true).y, viewer.viewport.getBounds().y, 0.0001) &&
Util.equalsWithVariance(viewer.viewport.getBounds(true).width, viewer.viewport.getBounds().width, .0001); Util.equalsWithVariance(viewer.viewport.getBounds(true).width, viewer.viewport.getBounds().width, 0.0001);
} }
catch (err) { catch (err) {
//Ignore. Subsequent code will try again shortly //Ignore. Subsequent code will try again shortly
@ -138,7 +144,7 @@ QUnit.config.autostart = false;
count++; count++;
setTimeout(function () { setTimeout(function () {
waitForViewer(handler, count, currentDisplayRegionLeft, currentDisplayWidth); waitForViewer(handler, count, currentDisplayRegionLeft, currentDisplayWidth);
}, 100) }, 100);
} }
else { else {
if (count === 40) { if (count === 40) {
@ -201,21 +207,21 @@ QUnit.config.autostart = false;
expecteYCoordinate = 1 / viewer.source.aspectRatio - viewer.viewport.getBounds().height; expecteYCoordinate = 1 / viewer.source.aspectRatio - viewer.viewport.getBounds().height;
} }
if (viewer.viewport.getBounds().width < 1) { if (viewer.viewport.getBounds().width < 1) {
Util.assessNumericValue(expectedXCoordinate, viewer.viewport.getBounds().x, .04, ' Viewer at ' + theContentCorner + ', x coord'); Util.assessNumericValue(expectedXCoordinate, viewer.viewport.getBounds().x, 0.04, ' Viewer at ' + theContentCorner + ', x coord');
} }
if (viewer.viewport.getBounds().height < 1 / viewer.source.aspectRatio) { if (viewer.viewport.getBounds().height < 1 / viewer.source.aspectRatio) {
Util.assessNumericValue(expecteYCoordinate, viewer.viewport.getBounds().y, .04, ' Viewer at ' + theContentCorner + ', y coord'); Util.assessNumericValue(expecteYCoordinate, viewer.viewport.getBounds().y, 0.04, ' Viewer at ' + theContentCorner + ', y coord');
} }
} };
}; };
var assessViewerInCenter = function () { var assessViewerInCenter = function () {
var yPositionVariance = .04; var yPositionVariance = 0.04;
if (viewer.source.aspectRatio < 1) { if (viewer.source.aspectRatio < 1) {
yPositionVariance = yPositionVariance / viewer.source.aspectRatio; yPositionVariance = yPositionVariance / viewer.source.aspectRatio;
} }
Util.assessNumericValue(1 / viewer.source.aspectRatio / 2, viewer.viewport.getCenter().y, yPositionVariance, ' Viewer at center, y coord'); Util.assessNumericValue(1 / viewer.source.aspectRatio / 2, viewer.viewport.getCenter().y, yPositionVariance, ' Viewer at center, y coord');
Util.assessNumericValue(.5, viewer.viewport.getCenter().x, .4, ' Viewer at center, x coord'); Util.assessNumericValue(0.5, viewer.viewport.getCenter().x, 0.4, ' Viewer at center, x coord');
}; };
var clickOnNavigator = function (theContentCorner) { var clickOnNavigator = function (theContentCorner) {
@ -239,7 +245,7 @@ QUnit.config.autostart = false;
yPos = contentStartFromTop + displayRegionHeight; yPos = contentStartFromTop + displayRegionHeight;
} }
simulateNavigatorClick(viewer.navigator, xPos, yPos); simulateNavigatorClick(viewer.navigator, xPos, yPos);
} };
}; };
var dragNavigatorBackToCenter = function () { var dragNavigatorBackToCenter = function () {
@ -354,11 +360,12 @@ QUnit.config.autostart = false;
clientX:1, clientX:1,
clientY:1 clientY:1
}; };
mainViewerElement.simulate('blur', event);
$("#" + seadragonProperties.id).simulate('blur', event);
if (testProperties.expectedAutoFade) { if (testProperties.expectedAutoFade) {
setTimeout(assessAutoFadeTriggered,autoFadeWaitTime); setTimeout(assessAutoFadeTriggered,autoFadeWaitTime);
} } else {
else {
setTimeout(assessAutoFadeDisabled,autoFadeWaitTime); setTimeout(assessAutoFadeDisabled,autoFadeWaitTime);
} }
} }

View File

@ -10,12 +10,6 @@
<body> <body>
<div id="qunit"></div> <div id="qunit"></div>
<div id="qunit-fixture"></div> <div id="qunit-fixture"></div>
<div>
<div id="example"></div>
<div id="exampleNavigator"></div>
</div>
<div id="wideexample"></div>
<div id="tallexample"></div>
<script src="/node_modules/grunt-contrib-qunit/test/libs/qunit.js"></script> <script src="/node_modules/grunt-contrib-qunit/test/libs/qunit.js"></script>
<script src="/test/lib/jquery-1.9.1.min.js"></script> <script src="/test/lib/jquery-1.9.1.min.js"></script>
<script src="/test/lib/jquery-ui-1.10.2/js/jquery-ui-1.10.2.min.js"></script> <script src="/test/lib/jquery-ui-1.10.2/js/jquery-ui-1.10.2.min.js"></script>

View File

@ -1,3 +1,5 @@
/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util */
(function() { (function() {
// ---------- // ----------
@ -30,16 +32,11 @@
.simulate('mouseup', event); .simulate('mouseup', event);
}, },
resetDom: function () { initializeTestDOM: function () {
if ($('#exampleNavigator').is(':ui-dialog')) { $("#qunit-fixture")
$('#exampleNavigator').dialog('destroy'); .append('<div><div id="example"></div><div id="exampleNavigator"></div></div>')
} .append('<div id="wideexample"></div>')
$("#exampleNavigator").remove(); .append('<div id="tallexample"></div>');
$(".navigator").remove();
$("#example").empty();
$("#tallexample").empty();
$("#wideexample").empty();
$("#example").parent().append('<div id="exampleNavigator"></div>');
}, },
equalsWithVariance: function (value1, value2, variance) { equalsWithVariance: function (value1, value2, variance) {