summaryrefslogtreecommitdiffstats
path: root/dom/payments/test/ConstructorChromeScript.js
blob: 17e59f62412cb6bffc4547b342d01527f8196660 (plain)
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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-env mozilla/chrome-script */

"use strict";

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

const paymentSrv = Cc[
  "@mozilla.org/dom/payments/payment-request-service;1"
].getService(Ci.nsIPaymentRequestService);

function emitTestFail(message) {
  sendAsyncMessage("test-fail", message);
}

function checkSimplestRequest(payRequest) {
  if (payRequest.topLevelPrincipal.origin != "https://example.com") {
    emitTestFail(
      "Top level principal's Origin should be 'https://example.com', but got '" +
        payRequest.topLevelPrincipal.origin +
        "'."
    );
  }

  if (payRequest.paymentMethods.length != 1) {
    emitTestFail("paymentMethods' length should be 1.");
  }

  const methodData = payRequest.paymentMethods.queryElementAt(
    0,
    Ci.nsIPaymentMethodData
  );
  if (!methodData) {
    emitTestFail("Fail to get payment methodData.");
  }
  const supportedMethod = methodData.supportedMethods;
  if (supportedMethod != "basic-card") {
    emitTestFail("supported method should be 'basic-card'.");
  }
  if (methodData.data) {
    emitTestFail("methodData.data should not exist.");
  }

  // checking the passed PaymentDetails parameter
  const details = payRequest.paymentDetails;
  if (details.totalItem.label != "Total") {
    emitTestFail("total item's label should be 'Total'.");
  }
  if (details.totalItem.amount.currency != "USD") {
    emitTestFail("total item's currency should be 'USD'.");
  }
  if (details.totalItem.amount.value != "1.00") {
    emitTestFail("total item's value should be '1.00'.");
  }

  if (details.displayItems.length !== 0) {
    emitTestFail("details.displayItems should be an empty array.");
  }
  if (details.modifiers.length !== 0) {
    emitTestFail("details.modifiers should be an empty array.");
  }
  if (details.shippingOptions.length !== 0) {
    emitTestFail("details.shippingOptions should be an empty array.");
  }

  // checking the default generated PaymentOptions parameter
  const paymentOptions = payRequest.paymentOptions;
  if (paymentOptions.requestPayerName) {
    emitTestFail("requestPayerName option should be false.");
  }
  if (paymentOptions.requestPayerEmail) {
    emitTestFail("requestPayerEmail option should be false.");
  }
  if (paymentOptions.requestPayerPhone) {
    emitTestFail("requestPayerPhone option should be false.");
  }
  if (paymentOptions.requestShipping) {
    emitTestFail("requestShipping option should be false.");
  }
  if (paymentOptions.shippingType != "shipping") {
    emitTestFail("shippingType option should be 'shipping'.");
  }
}

