summaryrefslogtreecommitdiffstats
path: root/dom/payments/test/test_showPayment.html
blob: 2a4a0bb4f7e60ad232dc838c8d582f7336009e6e (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1345366
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1345366</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript">

  "use strict";
  SimpleTest.waitForExplicitFinish();

  var gUrl = SimpleTest.getTestFileURL('ShowPaymentChromeScript.js');
  var gScript = SpecialPowers.loadChromeScript(gUrl);

  function testFailHandler(message) {
    ok(false, message);
  }
  function testPassHandler(message) {
    ok(true, message);
  }
  gScript.addMessageListener("test-fail", testFailHandler);
  gScript.addMessageListener("test-pass", testPassHandler);

  async function requestChromeAction(action, params) {
    await new Promise(resolve => {
      gScript.addMessageListener(`${action}-complete`, function completeListener() {
        gScript.removeMessageListener(`${action}-complete`, completeListener);
        resolve();
      });
      gScript.sendAsyncMessage(action, params);
    });
  }

  // testing data declaration
  // default parameters for PaymentRequest construction
  const defaultMethods = [{
    supportedMethods: "basic-card",
    data: {
      supportedNetworks: ['unionpay', 'visa', 'mastercard', 'amex', 'discover',
                          'diners', 'jcb', 'mir',
      ],
    },
  }, {
    supportedMethods: "testing-payment-method",
  }];

  const defaultTotal = {
    label: "Total",
    amount: {
      currency: "USD",
      value: "1.00",
    },
  }

  const defaultDetails = {
    id: "test payment",
    total: defaultTotal,
    shippingOptions: [
      {
        id: "NormalShipping",
        label: "NormalShipping",
        amount: {
          currency: "USD",
          value: "10.00"
        },
        selected: false,
      },
      {
        id: "FastShipping",
        label: "FastShipping",
        amount: {
          currency: "USD",
          value: "30.00"
        },
        selected: false,
      },
    ],
  };

  const defaultOptions = {
    requestPayerName: true,
    requestPayerEmail: false,
    requestPayerPhone: false,
    requestShipping: true,
    shippingType: "shipping"
  };

  // testing data for PaymentRequestUpdateEvent.updateWith()
  const updatedShippingOptionsDetails = {
    total: defaultTotal,
    shippingOptions: [
      {
        id: "NormalShipping",
        label: "NormalShipping",
        amount: {
          currency: "USD",
          value: "10.00"
        },
        selected: false,
      },
      {
        id: "FastShipping",
        label: "FastShipping",
        amount: {
          currency: "USD",
          value: "30.00"
        },
        selected: true,
      },
    ],
  };

  const updatedErrorDetails = {
    total: defaultTotal,
    error: "Update with Error",
  };

  // Promise function for PaymentRequestUpdateEvent.updateWith()
  function updateWithPromise(detailsUpdate) {
    return new Promise((resolve, reject) => {
      if (detailsUpdate) {
        resolve(detailsUpdate);
      } else {
        reject();
      }
    });
  }

  // testing data for PaymentRequest.show() with Non-supported methods
  const nonSupportedMethods = [{
    supportedMethods: "nonsupported-method",
  }];


  // checking functions
  function checkAddress(testName, address, fromEvent) {
    is(address.country,
       "USA",
       `${testName}: address.country should be 'USA'.`);
    is(address.region,
       "CA",
       `${testName}: address.region should be 'CA'.`);
    is(address.city,
       "San Bruno",
       `${testName}: address.city should be 'San Bruno'.`);
    is(address.dependentLocality,
       "Test locality",
       `${testName}: address.dependentLocality should be 'Test locality'.`);
    is(address.postalCode,
       "94066",
       `${testName}: address.postalCode should be '94066'.`);
    is(address.sortingCode,
       "123456",
       `${testName}: address.sortingCode should be '123456'.`);
    if (fromEvent) {
      is(address.addressLine.length,
         0,
         `${testName}: address.addressLine.length should be 0 from event.`);
      is(address.organization,
         "",
         `${testName}: address.organization should be empty from event.`);
      is(address.recipient,
         "",
         `${testName}: address.recipient should be empty from event.`);
      is(address.phone,
         "",
         `${testName}: address.phone should be empty from event.`);
    } else {
      is(address.addressLine.length,
         1,
         `${testName}: address.addressLine.length should be 1 from promise.`);
      is(address.addressLine[0],
         "Easton Ave",
         `${testName}: address.addressLine[0] should be 'Easton Ave' from promise.`);
      is(address.organization,
         "Testing Org",
         `${testName}: address.organization should be 'Testing Org' from promise.`);
      is(address.recipient,
         "Bill A. Pacheco",
         `${testName}: address.recipient should be 'Bill A. Pacheco' from promise.`);
      is(address.phone,
         "+1-434-441-3879",
         `${testName}: address.phone should be '+1-434-441-3879' from promise.`);
    }
  }

  function checkResponse(testName, response) {
    is(response.requestId,
       "test payment",
       `${testName}: response.requestId should be 'test payment'.`);
    is(response.methodName,
       "testing-payment-method",
       `${testName}: response.methodName should be 'testing-payment-method'.`);
    is(response.details.paymentToken,
       "6880281f-0df3-4b8e-916f-66575e2457c1",
       `${testName}: response.details.paymentToken should be '6880281f-0df3-4b8e-916f-66575e2457c1'.`);
    checkAddress(testName, response.shippingAddress, false/*fromEvent*/);
    is(response.shippingOption,
       "FastShipping",
       `${testName}: response.shippingOption should be 'FastShipping'.`);
    is(response.payerName,
       "Bill A. Pacheco",
       `${testName}: response.payerName should be 'Bill A. Pacheco'.`);
    ok(!response.payerEmail,
       `${testName}: response.payerEmail should be empty`);
    ok(!response.payerPhone,
       `${testName}: response.payerPhone should be empty`);
  }

  // testing functions
  async function testShowNormalFlow() {
    const testName = "testShowNormalFlow";
    await requestChromeAction("set-normal-ui-service", testName);

    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    request.addEventListener("shippingaddresschange", event => {
      checkAddress(testName, request.shippingAddress, true/*fromEvent*/);
      event.updateWith(updateWithPromise(defaultDetails));
    });
    request.addEventListener("shippingoptionchange", event => {
      event.updateWith(updateWithPromise(updatedShippingOptionsDetails));
    });
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    try {
      let response = await request.show();
      checkResponse(testName, response, false);
      await response.complete();
    } catch (error) {
      ok(false, `${testName} Unexpected error: ${e.name}`);
    }
    await handler.destruct();
  }

  // testing show with nonsupported methods
  async function testCannotMakePaymentShow() {
    const testName = "testCannotMakePaymentShow";
    await requestChromeAction("set-simple-ui-service", testName);

    const request = new PaymentRequest(nonSupportedMethods, defaultDetails);
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    let result = await request.canMakePayment();
    ok(!result, `${testName}: canMakePayment() should return false.`);
    try {
      await request.show();
      ok(false, `${testName}: should be rejected with 'NotSupportedError' but got resolved.`);
    } catch (error) {
      is(error.name, "NotSupportedError", `${testName}: should be rejected with 'NotSupportedError'.`);
    }
    await handler.destruct();
  }

  // testing show rejected by user
  async function testRejectShow() {
    const testName = "testRejectShow";
    await requestChromeAction("set-reject-ui-service", testName);

    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    try {
      await request.show();
      ok(false, `${testName}: Should be rejected with 'AbortError' but got resolved.`);
    } catch(error) {
      is(error.name, "AbortError", `${testName}: Should be rejected with 'AbortError'.`);
    }
    await handler.destruct();
  }

  // testing PaymentResponse.complete() with specified result
  async function testCompleteStatus(testName, result) {
    await requestChromeAction("set-simple-ui-service", testName);
    if (result) {
      await requestChromeAction(`set-complete-status-${result}`);
    } else {
      await requestChromeAction(`set-complete-status-unknown`);
    }

    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    try {
      let response = await request.show();
      await response.complete(result);
    } catch (error) {
      ok(false, `${testName}: Unexpected error ${error.name}.`);
    }
    await handler.destruct();
  }

  async function testCompleteFail() {
    const testName = "testCompleteFail";
    return testCompleteStatus(testName, "fail");
  }

  async function testCompleteSuccess() {
    const testName = "testCompleteSuccess";
    return testCompleteStatus(testName, "success");
  }

  async function testCompleteUnknown() {
    const testName = "testCompleteUnknown"
    return testCompleteStatus(testName, "unknown");
  }

  async function testCompleteEmpty() {
    const testName = "testCompleteEmpty";
    return testCompleteStatus(testName);
  }

  // testing PaymentRequestUpdateEvent.updateWith with specified details and error
  async function testUpdateWith(testName, detailsUpdate, expectedError) {
    if (expectedError) {
      await requestChromeAction("set-update-with-error-ui-service", testName);
    } else {
      await requestChromeAction("set-update-with-ui-service", testName);
    }

    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    request.addEventListener("shippingaddresschange", event => {
      event.updateWith(updateWithPromise(detailsUpdate));
    });
    request.addEventListener("shippingoptionchange", event => {
      event.updateWith(updateWithPromise(detailsUpdate));
    });
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    try {
      const response = await request.show();
      if (expectedError) {
        ok(false, `${testName}: Should be rejected with ${expectedError} but got resolved.`);
      } else {
        await response.complete("success");
      }
    } catch(error) {
      if (expectedError) {
        is(error.name, expectedError, `${testName}: Should be rejected with ${expectedError}.`);
      } else {
        ok(false, `${testName}: Unexpected error ${error.name}.`);
      }
    }
    await handler.destruct();
  }

  async function testUpdateWithReject() {
    const testName = "testUpdateWithReject";
    return testUpdateWith(testName, null, "AbortError");
  }

  async function testUpdateWithValidDetails() {
    const testName = "testUpdateWithValidDetails";
    return testUpdateWith(testName, updatedShippingOptionsDetails, null);
  }

  async function testUpdateWithInvalidDetails() {
    const testName = "testUpdateWithInvalidDetails";
    return testUpdateWith(testName, {total: "invalid details"}, "TypeError");
  }

  async function testUpdateWithError() {
    const testName = "testUpdateWithError";
    return testUpdateWith(testName, updatedErrorDetails, "AbortError");
  }

  // testing show with detailsUpdate promise
  async function testShowWithDetailsPromise(testName, detailsUpdate, expectedError) {
    if (expectedError) {
      await requestChromeAction("set-reject-ui-service", testName);
    } else {
      await requestChromeAction("set-simple-ui-service", testName);
    }

    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    ok(!request.shippingOption, `${testName}: request.shippingOption should be null.`);
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    try {
      let response = await request.show(updateWithPromise(detailsUpdate));
      if (expectedError) {
        ok(false, `${testName}: Should be rejected with ${expectedError} but got resolved.`);
      } else {
        ok(response.shippingOption,
           `${testName}: response.shippingOption should not be null.`);
      }
      await response.complete();
    } catch(error) {
      if (expectedError) {
        is(error.name, expectedError, `${testName}: Should be rejected with ${expectedError}.`);
      } else {
        ok(false, `${testName}: Unexpected error ${error.name}.`);
      }
    }
    await handler.destruct();
  }
  async function testShowWithValidPromise() {
    const testName = "testShowWithValidPromise";
    return testShowWithDetailsPromise(testName, updatedShippingOptionsDetails, null);
  }

  async function testShowWithRejectedPromise() {
    const testName = "testShowWithRejectedPromise";
    return testShowWithDetailsPromise(testName, null, "AbortError");
  }

  async function testShowWithInvalidPromise() {
    const testName = "testShowWithInvalidPromise";
    return testShowWithDetailsPromise(testName, {total: "invalid details"}, "TypeError");
  }

  async function testShowWithErrorPromise() {
    const testName = "testShowWithErrorPromise";
    return testShowWithDetailsPromise(testName, updatedErrorDetails, "AbortError");
  }

  async function testShowWithPromiseResolvedByRejectedPromise() {
    const testName = "testShowWithPromiseResolvedByRejectedPromise";
    await requestChromeAction("set-reject-ui-service", testName);

    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    let rejectPromise = Promise.reject(new TypeError());
    let detailsUpdatePromise = Promise.resolve(rejectPromise);
    try {
      await request.show(detailsUpdatePromise);
      ok(false, `${testName}: should be rejected with AbortError but got resolved.`);
    } catch(error) {
      is(error.name, "AbortError", `${testName}: should be rejected with AbortError.`);
    }
    await handler.destruct();
  }

  // testing show response initialization in chrome process
  async function testShowResponseInit() {
    const testName = "testShowResponseInit";
    await requestChromeAction("test-show-response-init", testName);
  }

  // testing show that is not triggered by user.
  async function testShowNotTriggeredByUser() {
    const testName = "testShowNotTriggeredByUser";
    await requestChromeAction("set-simple-ui-service", testName);

    const request = new PaymentRequest(defaultMethods, defaultDetails);
    try {
      await request.show();
      ok(false, `${testName}: should be rejected with SecurityError, but got resolved.`);
    } catch (error) {
      is(error.name, "SecurityError", `${testName}: should be rejected with SecurityError.`);
    }
  }

  // teardown function
  async function teardown() {
    gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() {
      gScript.removeMessageListener("teardown-complete", teardownCompleteHandler);
      gScript.removeMessageListener("test-fail", testFailHandler);
      gScript.removeMessageListener("test-pass", testPassHandler);
      gScript.destroy();
      SimpleTest.finish();
    });
    gScript.sendAsyncMessage("teardown");
  }

  // test main body
  async function runTests() {
    try {
      await testCannotMakePaymentShow();
      await testRejectShow();
      await testShowNormalFlow();
      await testCompleteSuccess();
      await testCompleteFail();
      await testCompleteUnknown();
      await testCompleteEmpty();
      await testUpdateWithReject();
      await testUpdateWithValidDetails();
      await testUpdateWithInvalidDetails();
      await testUpdateWithError();
      await testShowWithValidPromise();
      await testShowWithInvalidPromise();
      await testShowWithRejectedPromise();
      await testShowWithErrorPromise();
      await testShowWithPromiseResolvedByRejectedPromise();
      await testShowResponseInit();
      await testShowNotTriggeredByUser();
      await teardown();
    } catch (error) {
      ok(false, `test_showPayment: Unexpected error: ${error.name}`);
      SimpleTest.finish();
    }
  }

  window.addEventListener('load', function() {
    SpecialPowers.pushPrefEnv({
      'set': [
        ['dom.payments.request.enabled', true],
      ]
    }, runTests);
  });

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1345366">Mozilla Bug 1345366</a>
</body>
</html>