1<!doctype html> 2<!-- 3@license 4Copyright (c) 2015 The Polymer Project Authors. All rights reserved. 5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 8Code distributed by Google as part of the polymer project is also 9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 10--> 11 12<html> 13<head> 14 15 <title>iron-iconset-svg</title> 16 <meta charset="utf-8"> 17 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 18 19 <script src="../../webcomponentsjs/webcomponents-lite.js"></script> 20 <script src="../../web-component-tester/browser.js"></script> 21 <script src="../../test-fixture/test-fixture-mocha.js"></script> 22 23 <link rel="import" href="../iron-iconset-svg.html"> 24 <link rel="import" href="../../iron-meta/iron-meta.html"> 25 <link rel="import" href="../../iron-icon/iron-icon.html"> 26 <link rel="import" href="../../promise-polyfill/promise-polyfill.html"> 27 <link rel="import" href="../../test-fixture/test-fixture.html"> 28 29</head> 30<body> 31 32 <test-fixture id="TrivialIconsetSvg"> 33 <template> 34 <iron-iconset-svg name="foo"></iron-iconset-svg> 35 <iron-meta type="iconset"></iron-meta> 36 </template> 37 </test-fixture> 38 39 <test-fixture id="MirroredIconsetSvg"> 40 <template> 41 <iron-iconset-svg name="mirrored-icons" rtl-mirroring> 42 <circle id="circle" cx="20" cy="20" r="10"></circle> 43 <symbol id="rect" viewBox="0 0 50 25" mirror-in-rtl> 44 <rect x="0" y="0" width="50" height="25"></rect> 45 </symbol> 46 </iron-iconset-svg> 47 </template> 48 </test-fixture> 49 50 <test-fixture id="RtlContainer"> 51 <template> 52 <div dir="rtl"></div> 53 </template> 54 </test-fixture> 55 56 <test-fixture id="StandardIconsetSvg"> 57 <template> 58 <iron-iconset-svg name="my-icons" size="20"> 59 <svg> 60 <defs> 61 <circle id="circle" cx="20" cy="20" r="10"></circle> 62 <rect id="square" x="0" y="0" width="20" height="20"></rect> 63 <symbol id="rect" viewBox="0 0 50 25"> 64 <rect x="0" y="0" width="50" height="25"></rect> 65 </symbol> 66 </defs> 67 </svg> 68 </iron-iconset-svg> 69 <div></div> 70 <iron-icon></iron-icon> 71 </template> 72 </test-fixture> 73 74 <script> 75 76 suite('<iron-iconset>', function () { 77 78 suite('basic behavior', function () { 79 var iconset; 80 var meta; 81 var loadedPromise; 82 83 setup(function () { 84 loadedPromise = new Promise(function(resolve) { 85 window.addEventListener('iron-iconset-added', function(ev) { 86 if (ev && ev.detail === iconset) { 87 resolve(); 88 } 89 }); 90 }); 91 var elements = fixture('TrivialIconsetSvg'); 92 iconset = elements[0]; 93 meta = elements[1]; 94 }); 95 96 test('it can be accessed via iron-meta', function () { 97 expect(meta.byKey('foo')).to.be.equal(iconset); 98 }); 99 100 test('it does not have a size', function () { 101 var rect = iconset.getBoundingClientRect(); 102 expect(rect.width).to.be.equal(0); 103 expect(rect.height).to.be.equal(0); 104 }); 105 106 test('it fires an iron-iconset-added event on the window', function() { 107 return loadedPromise; 108 }); 109 }); 110 111 suite('when stamping in an RTL context', function() { 112 var iconset; 113 var rtlContainer; 114 115 setup(function() { 116 iconset = fixture('MirroredIconsetSvg'); 117 rtlContainer = fixture('RtlContainer'); 118 }); 119 120 test('icons marked as mirror-in-rtl are mirrored', function() { 121 iconset.applyIcon(rtlContainer, 'rect'); 122 var svg = rtlContainer.firstElementChild; 123 var computedStyle = window.getComputedStyle(svg); 124 var transform = computedStyle.transform || computedStyle.webkitTransform; 125 expect(transform).to.be.eql('matrix(-1, 0, 0, 1, 0, 0)'); 126 }); 127 128 test('icons not marked as mirror-in-rtl are not mirrored', function() { 129 iconset.applyIcon(rtlContainer, 'circle'); 130 var svg = rtlContainer.firstElementChild; 131 var computedStyle = window.getComputedStyle(svg); 132 var transform = computedStyle.transform || computedStyle.webkitTransform; 133 expect(transform).to.be.eql('none'); 134 }); 135 136 test('many mirrored icons only call getComputedStyle once', function() { 137 sinon.spy(window, 'getComputedStyle'); 138 139 for (var i = 0; i < 3; ++i) { 140 iconset.applyIcon(rtlContainer, 'rect'); 141 } 142 143 expect(window.getComputedStyle.callCount).to.be.eql(1); 144 window.getComputedStyle.restore(); 145 }); 146 }); 147 148 suite('when paired with a size and SVG definition', function () { 149 var iconset; 150 var div; 151 152 setup(function () { 153 var elements = fixture('StandardIconsetSvg'); 154 iconset = elements[0]; 155 div = elements[1]; 156 }); 157 158 test('it does not have a size', function () { 159 var rect = iconset.getBoundingClientRect(); 160 expect(rect.width).to.be.equal(0); 161 expect(rect.height).to.be.equal(0); 162 }); 163 164 test('appends a child to the target element', function () { 165 expect(div.firstElementChild).to.not.be.ok; 166 iconset.applyIcon(div, 'circle'); 167 expect(div.firstElementChild).to.be.ok; 168 }); 169 170 test('can be queried for all available icons', function () { 171 expect(iconset.getIconNames()).to.deep.eql(['my-icons:circle', 'my-icons:square', 'my-icons:rect']); 172 }); 173 174 test('supports any icon defined in the svg', function () { 175 var lastSvgIcon; 176 177 iconset.getIconNames().forEach(function (iconName) { 178 iconset.applyIcon(div, iconName.split(':').pop()); 179 expect(div.firstElementChild).to.not.be.equal(lastSvgIcon); 180 lastSvgIcon = div.firstElementChild; 181 }); 182 }); 183 184 test('prefers a viewBox attribute over the iconset size', function () { 185 iconset.applyIcon(div, 'rect'); 186 expect(div.firstElementChild.getAttribute('viewBox')).to.be.equal('0 0 50 25'); 187 }); 188 189 test('uses the iconset size when viewBox is not defined on the element', function () { 190 iconset.applyIcon(div, 'circle'); 191 expect(div.firstElementChild.getAttribute('viewBox')).to.be.equal('0 0 20 20'); 192 }); 193 }); 194 195 suite('Adding / removal from iron-icon', function () { 196 var iconset; 197 var div; 198 var ironIcon; 199 200 setup(function () { 201 var elements = fixture('StandardIconsetSvg'); 202 iconset = elements[0]; 203 div = elements[1]; 204 ironIcon = elements[2]; 205 }); 206 207 test('be able to remove an iconset from a standard DOM element', function () { 208 iconset.applyIcon(div, 'circle'); 209 Polymer.dom.flush(); 210 expect(div.children.length).to.be.equal(1); 211 iconset.removeIcon(div); 212 Polymer.dom.flush(); 213 expect(div.children.length).to.be.equal(0); 214 }); 215 216 test('be able to remove an iconset from a Polymer element', function () { 217 var baseLength = Polymer.dom(ironIcon.root).children.length; 218 iconset.applyIcon(ironIcon, 'circle'); 219 Polymer.dom.flush(); 220 expect(Polymer.dom(ironIcon.root).children.length - baseLength).to.be.equal(1); 221 iconset.removeIcon(ironIcon); 222 Polymer.dom.flush(); 223 expect(Polymer.dom(ironIcon.root).children.length - baseLength).to.be.equal(0); 224 }); 225 226 }); 227 228 }); 229 230 </script> 231 232</body> 233</html> 234