diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/payment-handler/can-make-payment-event.https.html | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/payment-handler/can-make-payment-event.https.html')
-rw-r--r-- | testing/web-platform/tests/payment-handler/can-make-payment-event.https.html | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/testing/web-platform/tests/payment-handler/can-make-payment-event.https.html b/testing/web-platform/tests/payment-handler/can-make-payment-event.https.html new file mode 100644 index 0000000000..941c206e3b --- /dev/null +++ b/testing/web-platform/tests/payment-handler/can-make-payment-event.https.html @@ -0,0 +1,264 @@ +<!doctype html> +<meta charset="utf-8"> +<title>Tests for CanMakePaymentEvent</title> +<link rel="help" href="https://w3c.github.io/payment-handler/#the-canmakepaymentevent"> +<link rel="manifest" href="manifest.json"> +<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 instrumentKey = 'instrument-key'; + +async function registerApp(methodName) { + await navigator.serviceWorker.register('app-can-make-payment.js'); + const registration = await navigator.serviceWorker.ready; + if (!registration.paymentManager) { + return; + } + if (registration.paymentManager.requestPermission) { + const permission = await registration.paymentManager.requestPermission(); + if (permission !== 'granted') { + return; + } + } + await registration.paymentManager.instruments.set(instrumentKey, { + name: 'Test Payment Method', + method: methodName, + }); + return registration; +} + +function buildPaymentRequest(methodName) { + 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 => { + const methodName = window.location.origin; + // Intentionally do not install the payment app. + const request = buildPaymentRequest(methodName); + 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.', + ); + + await test_driver.bless('PaymentRequest.show() requires user activation'); + await promise_rejects_dom(t, 'NotSupportedError', request.show()); +}, 'If a payment handler is not installed, then the payment method is not supported.'); + +promise_test(async t => { + const methodName = window.location.origin; + await registerApp(methodName); + navigator.serviceWorker.controller.postMessage( + {responseType: 'canMakePayment-false'}); + const request = buildPaymentRequest(methodName); + 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.', + ); + + await test_driver.bless('PaymentRequest.show() requires user activation'); + await promise_rejects_dom(t, 'NotSupportedError', request.show()); +}, 'If CanMakePaymentEvent.respondWith(false) is called, then the payment method is not supported.'); + +promise_test(async t => { + const methodName = window.location.origin; + await registerApp(methodName); + navigator.serviceWorker.controller.postMessage( + {responseType: 'canMakePayment-promise-false'}); + const request = buildPaymentRequest(methodName); + 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.', + ); + + await test_driver.bless('PaymentRequest.show() requires user activation'); + await promise_rejects_dom(t, 'NotSupportedError', request.show()); +}, 'If CanMakePaymentEvent.respondWith(Promise.resolve(false)) is called, then the payment method is not supported.'); + +promise_test(async t => { + const methodName = window.location.origin; + await registerApp(methodName); + navigator.serviceWorker.controller.postMessage( + {responseType: 'canMakePayment-true'}); + const request = buildPaymentRequest(methodName); + 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('PaymentRequest.show() requires user activation'); + const acceptPromise = request.show(); + await request.abort(); + await promise_rejects_dom(t, 'AbortError', acceptPromise); +}, 'If CanMakePaymentEvent.respondWith(true) is called, then the payment method is supported.'); + +promise_test(async t => { + const methodName = window.location.origin; + await registerApp(methodName); + navigator.serviceWorker.controller.postMessage( + {responseType: 'canMakePayment-promise-true'}); + const request = buildPaymentRequest(methodName); + 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('PaymentRequest.show() requires user activation'); + const acceptPromise = request.show(); + await request.abort(); + await promise_rejects_dom(t, 'AbortError', acceptPromise); +}, 'If CanMakePaymentEvent.respondWith(Promise.resolve(true)) is called, then the payment method is supported.'); + +promise_test(async t => { + const methodName = window.location.origin; + await registerApp(methodName); + navigator.serviceWorker.controller.postMessage( + {responseType: 'canMakePayment-custom-error'}); + const request = buildPaymentRequest(methodName); + 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.', + ); + + await test_driver.bless('PaymentRequest.show() requires user activation'); + await promise_rejects_dom(t, 'NotSupportedError', request.show()); +}, 'If CanMakePaymentEvent.respondWith(Promise.reject(error)) is called, then the payment method is not supported.'); +</script> |