From c2d0cab07ad0852c91bc913d9185d8ab538bd38e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 4 Feb 2014 15:24:26 +0100 Subject: [PATCH] Add tests for ol.style.IconImageCache --- src/ol/style/iconstyle.js | 9 ++++ test/spec/ol/style/iconstyle.test.js | 71 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 test/spec/ol/style/iconstyle.test.js diff --git a/src/ol/style/iconstyle.js b/src/ol/style/iconstyle.js index 041daefb4b..6af27dd27a 100644 --- a/src/ol/style/iconstyle.js +++ b/src/ol/style/iconstyle.js @@ -437,6 +437,15 @@ ol.style.IconImageCache.getKey = function(src, crossOrigin) { }; +/** + * FIXME empty description for jsdoc + */ +ol.style.IconImageCache.prototype.clear = function() { + this.cache_ = {}; + this.cacheSize_ = 0; +}; + + /** * FIXME empty description for jsdoc */ diff --git a/test/spec/ol/style/iconstyle.test.js b/test/spec/ol/style/iconstyle.test.js new file mode 100644 index 0000000000..f6d7dfdafb --- /dev/null +++ b/test/spec/ol/style/iconstyle.test.js @@ -0,0 +1,71 @@ +goog.provide('ol.test.style.IconImageCache'); + +describe('ol.style.IconImageCache', function() { + var originalMaxCacheSize; + + beforeEach(function() { + var cache = ol.style.IconImageCache.getInstance(); + cache.clear(); + originalMaxCacheSize = cache.maxCacheSize; + cache.maxCacheSize_ = 4; + }); + + afterEach(function() { + var cache = ol.style.IconImageCache.getInstance(); + cache.maxCacheSize_ = originalMaxCacheSize; + cache.clear(); + }); + + describe('#expire', function() { + it('expires images when expected', function() { + var cache = ol.style.IconImageCache.getInstance(); + + var i, src, iconImage, key; + + for (i = 0; i < 4; ++i) { + src = i + ''; + iconImage = new ol.style.IconImage_(src, null); + cache.set(src, null, iconImage); + } + + expect(cache.cacheSize_).to.eql(4); + + cache.expire(); + expect(cache.cacheSize_).to.eql(4); + + src = '4'; + iconImage = new ol.style.IconImage_(src, null); + cache.set(src, null, iconImage); + expect(cache.cacheSize_).to.eql(5); + + cache.expire(); // remove '0' and '4' + expect(cache.cacheSize_).to.eql(3); + + src = '0'; + iconImage = new ol.style.IconImage_(src, null); + goog.events.listen(iconImage, goog.events.EventType.CHANGE, + goog.nullFunction, false); + cache.set(src, null, iconImage); + expect(cache.cacheSize_).to.eql(4); + + src = '4'; + iconImage = new ol.style.IconImage_(src, null); + goog.events.listen(iconImage, goog.events.EventType.CHANGE, + goog.nullFunction, false); + cache.set(src, null, iconImage); + expect(cache.cacheSize_).to.eql(5); + + // check that '0' and '4' are not removed from the cache + cache.expire(); + key = ol.style.IconImageCache.getKey('0', null); + expect(key in cache.cache_).to.be.ok(); + key = ol.style.IconImageCache.getKey('4', null); + expect(key in cache.cache_).to.be.ok(); + + }); + }); +}); + +goog.require('goog.events'); +goog.require('goog.events.EventType'); +goog.require('ol.style.IconImageCache');