openseadragon/test/basic.js

246 lines
8.0 KiB
JavaScript
Raw Normal View History

/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util, testLog */
2013-02-14 22:21:07 +04:00
(function() {
var viewer;
2013-02-14 22:21:07 +04:00
module('Basic', {
setup: function () {
var example = $('<div id="example"></div>').appendTo("#qunit-fixture");
testLog.reset();
2013-03-07 23:52:01 +04:00
viewer = OpenSeadragon({
2013-03-07 22:44:08 +04:00
id: 'example',
prefixUrl: '/build/openseadragon/images/',
springStiffness: 100 // Faster animation = faster tests
2013-03-07 22:44:08 +04:00
});
},
teardown: function () {
2013-07-02 21:56:55 +04:00
if (viewer && viewer.close) {
viewer.close();
}
viewer = null;
}
});
// ----------
asyncTest('Open', function() {
ok(viewer, 'Viewer exists');
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();
};
2013-03-07 22:44:08 +04:00
viewer.addHandler('open', openHandler);
viewer.open('/test/data/testpattern.dzi');
});
2013-03-07 23:52:01 +04:00
asyncTest('Open Error Handling', function() {
ok(viewer, 'Viewer exists');
2013-03-07 23:52:01 +04:00
viewer.addHandler('open', function(eventSender, eventData) {
ok(false, "The open event should not fire for failed opens");
start();
2013-03-07 22:44:08 +04:00
});
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");
ok(testLog.log.contains('["AJAX request returned %s: %s",404,"/test/data/not-a-real-file"]'),
"AJAX failures should be logged to the console");
start();
});
viewer.open('/test/data/not-a-real-file');
2013-03-07 23:52:01 +04:00
});
// ----------
asyncTest('Zoom', function() {
viewer.addHandler("open", function () {
var viewport = viewer.viewport;
2013-03-07 23:52:01 +04:00
equal(viewport.getZoom(), 1, 'We start out unzoomed');
var zoomHandler = function() {
viewer.removeHandler('animation-finish', zoomHandler);
equal(viewport.getZoom(), 2, 'Zoomed correctly');
start();
};
2013-03-07 23:52:01 +04:00
viewer.addHandler('animation-finish', zoomHandler);
viewport.zoomTo(2);
});
viewer.open('/test/data/testpattern.dzi');
2013-03-07 23:52:01 +04:00
});
// ----------
asyncTest('Pan', function() {
viewer.addHandler("open", function () {
var viewport = viewer.viewport,
center = viewport.getCenter();
2013-03-07 23:52:01 +04:00
ok(center.x === 0.5 && center.y === 0.5, 'We start out unpanned');
var panHandler = function() {
viewer.removeHandler('animation-finish', panHandler);
center = viewport.getCenter();
ok(center.x === 0.1 && center.y === 0.1, 'Panned correctly');
start();
};
viewer.addHandler('animation-finish', panHandler);
viewport.panTo(new OpenSeadragon.Point(0.1, 0.1));
});
2013-03-07 23:52:01 +04:00
viewer.open('/test/data/testpattern.dzi');
2013-03-07 23:52:01 +04:00
});
2013-03-22 00:44:22 +04:00
// ----------
asyncTest('Home', function() {
// Test setup:
function opener() {
var viewport = viewer.viewport;
viewport.panTo(new OpenSeadragon.Point(0.1, 0.1));
viewport.zoomTo(2);
}
function stage1() {
var viewport = viewer.viewport,
center = viewport.getCenter();
viewer.removeHandler('animation-finish', stage1);
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('animation-finish', 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();
};
2013-03-22 00:44:22 +04:00
viewer.addHandler('animation-finish', homeHandler);
viewport.goHome(true);
}
viewer.addHandler("open", opener);
viewer.addHandler("animation-finish", stage1);
2013-03-22 00:44:22 +04:00
viewer.open('/test/data/testpattern.dzi');
2013-03-22 00:44:22 +04:00
});
// ----------
asyncTest('Click', function() {
viewer.addHandler("open", function () {
var viewport = viewer.viewport,
center = viewport.getCenter();
ok(center.x === 0.5 && center.y === 0.5, 'We start out unpanned');
equal(viewport.getZoom(), 1, 'We start out unzoomed');
2013-03-22 00:44:22 +04:00
var clickHandler = function() {
viewer.removeHandler('animation-finish', clickHandler);
center = viewport.getCenter();
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');
start();
};
2013-03-22 00:44:22 +04:00
viewer.addHandler('animation-finish', clickHandler);
Util.simulateViewerClickWithDrag( {
viewer: viewer,
widthFactor: 0.25,
heightFactor: 0.25,
dragCount: 0,
dragDx: 0,
dragDy: 0
} );
} );
viewer.open('/test/data/testpattern.dzi');
2013-03-22 00:44:22 +04:00
});
// ----------
asyncTest('Fullscreen', function() {
viewer.addHandler("open", function () {
ok(!viewer.isFullPage(), 'Started out not fullpage');
ok(!$(viewer.element).hasClass('fullpage'),
'No fullpage class on div');
viewer.setFullPage(true);
ok(viewer.isFullPage(), 'Enabled fullpage');
ok($(viewer.element).hasClass('fullpage'),
'Fullpage class added to div');
viewer.setFullPage(false);
ok(!viewer.isFullPage(), 'Disabled fullpage');
ok(!$(viewer.element).hasClass('fullpage'),
'Fullpage class removed from div');
start();
});
viewer.open('/test/data/testpattern.dzi');
});
2013-03-07 23:52:01 +04:00
// ----------
asyncTest('Close', function() {
viewer.addHandler("open", function () {
var closeHandler = function() {
viewer.removeHandler('close', closeHandler);
ok(!viewer.source, 'no source');
ok(true, 'Close event was sent');
ok(!viewer._updateRequestId, 'timer is off');
setTimeout(function() {
ok(!viewer._updateRequestId, 'timer is still off');
start();
}, 100);
};
2013-03-07 23:52:01 +04:00
viewer.addHandler('close', closeHandler);
viewer.close();
});
viewer.open('/test/data/testpattern.dzi');
2013-03-07 23:52:01 +04:00
});
// ----------
asyncTest('Destroy', function() {
viewer.addHandler("open", function () {
// Check that the DOM has been modified
notEqual(0, $('#example').children().length);
2013-08-13 04:33:12 +04:00
var closeCalled = false;
var closeHandler = function() {
viewer.removeHandler('close', closeHandler);
2013-08-13 04:33:12 +04:00
closeCalled = true;
};
viewer.addHandler('close', closeHandler);
viewer.destroy();
// Check that the DOM has been cleaned up
equal(0, $('#example').children().length);
equal(null, viewer.canvas);
equal(null, viewer.keyboardCommandArea);
equal(null, viewer.container);
equal(null, viewer.element);
2013-08-13 04:33:12 +04:00
equal(true, closeCalled);
start();
});
viewer.open('/test/data/testpattern.dzi');
});
2013-02-14 22:21:07 +04:00
})();