Move the installPaymentApps() method to its own file
The AwPaymentRequestServiceTest.java file has become large and monolithic, which makes changing and reviewing it more difficult. This patch moves the AwPaymentRequestServiceTest.installPaymentApps() method to its own file MockPaymentAppInstaller.java in the "test_support_java" build target. After this patch, the AwPaymentRequestServiceTest.java is simpler and the other test files can reuse the new MockPaymentAppInstaller class, instead of creating their own. This patch is only a move, or an extraction. The current suboptimal API shape will be improved in a follow up patch, as tracked in https://crbug.com/401515259. Fixed: 401514098 Bug: 401515259 Change-Id: Ie0182fe40d94393c6c959b1fff3ea3e770b3eb18 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6333973 Reviewed-by: Stephen McGruer <smcgruer@chromium.org> Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org> Cr-Commit-Position: refs/heads/main@{#1429643}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
c1e98b146f
commit
4844c87a91
android_webview/javatests/src/org/chromium/android_webview/test/payments
components/payments/content/android
@ -28,10 +28,7 @@ import org.chromium.android_webview.test.util.JSUtils;
|
||||
import org.chromium.base.test.util.Batch;
|
||||
import org.chromium.base.test.util.Features.DisableFeatures;
|
||||
import org.chromium.base.test.util.Features.EnableFeatures;
|
||||
import org.chromium.components.payments.AndroidPaymentAppFinder;
|
||||
import org.chromium.components.payments.MockAndroidIntentLauncher;
|
||||
import org.chromium.components.payments.MockPackageManagerDelegate;
|
||||
import org.chromium.components.payments.MockPaymentManifestDownloader;
|
||||
import org.chromium.components.payments.MockPaymentAppInstaller;
|
||||
import org.chromium.content_public.common.ContentFeatures;
|
||||
import org.chromium.net.test.util.TestWebServer;
|
||||
|
||||
@ -45,6 +42,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
"https://other-payments.example/web-pay";
|
||||
|
||||
@Rule public AwActivityTestRule mActivityTestRule;
|
||||
private MockPaymentAppInstaller mMockPaymentAppInstaller;
|
||||
private AwTestContainerView mTestContainerView;
|
||||
private AwContents mAwContents;
|
||||
private TestWebMessageListener mWebMessageListener;
|
||||
@ -56,6 +54,9 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mMockPaymentAppInstaller =
|
||||
new MockPaymentAppInstaller(PAYMENT_METHOD_NAME, OTHER_PAYMENT_METHOD_NAME);
|
||||
|
||||
mTestContainerView =
|
||||
mActivityTestRule.createAwTestContainerViewOnMainSync(new TestAwContentsClient());
|
||||
mAwContents = mTestContainerView.getAwContents();
|
||||
@ -70,8 +71,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
AndroidPaymentAppFinder.setPackageManagerDelegateForTest(null);
|
||||
AndroidPaymentAppFinder.setDownloaderForTest(null);
|
||||
mMockPaymentAppInstaller.reset();
|
||||
mMerchantServer.close();
|
||||
}
|
||||
|
||||
@ -169,7 +169,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@SmallTest
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestCanMakePayments() throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
|
||||
@ -186,7 +186,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@SmallTest
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestHasEnrolledInstrument() throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(
|
||||
@ -202,7 +202,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@SmallTest
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestLaunchPaymentApp() throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
|
||||
@ -224,7 +224,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestCanMakePaymentsWhenMerchantSupportsMultiplePaymentMethods()
|
||||
throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
|
||||
@ -245,7 +245,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestHasEnrolledInstrumentWhenMerchantSupportsMultiplePaymentMethods()
|
||||
throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(
|
||||
@ -266,7 +266,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestLaunchPaymentAppWhenMerchantSupportsMultiplePaymentMethods()
|
||||
throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
|
||||
@ -288,7 +288,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@SmallTest
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestCannotMakePaymentsWithMoreThaOneAppAtOnce() throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ true);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ true);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "checkCanMakePayment");
|
||||
@ -309,7 +309,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestHasNoEnrolledInstrumentWithMoreThaOneAppAtOnce()
|
||||
throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ true);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ true);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(
|
||||
@ -330,7 +330,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testPaymentRequestCannotLaunchPaymentAppWithMoreThanOneAppAtOnce()
|
||||
throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ true);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ true);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ true);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "launchPaymentApp");
|
||||
@ -345,7 +345,7 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
@SmallTest
|
||||
@EnableFeatures(ContentFeatures.WEB_PAYMENTS)
|
||||
public void testCannotRetry() throws Exception {
|
||||
installPaymentApps(/* multipleApps= */ false);
|
||||
mMockPaymentAppInstaller.installPaymentApps(/* multipleApps= */ false);
|
||||
loadMerchantCheckoutPage(/* multiplePaymentMethods= */ false);
|
||||
|
||||
JSUtils.clickNodeWithUserGesture(mAwContents.getWebContents(), "retryPayment");
|
||||
@ -466,38 +466,4 @@ public class AwPaymentRequestServiceTest extends AwParameterizedTest {
|
||||
Data messageFromPage = mWebMessageListener.waitForOnPostMessage();
|
||||
Assert.assertEquals("Page loaded.", messageFromPage.getAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects a fake Android payment app into the package manager delegate, with the correct
|
||||
* signature being returned from the downloader. Also turns off connecting to the
|
||||
* IS_READY_TO_PAY service or sending the PAY intent to this app.
|
||||
*
|
||||
* @param multipleApps Whether multiple apps should be installed.
|
||||
*/
|
||||
private void installPaymentApps(boolean multipleApps) {
|
||||
MockPackageManagerDelegate packageManagerDelegate = new MockPackageManagerDelegate();
|
||||
// The SHA256 of the string "AABBCCDDEEFF001122334455" equals to the fingerprints[0].value
|
||||
// in the "downloaded" manifest file.
|
||||
packageManagerDelegate.installPaymentApp(
|
||||
"Test Payment App",
|
||||
"test.payments.app",
|
||||
PAYMENT_METHOD_NAME,
|
||||
"AABBCCDDEEFF001122334455");
|
||||
if (multipleApps) {
|
||||
// The SHA256 of the string "001122334455AABBCCDDEEFF" equals to the
|
||||
// fingerprints[0].value in the "downloaded" manifest file.
|
||||
packageManagerDelegate.installPaymentApp(
|
||||
"Other Test Payment App",
|
||||
"test.payments.other.app",
|
||||
OTHER_PAYMENT_METHOD_NAME,
|
||||
"001122334455AABBCCDDEEFF");
|
||||
}
|
||||
AndroidPaymentAppFinder.setPackageManagerDelegateForTest(packageManagerDelegate);
|
||||
AndroidPaymentAppFinder.setDownloaderForTest(
|
||||
new MockPaymentManifestDownloader(PAYMENT_METHOD_NAME));
|
||||
AndroidPaymentAppFinder.setAndroidIntentLauncherForTest(
|
||||
new MockAndroidIntentLauncher(
|
||||
/* returnShippingAddress= */ false, /* returnContactInfo= */ false));
|
||||
AndroidPaymentAppFinder.bypassIsReadyToPayServiceInTest();
|
||||
}
|
||||
}
|
||||
|
@ -453,6 +453,7 @@ android_library("test_support_java") {
|
||||
sources = [
|
||||
"javatests/src/org/chromium/components/payments/MockAndroidIntentLauncher.java",
|
||||
"javatests/src/org/chromium/components/payments/MockPackageManagerDelegate.java",
|
||||
"javatests/src/org/chromium/components/payments/MockPaymentAppInstaller.java",
|
||||
"javatests/src/org/chromium/components/payments/MockPaymentManifestDownloader.java",
|
||||
]
|
||||
deps = [
|
||||
|
79
components/payments/content/android/javatests/src/org/chromium/components/payments/MockPaymentAppInstaller.java
Normal file
79
components/payments/content/android/javatests/src/org/chromium/components/payments/MockPaymentAppInstaller.java
Normal file
@ -0,0 +1,79 @@
|
||||
// Copyright 2025 The Chromium Authors
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package org.chromium.components.payments;
|
||||
|
||||
/**
|
||||
* An installer for fake Android payment apps for testing. Can install either one or more than one
|
||||
* payment app.
|
||||
*
|
||||
* <p>Installing fake Android payment apps affects global state, in {@link AndroidPaymentAppFinder}.
|
||||
*
|
||||
* <p>The first payment app's package name is "test.payments.app". Its signature is
|
||||
* "AABBCCDDEEFF001122334455".
|
||||
*
|
||||
* <p>If multiple payment apps will be installed, then the second payment app's package name is
|
||||
* "test.payments.other.app". Its signature is "001122334455AABBCCDDEEFF".
|
||||
*/
|
||||
public class MockPaymentAppInstaller {
|
||||
private final String mPaymentMethodName;
|
||||
private final String mOtherPaymentMethodName;
|
||||
|
||||
/**
|
||||
* Constructs a fake payment app installer.
|
||||
*
|
||||
* @param paymentMethodName The method name for the first Android payment app, e.g.,
|
||||
* "https://payments.test/web-pay".
|
||||
* @param otherPaymentMethodName The method name for the second Android payment app, if multiple
|
||||
* payment apps will be installed, e.g., "https://other-payments.example/web-pay".
|
||||
*/
|
||||
public MockPaymentAppInstaller(String paymentMethodName, String otherPaymentMethodName) {
|
||||
mPaymentMethodName = paymentMethodName;
|
||||
mOtherPaymentMethodName = otherPaymentMethodName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects a fake Android payment app into the package manager delegate, with the correct
|
||||
* signature being returned from the downloader. Also turns off connecting to the
|
||||
* IS_READY_TO_PAY service or sending the PAY intent to this app.
|
||||
*
|
||||
* @param multipleApps Whether multiple apps should be installed.
|
||||
*/
|
||||
public void installPaymentApps(boolean multipleApps) {
|
||||
MockPackageManagerDelegate packageManagerDelegate = new MockPackageManagerDelegate();
|
||||
// The SHA256 of the string "AABBCCDDEEFF001122334455" equals to the fingerprints[0].value
|
||||
// in the "downloaded" manifest file.
|
||||
packageManagerDelegate.installPaymentApp(
|
||||
"Test Payment App",
|
||||
"test.payments.app",
|
||||
mPaymentMethodName,
|
||||
"AABBCCDDEEFF001122334455");
|
||||
if (multipleApps) {
|
||||
// The SHA256 of the string "001122334455AABBCCDDEEFF" equals to the
|
||||
// fingerprints[0].value in the "downloaded" manifest file.
|
||||
packageManagerDelegate.installPaymentApp(
|
||||
"Other Test Payment App",
|
||||
"test.payments.other.app",
|
||||
mOtherPaymentMethodName,
|
||||
"001122334455AABBCCDDEEFF");
|
||||
}
|
||||
AndroidPaymentAppFinder.setPackageManagerDelegateForTest(packageManagerDelegate);
|
||||
AndroidPaymentAppFinder.setDownloaderForTest(
|
||||
new MockPaymentManifestDownloader(mPaymentMethodName));
|
||||
AndroidPaymentAppFinder.setAndroidIntentLauncherForTest(
|
||||
new MockAndroidIntentLauncher(
|
||||
/* returnShippingAddress= */ false, /* returnContactInfo= */ false));
|
||||
AndroidPaymentAppFinder.bypassIsReadyToPayServiceInTest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the Android payment app finder to its own original state of querying the Android
|
||||
* package manager and downloading manifest files from the web.
|
||||
*/
|
||||
public void reset() {
|
||||
AndroidPaymentAppFinder.setPackageManagerDelegateForTest(null);
|
||||
AndroidPaymentAppFinder.setDownloaderForTest(null);
|
||||
AndroidPaymentAppFinder.setAndroidIntentLauncherForTest(null);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user