From 2dc70945f834b7c4f50d353c166c5190a751beca Mon Sep 17 00:00:00 2001 From: ghybs Date: Wed, 24 Jan 2018 18:24:02 +0400 Subject: [PATCH] Test(ImageOverlay+TileLayer): crossOrigin option to IMG crossorigin attribute (#6022) * Test(ImageOverlay): crossOrigin option to IMG crossorigin attribute typical values: false (default), true => '', => now that a string value can be passed to this option. * Test(TileLayer): crossOrigin option to IMG crossorigin attribute typical values: false (default), true => '', => now that a string value can be passed to this option. * Style(TileLayerSpec): remove commented code * Style(TileLayerSpec): remove more commented code --- spec/suites/layer/ImageOverlaySpec.js | 41 ++++++++++++++++++ spec/suites/layer/tile/TileLayerSpec.js | 56 ++++++++++++++++++++----- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/spec/suites/layer/ImageOverlaySpec.js b/spec/suites/layer/ImageOverlaySpec.js index 83b23112..08b65a78 100644 --- a/spec/suites/layer/ImageOverlaySpec.js +++ b/spec/suites/layer/ImageOverlaySpec.js @@ -141,4 +141,45 @@ describe('ImageOverlay', function () { expect(overlay.setZIndex()).to.equal(overlay); }); }); + + // For tests that do not actually need to append the map container to the document. + // This saves PhantomJS memory. + describe('_image2', function () { + var c, map, overlay; + var blankUrl = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="; + var bounds = [[40.712216, -74.22655], [40.773941, -74.12544]]; + + // Create map and overlay for each test + beforeEach(function () { + c = document.createElement('div'); + c.style.width = '400px'; + c.style.height = '400px'; + map = new L.Map(c); + map.setView(new L.LatLng(55.8, 37.6), 6); // view needs to be set so when layer is added it is initialized + }); + + // Clean up after each test run + afterEach(function () { + map.removeLayer(overlay); + overlay = null; + map = null; + }); + + // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes + testCrossOriginValue(undefined, null); // Falsy value (other than empty string '') => no attribute set. + testCrossOriginValue(true, ''); + testCrossOriginValue('anonymous', 'anonymous'); + testCrossOriginValue('use-credentials', 'use-credentials'); + + function testCrossOriginValue(crossOrigin, expectedValue) { + it('uses crossOrigin option value ' + crossOrigin, function () { + overlay = L.imageOverlay(blankUrl, bounds, { + crossOrigin: crossOrigin + }); + map.addLayer(overlay); + + expect(overlay._image.getAttribute('crossorigin')).to.be(expectedValue); + }); + } + }); }); diff --git a/spec/suites/layer/tile/TileLayerSpec.js b/spec/suites/layer/tile/TileLayerSpec.js index dbb85776..2813d032 100644 --- a/spec/suites/layer/tile/TileLayerSpec.js +++ b/spec/suites/layer/tile/TileLayerSpec.js @@ -173,13 +173,24 @@ describe('TileLayer', function () { }); afterEach(function () { - document.body.removeChild(div); + if (div) { + document.body.removeChild(div); + } }); function kittenLayerFactory(options) { return L.tileLayer(placeKitten, options || {}); } + function eachImg(layer, callback) { + var imgtags = layer._container.children[0].children; + for (var i in imgtags) { + if (imgtags[i].tagName === 'IMG') { + callback(imgtags[i]); + } + } + } + describe("number of kittens loaded", function () { var clock, kittenLayer, counts; @@ -299,15 +310,6 @@ describe('TileLayer', function () { map = L.map(div).setView([0, 0], 2); }); - function eachImg(layer, callback) { - var imgtags = layer._container.children[0].children; - for (var i in imgtags) { - if (imgtags[i].tagName === 'IMG') { - callback(imgtags[i]); - } - } - } - it('replaces {y} with y coordinate', function () { var layer = L.tileLayer('http://example.com/{z}/{y}/{x}.png').addTo(map); @@ -419,4 +421,38 @@ describe('TileLayer', function () { }); }); + + describe('options', function () { + + beforeEach(function () { + div = document.createElement('div'); + div.style.width = '400px'; + div.style.height = '400px'; + + map = L.map(div).setView([0, 0], 2); + }); + + afterEach(function () { + map = null; + div = null; + }); + + // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes + testCrossOriginValue(undefined, null); // Falsy value (other than empty string '') => no attribute set. + testCrossOriginValue(true, ''); + testCrossOriginValue('anonymous', 'anonymous'); + testCrossOriginValue('use-credentials', 'use-credentials'); + + function testCrossOriginValue(crossOrigin, expectedValue) { + it('uses crossOrigin value ' + crossOrigin, function () { + var layer = L.tileLayer('http://example.com/{z}/{y}/{x}.png', { + crossOrigin: crossOrigin + }).addTo(map); + + eachImg(layer, function (img) { + expect(img.getAttribute('crossorigin')).to.be(expectedValue); + }); + }); + } + }); });