openseadragon/test/modules/world.js

256 lines
9.2 KiB
JavaScript
Raw Normal View History

2017-12-07 04:20:39 +03:00
/* global QUnit, $, testLog */
2014-11-18 03:24:40 +03:00
(function() {
var viewer;
2017-12-07 04:20:39 +03:00
QUnit.module('World', {
beforeEach: function () {
$('<div id="example"></div>').appendTo("#qunit-fixture");
2014-11-18 03:24:40 +03:00
testLog.reset();
viewer = OpenSeadragon({
id: 'example',
prefixUrl: '/build/openseadragon/images/',
springStiffness: 100 // Faster animation = faster tests
});
},
2017-12-07 04:20:39 +03:00
afterEach: function () {
2014-11-18 03:24:40 +03:00
if (viewer && viewer.close) {
viewer.close();
}
if (viewer && viewer.destroy){
viewer.destroy();
}
2014-11-18 03:24:40 +03:00
viewer = null;
}
});
// ----------
2017-12-07 04:20:39 +03:00
var checkBounds = function(assert, expected, message) {
2014-11-18 03:24:40 +03:00
var bounds = viewer.world.getHomeBounds();
2017-12-07 04:20:39 +03:00
assert.ok(bounds.equals(expected), message + ' ' + bounds.toString());
2014-11-18 03:24:40 +03:00
};
// ----------
2017-12-07 04:20:39 +03:00
QUnit.test('adding a tiled image', function(assert) {
var done = assert.async();
assert.ok(viewer.world, 'World exists');
2014-11-18 03:24:40 +03:00
viewer.world.addHandler('add-item', function(event) {
2017-12-07 04:20:39 +03:00
assert.ok(event, 'add-item handler received event data');
assert.equal(event.eventSource, viewer.world, 'sender of add-item event was world');
assert.ok(event.item, 'add-item event includes item');
assert.equal(viewer.world.getItemCount(), 1, 'there is now 1 item');
assert.equal(event.item, viewer.world.getItemAt(0), 'item is accessible via getItemAt');
assert.equal(viewer.world.getIndexOfItem(event.item), 0, 'item index is 0');
done();
2014-11-18 03:24:40 +03:00
});
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.getItemCount(), 0, 'no items to start with');
2014-11-18 03:24:40 +03:00
viewer.open('/test/data/testpattern.dzi');
});
// ----------
2017-12-07 04:20:39 +03:00
QUnit.test('metrics', function(assert) {
var done = assert.async();
2014-11-18 03:24:40 +03:00
viewer.addHandler('open', function(event) {
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 4, 4), 'bounds after open');
2014-11-18 03:24:40 +03:00
var expectedContentFactor = viewer.world.getItemAt(1).getContentSize().x / 2;
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.getContentFactor(), expectedContentFactor, 'content factor has changed');
2014-11-18 03:24:40 +03:00
viewer.world.addHandler('metrics-change', function metricsChangeHandler(event) {
viewer.world.removeHandler('metrics-change', metricsChangeHandler);
2017-12-07 04:20:39 +03:00
assert.ok(event, 'metrics-change handler received event data');
assert.equal(event.eventSource, viewer.world, 'sender of metrics-change event was world');
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 7, 12), 'bounds after position');
2014-11-18 03:24:40 +03:00
viewer.world.getItemAt(0).setWidth(20);
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 20, 20), 'bounds after size');
2014-11-18 03:24:40 +03:00
2017-12-07 04:20:39 +03:00
done();
2014-11-18 03:24:40 +03:00
});
viewer.world.getItemAt(1).setPosition(new OpenSeadragon.Point(5, 10));
});
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 1, 1), 'default bounds');
assert.equal(viewer.world.getContentFactor(), 1, 'default content factor');
2014-11-18 03:24:40 +03:00
viewer.open([
{
tileSource: '/test/data/testpattern.dzi',
width: 4
}, {
tileSource: '/test/data/testpattern.dzi',
width: 2
}
]);
});
// ----------
2017-12-07 04:20:39 +03:00
QUnit.test('remove/reorder tiled images', function(assert) {
var done = assert.async();
2014-11-18 03:24:40 +03:00
var handlerCount = 0;
viewer.addHandler('open', function(event) {
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.getItemCount(), 3, 'there are now 3 items');
2014-11-18 03:24:40 +03:00
var item0 = viewer.world.getItemAt(0);
var item1 = viewer.world.getItemAt(1);
viewer.world.addHandler('item-index-change', function(event) {
handlerCount++;
2017-12-07 04:20:39 +03:00
assert.ok(event, 'item-index-change handler received event data');
assert.equal(event.eventSource, viewer.world, 'sender of item-index-change event was world');
assert.equal(event.item, item0, 'item-index-change event includes correct item');
assert.equal(event.newIndex, 1, 'item-index-change event includes correct newIndex');
assert.equal(event.previousIndex, 0, 'item-index-change event includes correct previousIndex');
assert.equal(viewer.world.getItemAt(0), item1, 'item1 is now at index 0');
assert.equal(viewer.world.getItemAt(1), item0, 'item0 is now at index 1');
2014-11-18 03:24:40 +03:00
});
viewer.world.setItemIndex(item0, 1);
viewer.world.addHandler('remove-item', function removeHandler(event) {
viewer.world.removeHandler('remove-item', removeHandler);
handlerCount++;
2017-12-07 04:20:39 +03:00
assert.ok(event, 'remove-item handler received event data');
assert.equal(event.eventSource, viewer.world, 'sender of remove-item event was world');
assert.equal(event.item, item1, 'remove-item event includes correct item');
assert.equal(viewer.world.getItemCount(), 2, 'after removal, only two items remain');
assert.equal(viewer.world.getItemAt(0), item0, 'item0 is now at index 0');
2014-11-18 03:24:40 +03:00
});
viewer.world.removeItem(item1);
var removeCount = 0;
viewer.world.addHandler('remove-item', function() {
removeCount++;
if (removeCount === 2) {
handlerCount++;
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.getItemCount(), 0, 'after removeAll, no items remain');
2014-11-18 03:24:40 +03:00
}
});
viewer.world.removeAll();
2017-12-07 04:20:39 +03:00
assert.equal(handlerCount, 3, 'correct number of handlers called');
done();
2014-11-18 03:24:40 +03:00
});
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.getItemCount(), 0, 'no items to start with');
2014-11-18 03:24:40 +03:00
viewer.open([
'/test/data/testpattern.dzi',
'/test/data/testpattern.dzi',
'/test/data/testpattern.dzi'
]);
});
// ----------
2017-12-07 04:20:39 +03:00
QUnit.test('draw', function(assert) {
var done = assert.async();
2014-11-18 03:24:40 +03:00
var handlerCount = 0;
viewer.addHandler('open', function(event) {
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.needsDraw(), true, 'needs draw after open');
2014-11-18 03:24:40 +03:00
viewer.addHandler('update-level', function updateHandler() {
viewer.removeHandler('update-level', updateHandler);
handlerCount++;
});
2023-06-08 22:10:55 +03:00
viewer.world.update();
2014-11-18 03:24:40 +03:00
2017-12-07 04:20:39 +03:00
assert.equal(handlerCount, 1, 'correct number of handlers called');
done();
2014-11-18 03:24:40 +03:00
});
2017-12-07 04:20:39 +03:00
assert.equal(viewer.world.needsDraw(), false, 'needs no draw at first');
2014-11-18 03:24:40 +03:00
viewer.open('/test/data/testpattern.dzi');
});
// ----------
2017-12-07 04:20:39 +03:00
QUnit.test('resetItems', function(assert) {
var done = assert.async();
2014-11-18 03:24:40 +03:00
viewer.addHandler('tile-drawn', function updateHandler() {
viewer.removeHandler('tile-drawn', updateHandler);
2017-12-07 04:20:39 +03:00
assert.ok(viewer.tileCache.numTilesLoaded() > 0, 'we have tiles after tile-drawn');
2014-11-18 03:24:40 +03:00
viewer.world.resetItems();
2017-12-07 04:20:39 +03:00
assert.equal(viewer.tileCache.numTilesLoaded(), 0, 'no tiles after reset');
done();
2014-11-18 03:24:40 +03:00
});
2017-12-07 04:20:39 +03:00
assert.equal(viewer.tileCache.numTilesLoaded(), 0, 'no tiles at start');
2014-11-18 03:24:40 +03:00
viewer.open('/test/data/testpattern.dzi');
});
// ----------
2017-12-07 04:20:39 +03:00
QUnit.test('arrange', function(assert) {
var done = assert.async();
2014-11-18 03:24:40 +03:00
viewer.addHandler('open', function(event) {
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 1, 1), 'all stacked');
2014-11-18 03:24:40 +03:00
viewer.world.arrange({
layout: 'horizontal',
rows: 1,
tileSize: 1,
tileMargin: 0.5
});
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 4, 1), 'one horizontal row');
2014-11-18 03:24:40 +03:00
viewer.world.arrange({
layout: 'horizontal',
rows: 2,
tileSize: 1,
tileMargin: 0.5
});
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 2.5, 2.5), 'grid');
2014-11-18 03:24:40 +03:00
viewer.world.arrange({
layout: 'vertical',
rows: 1,
tileSize: 1,
tileMargin: 0.5
});
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 1, 4), 'one vertical column');
2014-11-18 03:24:40 +03:00
viewer.world.arrange({
layout: 'horizontal',
rows: false,
columns: 3,
tileSize: 1,
tileMargin: 0.5
});
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 4, 1), 'three horizontal columns (one horizontal row)');
viewer.world.arrange({
layout: 'vertical',
rows: false,
columns: 3,
tileSize: 1,
tileMargin: 0.5
});
2017-12-07 04:20:39 +03:00
checkBounds(assert, new OpenSeadragon.Rect(0, 0, 1, 4), 'three vertical rows (one vertical column)');
2017-12-07 04:20:39 +03:00
done();
2014-11-18 03:24:40 +03:00
});
viewer.open([
'/test/data/testpattern.dzi',
'/test/data/testpattern.dzi',
'/test/data/testpattern.dzi'
]);
});
})();