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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
"use strict";
/* global info */
var EXPORTED_SYMBOLS = ["PaymentTestUtils"];
var PaymentTestUtils = {
/**
* Common content tasks functions to be used with ContentTask.spawn.
*/
ContentTasks: {
/* eslint-env mozilla/frame-script */
/**
* Add a completion handler to the existing `showPromise` to call .complete().
* @returns {Object} representing the PaymentResponse
*/
addCompletionHandler: async ({ result, delayMs = 0 }) => {
let response = await content.showPromise;
let completeException;
// delay the given # milliseconds
await new Promise(resolve => content.setTimeout(resolve, delayMs));
try {
await response.complete(result);
} catch (ex) {
info(`Complete error: ${ex}`);
completeException = {
name: ex.name,
message: ex.message,
};
}
return {
completeException,
response: response.toJSON(),
// XXX: Bug NNN: workaround for `details` not being included in `toJSON`.
methodDetails: response.details,
};
},
/**
* Add a retry handler to the existing `showPromise` to call .retry().
* @returns {Object} representing the PaymentResponse
*/
addRetryHandler: async ({ validationErrors, delayMs = 0 }) => {
let response = await content.showPromise;
let retryException;
// delay the given # milliseconds
await new Promise(resolve => content.setTimeout(resolve, delayMs));
try {
await response.retry(Cu.cloneInto(validationErrors, content));
} catch (ex) {
info(`Retry error: ${ex}`);
retryException = {
name: ex.name,
message: ex.message,
};
}
return {
retryException,
response: response.toJSON(),
// XXX: Bug NNN: workaround for `details` not being included in `toJSON`.
methodDetails: response.details,
};
},
ensureNoPaymentRequestEvent: ({ eventName }) => {
content.rq.addEventListener(eventName, event => {
ok(false, `Unexpected ${eventName}`);
});
},
promisePaymentRequestEvent: ({ eventName }) => {
content[eventName + "Promise"] = new Promise(resolve => {
content.rq.addEventListener(eventName, () => {
info(`Received event: ${eventName}`);
resolve();
});
});
},
/**
* Used for PaymentRequest and PaymentResponse event promises.
*/
awaitPaymentEventPromise: async ({ eventName }) => {
await content[eventName + "Promise"];
return true;
},
promisePaymentResponseEvent: async ({ eventName }) => {
let response = await content.showPromise;
content[eventName + "Promise"] = new Promise(resolve => {
response.addEventListener(eventName, () => {
info(`Received event: ${eventName}`);
resolve();
});
});
},
updateWith: async ({ eventName, details }) => {
/* globals ok */
if (
details.error &&
(!details.shippingOptions || details.shippingOptions.length)
) {
ok(false, "Need to clear the shipping options to show error text");
}
if (!details.total) {
ok(
false,
"`total: { label, amount: { value, currency } }` is required for updateWith"
);
}
content[eventName + "Promise"] = new Promise(resolve => {
content.rq.addEventListener(
eventName,
event => {
event.updateWith(details);
resolve();
},
{ once: true }
);
});
},
/**
* Create a new payment request cached as `rq` and then show it.
*
* @param {Object} args
* @param {PaymentMethodData[]} methodData
* @param {PaymentDetailsInit} details
* @param {PaymentOptions} options
* @returns {Object}
*/
createAndShowRequest: ({ methodData, details, options }) => {
const rq = new content.PaymentRequest(
Cu.cloneInto(methodData, content),
details,
options
);
content.rq = rq; // assign it so we can retrieve it later
const handle = content.windowUtils.setHandlingUserInput(true);
content.showPromise = rq.show();
handle.destruct();
return {
requestId: rq.id,
};
},
},
DialogContentTasks: {
getShippingOptions: () => {
let picker = content.document.querySelector("shipping-option-picker");
let popupBox = Cu.waiveXrays(picker).dropdown.popupBox;
let selectedOptionIndex = popupBox.selectedIndex;
let selectedOption = Cu.waiveXrays(picker).dropdown.selectedOption;
let result = {
optionCount: popupBox.children.length,
selectedOptionIndex,
};
if (!selectedOption) {
return result;
}
return Object.assign(result, {
selectedOptionID: selectedOption.getAttribute("value"),
selectedOptionLabel: selectedOption.getAttribute("label"),
selectedOptionCurrency: selectedOption.getAttribute("amount-currency"),
selectedOptionValue: selectedOption.getAttribute("amount-value"),
});
},
getShippingAddresses: () => {
let doc = content.document;
let addressPicker = doc.querySelector(
"address-picker[selected-state-key='selectedShippingAddress']"
);
let popupBox = Cu.waiveXrays(addressPicker).dropdown.popupBox;
let options = Array.from(popupBox.children).map(option => {
return {
guid: option.getAttribute("guid"),
country: option.getAttribute("country"),
selected: option.selected,
};
});
let selectedOptionIndex = options.findIndex(item => item.selected);
return {
selectedOptionIndex,
options,
};
},
selectShippingAddressByCountry: country => {
let doc = content.document;
let addressPicker = doc.querySelector(
"address-picker[selected-state-key='selectedShippingAddress']"
);
let select = Cu.waiveXrays(addressPicker).dropdown.popupBox;
let option = select.querySelector(`[country="${country}"]`);
content.fillField(select, option.value);
},
selectPayerAddressByGuid: guid => {
let doc = content.document;
let addressPicker = doc.querySelector(
"address-picker[selected-state-key='selectedPayerAddress']"
);
let select = Cu.waiveXrays(addressPicker).dropdown.popupBox;
content.fillField(select, guid);
},
selectShippingAddressByGuid: guid => {
let doc = content.document;
let addressPicker = doc.querySelector(
"address-picker[selected-state-key='selectedShippingAddress']"
);
let select = Cu.waiveXrays(addressPicker).dropdown.popupBox;
content.fillField(select, guid);
},
selectShippingOptionById: value => {
let doc = content.document;
let optionPicker = doc.querySelector("shipping-option-picker");
let select = Cu.waiveXrays(optionPicker).dropdown.popupBox;
content.fillField(select, value);
},
selectPaymentOptionByGuid: guid => {
let doc = content.document;
let methodPicker = doc.querySelector("payment-method-picker");
let select = Cu.waiveXrays(methodPicker).dropdown.popupBox;
content.fillField(select, guid);
},
/**
* Click the primary button for the current page
*
* Don't await on this method from a ContentTask when expecting the dialog to close
*
* @returns {undefined}
*/
clickPrimaryButton: () => {
let { requestStore } = Cu.waiveXrays(
content.document.querySelector("payment-dialog")
);
let { page } = requestStore.getState();
let button = content.document.querySelector(`#${page.id} button.primary`);
ok(
!button.disabled,
`#${page.id} primary button should not be disabled when clicking it`
);
button.click();
},
/**
* Click the cancel button
*
* Don't await on this task since the cancel can close the dialog before
* ContentTask can resolve the promise.
*
* @returns {undefined}
*/
manuallyClickCancel: () => {
content.document.getElementById("cancel").click();
},
/**
* Do the minimum possible to complete the payment succesfully.
*
* Don't await on this task since the cancel can close the dialog before
* ContentTask can resolve the promise.
*
* @returns {undefined}
*/
completePayment: async () => {
let { PaymentTestUtils: PTU } = ChromeUtils.import(
"resource://testing-common/PaymentTestUtils.jsm"
);
await PTU.DialogContentUtils.waitForState(
content,
state => {
return state.page.id == "payment-summary";
},
"Wait for change to payment-summary before clicking Pay"
);
let button = content.document.getElementById("pay");
ok(
!button.disabled,
"Pay button should not be disabled when clicking it"
);
button.click();
},
setSecurityCode: ({ securityCode }) => {
// Waive the xray to access the untrusted `securityCodeInput` property
let picker = Cu.waiveXrays(
content.document.querySelector("payment-method-picker")
);
// Unwaive to access the ChromeOnly `setUserInput` API.
// setUserInput dispatches changes events.
Cu.unwaiveXrays(picker.securityCodeInput)
.querySelector("input")
.setUserInput(securityCode);
},
},
DialogContentUtils: {
waitForState: async (content, stateCheckFn, msg) => {
const { ContentTaskUtils } = ChromeUtils.import(
"resource://testing-common/ContentTaskUtils.jsm"
);
let { requestStore } = Cu.waiveXrays(
content.document.querySelector("payment-dialog")
);
await ContentTaskUtils.waitForCondition(
() => stateCheckFn(requestStore.getState()),
msg
);
return requestStore.getState();
},
getCurrentState: async content => {
let { requestStore } = Cu.waiveXrays(
content.document.querySelector("payment-dialog")
);
return requestStore.getState();
},
},
/**
* Common PaymentMethodData for testing
*/
MethodData: {
basicCard: {
supportedMethods: "basic-card",
},
bobPay: {
supportedMethods: "https://www.example.com/bobpay",
},
},
/**
* Common PaymentDetailsInit for testing
*/
Details: {
total2USD: {
total: {
label: "Total due",
amount: { currency: "USD", value: "2.00" },
},
},
total32USD: {
total: {
label: "Total due",
amount: { currency: "USD", value: "32.00" },
},
},
total60USD: {
total: {
label: "Total due",
amount: { currency: "USD", value: "60.00" },
},
},
total1pt75EUR: {
total: {
label: "Total due",
amount: { currency: "EUR", value: "1.75" },
},
},
total60EUR: {
total: {
label: "Total due",
amount: { currency: "EUR", value: "75.00" },
},
},
twoDisplayItems: {
displayItems: [
{
label: "First",
amount: { currency: "USD", value: "1" },
},
{
label: "Second",
amount: { currency: "USD", value: "2" },
},
],
},
twoDisplayItemsEUR: {
displayItems: [
{
label: "First",
amount: { currency: "EUR", value: "0.85" },
},
{
label: "Second",
amount: { currency: "EUR", value: "1.70" },
},
],
},
twoShippingOptions: {
shippingOptions: [
{
id: "1",
label: "Meh Unreliable Shipping",
amount: { currency: "USD", value: "1" },
},
{
id: "2",
label: "Premium Slow Shipping",
amount: { currency: "USD", value: "2" },
selected: true,
},
],
},
twoShippingOptionsEUR: {
shippingOptions: [
{
id: "1",
label: "Sloth Shipping",
amount: { currency: "EUR", value: "1.01" },
},
{
id: "2",
label: "Velociraptor Shipping",
amount: { currency: "EUR", value: "63545.65" },
selected: true,
},
],
},
noShippingOptions: {
shippingOptions: [],
},
bobPayPaymentModifier: {
modifiers: [
{
additionalDisplayItems: [
{
label: "Credit card fee",
amount: { currency: "USD", value: "0.50" },
},
],
supportedMethods: "basic-card",
total: {
label: "Total due",
amount: { currency: "USD", value: "2.50" },
},
data: {},
},
{
additionalDisplayItems: [
{
label: "Bob-pay fee",
amount: { currency: "USD", value: "1.50" },
},
],
supportedMethods: "https://www.example.com/bobpay",
total: {
label: "Total due",
amount: { currency: "USD", value: "3.50" },
},
},
],
},
additionalDisplayItemsEUR: {
modifiers: [
{
additionalDisplayItems: [
{
label: "Handling fee",
amount: { currency: "EUR", value: "1.00" },
},
],
supportedMethods: "basic-card",
total: {
label: "Total due",
amount: { currency: "EUR", value: "2.50" },
},
},
],
},
noError: {
error: "",
},
genericShippingError: {
error: "Cannot ship with option 1 on days that end with Y",
},
fieldSpecificErrors: {
error: "There are errors related to specific parts of the address",
shippingAddressErrors: {
addressLine:
"Can only ship to ROADS, not DRIVES, BOULEVARDS, or STREETS",
city: "Can only ship to CITIES, not TOWNSHIPS or VILLAGES",
country: "Can only ship to USA, not CA",
dependentLocality: "Can only be SUBURBS, not NEIGHBORHOODS",
organization: "Can only ship to CORPORATIONS, not CONSORTIUMS",
phone: "Only allowed to ship to area codes that start with 9",
postalCode: "Only allowed to ship to postalCodes that start with 0",
recipient: "Can only ship to names that start with J",
region: "Can only ship to regions that start with M",
regionCode:
"Regions must be 1 to 3 characters in length (sometimes ;) )",
},
},
},
Options: {
requestShippingOption: {
requestShipping: true,
},
requestPayerNameAndEmail: {
requestPayerName: true,
requestPayerEmail: true,
},
requestPayerNameEmailAndPhone: {
requestPayerName: true,
requestPayerEmail: true,
requestPayerPhone: true,
},
},
Addresses: {
TimBR: {
"given-name": "Timothy",
"additional-name": "João",
"family-name": "Berners-Lee",
organization: "World Wide Web Consortium",
"street-address": "Rua Adalberto Pajuaba, 404",
"address-level3": "Campos Elísios",
"address-level2": "Ribeirão Preto",
"address-level1": "SP",
"postal-code": "14055-220",
country: "BR",
tel: "+0318522222222",
email: "timbr@example.org",
},
TimBL: {
"given-name": "Timothy",
"additional-name": "John",
"family-name": "Berners-Lee",
organization: "World Wide Web Consortium",
"street-address": "32 Vassar Street\nMIT Room 32-G524",
"address-level2": "Cambridge",
"address-level1": "MA",
"postal-code": "02139",
country: "US",
tel: "+16172535702",
email: "timbl@example.org",
},
TimBL2: {
"given-name": "Timothy",
"additional-name": "Johann",
"family-name": "Berners-Lee",
organization: "World Wide Web Consortium",
"street-address": "1 Pommes Frittes Place",
"address-level2": "Berlin",
// address-level1 isn't used in our forms for Germany
"postal-code": "02138",
country: "DE",
tel: "+16172535702",
email: "timbl@example.com",
},
/* Used as a temporary (not persisted in autofill storage) address in tests */
Temp: {
"given-name": "Temp",
"family-name": "McTempFace",
organization: "Temps Inc.",
"street-address": "1a Temporary Ave.",
"address-level2": "Temp Town",
"address-level1": "CA",
"postal-code": "31337",
country: "US",
tel: "+15032541000",
email: "tempie@example.com",
},
},
BasicCards: {
JohnDoe: {
"cc-exp-month": 1,
"cc-exp-year": new Date().getFullYear() + 9,
"cc-name": "John Doe",
"cc-number": "4111111111111111",
"cc-type": "visa",
},
JaneMasterCard: {
"cc-exp-month": 12,
"cc-exp-year": new Date().getFullYear() + 9,
"cc-name": "Jane McMaster-Card",
"cc-number": "5555555555554444",
"cc-type": "mastercard",
},
MissingFields: {
"cc-name": "Missy Fields",
"cc-number": "340000000000009",
},
Temp: {
"cc-exp-month": 12,
"cc-exp-year": new Date().getFullYear() + 9,
"cc-name": "Temp Name",
"cc-number": "5105105105105100",
"cc-type": "mastercard",
},
},
};
|