openseadragon/test/modules/tilecache.js

164 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-12-07 04:20:39 +03:00
/* global QUnit, testLog */
2014-11-20 22:51:24 +03:00
(function() {
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 = {
viewer: fakeViewer,
source: tileSourceCacheAPI
2015-06-11 22:27:36 +03:00
};
var fakeTiledImage1 = {
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',
cacheKey: 'foo.jpg',
2014-11-20 22:51:24 +03:00
image: {},
unload: function() {}
};
var fakeTile1 = {
url: 'foo.jpg',
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 = {
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',
cacheKey: 'different.jpg',
2014-11-20 22:51:24 +03:00
image: {},
unload: function() {}
};
var fakeTile1 = {
url: 'same.jpg',
cacheKey: 'same.jpg',
2014-11-20 22:51:24 +03:00
image: {},
unload: function() {}
};
var fakeTile2 = {
url: 'same.jpg',
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
});
})();