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
|
<!DOCTYPE html>
<title>MediaMetadata interface</title>
<script src=/resources/testharness.js></script>
<script src="/resources/testharnessreport.js"></script>
<script>
function load_iframe(src) {
return new Promise(resolve => {
const iframe = document.createElement('iframe');
iframe.onload = () => { resolve(iframe); };
iframe.src = src;
iframe.style.display = 'none';
document.documentElement.appendChild(iframe);
});
}
test(function() {
var metadata = new MediaMetadata({});
navigator.mediaSession.metadata = metadata;
assert_equals(navigator.mediaSession.metadata, metadata);
}, "Test that mediaSession.metadata is properly set");
test(function() {
var metadata = new MediaMetadata({});
navigator.mediaSession.metadata = metadata;
metadata.title = 'foo';
assert_equals(navigator.mediaSession.metadata.title, 'foo');
}, "Test that changes to metadata propagate properly");
test(function() {
var metadata = new MediaMetadata({});
navigator.mediaSession.metadata = metadata;
navigator.mediaSession.metadata = null;
assert_equals(navigator.mediaSession.metadata, null);
}, "Test that resetting metadata to null is reflected");
test(function() {
var metadata = new MediaMetadata({});
assert_not_equals(metadata, null);
assert_throws_js(TypeError, _ => new MediaMetadata('foobar'));
assert_throws_js(TypeError, _ => new MediaMetadata(42));
}, 'Test that MediaMetadata is constructed using a dictionary');
test(function() {
var metadata = new MediaMetadata();
assert_not_equals(metadata, null);
}, "Test that MediaMetadata constructor can take no parameter");
test(function() {
var image1 = { src: 'http://example.com/1', sizes: 'sizes1', type: 'type1' };
var image2 = { src: 'http://example.com/2', sizes: 'sizes2', type: 'type2' };
var metadata = new MediaMetadata({
title: 'foo', album: 'bar', artist: 'plop', artwork: [ image1, image2 ]
});
assert_equals(metadata.title, 'foo');
assert_equals(metadata.album, 'bar');
assert_equals(metadata.artist, 'plop');
assert_equals(metadata.artwork.length, 2);
assert_equals(metadata.artwork[0].src, image1.src);
assert_equals(metadata.artwork[0].sizes, image1.sizes);
assert_equals(metadata.artwork[0].type, image1.type);
assert_equals(metadata.artwork[1].src, image2.src);
assert_equals(metadata.artwork[1].sizes, image2.sizes);
assert_equals(metadata.artwork[1].type, image2.type);
}, 'Test the different values allowed in MediaMetadata init dictionary');
test(function() {
var metadata = new MediaMetadata({});
assert_equals(metadata.title, '');
assert_equals(metadata.artist, '');
assert_equals(metadata.album, '');
assert_equals(0, metadata.artwork.length);
}, 'Test the default values for MediaMetadata with empty init dictionary');
test(function() {
var metadata = new MediaMetadata();
assert_equals(metadata.title, '');
assert_equals(metadata.artist, '');
assert_equals(metadata.album, '');
assert_equals(0, metadata.artwork.length);
}, 'Test the default values for MediaMetadata with no init dictionary');
test(function() {
var metadata = new MediaMetadata({ randomValueThatWillNotBeAdded: '... hopefully ;)' });
assert_equals(metadata.randomValueThatWillNotBeAdded, undefined);
}, 'Test that passing unknown values to the dictionary is a no-op');
test(function() {
var image1 = { src: 'http://example.com/1', sizes: 'sizes1', type: 'type1' };
var image2 = { src: 'http://example.com/2', sizes: 'sizes2', type: 'type2' };
var metadata = new MediaMetadata({
title: 'foo', album: 'bar', artist: 'plop', artwork: [ image1, image2 ]
});
metadata.title = 'something else';
assert_equals(metadata.title, 'something else');
metadata.album = 'other value';
assert_equals(metadata.album, 'other value');
metadata.artist = 'someone else';
assert_equals(metadata.artist, 'someone else');
var image = { src: 'http://example.com/', sizes: '40x40', type: 'image/png' };
metadata.artwork = [ image ];
assert_equals(metadata.artwork.length, 1);
assert_equals(metadata.artwork[0].src, 'http://example.com/');
assert_equals(metadata.artwork[0].sizes, '40x40');
assert_equals(metadata.artwork[0].type, 'image/png');
}, "Test that MediaMetadata is read/write");
test(function() {
var metadata = new MediaMetadata({ artwork: [ { src: 'http://foo.com/' } ] });
assert_throws_js(TypeError, _ => {
metadata.artwork.push({
src: 'http://example.com/', sizes: '40x40', type: 'image/png',
});
});
metadata.artwork[0].src = 'bar';
assert_equals(metadata.artwork[0].src, 'http://foo.com/');
}, "Test that MediaMetadat.artwork can't be modified");
test(function() {
var metadata = new MediaMetadata({ artwork: [{
src: 'http://example.com/', sizes: '40x40', type: 'image/png',
some_other_value: 'foo',
}]});
assert_equals(metadata.artwork[0].src, 'http://example.com/');
assert_equals(metadata.artwork[0].sizes, '40x40');
assert_equals(metadata.artwork[0].type, 'image/png');
assert_false('some_other_value' in metadata.artwork[0]);
metadata.artwork[0].something_else = 'bar';
assert_false('something_else' in metadata.artwork[0]);
}, "Test that MediaMetadata.artwork will not expose unknown properties");
test(function() {
var metadata = new MediaMetadata({ artwork: [
{ src: 'http://example.com/1', sizes: '40x40', type: 'image/png' },
{ src: 'http://example.com/2', sizes: '40x40', type: 'image/png' },
]});
assert_true(Object.isFrozen(metadata.artwork));
for (var i = 0; i < metadata.artwork.length; ++i)
assert_true(Object.isFrozen(metadata.artwork[i]));
}, "Test that MediaMetadata.artwork is Frozen");
test(function() {
var metadata = new MediaMetadata({ artwork: [
{ src: 'http://example.com', sizes: '40x40', type: 'image/png' },
{ src: '../foo', sizes: '40x40', type: 'image/png' },
{ src: '/foo/bar', sizes: '40x40', type: 'image/png' },
]});
assert_equals(metadata.artwork[0].src, new URL('http://example.com', document.URL).href)
assert_equals(metadata.artwork[1].src, new URL('../foo', document.URL).href)
assert_equals(metadata.artwork[2].src, new URL('/foo/bar', document.URL).href)
}, "Test that MediaMetadata.artwork returns parsed urls");
test(function() {
var metadata = 42;
assert_throws_js(TypeError, _ => {
metadata
new MediaMetadata({ artwork: [ { src: 'http://[example.com]' }] });
});
assert_equals(metadata, 42);
metadata = new MediaMetadata();
assert_throws_js(TypeError, _ => {
metadata.artwork = [
// Valid url.
{ src: 'http://example.com' },
// Invalid url.
{ src: 'http://example.com:demo' },
];
});
assert_equals(metadata.artwork.length, 0);
}, "Test that MediaMetadata throws when setting an invalid url");
test(function() {
var metadata = new MediaMetadata({ artwork: [ { src: 'foo.jpg' } ] });
assert_equals(metadata.artwork[0].type, '');
assert_equals(metadata.artwork[0].sizes, '');
}, "Test MediaImage default values");
test(function() {
assert_throws_js(TypeError, _ => {
new MediaMetadata({ artwork: [ {} ] });
});
var metadata = new MediaMetadata();
assert_throws_js(TypeError, _ => {
metadata.artwork = [ { type: 'image/png', sizes: '40x40' } ];
});
}, "Test that MediaImage.src is required")
promise_test(async t => {
const URLs = [
'http://example.com',
'../foo',
'./foo/bar',
'/foo/bar',
];
const subframe = await load_iframe('helper/artwork-generator.html');
// createArtworkFromURLs is a function in the subframe.
const artwork = subframe.contentWindow.createArtworkFromURLs(URLs);
assert_equals(artwork.length, URLs.length);
for (let i = 0 ; i < artwork.length ; ++i) {
assert_equals(artwork[i].src, new URL(URLs[i], subframe.contentDocument.URL).href);
}
}, 'Test that the base URL of MediaImage is the base URL of entry setting object');
</script>
|