iron-image.html
11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<title>iron-image</title>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../test-fixture/test-fixture-mocha.js"></script>
<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../iron-image.html">
<style is="custom-style">
.fixed-width-container {
width: 500px;
}
.fixed-width-container iron-image {
width: 100%;
--iron-image-width: 100%;
}
.fixed-height-container {
height: 500px;
}
.fixed-height-container iron-image {
height: 100%;
--iron-image-height: 100%;
}
</style>
</head>
<body>
<test-fixture id="TrivialImage">
<template>
<iron-image></iron-image>
</template>
</test-fixture>
<test-fixture id="FixedWidthContainer">
<template>
<div class="fixed-width-container">
<iron-image></iron-image>
</div>
</template>
</test-fixture>
<test-fixture id="FixedHeightContainer">
<template>
<div class="fixed-height-container">
<iron-image></iron-image>
</div>
</template>
</test-fixture>
<script>
suite('<iron-image>', function() {
function randomImageUrl () {
return '../demo/polymer.svg?' + Math.random();
}
var image;
suite('basic behavior', function() {
setup(function() {
image = fixture('TrivialImage');
});
test('can load images given a src', function(done) {
image.addEventListener('loaded-changed', function onLoadedChanged() {
image.removeEventListener('loaded-changed', onLoadedChanged);
try {
expect(image.loaded).to.be.eql(true);
done();
} catch (e) {
done(e);
}
});
image.src = randomImageUrl();
});
test('will reload images when src changes', function(done) {
var loadCount = 0;
image.addEventListener('loaded-changed', function onLoadedChanged() {
if (image.loaded === true) {
loadCount++;
if (loadCount === 2) {
image.removeEventListener('loaded-changed', onLoadedChanged);
done();
} else {
image.src = randomImageUrl();
}
}
});
image.src = randomImageUrl();
});
test('error property is set when the image fails to load', function(done) {
image.addEventListener('error-changed', function onErrorChanged() {
assert(image.error, 'image has error property set');
image.removeEventListener('error-changed', onErrorChanged);
done();
});
image.src = '/this_image_should_not_exist.jpg';
});
// Test for PolymerElements/iron-image#16.
test('placeholder is hidden after loading when src is changed from invalid to valid', function(done) {
image.preload = true;
image.addEventListener('error-changed', function onErrorChanged() {
image.removeEventListener('error-changed', onErrorChanged);
assert.equal(image.loading, false, 'errored image loading = false');
assert.equal(image.loaded, false, 'errored image loaded = false');
assert.equal(image.error, true, 'errored image error = true');
image.addEventListener('loaded-changed', function onLoadedChanged() {
if (!image.loaded) return;
image.removeEventListener('loaded-changed', onLoadedChanged);
assert.equal(image.loading, false, 'ok image loading = false');
assert.equal(image.loaded, true, 'ok image loaded = true');
assert.equal(image.error, false, 'ok image error = false');
assert.equal(getComputedStyle(image.$.placeholder).display, 'none', 'placeholder has style.display = none');
done();
});
image.src = randomImageUrl();
});
image.src = '/this_image_should_not_exist.jpg';
});
// Test for PolymerElements/iron-image#23.
test('image is not shown below placeholder if previous image was loaded with' +
' sizing on and current image fails to load', function(done) {
image.preload = true;
image.sizing = 'cover';
image.addEventListener('loaded-changed', function onLoadedChanged() {
if (!image.loaded) return;
image.removeEventListener('loaded-changed', onLoadedChanged);
assert.notEqual(getComputedStyle(image.$.sizedImgDiv).backgroundImage, 'none', 'image visible after successful load');
assert.equal(getComputedStyle(image.$.placeholder).display, 'none', 'placeholder hidden after successful load');
image.addEventListener('error-changed', function onErrorChanged() {
if (!image.error) return;
image.removeEventListener('error-changed', onErrorChanged);
assert.equal(getComputedStyle(image.$.sizedImgDiv).backgroundImage, 'none', 'image hidden after failed load');
assert.notEqual(getComputedStyle(image.$.placeholder).display, 'none', 'placeholder visible after failed load');
done();
});
image.src = '/this_image_should_not_exist.jpg';
});
image.src = randomImageUrl();
});
});
suite('--iron-image-width, --iron-image-height', function() {
var fixedWidthContainer;
var fixedWidthIronImage;
var fixedHeightContainer;
var fixedHeightIronImage;
setup(function() {
fixedWidthContainer = fixture('FixedWidthContainer');
fixedWidthIronImage = fixedWidthContainer.querySelector('iron-image');
fixedHeightContainer = fixture('FixedHeightContainer');
fixedHeightIronImage = fixedHeightContainer.querySelector('iron-image');
});
test('100% width image fills container', function(done) {
fixedWidthIronImage.$.img.addEventListener('load', function onLoadedChanged(e) {
fixedWidthIronImage.$.img.removeEventListener('load', onLoadedChanged);
Polymer.updateStyles();
var containerRect = fixedWidthContainer.getBoundingClientRect();
var ironImageRect = fixedWidthIronImage.getBoundingClientRect();
var wrappedImageRect = fixedWidthIronImage.$.img.getBoundingClientRect();
expect(containerRect.width).to.be.closeTo(500, 0.5);
expect(ironImageRect.width).to.be.closeTo(500, 0.5);
expect(wrappedImageRect.width).to.be.closeTo(500, 0.5);
done();
});
fixedWidthIronImage.src = randomImageUrl();
});
test('100% height image fills container', function(done) {
fixedHeightIronImage.$.img.addEventListener('load', function onLoadedChanged(e) {
fixedHeightIronImage.$.img.removeEventListener('load', onLoadedChanged);
Polymer.updateStyles();
var containerRect = fixedHeightContainer.getBoundingClientRect();
var ironImageRect = fixedHeightIronImage.getBoundingClientRect();
var wrappedImageRect = fixedHeightIronImage.$.img.getBoundingClientRect();
expect(containerRect.height).to.be.closeTo(500, 0.5);
expect(ironImageRect.height).to.be.closeTo(500, 0.5);
expect(wrappedImageRect.height).to.be.closeTo(500, 0.5);
done();
});
fixedHeightIronImage.src = randomImageUrl();
});
});
suite('accessibility', function() {
suite('sizing inactive', function() {
var image;
setup(function() {
image = fixture('TrivialImage');
});
test('#sizedImgDiv is hidden', function() {
var sizedImgDivStyle = window.getComputedStyle(image.$.sizedImgDiv);
assert.strictEqual(sizedImgDivStyle.display, 'none');
});
test('img has no alt text by default', function() {
assert.isFalse(image.$.img.hasAttribute('alt'));
});
test('img alt text is empty string when iron-image alt text is empty string', function() {
image.alt = '';
assert.isTrue(image.$.img.hasAttribute('alt'));
assert.strictEqual(image.$.img.getAttribute('alt'), '');
});
test('img alt text matches iron-image alt text when defined', function() {
image.alt = 'alt text value';
assert.isTrue(image.$.img.hasAttribute('alt'));
assert.strictEqual(image.$.img.getAttribute('alt'), 'alt text value');
});
});
suite('sizing active', function() {
var image;
setup(function() {
image = fixture('TrivialImage');
image.sizing = 'cover';
});
test('inner img is hidden', function() {
var imgStyle = window.getComputedStyle(image.$.img);
assert.strictEqual(imgStyle.display, 'none');
});
test('#sizedImgDiv has empty aria-label text by default', function() {
assert.isTrue(image.$.sizedImgDiv.hasAttribute('aria-label'));
assert.strictEqual(image.$.sizedImgDiv.getAttribute('aria-label'), '');
});
test('#sizedImgDiv has aria-hidden when iron-image alt text is empty string', function() {
image.alt = '';
assert.isTrue(image.$.sizedImgDiv.hasAttribute('aria-hidden'));
var hiddenValue = image.$.sizedImgDiv.getAttribute('aria-hidden');
assert.isTrue(hiddenValue === '' || hiddenValue === 'true');
});
test('#sizedImgDiv aria-label matches iron-image alt text when defined', function() {
image.alt = 'alt text value';
assert.isTrue(image.$.sizedImgDiv.hasAttribute('aria-label'));
assert.strictEqual(image.$.sizedImgDiv.getAttribute('aria-label'), 'alt text value');
});
test('#sizedImgDiv aria-label text is last path component of src when iron-image alt text is undefined', function() {
image.src = '/some/path.components/file.jpg?a=b&c=d#anchor';
assert.isTrue(image.$.sizedImgDiv.hasAttribute('aria-label'));
assert.strictEqual(image.$.sizedImgDiv.getAttribute('aria-label'), 'file.jpg');
});
});
});
});
</script>
</body>
</html>