2017-12-07 04:20:39 +03:00
|
|
|
/* global QUnit, testLog */
|
2014-11-20 22:51:24 +03:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2022-04-16 22:19:54 +03:00
|
|
|
var tileSourceCacheAPI = {
|
|
|
|
createTileCache: function(cacheObject, data, tile) {
|
|
|
|
cacheObject._data = data;
|
|
|
|
},
|
|
|
|
destroyTileCache: function (cacheObject) {
|
|
|
|
cacheObject._data = null;
|
|
|
|
cacheObject._renderedContext = null;
|
|
|
|
},
|
|
|
|
getTileCacheData: function(cacheObject) {
|
|
|
|
return cacheObject._data;
|
|
|
|
},
|
|
|
|
getTileCacheDataAsImage: function(cacheObject) {
|
|
|
|
return cacheObject._data; //the data itself by default is Image
|
|
|
|
},
|
|
|
|
getTileCacheDataAsContext2D: function(cacheObject) {
|
|
|
|
if (!cacheObject._renderedContext) {
|
|
|
|
var canvas = document.createElement( 'canvas' );
|
|
|
|
canvas.width = cacheObject._data.width;
|
|
|
|
canvas.height = cacheObject._data.height;
|
|
|
|
cacheObject._renderedContext = canvas.getContext('2d');
|
|
|
|
cacheObject._renderedContext.drawImage( cacheObject._data, 0, 0 );
|
|
|
|
//since we are caching the prerendered image on a canvas
|
|
|
|
//allow the image to not be held in memory
|
|
|
|
cacheObject._data = null;
|
|
|
|
}
|
|
|
|
return cacheObject._renderedContext;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-20 22:51:24 +03:00
|
|
|
// ----------
|
2017-12-07 04:20:39 +03:00
|
|
|
QUnit.module('TileCache', {
|
|
|
|
beforeEach: function () {
|
2014-11-20 22:51:24 +03:00
|
|
|
testLog.reset();
|
|
|
|
},
|
2017-12-07 04:20:39 +03:00
|
|
|
afterEach: function () {
|
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,
|
|
|
|
source: tileSourceCacheAPI
|
2015-06-11 22:27:36 +03:00
|
|
|
};
|
|
|
|
var fakeTiledImage1 = {
|
2022-04-16 22:19:54 +03:00
|
|
|
viewer: fakeViewer,
|
|
|
|
source: tileSourceCacheAPI
|
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: {},
|
|
|
|
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: {},
|
|
|
|
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,
|
|
|
|
source: tileSourceCacheAPI
|
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: {},
|
|
|
|
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: {},
|
|
|
|
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: {},
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
})();
|