// eslint-disable-next-line complexity
function checkComplexRequest(payRequest) {
  if (payRequest.topLevelPrincipal.origin != "https://example.com") {
    emitTestFail(
      "Top level principal's origin should be 'https://example.com', but got '" +
        payRequest.topLevelPrincipal.origin +
        "'."
    );
  }

  if (payRequest.paymentMethods.length != 1) {
    emitTestFail("paymentMethods' length should be 1.");
  }

  const methodData = payRequest.paymentMethods.queryElementAt(
    0,
    Ci.nsIPaymentMethodData
  );
  if (!methodData) {
    emitTestFail("Fail to get payment methodData.");
  }
  let supportedMethod = methodData.supportedMethods;
  if (supportedMethod != "basic-card") {
    emitTestFail("supported method should be 'basic-card'.");
  }
  const data = methodData.data;
  const supportedNetworks = data.supportedNetworks;
  const expectedSupportedNetworks = [
    "unionpay",
    "visa",
    "mastercard",
    "amex",
    "discover",
    "diners",
    "jcb",
    "mir",
  ];
  if (supportedNetworks.length != expectedSupportedNetworks.length) {
    emitTestFail(
      "supportedNetworks.length should be " +
        expectedSupportedNetworks.length +
        ", but got " +
        supportedNetworks.length +
        "."
    );
  }
  for (let idx = 0; idx < supportedNetworks.length; idx++) {
    if (supportedNetworks[idx] != expectedSupportedNetworks[idx]) {
      emitTestFail(
        "supportedNetworks[" +
          idx +
          "] should be '" +
          expectedSupportedNetworks[idx] +
          "', but got '" +
          supportedNetworks[idx] +
          "'."
      );
    }
  }
  // checking the passed PaymentDetails parameter
  const details = payRequest.paymentDetails;
  if (details.id != "payment details") {
    emitTestFail("details.id should be 'payment details'.");
  }
  if (details.totalItem.label != "Total") {
    emitTestFail("total item's label should be 'Total'.");
  }
  if (details.totalItem.amount.currency != "USD") {
    emitTestFail("total item's currency should be 'USD'.");
  }
  if (details.totalItem.amount.value != "100.00") {
    emitTestFail("total item's value should be '100.00'.");
  }

  const displayItems = details.displayItems;
  if (!details.displayItems) {
    emitTestFail("details.displayItems should not be undefined.");
  }
  if (displayItems.length != 2) {
    emitTestFail("displayItems' length should be 2.");
  }
  let item = displayItems.queryElementAt(0, Ci.nsIPaymentItem);
  if (item.label != "First item") {
    emitTestFail("1st display item's label should be 'First item'.");
  }
  if (item.amount.currency != "USD") {
    emitTestFail("1st display item's currency should be 'USD'.");
  }
  if (item.amount.value != "60.00") {
    emitTestFail("1st display item's value should be '60.00'.");
  }
  item = displayItems.queryElementAt(1, Ci.nsIPaymentItem);
  if (item.label != "Second item") {
    emitTestFail("2nd display item's label should be 'Second item'.");
  }
  if (item.amount.currency != "USD") {
    emitTestFail("2nd display item's currency should be 'USD'.");
  }
  if (item.amount.value != "40.00") {
    emitTestFail("2nd display item's value should be '40.00'.");
  }

  const modifiers = details.modifiers;
  if (!modifiers) {
    emitTestFail("details.displayItems should not be undefined.");
  }
  if (modifiers.length != 1) {
    emitTestFail("modifiers' length should be 1.");
  }
  const modifier = modifiers.queryElementAt(0, Ci.nsIPaymentDetailsModifier);
  const supportedMethods = modifier.supportedMethods;
  if (supportedMethod != "basic-card") {
    emitTestFail("modifier's supported method name should be 'basic-card'.");
  }
  if (modifier.total.label != "Discounted Total") {
    emitTestFail("modifier's total label should be 'Discounted Total'.");
  }
  if (modifier.total.amount.currency != "USD") {
    emitTestFail("modifier's total currency should be 'USD'.");
  }
  if (modifier.total.amount.value != "90.00") {
    emitTestFail("modifier's total value should be '90.00'.");
  }

  const additionalItems = modifier.additionalDisplayItems;
  if (additionalItems.length != 1) {
    emitTestFail("additionalDisplayItems' length should be 1.");
  }
  const additionalItem = additionalItems.queryElementAt(0, Ci.nsIPaymentItem);
  if (additionalItem.label != "basic-card discount") {
    emitTestFail("additional item's label should be 'basic-card discount'.");
  }
  if (additionalItem.amount.currency != "USD") {
    emitTestFail("additional item's currency should be 'USD'.");
  }
  if (additionalItem.amount.value != "-10.00") {
    emitTestFail("additional item's value should be '-10.00'.");
  }
  if (modifier.data.discountProgramParticipantId != "86328764873265") {
    emitTestFail(
      "modifier's data should be '86328764873265', but got '" +
        modifier.data.discountProgramParticipantId +
        "'."
    );
  }

  const shippingOptions = details.shippingOptions;
  if (!shippingOptions) {
    emitTestFail("details.shippingOptions should not be undefined.");
  }
  if (shippingOptions.length != 2) {
    emitTestFail("shippingOptions' length should be 2.");
  }
  let shippingOption = shippingOptions.queryElementAt(
    0,
    Ci.nsIPaymentShippingOption
  );
  if (shippingOption.id != "NormalShipping") {
    emitTestFail("1st shippingOption's id should be 'NormalShipping'.");
  }
  if (shippingOption.label != "NormalShipping") {
    emitTestFail("1st shippingOption's lable should be 'NormalShipping'.");
  }
  if (shippingOption.amount.currency != "USD") {
    emitTestFail("1st shippingOption's amount currency should be 'USD'.");
  }
  if (shippingOption.amount.value != "10.00") {
    emitTestFail("1st shippingOption's amount value should be '10.00'.");
  }
  if (!shippingOption.selected) {
    emitTestFail("1st shippingOption should be selected.");
  }
  shippingOption = shippingOptions.queryElementAt(
    1,
    Ci.nsIPaymentShippingOption
  );
  if (shippingOption.id != "FastShipping") {
    emitTestFail("2nd shippingOption's id should be 'FastShipping'.");
  }
  if (shippingOption.label != "FastShipping") {
    emitTestFail("2nd shippingOption's lable should be 'FastShipping'.");
  }
  if (shippingOption.amount.currency != "USD") {
    emitTestFail("2nd shippingOption's amount currency should be 'USD'.");
  }
  if (shippingOption.amount.value != "30.00") {
    emitTestFail("2nd shippingOption's amount value should be '30.00'.");
  }
  if (shippingOption.selected) {
    emitTestFail("2nd shippingOption should not be selected.");
  }

  // checking the default generated PaymentOptions parameter
  const paymentOptions = payRequest.paymentOptions;
  if (!paymentOptions.requestPayerName) {
    emitTestFail("requestPayerName option should be true.");
  }
  if (!paymentOptions.requestPayerEmail) {
    emitTestFail("requestPayerEmail option should be true.");
  }
  if (!paymentOptions.requestPayerPhone) {
    emitTestFail("requestPayerPhone option should be true.");
  }
  if (!paymentOptions.requestShipping) {
    emitTestFail("requestShipping option should be true.");
  }
  if (paymentOptions.shippingType != "shipping") {
    emitTestFail("shippingType option should be 'shipping'.");
  }
}

