2017-12-07 04:20:39 +03:00
|
|
|
/* global QUnit, testLog */
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
(function() {
|
2023-11-18 22:16:35 +03:00
|
|
|
let viewer;
|
|
|
|
|
|
|
|
//we override jobs: remember original function
|
|
|
|
const originalJob = OpenSeadragon.ImageLoader.prototype.addJob;
|
|
|
|
|
|
|
|
//event awaiting
|
|
|
|
function waitFor(predicate) {
|
|
|
|
const time = setInterval(() => {
|
|
|
|
if (predicate()) {
|
|
|
|
clearInterval(time);
|
|
|
|
}
|
|
|
|
}, 20);
|
|
|
|
}
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
// ----------
|
2017-12-07 04:20:39 +03:00
|
|
|
QUnit.module('TileCache', {
|
|
|
|
beforeEach: function () {
|
2023-11-18 22:16:35 +03:00
|
|
|
$('<div id="example"></div>').appendTo("#qunit-fixture");
|
|
|
|
|
2014-11-20 22:51:24 +03:00
|
|
|
testLog.reset();
|
2023-11-18 22:16:35 +03:00
|
|
|
|
|
|
|
viewer = OpenSeadragon({
|
|
|
|
id: 'example',
|
|
|
|
prefixUrl: '/build/openseadragon/images/',
|
|
|
|
maxImageCacheCount: 200, //should be enough to fit test inside the cache
|
|
|
|
springStiffness: 100 // Faster animation = faster tests
|
|
|
|
});
|
|
|
|
OpenSeadragon.ImageLoader.prototype.addJob = originalJob;
|
2014-11-20 22:51:24 +03:00
|
|
|
},
|
2017-12-07 04:20:39 +03:00
|
|
|
afterEach: function () {
|
2023-11-18 22:16:35 +03:00
|
|
|
if (viewer && viewer.close) {
|
|
|
|
viewer.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer = null;
|
2014-11-20 22:51:24 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ----------
|
2017-12-07 04:20:39 +03:00
|
|
|
// TODO: this used to be async
|
|
|
|
QUnit.test('basics', function(assert) {
|
|
|
|
var done = assert.async();
|
2015-06-11 22:27:36 +03:00
|
|
|
var fakeViewer = {
|
|
|
|
raiseEvent: function() {}
|
|
|
|
};
|
|
|
|
var fakeTiledImage0 = {
|
2022-04-16 22:19:54 +03:00
|
|
|
viewer: fakeViewer,
|
2022-04-30 00:45:01 +03:00
|
|
|
source: OpenSeadragon.TileSource.prototype
|
2015-06-11 22:27:36 +03:00
|
|
|
};
|
|
|
|
var fakeTiledImage1 = {
|
2022-04-16 22:19:54 +03:00
|
|
|
viewer: fakeViewer,
|
2022-04-30 00:45:01 +03:00
|
|
|
source: OpenSeadragon.TileSource.prototype
|
2015-06-11 22:27:36 +03:00
|
|
|
};
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
var fakeTile0 = {
|
|
|
|
url: 'foo.jpg',
|
2016-10-22 00:28:12 +03:00
|
|
|
cacheKey: 'foo.jpg',
|
2014-11-20 22:51:24 +03:00
|
|
|
image: {},
|
2023-09-08 09:47:43 +03:00
|
|
|
loaded: true,
|
|
|
|
tiledImage: fakeTiledImage0,
|
|
|
|
_caches: [],
|
2014-11-20 22:51:24 +03:00
|
|
|
unload: function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var fakeTile1 = {
|
|
|
|
url: 'foo.jpg',
|
2016-10-22 00:28:12 +03:00
|
|
|
cacheKey: 'foo.jpg',
|
2014-11-20 22:51:24 +03:00
|
|
|
image: {},
|
2023-09-08 09:47:43 +03:00
|
|
|
loaded: true,
|
|
|
|
tiledImage: fakeTiledImage1,
|
|
|
|
_caches: [],
|
2014-11-20 22:51:24 +03:00
|
|
|
unload: function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var cache = new OpenSeadragon.TileCache();
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 0, 'no tiles to begin with');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.cacheTile({
|
|
|
|
tile: fakeTile0,
|
|
|
|
tiledImage: fakeTiledImage0
|
|
|
|
});
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 1, 'tile count after cache');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.cacheTile({
|
|
|
|
tile: fakeTile1,
|
|
|
|
tiledImage: fakeTiledImage1
|
|
|
|
});
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 2, 'tile count after second cache');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.clearTilesFor(fakeTiledImage0);
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 1, 'tile count after first clear');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.clearTilesFor(fakeTiledImage1);
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 0, 'tile count after second clear');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
done();
|
2014-11-20 22:51:24 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------
|
2017-12-07 04:20:39 +03:00
|
|
|
QUnit.test('maxImageCacheCount', function(assert) {
|
|
|
|
var done = assert.async();
|
2015-06-11 22:27:36 +03:00
|
|
|
var fakeViewer = {
|
|
|
|
raiseEvent: function() {}
|
|
|
|
};
|
|
|
|
var fakeTiledImage0 = {
|
2022-04-16 22:19:54 +03:00
|
|
|
viewer: fakeViewer,
|
2022-04-30 00:45:01 +03:00
|
|
|
source: OpenSeadragon.TileSource.prototype
|
2015-06-11 22:27:36 +03:00
|
|
|
};
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
var fakeTile0 = {
|
|
|
|
url: 'different.jpg',
|
2016-10-22 00:28:12 +03:00
|
|
|
cacheKey: 'different.jpg',
|
2014-11-20 22:51:24 +03:00
|
|
|
image: {},
|
2023-09-08 09:47:43 +03:00
|
|
|
loaded: true,
|
|
|
|
tiledImage: fakeTiledImage0,
|
|
|
|
_caches: [],
|
2014-11-20 22:51:24 +03:00
|
|
|
unload: function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var fakeTile1 = {
|
|
|
|
url: 'same.jpg',
|
2016-10-22 00:28:12 +03:00
|
|
|
cacheKey: 'same.jpg',
|
2014-11-20 22:51:24 +03:00
|
|
|
image: {},
|
2023-09-08 09:47:43 +03:00
|
|
|
loaded: true,
|
|
|
|
tiledImage: fakeTiledImage0,
|
|
|
|
_caches: [],
|
2014-11-20 22:51:24 +03:00
|
|
|
unload: function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var fakeTile2 = {
|
|
|
|
url: 'same.jpg',
|
2016-10-22 00:28:12 +03:00
|
|
|
cacheKey: 'same.jpg',
|
2014-11-20 22:51:24 +03:00
|
|
|
image: {},
|
2023-09-08 09:47:43 +03:00
|
|
|
loaded: true,
|
|
|
|
tiledImage: fakeTiledImage0,
|
|
|
|
_caches: [],
|
2014-11-20 22:51:24 +03:00
|
|
|
unload: function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
var cache = new OpenSeadragon.TileCache({
|
|
|
|
maxImageCacheCount: 1
|
|
|
|
});
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 0, 'no tiles to begin with');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.cacheTile({
|
|
|
|
tile: fakeTile0,
|
|
|
|
tiledImage: fakeTiledImage0
|
|
|
|
});
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 1, 'tile count after add');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.cacheTile({
|
|
|
|
tile: fakeTile1,
|
|
|
|
tiledImage: fakeTiledImage0
|
|
|
|
});
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 1, 'tile count after add of second image');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
cache.cacheTile({
|
|
|
|
tile: fakeTile2,
|
|
|
|
tiledImage: fakeTiledImage0
|
|
|
|
});
|
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
assert.equal(cache.numTilesLoaded(), 2, 'tile count after additional same image');
|
2014-11-20 22:51:24 +03:00
|
|
|
|
2017-12-07 04:20:39 +03:00
|
|
|
done();
|
2014-11-20 22:51:24 +03:00
|
|
|
});
|
|
|
|
|
2023-11-18 22:16:35 +03:00
|
|
|
QUnit.test('Zombie Cache', function(test) {
|
|
|
|
const done = test.async();
|
|
|
|
|
|
|
|
//test jobs by coverage: fail if
|
|
|
|
let jobCounter = 0, coverage = undefined;
|
|
|
|
OpenSeadragon.ImageLoader.prototype.addJob = function (options) {
|
|
|
|
jobCounter++;
|
|
|
|
if (coverage) {
|
|
|
|
//old coverage of previous tiled image: if loaded, fail --> should be in cache
|
|
|
|
const coverageItem = coverage[options.tile.level][options.tile.x][options.tile.y];
|
|
|
|
test.ok(!coverageItem, "Attempt to add job for tile that is not in cache OK if previously not loaded.");
|
|
|
|
}
|
|
|
|
return originalJob.call(this, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
let tilesFinished = 0;
|
|
|
|
const tileCounter = function (event) {tilesFinished++;}
|
|
|
|
|
|
|
|
const openHandler = function(event) {
|
|
|
|
event.item.allowZombieCache(true);
|
|
|
|
|
|
|
|
viewer.world.removeHandler('add-item', openHandler);
|
|
|
|
test.ok(jobCounter === 0, 'Initial state, no images loaded');
|
|
|
|
|
|
|
|
waitFor(() => {
|
|
|
|
if (tilesFinished === jobCounter && event.item._fullyLoaded) {
|
|
|
|
coverage = $.extend(true, {}, event.item.coverage);
|
|
|
|
viewer.world.removeAll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
let jobsAfterRemoval = 0;
|
|
|
|
const removalHandler = function (event) {
|
|
|
|
viewer.world.removeHandler('remove-item', removalHandler);
|
|
|
|
test.ok(jobCounter > 0, 'Tiled image removed after 100 ms, should load some images.');
|
|
|
|
jobsAfterRemoval = jobCounter;
|
|
|
|
|
|
|
|
viewer.world.addHandler('add-item', reopenHandler);
|
|
|
|
viewer.addTiledImage({
|
|
|
|
tileSource: '/test/data/testpattern.dzi'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const reopenHandler = function (event) {
|
|
|
|
event.item.allowZombieCache(true);
|
|
|
|
|
|
|
|
viewer.removeHandler('add-item', reopenHandler);
|
|
|
|
test.equal(jobCounter, jobsAfterRemoval, 'Reopening image does not fetch any tiles imemdiatelly.');
|
|
|
|
|
|
|
|
waitFor(() => {
|
|
|
|
if (event.item._fullyLoaded) {
|
|
|
|
viewer.removeHandler('tile-unloaded', unloadTileHandler);
|
|
|
|
viewer.removeHandler('tile-loaded', tileCounter);
|
|
|
|
|
|
|
|
//console test needs here explicit removal to finish correctly
|
|
|
|
OpenSeadragon.ImageLoader.prototype.addJob = originalJob;
|
|
|
|
done();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const unloadTileHandler = function (event) {
|
|
|
|
test.equal(event.destroyed, false, "Tile unload event should not delete with zombies!");
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer.world.addHandler('add-item', openHandler);
|
|
|
|
viewer.world.addHandler('remove-item', removalHandler);
|
|
|
|
viewer.addHandler('tile-unloaded', unloadTileHandler);
|
|
|
|
viewer.addHandler('tile-loaded', tileCounter);
|
|
|
|
|
|
|
|
viewer.open('/test/data/testpattern.dzi');
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('Zombie Cache Replace Item', function(test) {
|
|
|
|
const done = test.async();
|
|
|
|
|
|
|
|
//test jobs by coverage: fail if
|
|
|
|
let jobCounter = 0, coverage = undefined;
|
|
|
|
OpenSeadragon.ImageLoader.prototype.addJob = function (options) {
|
|
|
|
jobCounter++;
|
|
|
|
if (coverage) {
|
|
|
|
//old coverage of previous tiled image: if loaded, fail --> should be in cache
|
|
|
|
const coverageItem = coverage[options.tile.level][options.tile.x][options.tile.y];
|
|
|
|
if (!coverageItem) {
|
|
|
|
console.warn(coverage, coverage[options.tile.level][options.tile.x], options.tile);
|
|
|
|
}
|
|
|
|
test.ok(!coverageItem, "Attempt to add job for tile data that was previously loaded.");
|
|
|
|
}
|
|
|
|
return originalJob.call(this, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
let tilesFinished = 0;
|
|
|
|
const tileCounter = function (event) {tilesFinished++;}
|
|
|
|
|
|
|
|
const openHandler = function(event) {
|
|
|
|
event.item.allowZombieCache(true);
|
|
|
|
viewer.world.removeHandler('add-item', openHandler);
|
|
|
|
viewer.world.addHandler('add-item', reopenHandler);
|
|
|
|
|
2023-11-19 18:14:28 +03:00
|
|
|
waitFor(() => {
|
2023-11-18 22:16:35 +03:00
|
|
|
if (tilesFinished === jobCounter && event.item._fullyLoaded) {
|
|
|
|
coverage = $.extend(true, {}, event.item.coverage);
|
|
|
|
viewer.addTiledImage({
|
|
|
|
tileSource: '/test/data/testpattern.dzi',
|
|
|
|
index: 0,
|
2023-11-19 18:14:28 +03:00
|
|
|
replace: true
|
2023-11-18 22:16:35 +03:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const reopenHandler = function (event) {
|
|
|
|
event.item.allowZombieCache(true);
|
|
|
|
|
|
|
|
viewer.removeHandler('add-item', reopenHandler);
|
|
|
|
waitFor(() => {
|
|
|
|
if (event.item._fullyLoaded) {
|
|
|
|
viewer.removeHandler('tile-unloaded', unloadTileHandler);
|
|
|
|
viewer.removeHandler('tile-loaded', tileCounter);
|
|
|
|
|
|
|
|
//console test needs here explicit removal to finish correctly
|
|
|
|
OpenSeadragon.ImageLoader.prototype.addJob = originalJob;
|
|
|
|
done();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const unloadTileHandler = function (event) {
|
|
|
|
test.equal(event.destroyed, false, "Tile unload event should not delete with zombies!");
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer.world.addHandler('add-item', openHandler);
|
|
|
|
viewer.addHandler('tile-unloaded', unloadTileHandler);
|
|
|
|
viewer.addHandler('tile-loaded', tileCounter);
|
|
|
|
|
|
|
|
viewer.open('/test/data/testpattern.dzi');
|
|
|
|
});
|
|
|
|
|
2014-11-20 22:51:24 +03:00
|
|
|
})();
|