1 <!doctype html>
2 <!--
3 @license
4 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8 Code distributed by Google as part of the polymer project is also
9 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10 -->
11
12 <html>
13 <head>
14
15 <title>iron-icon</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-icon.html">
24 <link rel="import" href="../../iron-iconset/iron-iconset.html">
25 <link rel="import" href="../../promise-polyfill/promise-polyfill.html">
26 <link rel="import" href="../../test-fixture/test-fixture.html">
27 <link rel="import" href="icon-holder.html">
28
29 </head>
30 <body>
31
32 <test-fixture id="TrivialIcon">
33 <template>
34 <iron-icon src="../demo/location.png"></iron-icon>
35 </template>
36 </test-fixture>
37
38 <test-fixture id="IconFromIconset">
39 <template>
40 <iron-iconset name="example" icons="location blank" src="location.png" size="24" width="48"></iron-iconset>
41 <iron-icon icon="example:location"></iron-icon>
42 </template>
43 </test-fixture>
44
45 <test-fixture id="WithoutAnIconSource">
46 <template>
47 <iron-icon icon=""></iron-icon>
48 <iron-icon></iron-icon>
49 <iron-icon src=""></iron-icon>
50 </template>
51 </test-fixture>
52
53 <test-fixture id="UsingAsyncIconset">
54 <template>
55 <iron-icon icon="async:location"></iron-icon>
56 </template>
57 </test-fixture>
58
59 <test-fixture id="AsyncIconset">
60 <template>
61 <iron-iconset name="async" icons="location blank" src="location.png" size="24" width="48"></iron-iconset>
62 </template>
63 </test-fixture>
64
65 <test-fixture id="SrcIconSwitch">
66 <template>
67 <iron-iconset name="example" icons="location blank" src="location.png" size="24" width="48"></iron-iconset>
68 <iron-icon></iron-icon>
69 </template>
70 </test-fixture>
71
72 <test-fixture id="ParentForceUpdate">
73 <template>
74 <icon-holder>
75 <iron-icon></iron-icon>
76 </icon-holder>
77 </template>
78 </test-fixture>
79
80 <script>
81 function iconElementFor (node) {
82 var nodes = Polymer.dom(node.root).childNodes;
83
84 for (var i = 0; i < nodes.length; ++i) {
85 if (nodes[i].nodeName.match(/DIV|IMG/)) {
86 return nodes[i];
87 }
88 }
89 }
90
91 function hasIcon (node) {
92 return /png/.test(node.style.backgroundImage);
93 }
94
95 suite('', function() {
96 suite('basic behavior', function() {
97 var icon;
98
99 setup(function() {
100 icon = fixture('TrivialIcon');
101 });
102
103 test('can be assigned an icon with the src attribute', function() {
104 expect(iconElementFor(icon)).to.be.ok;
105 expect(iconElementFor(icon).src).to.match(/demo\/location\.png/);
106 });
107
108 test('can change its src dynamically', function() {
109 icon.src = 'foo.png';
110
111 expect(iconElementFor(icon).src).to.match(/foo\.png/);
112 });
113 });
114
115 suite('when paired with an iconset', function() {
116 var icon;
117
118 setup(function() {
119 var elements = fixture('IconFromIconset');
120
121 icon = elements[1];
122 });
123
124 test('can be assigned an icon from the iconset', function() {
125 expect(hasIcon(icon)).to.be.ok;
126 });
127
128 test('can change its icon dynamically', function() {
129 var style = icon.style;
130
131 expect(style.backgroundPosition).to.match(/0(%|px) 0(%|px)/);
132
133 icon.icon = "example:blank";
134
135 expect(style.backgroundPosition).to.match(/-24px 0(%|px)/);
136 });
137 });
138
139 suite('when no icon source is provided', function() {
140 test('will politely wait for an icon source without throwing', function() {
141 document.createElement('iron-icon');
142 fixture('WithoutAnIconSource');
143 });
144 })
145
146 suite('when loading async', function() {
147 test('will get icon after iconset is added', function() {
148 var icon = fixture('UsingAsyncIconset');
149 expect(hasIcon(icon)).to.be.false;
150 return new Promise(function(resolve, reject) {
151 window.addEventListener('iron-iconset-added', function() {
152 if (hasIcon(icon)) {
153 resolve();
154 } else {
155 reject(new Error('icon didn\'t load after iconset loaded'));
156 }
157 });
158 fixture('AsyncIconset');
159 });
160 });
161 });
162
163 suite('when switching between src and icon properties', function() {
164 var icon;
165
166 setup(function() {
167 var elements = fixture('IconFromIconset');
168 icon = elements[1];
169 });
170
171 test('will display the icon if both icon and src are set', function() {
172 icon.src = '../demo/location.png';
173 icon.icon = 'example:location';
174 expect(hasIcon(icon)).to.be.true;
175 expect(iconElementFor(icon)).to.not.exist;
176
177 // Check if it works too it we change the affectation order
178 icon.icon = 'example:location';
179 icon.src = '../demo/location.png';
180 expect(hasIcon(icon)).to.be.true;
181 expect(iconElementFor(icon)).to.not.exist;
182 });
183
184 test('will display the icon when src is defined first and then reset', function() {
185 icon.src = '../demo/location.png';
186 icon.icon = null;
187 icon.src = null;
188 icon.icon = 'example:location';
189 expect(hasIcon(icon)).to.be.true;
190 expect(iconElementFor(icon)).to.not.exist;
191 });
192
193 test('will display the src when icon is defined first and then reset', function() {
194 icon.src = null;
195 icon.icon = 'example:location';
196 icon.src = '../demo/location.png';
197 icon.icon = null;
198 expect(hasIcon(icon)).to.be.false;
199 expect(iconElementFor(icon)).to.exist;
200 });
201
202 test('will display nothing if both properties are unset', function() {
203 icon.src = '../demo/location.png';
204 icon.icon = 'example:location';
205 icon.src = null;
206 icon.icon = null;
207 expect(hasIcon(icon)).to.be.false;
208 expect(iconElementFor(icon)).to.not.exist;
209 });
210 });
211 suite('ancestor direct updates', function() {
212 test('handle properties set before ready', function() {
213 var holder = fixture('ParentForceUpdate');
214 });
215 });
216 });
217
218
219