function checkNonBasicCardRequest(payRequest) {
  if (payRequest.paymentMethods.length != 1) {
    emitTestFail("paymentMethods' length should be 1.");
  }

  const methodData = payRequest.paymentMethods.queryElementAt(
    0,
    Ci.nsIPaymentMethodData
  );
  if (!methodData) {
    emitTestFail("Fail to get payment methodData.");
  }
  const supportedMethod = methodData.supportedMethods;
  if (supportedMethod != "testing-payment-method") {
    emitTestFail("supported method should be 'testing-payment-method'.");
  }

  const paymentId = methodData.data.paymentId;
  if (paymentId != "P3892940") {
    emitTestFail(
      "methodData.data.paymentId should be 'P3892940', but got " +
        paymentId +
        "."
    );
  }
  const paymentType = methodData.data.paymentType;
  if (paymentType != "prepaid") {
    emitTestFail(
      "methodData.data.paymentType should be 'prepaid', but got " +
        paymentType +
        "."
    );
  }

  // checking the passed PaymentDetails parameter
  const details = payRequest.paymentDetails;
  if (details.totalItem.label != "Total") {
    emitTestFail("total item's label should be 'Total'.");
  }
  if (details.totalItem.amount.currency != "USD") {
    emitTestFail("total item's currency should be 'USD'.");
  }
  if (details.totalItem.amount.value != "1.00") {
    emitTestFail("total item's value should be '1.00'.");
  }

  if (details.displayItems.length !== 0) {
    emitTestFail("details.displayItems should be an zero length array.");
  }
  if (details.displayItems.length !== 0) {
    emitTestFail("details.modifiers should be an zero length array.");
  }
  if (details.displayItems.length !== 0) {
    emitTestFail("details.shippingOptions should be an zero length array.");
  }

  // checking the default generated PaymentOptions parameter
  const paymentOptions = payRequest.paymentOptions;
  if (paymentOptions.requestPayerName) {
    emitTestFail("requestPayerName option should be false.");
  }
  if (paymentOptions.requestPayerEmail) {
    emitTestFail("requestPayerEmail option should be false.");
  }
  if (paymentOptions.requestPayerPhone) {
    emitTestFail("requestPayerPhone option should be false.");
  }
  if (paymentOptions.requestShipping) {
    emitTestFail("requestShipping option should be false.");
  }
  if (paymentOptions.shippingType != "shipping") {
    emitTestFail("shippingType option should be 'shipping'.");
  }
}

