2012-11-19 11:36:13 +08:00
|
|
|
|
|
|
|
describe('TileLayer', function () {
|
2014-03-06 05:27:32 +08:00
|
|
|
var tileUrl = '',
|
|
|
|
map;
|
2013-02-21 03:22:47 +08:00
|
|
|
|
2014-03-06 05:27:32 +08:00
|
|
|
beforeEach(function () {
|
|
|
|
map = L.map(document.createElement('div'));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#onAdd", function () {
|
|
|
|
it('is called after viewreset on first map load', function () {
|
|
|
|
var layer = L.tileLayer(tileUrl).addTo(map);
|
|
|
|
|
|
|
|
var onAdd = layer.onAdd,
|
|
|
|
onAddSpy = sinon.spy();
|
|
|
|
layer.onAdd = function () {
|
|
|
|
onAdd.apply(this, arguments);
|
|
|
|
onAddSpy();
|
|
|
|
};
|
|
|
|
|
|
|
|
var onReset = sinon.spy();
|
|
|
|
map.on('viewreset', onReset);
|
|
|
|
map.setView([0, 0], 0);
|
|
|
|
|
|
|
|
expect(onReset.calledBefore(onAddSpy)).to.be.ok();
|
2012-11-19 11:36:13 +08:00
|
|
|
});
|
2014-03-06 05:27:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#getMaxZoom, #getMinZoom", function () {
|
2012-11-19 11:36:13 +08:00
|
|
|
describe("when a tilelayer is added to a map with no other layers", function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it("has the same zoomlevels as the tilelayer", function () {
|
2012-11-19 11:36:13 +08:00
|
|
|
var maxZoom = 10,
|
|
|
|
minZoom = 5;
|
2013-11-08 05:54:33 +08:00
|
|
|
|
|
|
|
map.setView([0, 0], 1);
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2013-07-12 20:05:59 +08:00
|
|
|
L.tileLayer(tileUrl, {
|
|
|
|
maxZoom: maxZoom,
|
|
|
|
minZoom: minZoom
|
|
|
|
}).addTo(map);
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMaxZoom()).to.be(maxZoom);
|
|
|
|
expect(map.getMinZoom()).to.be(minZoom);
|
2012-11-19 11:36:13 +08:00
|
|
|
});
|
|
|
|
});
|
2013-02-20 09:05:44 +08:00
|
|
|
|
2013-02-20 10:15:36 +08:00
|
|
|
describe("accessing a tilelayer's properties", function () {
|
|
|
|
it('provides a container', function () {
|
2013-02-20 09:05:44 +08:00
|
|
|
map.setView([0, 0], 1);
|
|
|
|
|
2013-02-21 03:22:47 +08:00
|
|
|
var layer = L.tileLayer(tileUrl).addTo(map);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(layer.getContainer()).to.be.ok();
|
2013-02-20 10:15:36 +08:00
|
|
|
});
|
|
|
|
});
|
2013-02-20 09:05:44 +08:00
|
|
|
|
2012-11-19 11:36:13 +08:00
|
|
|
describe("when a tilelayer is added to a map that already has a tilelayer", function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it("has its zoomlevels updated to fit the new layer", function () {
|
2012-11-19 11:36:13 +08:00
|
|
|
map.setView([0, 0], 1);
|
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
L.tileLayer(tileUrl, {minZoom: 10, maxZoom: 15}).addTo(map);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(10);
|
|
|
|
expect(map.getMaxZoom()).to.be(15);
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
L.tileLayer(tileUrl, {minZoom: 5, maxZoom: 10}).addTo(map);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(5); // changed
|
|
|
|
expect(map.getMaxZoom()).to.be(15); // unchanged
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
L.tileLayer(tileUrl, {minZoom: 10, maxZoom: 20}).addTo(map);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(5); // unchanged
|
|
|
|
expect(map.getMaxZoom()).to.be(20); // changed
|
2012-11-19 11:36:13 +08:00
|
|
|
|
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
L.tileLayer(tileUrl, {minZoom: 0, maxZoom: 25}).addTo(map);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(0); // changed
|
|
|
|
expect(map.getMaxZoom()).to.be(25); // changed
|
2012-11-19 11:36:13 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("when a tilelayer is removed from a map", function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it("has its zoomlevels updated to only fit the layers it currently has", function () {
|
2013-11-08 05:54:33 +08:00
|
|
|
var tiles = [ L.tileLayer(tileUrl, {minZoom: 10, maxZoom: 15}).addTo(map),
|
|
|
|
L.tileLayer(tileUrl, {minZoom: 5, maxZoom: 10}).addTo(map),
|
|
|
|
L.tileLayer(tileUrl, {minZoom: 10, maxZoom: 20}).addTo(map),
|
|
|
|
L.tileLayer(tileUrl, {minZoom: 0, maxZoom: 25}).addTo(map)
|
2012-11-19 11:36:13 +08:00
|
|
|
];
|
2013-11-08 05:54:33 +08:00
|
|
|
map.whenReady(function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(0);
|
|
|
|
expect(map.getMaxZoom()).to.be(25);
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2012-11-29 15:05:08 +08:00
|
|
|
map.removeLayer(tiles[0]);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(0);
|
|
|
|
expect(map.getMaxZoom()).to.be(25);
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2012-11-29 15:05:08 +08:00
|
|
|
map.removeLayer(tiles[3]);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(5);
|
|
|
|
expect(map.getMaxZoom()).to.be(20);
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2012-11-29 15:05:08 +08:00
|
|
|
map.removeLayer(tiles[2]);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(5);
|
|
|
|
expect(map.getMaxZoom()).to.be(10);
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2012-11-29 15:05:08 +08:00
|
|
|
map.removeLayer(tiles[1]);
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(map.getMinZoom()).to.be(0);
|
|
|
|
expect(map.getMaxZoom()).to.be(Infinity);
|
2012-11-29 15:05:08 +08:00
|
|
|
});
|
2012-11-19 11:36:13 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-01 05:38:21 +08:00
|
|
|
|
|
|
|
describe("'loading' event", function() {
|
|
|
|
var tileUrl1 = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
|
|
|
|
tileUrl2 = 'http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png';
|
|
|
|
|
|
|
|
// Inject Leaflet css
|
|
|
|
var head = document.getElementsByTagName("head")[0],
|
|
|
|
link = document.createElement("link");
|
|
|
|
link.href = "../dist/leaflet.css";
|
|
|
|
link.type = "text/css";
|
|
|
|
link.rel = "stylesheet";
|
|
|
|
head.appendChild(link);
|
|
|
|
|
|
|
|
// Add map div to DOM. The map panning tests do not work reliably unless
|
|
|
|
// the Leaflet map is properly styled and part of the DOM
|
|
|
|
var mapDiv = document.createElement('div');
|
|
|
|
mapDiv.style.height = '256px';
|
|
|
|
mapDiv.style.width = '256px';
|
|
|
|
document.body.appendChild(mapDiv);
|
|
|
|
|
|
|
|
this.afterAll(function() {
|
|
|
|
document.body.removeChild(mapDiv);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set the map zoom high enough that panning by 256 pixels necessarily loads more tiles
|
|
|
|
var myMap = L.map(mapDiv).setView([0, 0], 13);
|
|
|
|
|
|
|
|
describe("after a tilelayer has been initialized with an empty string", function() {
|
|
|
|
var layer = L.tileLayer('');
|
|
|
|
var updateInterval = layer.options.updateInterval + 500;
|
|
|
|
|
|
|
|
var loadingSpy;
|
|
|
|
beforeEach(function() {
|
|
|
|
loadingSpy = sinon.spy();
|
|
|
|
layer.on('loading', function() { loadingSpy(); });
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
layer.off('loading');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is fired when the tilelayer is added to the map", function() {
|
|
|
|
layer.addTo(myMap);
|
|
|
|
expect(loadingSpy.calledOnce).to.be.ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is fired again when the tilelayer has its url set to a real tile url", function(done) {
|
|
|
|
layer.setUrl(tileUrl2);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(loadingSpy.calledOnce).to.be.ok();
|
|
|
|
done();
|
|
|
|
}, updateInterval);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is fired again when the map is panned enough to load more tiles", function(done) {
|
|
|
|
myMap.panBy([256,256]);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(loadingSpy.calledOnce).to.be.ok();
|
|
|
|
done();
|
|
|
|
}, updateInterval);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("after a tilelayer has been initialized with a real tile url", function() {
|
|
|
|
var layer = L.tileLayer(tileUrl1);
|
|
|
|
var updateInterval = layer.options.updateInterval + 500;
|
|
|
|
|
|
|
|
var loadingSpy;
|
|
|
|
beforeEach(function() {
|
|
|
|
loadingSpy = sinon.spy();
|
|
|
|
layer.on('loading', function() { loadingSpy(); });
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
layer.off('loading');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is fired when the tilelayer is added to the map", function() {
|
|
|
|
layer.addTo(myMap);
|
|
|
|
expect(loadingSpy.calledOnce).to.be.ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is fired again when the tilelayer has its url set to a real tile url", function(done) {
|
|
|
|
layer.setUrl(tileUrl2);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(loadingSpy.calledOnce).to.be.ok();
|
|
|
|
done();
|
|
|
|
}, updateInterval);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is fired again when the map is panned enough to load more tiles", function(done) {
|
|
|
|
myMap.panBy([256,256]);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(loadingSpy.calledOnce).to.be.ok();
|
|
|
|
done();
|
|
|
|
}, updateInterval);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-02-20 03:21:06 +08:00
|
|
|
});
|