Files
tubestation/testing/web-platform/tests/payment-handler/app-simple.js
Rouslan Solomakhin ff2057cb82 Bug 1811342 [wpt PR 38069] - [Web Platform Test] Just-in-time install for paymentrequest event test, a=testonly
Automatic update from web-platform-tests
[Web Platform Test] Just-in-time install for paymentrequest event test

Before this patch, the payment-request-event-manual.https.html
web-platform-test (WPT) was using the PaymentInstruments API to install
a payment handler, attempted to access the `window` object from a
service worker context to determine the test server origin, and did not
specify `details` in the "paymentrequest" event response.

This was suboptimal because the PaymentInstruments API is planned to be
removed, service workers do not have access to the `window` object, and
the `details` field is required for the "paymentrequest" event response.

This patch switches the payment-request-event-manual.https.html to
install a payment handler just-in-time (JIT), removes the dependency on
the test server origin, and adds the `details` field to the
"paymentrequest" event response.

After this patch, the test is passing locally in full Chrome browser and
is not using any APIs that are planned to be removed.

Bug: 1327265
Change-Id: I525acb167d047505553eab6c030ff6a912cfcecf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4178408
Reviewed-by: Stephen McGruer <smcgruer@chromium.org>
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1094690}

--

wpt-commits: 505b5cbeb696c227fbab73807645f2422e9ad6cd
wpt-pr: 38069
2023-02-01 13:59:02 +00:00

85 lines
2.5 KiB
JavaScript

self.addEventListener('canmakepayment', event => {
event.respondWith(true);
});
self.addEventListener('paymentrequest', event => {
const expectedId = 'test-payment-request-identifier';
if (event.paymentRequestId !== expectedId) {
const msg = `Expected payment request identifier "${expectedId}", but got "${
event.paymentRequestId
}"`;
event.respondWith(Promise.reject(new Error(msg)));
return;
}
if (event.methodData.length !== 1) {
const msg = `Expected one method data, but got ${
event.methodData.length
} instead`;
event.respondWith(Promise.reject(new Error(msg)));
return;
}
const methodData = event.methodData[0];
const expectedMethodNamePrefix = 'http';
if (!methodData.supportedMethods.startsWith(expectedMethodNamePrefix)) {
const msg = `Expected payment method name "${methodData.supportedMethods}" to start with ${expectedMethodNamePrefix}"`;
event.respondWith(Promise.reject(new Error(msg)));
return;
}
const expectedMethodNameSuffix = '/payment-handler/payment-request-event-manual-manifest.json';
if (!methodData.supportedMethods.endsWith(expectedMethodNameSuffix)) {
const msg = `Expected payment method name "${methodData.supportedMethods}" to end with ${expectedMethodNameSuffix}"`;
event.respondWith(Promise.reject(new Error(msg)));
return;
}
if (methodData.data.supportedNetworks) {
const msg =
'Expected no supported networks in payment method specific data';
event.respondWith(Promise.reject(new Error(msg)));
return;
}
if (methodData.displayItems) {
const msg = 'Expected no display items';
event.respondWith(Promise.reject(new Error(msg)));
return;
}
const total = event.total;
if (!total) {
const msg = 'Expected total';
event.respondWith(Promise.reject(new Error(msg)));
return;
}
if (total.label) {
const msg = 'Expected no total label';
event.respondWith(Promise.reject(new Error(msg)));
return;
}
const expectedCurrency = 'USD';
if (total.currency !== expectedCurrency) {
const msg = `Expected currency "${expectedCurrency}", but got "${
total.currency
}"`;
event.respondWith(Promise.reject(new Error(msg)));
return;
}
const expectedValue = '0.01';
if (total.value !== expectedValue) {
const msg = `Expected value "${expectedValue}", but got "${total.value}"`;
event.respondWith(Promise.reject(new Error(msg)));
return;
}
event.respondWith({
methodName: methodData.supportedMethods,
details: {status: 'success'},
});
});