can-make-payment-event.https.html (8628B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title>Tests for CanMakePaymentEvent</title> 4 <link rel="help" href="https://w3c.github.io/payment-handler/#the-canmakepaymentevent"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/resources/testdriver.js"></script> 8 <script src="/resources/testdriver-vendor.js"></script> 9 <script> 10 const DEFAULT_METHOD_NAME = window.location.origin + '/payment-handler/' 11 + 'can-make-payment-event-manifest.json'; 12 13 async function registerApp(responseType = 'canMakePayment-true') { 14 const request = new PaymentRequest( 15 [{supportedMethods: DEFAULT_METHOD_NAME, data: {responseType}}], 16 {total: {label: 'Total', amount: {currency: 'USD', value: '0.01'}}}); 17 const response = await test_driver.bless('installing a payment app', () => 18 request.show() 19 ); 20 return response.complete('success'); 21 } 22 23 function buildPaymentRequest(methodName = DEFAULT_METHOD_NAME) { 24 const unsupportedMethodName = methodName + '/unsupported'; 25 return new PaymentRequest( 26 [ 27 { 28 supportedMethods: methodName, 29 data: { 30 defaultParameter: 'defaultValue', 31 }, 32 }, 33 { 34 supportedMethods: unsupportedMethodName, 35 data: { 36 defaultUnsupportedParameter: 'defaultUnsupportedValue', 37 }, 38 }, 39 ], 40 { 41 total: { 42 label: 'Total', 43 amount: { 44 currency: 'USD', 45 value: '0', 46 }, 47 }, 48 displayItems: [ 49 { 50 label: 'Nada', 51 amount: {currency: 'USD', value: '0'}, 52 }, 53 ], 54 modifiers: [ 55 { 56 supportedMethods: [methodName], 57 data: { 58 modifiedParameter: 'modifiedValue', 59 }, 60 total: { 61 label: 'Modified Total', 62 amount: { 63 currency: 'USD', 64 value: '0.0001', 65 }, 66 }, 67 additionalDisplayItems: [ 68 { 69 label: 'Something', 70 amount: {currency: 'USD', value: '0.0001'}, 71 }, 72 ], 73 }, 74 { 75 supportedMethods: [unsupportedMethodName], 76 data: { 77 modifiedUnsupportedParameter: 'modifiedUnsupportedValue', 78 }, 79 total: { 80 label: 'Modified Unsupported Total', 81 amount: { 82 currency: 'USD', 83 value: '10', 84 }, 85 }, 86 additionalDisplayItems: [ 87 { 88 label: 'Something Unsupported', 89 amount: {currency: 'USD', value: '10'}, 90 }, 91 ], 92 }, 93 ], 94 }, 95 ); 96 } 97 98 promise_test(async t => { 99 // Intentionally do not install the payment app. 100 const request = buildPaymentRequest(); 101 assert_not_equals(request, undefined); 102 let paymentRequestCanMakePaymentResult; 103 try { 104 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 105 } catch (err) { 106 assert_equals( 107 err.name, 108 'NotAllowedError', 109 'If it throws, then it must be NotAllowedError', 110 ); 111 } 112 assert_true( 113 paymentRequestCanMakePaymentResult, 114 'canMakePayment() must return true.', 115 ); 116 117 await test_driver.bless('installing a payment app just-in-time'); 118 const response = await request.show(); 119 assert_equals('success', response.details.status); 120 return response.complete('success'); 121 }, 'If a payment handler is not installed, but can be installed just-in-time, then the payment method is supported.'); 122 123 promise_test(async t => { 124 // Intentionally do not install the payment app. 125 const request = buildPaymentRequest(DEFAULT_METHOD_NAME + '/non-existent'); 126 assert_not_equals(request, undefined); 127 let paymentRequestCanMakePaymentResult; 128 try { 129 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 130 } catch (err) { 131 assert_equals( 132 err.name, 133 'NotAllowedError', 134 'If it throws, then it must be NotAllowedError', 135 ); 136 } 137 assert_false( 138 paymentRequestCanMakePaymentResult, 139 'canMakePayment() must return false.', 140 ); 141 142 const response = await test_driver.bless('invoking a payemnt app'); 143 await promise_rejects_dom(t, 'NotSupportedError', request.show()); 144 }, 'If a payment handler is not installed and cannot be installed just-in-time, then the payment method is not supported.'); 145 146 promise_test(async t => { 147 await registerApp('canMakePayment-false'); 148 const request = buildPaymentRequest(); 149 assert_not_equals(request, undefined); 150 let paymentRequestCanMakePaymentResult; 151 try { 152 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 153 } catch (err) { 154 assert_equals( 155 err.name, 156 'NotAllowedError', 157 'If it throws, then it must be NotAllowedError', 158 ); 159 } 160 assert_true( 161 paymentRequestCanMakePaymentResult, 162 'canMakePayment() must return true.', 163 ); 164 165 await test_driver.bless('invoking a payment app'); 166 const response = await request.show(); 167 assert_equals('success', response.details.status); 168 return response.complete('success'); 169 }, 'If CanMakePaymentEvent.respondWith(false) is called, then canMakePayment() still returns true and the app can still be invoked.'); 170 171 promise_test(async t => { 172 await registerApp('canMakePayment-promise-false'); 173 const request = buildPaymentRequest(); 174 assert_not_equals(request, undefined); 175 let paymentRequestCanMakePaymentResult; 176 try { 177 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 178 } catch (err) { 179 assert_equals( 180 err.name, 181 'NotAllowedError', 182 'If it throws, then it must be NotAllowedError', 183 ); 184 } 185 assert_true( 186 paymentRequestCanMakePaymentResult, 187 'canMakePayment() must return true.', 188 ); 189 190 await test_driver.bless('invoking a payment app'); 191 const response = await request.show(); 192 assert_equals('success', response.details.status); 193 return response.complete('success'); 194 }, 'If CanMakePaymentEvent.respondWith(Promise.resolve(false)) is called, then canMakePayment() still returns true and the app can still be invoked.'); 195 196 promise_test(async t => { 197 await registerApp('canMakePayment-true'); 198 const request = buildPaymentRequest(); 199 assert_not_equals(request, undefined); 200 let paymentRequestCanMakePaymentResult; 201 try { 202 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 203 } catch (err) { 204 assert_equals( 205 err.name, 206 'NotAllowedError', 207 'If it throws, then it must be NotAllowedError', 208 ); 209 } 210 assert_true( 211 paymentRequestCanMakePaymentResult, 212 'canMakePayment() must return true.', 213 ); 214 215 await test_driver.bless('invoking a payment app'); 216 const response = await request.show(); 217 assert_equals('success', response.details.status); 218 return response.complete('success'); 219 }, 'If CanMakePaymentEvent.respondWith(true) is called, then canMakePayment() returns true and the app can be invoked.'); 220 221 promise_test(async t => { 222 await registerApp('canMakePayment-promise-true'); 223 const request = buildPaymentRequest(); 224 assert_not_equals(request, undefined); 225 let paymentRequestCanMakePaymentResult; 226 try { 227 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 228 } catch (err) { 229 assert_equals( 230 err.name, 231 'NotAllowedError', 232 'If it throws, then it must be NotAllowedError', 233 ); 234 } 235 assert_true( 236 paymentRequestCanMakePaymentResult, 237 'canMakePayment() must return true.', 238 ); 239 240 await test_driver.bless('invoking a payment app'); 241 const response = await request.show(); 242 assert_equals('success', response.details.status); 243 return response.complete('success'); 244 }, 'If CanMakePaymentEvent.respondWith(Promise.resolve(true)) is called, then canMakePayment() returns true and the app can be invoked.'); 245 246 promise_test(async t => { 247 await registerApp('canMakePayment-custom-error'); 248 const request = buildPaymentRequest(); 249 assert_not_equals(request, undefined); 250 let paymentRequestCanMakePaymentResult; 251 try { 252 paymentRequestCanMakePaymentResult = await request.canMakePayment(); 253 } catch (err) { 254 assert_equals( 255 err.name, 256 'NotAllowedError', 257 'If it throws, then it must be NotAllowedError', 258 ); 259 } 260 assert_true( 261 paymentRequestCanMakePaymentResult, 262 'canMakePayment() must return true.', 263 ); 264 265 await test_driver.bless('invoking a payment app'); 266 const response = await request.show(); 267 assert_equals('success', response.details.status); 268 return response.complete('success'); 269 }, 'If CanMakePaymentEvent.respondWith(Promise.reject(error)) is called, then canMakePayment() still returns true and the app can still be invoked.'); 270 </script>