Implement getChecksum for RegularShape

This commit is contained in:
tsauerwein
2014-11-13 10:50:07 +01:00
parent 7e9dd65f8b
commit ea65f65b37
2 changed files with 221 additions and 0 deletions

View File

@@ -18,12 +18,19 @@ goog.require('ol.style.Stroke');
* @constructor
* @param {olx.style.RegularShapeOptions=} opt_options Options.
* @extends {ol.style.Image}
* @implements {ol.structs.IHasChecksum}
* @api
*/
ol.style.RegularShape = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
/**
* @private
* @type {Array.<ol.structs.Checksum>|null}
*/
this.checksums_ = null;
/**
* @private
* @type {HTMLCanvasElement}
@@ -305,3 +312,34 @@ ol.style.RegularShape.prototype.render_ = function() {
return size;
};
/**
* @inheritDoc
*/
ol.style.RegularShape.prototype.getChecksum = function() {
var strokeChecksum = !goog.isNull(this.stroke_) ?
this.stroke_.getChecksum() : '-';
var fillChecksum = !goog.isNull(this.fill_) ?
this.fill_.getChecksum() : '-';
var recalculate = goog.isNull(this.checksums_) ||
(strokeChecksum != this.checksums_[1] ||
fillChecksum != this.checksums_[2] ||
this.radius_ != this.checksums_[3] ||
this.radius2_ != this.checksums_[4] ||
this.angle_ != this.checksums_[5] ||
this.points_ != this.checksums_[6]);
if (recalculate) {
var checksum = 'r' + strokeChecksum + fillChecksum +
(goog.isDef(this.radius_) ? this.radius_.toString() : '-') +
(goog.isDef(this.radius2_) ? this.radius2_.toString() : '-') +
(goog.isDef(this.angle_) ? this.angle_.toString() : '-') +
(goog.isDef(this.points_) ? this.points_.toString() : '-');
this.checksums_ = [checksum, strokeChecksum, fillChecksum,
this.radius_, this.radius2_, this.angle_, this.points_];
}
return this.checksums_[0];
};

View File

@@ -0,0 +1,183 @@
goog.provide('ol.test.style.RegularShape');
describe('ol.style.RegularShape', function() {
describe('#getChecksum', function() {
it('calculates the same hash code for default options', function() {
var style1 = new ol.style.RegularShape();
var style2 = new ol.style.RegularShape();
expect(style1.getChecksum()).to.eql(style2.getChecksum());
});
it('calculates not the same hash code (radius)', function() {
var style1 = new ol.style.RegularShape({
radius2: 5
});
var style2 = new ol.style.RegularShape({
radius: 5
});
expect(style1.getChecksum()).to.not.eql(style2.getChecksum());
});
it('calculates the same hash code (radius)', function() {
var style1 = new ol.style.RegularShape({
radius: 5
});
var style2 = new ol.style.RegularShape({
radius: 5
});
expect(style1.getChecksum()).to.eql(style2.getChecksum());
});
it('calculates not the same hash code (color)', function() {
var style1 = new ol.style.RegularShape({
radius: 5,
fill: new ol.style.Fill({
color: '#319FD3'
})
});
var style2 = new ol.style.RegularShape({
radius: 5,
stroke: new ol.style.Stroke({
color: '#319FD3'
})
});
expect(style1.getChecksum()).to.not.eql(style2.getChecksum());
});
it('calculates the same hash code (everything set)', function() {
var style1 = new ol.style.RegularShape({
radius: 5,
radius2: 3,
angle: 1.41,
points: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
lineCap: 'round',
lineDash: [5, 15, 25],
lineJoin: 'miter',
miterLimit: 4,
width: 2
})
});
var style2 = new ol.style.RegularShape({
radius: 5,
radius2: 3,
angle: 1.41,
points: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
lineCap: 'round',
lineDash: [5, 15, 25],
lineJoin: 'miter',
miterLimit: 4,
width: 2
})
});
expect(style1.getChecksum()).to.eql(style2.getChecksum());
});
it('calculates not the same hash code (stroke width differs)', function() {
var style1 = new ol.style.RegularShape({
radius: 5,
radius2: 3,
angle: 1.41,
points: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
lineCap: 'round',
lineDash: [5, 15, 25],
lineJoin: 'miter',
miterLimit: 4,
width: 3
})
});
var style2 = new ol.style.RegularShape({
radius: 5,
radius2: 3,
angle: 1.41,
points: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
lineCap: 'round',
lineDash: [5, 15, 25],
lineJoin: 'miter',
miterLimit: 4,
width: 2
})
});
expect(style1.getChecksum()).to.not.eql(style2.getChecksum());
});
it('invalidates a cached checksum if values change (fill)', function() {
var style1 = new ol.style.RegularShape({
radius: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3'
})
});
var style2 = new ol.style.RegularShape({
radius: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3'
})
});
expect(style1.getChecksum()).to.eql(style2.getChecksum());
style1.getFill().setColor('red');
expect(style1.getChecksum()).to.not.eql(style2.getChecksum());
});
it('invalidates a cached checksum if values change (stroke)', function() {
var style1 = new ol.style.RegularShape({
radius: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3'
})
});
var style2 = new ol.style.RegularShape({
radius: 5,
fill: new ol.style.Fill({
color: '#319FD3'
}),
stroke: new ol.style.Stroke({
color: '#319FD3'
})
});
expect(style1.getChecksum()).to.eql(style2.getChecksum());
style1.getStroke().setWidth(4);
expect(style1.getChecksum()).to.not.eql(style2.getChecksum());
});
});
});
goog.require('ol.style.AtlasManager');
goog.require('ol.style.RegularShape');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');