0

Convert PaymentRequestTestWebPageContents to builder-like pattern

Before this patch, the interface for PaymentRequestTestWebPageContents
was difficult to extend, because the test page parameters were being
passed into its constructor and build() method. Typical usage for this
class was:
  new PaymentRequestTestWebPageContents(methodOne, methodTwo)
          .build(/* multiplePaymentMethods= */ true);

This patch removes parameters from the PaymentRequestTestWebPageContents
class constructor and build() method and, instead, adds a builder-like
method addMethod(), which can be called multiple times in a row, before
building the test checkout web page.

After this patch, the PaymentRequestTestWebPageContents class is easier
to extend with new functionality, such as shipping address or contact
information support. The new usage for this class is:
  new PaymentRequestTestWebPageContents().addMethod(methodOne)
                                         .addMethod(methodTwo)
                                         .build();

Fixed: 401515769
Change-Id: If68063c1bded1508001fd8b39c5c9fe97bbcb71b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6335797
Reviewed-by: Slobodan Pejic <slobodan@chromium.org>
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1430980}
This commit is contained in:
Rouslan Solomakhin
2025-03-11 10:26:44 -07:00
committed by Chromium LUCI CQ
parent 7387b18bdc
commit 5d6db44d95
2 changed files with 73 additions and 55 deletions
android_webview/javatests/src/org/chromium/android_webview/test/payments
components/payments/content/android/javatests/src/org/chromium/components/payments

