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
|
<!DOCTYPE html>
<!-- Copyright © 2017 World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<meta charset="utf-8">
<title>Payment Method Basic Card: Tests that basic card is a supported method</title>
<link rel="help" href="https://w3c.github.io/browser-payment-api/#show-method">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
const basicCard = Object.freeze({ supportedMethods: "basic-card", data: {} });
const defaultMethods = Object.freeze([basicCard]);
const defaultDetails = Object.freeze({
total: {
label: "Total",
amount: {
currency: "USD",
value: "1.00",
},
},
});
const notSupportedMethod = Object.freeze({
supportedMethods: "this-is-not-supported",
});
promise_test(async t => {
const request = new PaymentRequest([notSupportedMethod], defaultDetails);
assert_false(
await request.canMakePayment(),
`canMakePaymentPromise should be false`
);
}, `Must return false when the PMI is not supported at by the user agent.`);
function* pmiGenerator(howMany = 256) {
for (i = 0; i < howMany; i++) {
yield {
supportedMethods: `this-is-not-supported`,
data: { key: "value" },
};
}
}
promise_test(async t => {
//Smoke test
const canMakePaymentPromise = new PaymentRequest(
[notSupportedMethod],
defaultDetails
).canMakePayment();
assert_false(
await canMakePaymentPromise,
"Expected canMakePaymentPromise smoke test to resolve with false"
);
// Actual test - Make a big array with random PMIs,
// put basic-card in the middle of it!
const pmis = []
.concat(Array.from(pmiGenerator(250)))
.concat(basicCard)
.concat(Array.from(pmiGenerator(250)));
const request = new PaymentRequest(pmis, defaultDetails);
assert_true(
await request.canMakePayment(),
`canMakePaymentPromise should be true, because basic-card is present`
);
}, `Must return true when basic-card is amongst unsupported PMIs.`);
promise_test(async t => {
const request = new PaymentRequest(defaultMethods, defaultDetails);
try {
assert_true(
await request.canMakePayment(),
`canMakePaymentPromise must resolve to true`
);
assert_true(
await request.canMakePayment(),
`canMakePaymentPromise must resolve to true`
);
// try to trigger optional behavior
for (let i = 0; i < 1024; i++) {
const temp = new PaymentRequest(defaultMethods, defaultDetails);
await Promise.all([temp.canMakePayment(), request.canMakePayment()]);
}
} catch (err) {
assert_equals(
err.name,
"NotAllowedError",
"if it throws, then it must be a NotAllowedError."
);
}
}, `If basic-card is supported, then return a promise that resolves to true.`);
</script>
|