Skip to content

Commit

Permalink
Merge pull request #116 from elemoine/redraw
Browse files Browse the repository at this point in the history
make Layer.redraw not unconditionally set zoomChanged to true when calling Layer.moveTo
  • Loading branch information
Éric Lemoine committed Jan 13, 2012
2 parents a719de7 + 0cc4dc8 commit 7a5b469
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
16 changes: 15 additions & 1 deletion lib/OpenLayers/Layer.js
Expand Up @@ -265,6 +265,16 @@ OpenLayers.Layer = OpenLayers.Class({
* {Float}
*/
minResolution: null,

/**
* Property: resolution
* {Float} Current resolution that the layer is drawn in. This is
* used to determine whether the zoom has changed when calling
* <moveTo> from <redraw>. Subclasses may set this.resolution to
* null prior to calling redraw to force passing zoomChanged
* true to moveTo.
*/
resolution: null,

/**
* APIProperty: numZoomLevels
Expand Down Expand Up @@ -552,7 +562,8 @@ OpenLayers.Layer = OpenLayers.Class({
var extent = this.getExtent();

if (extent && this.inRange && this.visibility) {
var zoomChanged = true;
zoomChanged = this.resolution == null ||
this.resolution !== this.map.getResolution();
this.moveTo(extent, zoomChanged, false);
this.events.triggerEvent("moveend",
{"zoomChanged": zoomChanged});
Expand All @@ -577,6 +588,7 @@ OpenLayers.Layer = OpenLayers.Class({
display = display && this.inRange;
}
this.display(display);
this.resolution = this.map.getResolution();
},

/**
Expand Down Expand Up @@ -632,6 +644,8 @@ OpenLayers.Layer = OpenLayers.Class({

// deal with gutters
this.setTileSize();

this.resolution = null;
}
},

Expand Down
3 changes: 2 additions & 1 deletion lib/OpenLayers/Layer/HTTPRequest.js
Expand Up @@ -120,6 +120,7 @@ OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
*/
mergeNewParams:function(newParams) {
this.params = OpenLayers.Util.extend(this.params, newParams);
this.resolution = null;
var ret = this.redraw();
if(this.map != null) {
this.map.events.triggerEvent("changelayer", {
Expand All @@ -144,7 +145,7 @@ OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
if (force) {
return this.mergeNewParams({"_olSalt": Math.random()});
} else {
return OpenLayers.Layer.prototype.redraw.apply(this, []);
return OpenLayers.Layer.prototype.redraw.call(this);
}
},

Expand Down
69 changes: 66 additions & 3 deletions tests/Layer.html
Expand Up @@ -672,19 +672,82 @@
// test that the moveend event was triggered
t.ok(log.event, "an event was logged");
t.eq(log.event.type, "moveend", "moveend was triggered");
t.eq(log.event.zoomChanged, true, "event says zoomChanged true - poor name");
t.eq(log.event.zoomChanged, false, "event says zoomChanged false");

layer.moveTo = function(bounds, zoomChanged, dragging) {
var extent = layer.map.getExtent();
t.ok(bounds.equals(extent),
"redraw calls moveTo with the map extent");
t.ok(zoomChanged,
"redraw calls moveTo with zoomChanged true");
t.ok(!zoomChanged,
"redraw calls moveTo with zoomChanged false");
t.ok(!dragging,
"redraw calls moveTo with dragging false");
}
layer.redraw();
}

// This function includes integration tests to verify that the
// layer's moveTo function is called with the expected value
// for zoomChanged
function test_moveTo_zoomChanged(t) {
t.plan(6);

var log = {};
var map = new OpenLayers.Map('map');

var l1 = new OpenLayers.Layer('l1', {isBaseLayer: true});
l1.moveTo = function(bounds, zoomChanged, dragging) {
log.moveTo = {zoomChanged: zoomChanged};
OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
};

map.addLayer(l1);
map.zoomToMaxExtent();

log = {};
l1.redraw();
t.eq(log.moveTo.zoomChanged, false,
"[a] redraw calls moveTo with zoomChanged false");

log = {};
l1.resolution = null;
l1.redraw();
t.eq(log.moveTo.zoomChanged, true,
"[b] redraw calls moveTo with zoomChanged true");

l1.setVisibility(false);
log = {};
l1.setVisibility(true);
t.eq(log.moveTo.zoomChanged, false,
"[c] redraw calls moveTo with zoomChanged false");

l1.setVisibility(false);
map.zoomIn();
log = {};
l1.setVisibility(true);
t.eq(log.moveTo.zoomChanged, true,
"[d] redraw calls moveTo with zoomChanged true");

l1.moveTo = OpenLayers.Layer.prototype.moveTo;

var l2 = new OpenLayers.Layer('l2');
l2.moveTo = function(bounds, zoomChanged, dragging) {
log.moveTo = {zoomChanged: zoomChanged};
OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
};
log = {};
map.addLayer(l2);
t.eq(log.moveTo.zoomChanged, true,
"[e] redraw calls moveTo with zoomChanged true");

map.removeLayer(l2);
log = {};
map.addLayer(l2);
t.eq(log.moveTo.zoomChanged, true,
"[f] redraw calls moveTo with zoomChanged true");

map.destroy();
}

function test_layer_setIsBaseLayer(t) {
t.plan(2);
Expand Down
6 changes: 4 additions & 2 deletions tests/Layer/HTTPRequest.html
Expand Up @@ -65,7 +65,7 @@
}

function test_Layer_HTTPRequest_mergeNewParams (t) {
t.plan( 8 );
t.plan( 9 );

var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
Expand Down Expand Up @@ -97,7 +97,9 @@

layer.redraw = function() {
t.ok(true, "layer.mergeNewParams calls layer.redraw");
}
t.ok(layer.resolution === null, "layer.mergeNewParams sets resolution to null");
};
layer.resolution = 'fake';
layer.mergeNewParams();
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Tile/Image.html
Expand Up @@ -342,6 +342,38 @@
t.ok(tile.imgDiv == null, "image reference removed from tile");
map.destroy();
}

// test for https://github.com/openlayers/openlayers/pull/36
// (more an integration test than a unit test)
function test_olImageLoadError(t) {
t.plan(2);

var map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.WMS("invalid", "", {layers: 'basic'});
map.addLayer(layer);

var size = new OpenLayers.Size(5, 6);
var position = new OpenLayers.Pixel(20, 30);
var bounds = new OpenLayers.Bounds(1, 2, 3, 4);

var tile = new OpenLayers.Tile.Image(layer, position, bounds, null, size);
tile.draw();

t.delay_call(0.1, function() {

// check initial state
t.ok(OpenLayers.Element.hasClass(tile.imgDiv, 'olImageLoadError'),
'tile image has the olImageLoadError class (init state)');

layer.setVisibility(false);
layer.setVisibility(true);

t.ok(OpenLayers.Element.hasClass(tile.imgDiv, 'olImageLoadError'),
'tile image still has the olImageLoadError class');

map.destroy();
});
}

</script>
</head>
Expand Down

0 comments on commit 7a5b469

Please sign in to comment.