@ -69,9 +69,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
mAwContents, "resultListener", new String[] {"*"}, mWebMessageListener);
mMerchantServer = TestWebServer.start();
mPageContents =
new PaymentRequestTestWebPageContents(
PAYMENT_METHOD_NAME, OTHER_PAYMENT_METHOD_NAME);
mPageContents = new PaymentRequestTestWebPageContents();
}
@After
@ -88,7 +86,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@SmallTest
@DisableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestIsNotDefined() throws Exception {
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(
mAwContents.getWebContents(), "checkPaymentRequestDefined");
@ -106,7 +104,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@SmallTest
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestIsDefined() throws Exception {
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(
mAwContents.getWebContents(), "checkPaymentRequestDefined");
@ -124,7 +122,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@SmallTest
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestCannotMakePaymentWithoutApps() throws Exception {
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
@ -141,7 +139,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@SmallTest
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestHasNoEnrolledInstrumentsWithoutApps() throws Exception {
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(
mAwContents.getWebContents(), "checkHasEnrolledInstrument");
@ -159,7 +157,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@SmallTest
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestCannotLaunchAppsWithoutApps() throws Exception {
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
@ -175,7 +173,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestCanMakePayments() throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
@ -192,7 +190,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestHasEnrolledInstrument() throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(
mAwContents.getWebContents(), "checkHasEnrolledInstrument");
@ -208,7 +206,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testPaymentRequestLaunchPaymentApp() throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
@ -230,7 +228,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
public void testPaymentRequestCanMakePaymentsWhenMerchantSupportsMultiplePaymentMethods()
throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
loadMerchantCheckoutPage(
mPageContents
.addMethod(PAYMENT_METHOD_NAME)
.addMethod(OTHER_PAYMENT_METHOD_NAME)
.build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
@ -251,7 +253,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
public void testPaymentRequestHasEnrolledInstrumentWhenMerchantSupportsMultiplePaymentMethods()
throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
loadMerchantCheckoutPage(
mPageContents
.addMethod(PAYMENT_METHOD_NAME)
.addMethod(OTHER_PAYMENT_METHOD_NAME)
.build());
JSUtils.clickNodeWithUserGesture(
mAwContents.getWebContents(), "checkHasEnrolledInstrument");
@ -272,7 +278,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
public void testPaymentRequestLaunchPaymentAppWhenMerchantSupportsMultiplePaymentMethods()
throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
loadMerchantCheckoutPage(
mPageContents
.addMethod(PAYMENT_METHOD_NAME)
.addMethod(OTHER_PAYMENT_METHOD_NAME)
.build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
@ -297,7 +307,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
.addApp(createPaymentApp())
.addApp(createOtherPaymentApp())
.install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
loadMerchantCheckoutPage(
mPageContents
.addMethod(PAYMENT_METHOD_NAME)
.addMethod(OTHER_PAYMENT_METHOD_NAME)
.build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
@ -321,7 +335,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
.addApp(createPaymentApp())
.addApp(createOtherPaymentApp())
.install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
loadMerchantCheckoutPage(
mPageContents
.addMethod(PAYMENT_METHOD_NAME)
.addMethod(OTHER_PAYMENT_METHOD_NAME)
.build());
JSUtils.clickNodeWithUserGesture(
mAwContents.getWebContents(), "checkHasEnrolledInstrument");
@ -345,7 +363,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
.addApp(createPaymentApp())
.addApp(createOtherPaymentApp())
.install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
loadMerchantCheckoutPage(
mPageContents
.addMethod(PAYMENT_METHOD_NAME)
.addMethod(OTHER_PAYMENT_METHOD_NAME)
.build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
@ -360,7 +382,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
public void testCannotRetry() throws Exception {
mMockPaymentAppInstaller.addApp(createPaymentApp()).install();
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
loadMerchantCheckoutPage(mPageContents.addMethod(PAYMENT_METHOD_NAME).build());
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "retryPayment");
@ -372,15 +394,11 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
/**
* Loads a test web-page for exercising the PaymentRequest API.
*
* @param multiplePaymentMethods Whether multiple payment methods should be requested in the
* PaymentRequest API call.
* @param webPageContents The contents of the test web page to load.
*/
private void loadMerchantCheckoutPage(boolean multiplePaymentMethods) throws Exception {
private void loadMerchantCheckoutPage(String contents) throws Exception {
String merchantCheckoutPageUrl =
mMerchantServer.setResponse(
"/checkout",
mPageContents.build(multiplePaymentMethods),
/* responseHeaders= */ null);
mMerchantServer.setResponse("/checkout", contents, /* responseHeaders= */ null);
mActivityTestRule.loadUrlAsync(mAwContents, merchantCheckoutPageUrl);
Data messageFromPage = mWebMessageListener.waitForOnPostMessage();
Assert.assertEquals("Page loaded.", messageFromPage.getAsString());

@ -4,43 +4,51 @@
package org.chromium.components.payments;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* The contents of a merchant checkout page for testing the PaymentRequest API.
* The contents of a merchant checkout page for testing PaymentRequest API.
*
* <p>Sample usage:
*
* <pre>
* loadUrlAsync(
* TestWebServer.start().setResponse(
* "/checkout",
* new PaymentRequestTestWebPageContents("https://test-1.example/pay",
* "https://test-2.example/pay").build(true)));
* loadUrl(TestWebServer.start().setResponse(
* "/checkout",
* new PaymentRequestTestWebPageContents().addMethod("https://test-1.example/pay")
* .addMethod("https://test-2.example/pay")
* .build()));
* </pre>
*/
public class PaymentRequestTestWebPageContents {
private final String mPaymentMethodName;
private final String mOtherPaymentMethodName;
private final List<String> mMethods = new ArrayList<>();
/**
* Constructs an instance of test checkout page contents.
* Adds a payment method to the list of payment methods that will be requested in the
* PaymentRequest API on the test checkout page.
*
* @param paymentMethodName The payment method name to use in PaymentRequest API.
* @param otherPaymentMethodName An additional payment method name to use in PaymentRequest API,
* if need to request multiple payment methods.
* @param method The payment method to request in PaymentRequest API. Must be a URL, e.g.,
* "https://payments.example/web-pay". The string should not contain any apostrophes,
* because it is encoded into JavaScript via String.format().
* @return A reference to this {@link PaymentRequestTestWebPageContents} instance.
*/
public PaymentRequestTestWebPageContents(
String paymentMethodName, String otherPaymentMethodName) {
mPaymentMethodName = paymentMethodName;
mOtherPaymentMethodName = otherPaymentMethodName;
public PaymentRequestTestWebPageContents addMethod(String method) {
assert !method.contains("'") : "Payment method name should not contain any apostrophes.";
mMethods.add(method);
return this;
}
/**
* Builds the test web page contents for exercising the PaymentRequest API.
* Builds the test web page contents for exercising PaymentRequest API.
*
* @param multiplePaymentMethods Whether multiple payment methods should be requested in the
* PaymentRequest API call.
* @return The web page contents.
*/
public String build(boolean multiplePaymentMethods) {
public String build() {
String supportedMethods =
mMethods.stream()
.map(method -> String.format("{supportedMethods: '%s'}", method))
.collect(Collectors.joining(", "));
String checkoutPageHtmlFormat =
"""
<!doctype html>
@ -52,13 +60,8 @@ public class PaymentRequestTestWebPageContents {
<script>
function createPaymentRequest() {
const firstMethod = '%s';
const secondMethod = '%s';
const total = {label: 'Total', amount: {value: '0.01', currency: 'USD'}};
return secondMethod
? new PaymentRequest([{supportedMethods: firstMethod},
{supportedMethods: secondMethod}], {total})
: new PaymentRequest([{supportedMethods: firstMethod}], {total});
return new PaymentRequest([%s], {total});
}
function checkPaymentRequestDefined() {
@ -133,9 +136,6 @@ public class PaymentRequestTestWebPageContents {
</script>
""";
return String.format(
checkoutPageHtmlFormat,
mPaymentMethodName,
multiplePaymentMethods ? mOtherPaymentMethodName : "");
return String.format(checkoutPageHtmlFormat, supportedMethods);
}
}