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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
<!doctype html>
<meta charset="utf-8">
<title>Tests for PaymentInstruments interface</title>
<link rel="help" href="https://w3c.github.io/payment-handler/#paymentinstruments-interface">
<link rel="manifest" href="manifest.json">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="register-and-activate-service-worker.js"></script>
<script>
function runTests(registration) {
const methodName = window.location.origin + '/payment-handler/payment-app/';
promise_test(async t => {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set('instrument-key-1', {
name: 'Instrument Name 1',
});
await registration.paymentManager.instruments.set('instrument-key-2', {
name: 'Instrument Name 2',
});
await registration.paymentManager.instruments.delete('instrument-key-1');
await registration.paymentManager.instruments.set('instrument-key-1', {
name: 'Instrument Name 1',
});
const keys = await registration.paymentManager.instruments.keys();
assert_array_equals(keys, ['instrument-key-2', 'instrument-key-1']);
}, 'Instrument keys are returned in the original insertion order');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set(
'existing-instrument-key',
{
name: 'Instrument Name',
},
);
const result = await registration.paymentManager.instruments.delete(
'existing-instrument-key',
);
assert_true(result);
}, 'Deleting an existing instrument returns true');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set(
'existing-instrument-key',
{
name: 'Instrument Name',
},
);
await registration.paymentManager.instruments.delete(
'existing-instrument-key',
);
const result = await registration.paymentManager.instruments.delete(
'existing-instrument-key',
);
assert_false(result);
}, 'Deleting an existing instrument the second time returns false');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const result = await registration.paymentManager.instruments.delete(
'non-existing-instrument-key',
);
assert_false(result);
}, 'Deleting a non-existing instrument returns false');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set(
'existing-instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256x256',
type: 'image/png',
},
],
method: methodName,
},
);
const result = await registration.paymentManager.instruments.get(
'existing-instrument-key',
);
assert_equals(result.name, 'Instrument Name');
}, 'Getting an existing instrument returns the instrument');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const result = await registration.paymentManager.instruments.get(
'non-existing-instrument-key',
);
assert_equals(result, undefined);
}, 'Getting a non-existing instrument returns undefined');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set(
'existing-instrument-key',
{
name: 'Instrument Name v1',
icons: [
{src: '/images/green-16x16.png', sizes: '16x16', type: 'image/png'},
],
method: methodName,
},
);
let result = await registration.paymentManager.instruments.get(
'existing-instrument-key',
);
assert_equals(result.name, 'Instrument Name v1');
assert_equals(result.icons.length, 1);
assert_equals(
result.icons[0].src,
new URL('/images/green-16x16.png', window.location.href).href,
);
assert_equals(result.icons[0].sizes, '16x16');
assert_equals(result.icons[0].type, 'image/png');
assert_equals(result.method, methodName);
assert_array_equals(result.capabilities.supportedNetworks, ['mir']);
await registration.paymentManager.instruments.set(
'existing-instrument-key',
{
name: 'Instrument Name v2',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256x256',
type: 'image/png',
},
],
method: methodName,
},
);
result = await registration.paymentManager.instruments.get(
'existing-instrument-key',
);
assert_equals(result.name, 'Instrument Name v2');
assert_equals(result.icons.length, 1);
assert_equals(
result.icons[0].src,
new URL('/images/rgrg-256x256.png', window.location.href).href,
);
assert_equals(result.icons[0].sizes, '256x256');
assert_equals(result.icons[0].type, 'image/png');
assert_equals(result.method, methodName);
}, 'Resetting an existing instrument updates the instrument');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set(
'existing-instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256x256',
type: 'image/png',
},
],
method: methodName,
},
);
await registration.paymentManager.instruments.clear();
const result = await registration.paymentManager.instruments.get(
'existing-instrument-key',
);
assert_equals(result, undefined);
}, 'Clearing the instruments');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const setPromise = registration.paymentManager.instruments.set(
'instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256x256',
type: 'image/jif',
},
],
method: methodName,
},
);
return promise_rejects_js(t, TypeError, setPromise);
}, 'Cannot register instruments with invalid icon media type image/jif');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const setPromise = registration.paymentManager.instruments.set(
'instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256x256',
type: 'image/pn' + 'g'.repeat(100000),
},
],
method: methodName,
},
);
return promise_rejects_js(t, TypeError, setPromise);
}, "Don't crash when registering instruments with very long icon media type image/pngggggg...");
promise_test(async t => {
await registration.paymentManager.instruments.clear();
return registration.paymentManager.instruments.set('instrument-key', {
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '8'.repeat(100000) + 'x' + '8'.repeat(100000),
type: 'image/png',
},
],
method: methodName,
});
}, "Don't crash when registering an instrument with a very long icon size 888...x888...");
promise_test(async t => {
await registration.paymentManager.instruments.clear();
return registration.paymentManager.instruments.set('instrument-key', {
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
type: 'image/png',
},
],
method: methodName,
});
}, "Don't crash when 'sizes' missing from icon definition");
promise_test(async t => {
await registration.paymentManager.instruments.clear();
return registration.paymentManager.instruments.set('instrument-key', {
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256x256',
},
],
method: methodName,
});
}, "Don't crash when 'type' missing from icon definition");
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const setPromise = registration.paymentManager.instruments.set(
'instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: '/images/rgrg-256x256.png',
sizes: '256 256',
type: 'image/png',
},
],
method: methodName,
},
);
return promise_rejects_js(t, TypeError, setPromise);
}, 'Cannot register instruments with invalid icon size "256 256" (missing "x")');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const setPromise = registration.paymentManager.instruments.set(
'instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: '/images/rg\0rg-256x256.png',
sizes: '256x256',
type: 'image/png',
},
],
method: methodName,
},
);
return promise_rejects_js(t, TypeError, setPromise);
}, 'Cannot register instruments with invalid icon URL (has a null character)');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const setPromise = registration.paymentManager.instruments.set(
'instrument-key',
{
name: 'Instrument Name',
icons: [
{
src: 'http://test.example/images/rgrg-256x256.png',
sizes: '256x256',
type: 'image/png',
},
],
method: methodName,
},
);
return promise_rejects_js(t, TypeError, setPromise);
}, 'Cannot register instruments with non-existing non-https icon URL');
promise_test(async t => {
await registration.paymentManager.instruments.clear();
const setPromise = registration.paymentManager.instruments.set(
'instrument-key',
{
name: 'Instrument Name',
icons: [
{
src:
'http://www.chromium.org/_/rsrc/1438879449147/config/customLogo.gif',
sizes: '48x48',
type: 'image/gif',
},
],
method: methodName,
},
);
return promise_rejects_js(t, TypeError, setPromise);
}, 'Cannot register instruments with an existing non-https icon URL');
async function testUnusualStrings(existingKey, nonExistingKey) {
await registration.paymentManager.instruments.clear();
await registration.paymentManager.instruments.set(existingKey, {
name: existingKey,
icons: [
{src: '/images/rgrg-256x256.png', sizes: '256x256', type: 'image/png'},
],
method: existingKey,
capabilities: {aCapabilityName: existingKey},
});
const hasExistingInstrument = await registration.paymentManager.instruments.has(
existingKey,
);
assert_true(hasExistingInstrument);
const hasNonExistingInstrument = await registration.paymentManager.instruments.has(
nonExistingKey,
);
assert_false(hasNonExistingInstrument);
const existingInstrument = await registration.paymentManager.instruments.get(
existingKey,
);
assert_equals(existingInstrument.name, existingKey);
const nonExistingInstrument = await registration.paymentManager.instruments.get(
nonExistingKey,
);
assert_equals(nonExistingInstrument, undefined);
const deletedExistingInstrument = await registration.paymentManager.instruments.delete(
existingKey,
);
assert_true(deletedExistingInstrument);
const deletedNonExistingInstrument = await registration.paymentManager.instruments.delete(
nonExistingKey,
);
assert_false(deletedNonExistingInstrument);
}
promise_test(async t => {
const length = 100000;
await testUnusualStrings('0'.repeat(length), '1'.repeat(length));
}, "Don't crash on very long key, name, method, and capability strings.");
promise_test(async t => {
await testUnusualStrings('foo\0bar', 'foo\0baz');
}, "Don't crash on null characters in key, name, method, and capability strings.");
}
registerAndActiveServiceWorker('app-simple.js', 'payment-app/', runTests);
</script>
|