function checkSimplestRequestHandler() {
  const paymentEnum = paymentSrv.enumerate();
  if (!paymentEnum.hasMoreElements()) {
    emitTestFail(
      "PaymentRequestService should have at least one payment request."
    );
  }
  for (let payRequest of paymentEnum) {
    if (!payRequest) {
      emitTestFail("Fail to get existing payment request.");
      break;
    }
    checkSimplestRequest(payRequest);
  }
  paymentSrv.cleanup();
  sendAsyncMessage("check-complete");
}

function checkComplexRequestHandler() {
  const paymentEnum = paymentSrv.enumerate();
  if (!paymentEnum.hasMoreElements()) {
    emitTestFail(
      "PaymentRequestService should have at least one payment request."
    );
  }
  for (let payRequest of paymentEnum) {
    if (!payRequest) {
      emitTestFail("Fail to get existing payment request.");
      break;
    }
    checkComplexRequest(payRequest);
  }
  paymentSrv.cleanup();
  sendAsyncMessage("check-complete");
}

function checkNonBasicCardRequestHandler() {
  const paymentEnum = paymentSrv.enumerate();
  if (!paymentEnum.hasMoreElements()) {
    emitTestFail(
      "PaymentRequestService should have at least one payment request."
    );
  }
  for (let payRequest of paymentEnum) {
    if (!payRequest) {
      emitTestFail("Fail to get existing payment request.");
      break;
    }
    checkNonBasicCardRequest(payRequest);
  }
  paymentSrv.cleanup();
  sendAsyncMessage("check-complete");
}

function checkMultipleRequestsHandler() {
  const paymentEnum = paymentSrv.enumerate();
  if (!paymentEnum.hasMoreElements()) {
    emitTestFail(
      "PaymentRequestService should have at least one payment request."
    );
  }
  for (let payRequest of paymentEnum) {
    if (!payRequest) {
      emitTestFail("Fail to get existing payment request.");
      break;
    }
    if (payRequest.paymentDetails.id == "payment details") {
      checkComplexRequest(payRequest);
    } else {
      checkSimplestRequest(payRequest);
    }
  }
  paymentSrv.cleanup();
  sendAsyncMessage("check-complete");
}

function checkCrossOriginTopLevelPrincipalHandler() {
  const paymentEnum = paymentSrv.enumerate();
  if (!paymentEnum.hasMoreElements()) {
    emitTestFail(
      "PaymentRequestService should have at least one payment request."
    );
  }
  for (let payRequest of paymentEnum) {
    if (!payRequest) {
      emitTestFail("Fail to get existing payment request.");
      break;
    }
    if (payRequest.topLevelPrincipal.origin != "https://example.com") {
      emitTestFail(
        "Top level principal's origin should be 'https://example.com', but got '" +
          payRequest.topLevelPrincipal.origin +
          "'."
      );
    }
  }
  paymentSrv.cleanup();
  sendAsyncMessage("check-complete");
}

addMessageListener("check-simplest-request", checkSimplestRequestHandler);
addMessageListener("check-complex-request", checkComplexRequestHandler);
addMessageListener("check-multiple-requests", checkMultipleRequestsHandler);
addMessageListener(
  "check-nonbasiccard-request",
  checkNonBasicCardRequestHandler
);
addMessageListener(
  "check-cross-origin-top-level-principal",
  checkCrossOriginTopLevelPrincipalHandler
);

addMessageListener("teardown", function () {
  sendAsyncMessage("teardown-complete");
});