| <!doctype html> | |
| <meta charset="utf-8"> | |
| <title>Tests for CanMakePaymentEvent</title> | |
| <link rel="help" href="https://w3c.github.io/web-based-payment-handler/#the-canmakepaymentevent"> | |
| <script src="/resources/testharness.js"></script> | |
| <script src="/resources/testharnessreport.js"></script> | |
| <script src="/resources/testdriver.js"></script> | |
| <script src="/resources/testdriver-vendor.js"></script> | |
| <script> | |
| const DEFAULT_METHOD_NAME = window.location.origin + '/web-based-payment-handler/' | |
| + 'can-make-payment-event-manifest.json'; | |
| async function registerApp(responseType = 'canMakePayment-true') { | |
| const request = new PaymentRequest( | |
| [{supportedMethods: DEFAULT_METHOD_NAME, data: {responseType}}], | |
| {total: {label: 'Total', amount: {currency: 'USD', value: '0.01'}}}); | |
| const response = await test_driver.bless('installing a payment app', () => | |
| request.show() | |
| ); | |
| return response.complete('success'); | |
| } | |
| function buildPaymentRequest(methodName = DEFAULT_METHOD_NAME) { | |
| const unsupportedMethodName = methodName + '/unsupported'; | |
| return new PaymentRequest( | |
| [ | |
| { | |
| supportedMethods: methodName, | |
| data: { | |
| defaultParameter: 'defaultValue', | |
| }, | |
| }, | |
| { | |
| supportedMethods: unsupportedMethodName, | |
| data: { | |
| defaultUnsupportedParameter: 'defaultUnsupportedValue', | |
| }, | |
| }, | |
| ], | |
| { | |
| total: { | |
| label: 'Total', | |
| amount: { | |
| currency: 'USD', | |
| value: '0', | |
| }, | |
| }, | |
| displayItems: [ | |
| { | |
| label: 'Nada', | |
| amount: {currency: 'USD', value: '0'}, | |
| }, | |
| ], | |
| modifiers: [ | |
| { | |
| supportedMethods: [methodName], | |
| data: { | |
| modifiedParameter: 'modifiedValue', | |
| }, | |
| total: { | |
| label: 'Modified Total', | |
| amount: { | |
| currency: 'USD', | |
| value: '0.0001', | |
| }, | |
| }, | |
| additionalDisplayItems: [ | |
| { | |
| label: 'Something', | |
| amount: {currency: 'USD', value: '0.0001'}, | |
| }, | |
| ], | |
| }, | |
| { | |
| supportedMethods: [unsupportedMethodName], | |
| data: { | |
| modifiedUnsupportedParameter: 'modifiedUnsupportedValue', | |
| }, | |
| total: { | |
| label: 'Modified Unsupported Total', | |
| amount: { | |
| currency: 'USD', | |
| value: '10', | |
| }, | |
| }, | |
| additionalDisplayItems: [ | |
| { | |
| label: 'Something Unsupported', | |
| amount: {currency: 'USD', value: '10'}, | |
| }, | |
| ], | |
| }, | |
| ], | |
| }, | |
| ); | |
| } | |
| promise_test(async t => { | |
| // Intentionally do not install the payment app. | |
| const request = buildPaymentRequest(); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_true( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return true.', | |
| ); | |
| await test_driver.bless('installing a payment app just-in-time'); | |
| const response = await request.show(); | |
| assert_equals('success', response.details.status); | |
| return response.complete('success'); | |
| }, 'If a payment handler is not installed, but can be installed just-in-time, then the payment method is supported.'); | |
| promise_test(async t => { | |
| // Intentionally do not install the payment app. | |
| const request = buildPaymentRequest(DEFAULT_METHOD_NAME + '/non-existent'); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_false( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return false.', | |
| ); | |
| const response = await test_driver.bless('invoking a payemnt app'); | |
| await promise_rejects_dom(t, 'NotSupportedError', request.show()); | |
| }, 'If a payment handler is not installed and cannot be installed just-in-time, then the payment method is not supported.'); | |
| promise_test(async t => { | |
| await registerApp('canMakePayment-false'); | |
| const request = buildPaymentRequest(); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_true( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return true.', | |
| ); | |
| await test_driver.bless('invoking a payment app'); | |
| const response = await request.show(); | |
| assert_equals('success', response.details.status); | |
| return response.complete('success'); | |
| }, 'If CanMakePaymentEvent.respondWith(false) is called, then canMakePayment() still returns true and the app can still be invoked.'); | |
| promise_test(async t => { | |
| await registerApp('canMakePayment-promise-false'); | |
| const request = buildPaymentRequest(); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_true( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return true.', | |
| ); | |
| await test_driver.bless('invoking a payment app'); | |
| const response = await request.show(); | |
| assert_equals('success', response.details.status); | |
| return response.complete('success'); | |
| }, 'If CanMakePaymentEvent.respondWith(Promise.resolve(false)) is called, then canMakePayment() still returns true and the app can still be invoked.'); | |
| promise_test(async t => { | |
| await registerApp('canMakePayment-true'); | |
| const request = buildPaymentRequest(); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_true( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return true.', | |
| ); | |
| await test_driver.bless('invoking a payment app'); | |
| const response = await request.show(); | |
| assert_equals('success', response.details.status); | |
| return response.complete('success'); | |
| }, 'If CanMakePaymentEvent.respondWith(true) is called, then canMakePayment() returns true and the app can be invoked.'); | |
| promise_test(async t => { | |
| await registerApp('canMakePayment-promise-true'); | |
| const request = buildPaymentRequest(); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_true( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return true.', | |
| ); | |
| await test_driver.bless('invoking a payment app'); | |
| const response = await request.show(); | |
| assert_equals('success', response.details.status); | |
| return response.complete('success'); | |
| }, 'If CanMakePaymentEvent.respondWith(Promise.resolve(true)) is called, then canMakePayment() returns true and the app can be invoked.'); | |
| promise_test(async t => { | |
| await registerApp('canMakePayment-custom-error'); | |
| const request = buildPaymentRequest(); | |
| assert_not_equals(request, undefined); | |
| let paymentRequestCanMakePaymentResult; | |
| try { | |
| paymentRequestCanMakePaymentResult = await request.canMakePayment(); | |
| } catch (err) { | |
| assert_equals( | |
| err.name, | |
| 'NotAllowedError', | |
| 'If it throws, then it must be NotAllowedError', | |
| ); | |
| } | |
| assert_true( | |
| paymentRequestCanMakePaymentResult, | |
| 'canMakePayment() must return true.', | |
| ); | |
| await test_driver.bless('invoking a payment app'); | |
| const response = await request.show(); | |
| assert_equals('success', response.details.status); | |
| return response.complete('success'); | |
| }, 'If CanMakePaymentEvent.respondWith(Promise.reject(error)) is called, then canMakePayment() still returns true and the app can still be invoked.'); | |
| </script> |