Commit 58917f74 authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Commit Bot

[WebLayer] Simplify the way test bridge accesses PaymentUiService

Context:
Before this CL, PaymentRequestTestBridge accessed PaymentUiService
by CPRService static methods, a CPRService instance and then
PaymentUiService. The excessive use of plumbings in this route can be
simplified.

Changes:
* PaymentRequestTestBridge no longer need to take CPRService to access
  PaymetUiService each time. Instead, CPRService is changed to connect
  PaymentUiService and PaymentRequestTestBridge for only once at the
  beginning, which allows PaymentRequestTestBridge to retain a
  PaymetUiService instance and access it directly since that time.
* The test depends less on the CPRService static instance. This would
  make it easier to let PRService take the static instance instead.

Bug: 1147269

Change-Id: I44f2c469c0db6b1c86020bb44ff29283e69bb1fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2527705
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826041}
parent 47341945
...@@ -7,7 +7,6 @@ package org.chromium.chrome.browser.payments; ...@@ -7,7 +7,6 @@ package org.chromium.chrome.browser.payments;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.collection.ArrayMap; import androidx.collection.ArrayMap;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
...@@ -154,6 +153,10 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, ...@@ -154,6 +153,10 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest,
/*params=*/mPaymentRequestService, mWebContents, /*params=*/mPaymentRequestService, mWebContents,
paymentRequestService.isOffTheRecord(), mJourneyLogger, topLevelOrigin, paymentRequestService.isOffTheRecord(), mJourneyLogger, topLevelOrigin,
/*observer=*/this); /*observer=*/this);
if (PaymentRequestService.getNativeObserverForTest() != null) {
PaymentRequestService.getNativeObserverForTest().onPaymentUiServiceCreated(
mPaymentUiService);
}
} }
// Implements BrowserPaymentRequest: // Implements BrowserPaymentRequest:
...@@ -497,89 +500,6 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, ...@@ -497,89 +500,6 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest,
methodDataMap.putAll(result); methodDataMap.putAll(result);
} }
/**
* Get the WebContents of the Expandable Payment Handler for testing purpose; return null if
* nonexistent.
*
* @return The WebContents of the Expandable Payment Handler.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public static WebContents getPaymentHandlerWebContentsForTest() {
if (sShowingPaymentRequest == null) return null;
return sShowingPaymentRequest.getPaymentHandlerWebContentsForTestInternal();
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private WebContents getPaymentHandlerWebContentsForTestInternal() {
return mPaymentUiService.getPaymentHandlerWebContentsForTest();
}
/**
* Clicks the security icon of the Expandable Payment Handler for testing purpose; return false
* if failed.
*
* @return Whether the click is successful.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public static boolean clickPaymentHandlerSecurityIconForTest() {
if (sShowingPaymentRequest == null) return false;
return sShowingPaymentRequest.clickPaymentHandlerSecurityIconForTestInternal();
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private boolean clickPaymentHandlerSecurityIconForTestInternal() {
return mPaymentUiService.clickPaymentHandlerSecurityIconForTest();
}
/**
* Simulates a click on the close button of the Payment Handler for testing purpose; return
* false if failed.
*
* @return Whether the click is successful.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public static boolean clickPaymentHandlerCloseButtonForTest() {
if (sShowingPaymentRequest == null) return false;
return sShowingPaymentRequest.clickPaymentHandlerCloseButtonForTestInternal();
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private boolean clickPaymentHandlerCloseButtonForTestInternal() {
return mPaymentUiService.clickPaymentHandlerCloseButtonForTest();
}
/**
* Confirms payment in minimal UI. Used only in test.
*
* @return Whether the payment was confirmed successfully.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public static boolean confirmMinimalUIForTest() {
return sShowingPaymentRequest != null
&& sShowingPaymentRequest.confirmMinimalUIForTestInternal();
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private boolean confirmMinimalUIForTestInternal() {
return mPaymentUiService.confirmMinimalUIForTest();
}
/**
* Dismisses the minimal UI. Used only in test.
*
* @return Whether the dismissal was successful.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public static boolean dismissMinimalUIForTest() {
return sShowingPaymentRequest != null
&& sShowingPaymentRequest.dismissMinimalUIForTestInternal();
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private boolean dismissMinimalUIForTestInternal() {
return mPaymentUiService.dismissMinimalUIForTest();
}
/** /**
* Called to open a new PaymentHandler UI on the showing PaymentRequest. * Called to open a new PaymentHandler UI on the showing PaymentRequest.
* @param url The url of the payment app to be displayed in the UI. * @param url The url of the payment app to be displayed in the UI.
......
...@@ -64,6 +64,7 @@ import org.chromium.components.payments.PaymentFeatureList; ...@@ -64,6 +64,7 @@ import org.chromium.components.payments.PaymentFeatureList;
import org.chromium.components.payments.PaymentOptionsUtils; import org.chromium.components.payments.PaymentOptionsUtils;
import org.chromium.components.payments.PaymentRequestParams; import org.chromium.components.payments.PaymentRequestParams;
import org.chromium.components.payments.PaymentUIsObserver; import org.chromium.components.payments.PaymentUIsObserver;
import org.chromium.components.payments.PaymentUiServiceTestInterface;
import org.chromium.components.payments.Section; import org.chromium.components.payments.Section;
import org.chromium.components.security_state.SecurityStateModel; import org.chromium.components.security_state.SecurityStateModel;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
...@@ -99,6 +100,7 @@ import java.util.Set; ...@@ -99,6 +100,7 @@ import java.util.Set;
*/ */
public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Observer, public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Observer,
PaymentHandlerUiObserver, FocusChangedObserver, PaymentHandlerUiObserver, FocusChangedObserver,
PaymentUiServiceTestInterface,
NormalizedAddressRequestDelegate, PaymentRequestUI.Client { NormalizedAddressRequestDelegate, PaymentRequestUI.Client {
/** Limit in the number of suggested items in a section. */ /** Limit in the number of suggested items in a section. */
/* package */ static final int SUGGESTIONS_LIMIT = 4; /* package */ static final int SUGGESTIONS_LIMIT = 4;
...@@ -393,11 +395,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -393,11 +395,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
return mPaymentRequestUI != null || mMinimalUi != null; return mPaymentRequestUI != null || mMinimalUi != null;
} }
/** // Implements PaymentUiServiceTestInterface:
* Confirms payment in minimal UI. Used only in test. @Override
*
* @return Whether the payment was confirmed successfully.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean confirmMinimalUIForTest() { public boolean confirmMinimalUIForTest() {
if (mMinimalUi == null) return false; if (mMinimalUi == null) return false;
...@@ -405,11 +404,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -405,11 +404,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
return true; return true;
} }
/** // Implements PaymentUiServiceTestInterface:
* Dismisses the minimal UI. Used only in test. @Override
*
* @return Whether the dismissal was successful.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean dismissMinimalUIForTest() { public boolean dismissMinimalUIForTest() {
if (mMinimalUi == null) return false; if (mMinimalUi == null) return false;
...@@ -945,12 +941,16 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -945,12 +941,16 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
return paymentHandlerWebContents; return paymentHandlerWebContents;
} }
// Implements PaymentUiServiceTestInterface:
@Override
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
public WebContents getPaymentHandlerWebContentsForTest() { public WebContents getPaymentHandlerWebContentsForTest() {
if (mPaymentHandlerUi == null) return null; if (mPaymentHandlerUi == null) return null;
return mPaymentHandlerUi.getWebContentsForTest(); return mPaymentHandlerUi.getWebContentsForTest();
} }
// Implements PaymentUiServiceTestInterface:
@Override
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean clickPaymentHandlerSecurityIconForTest() { public boolean clickPaymentHandlerSecurityIconForTest() {
if (mPaymentHandlerUi == null) return false; if (mPaymentHandlerUi == null) return false;
...@@ -958,6 +958,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -958,6 +958,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
return true; return true;
} }
// Implements PaymentUiServiceTestInterface:
@Override
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean clickPaymentHandlerCloseButtonForTest() { public boolean clickPaymentHandlerCloseButtonForTest() {
if (mPaymentHandlerUi == null) return false; if (mPaymentHandlerUi == null) return false;
......
...@@ -16,6 +16,7 @@ import org.chromium.chrome.browser.payments.ChromePaymentRequestService; ...@@ -16,6 +16,7 @@ import org.chromium.chrome.browser.payments.ChromePaymentRequestService;
import org.chromium.components.autofill.EditableOption; import org.chromium.components.autofill.EditableOption;
import org.chromium.components.payments.PaymentRequestService; import org.chromium.components.payments.PaymentRequestService;
import org.chromium.components.payments.PaymentRequestService.NativeObserverForTest; import org.chromium.components.payments.PaymentRequestService.NativeObserverForTest;
import org.chromium.components.payments.PaymentUiServiceTestInterface;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.payments.mojom.PaymentItem; import org.chromium.payments.mojom.PaymentItem;
...@@ -26,6 +27,8 @@ import java.util.List; ...@@ -26,6 +27,8 @@ import java.util.List;
*/ */
@JNINamespace("payments") @JNINamespace("payments")
public class PaymentRequestTestBridge { public class PaymentRequestTestBridge {
private static PaymentUiServiceTestInterface sUiService;
/** /**
* A test override of the ChromePaymentRequestService's Delegate. Allows tests to control the * A test override of the ChromePaymentRequestService's Delegate. Allows tests to control the
* answers about the state of the system, in order to control which paths should be tested in * answers about the state of the system, in order to control which paths should be tested in
...@@ -127,6 +130,17 @@ public class PaymentRequestTestBridge { ...@@ -127,6 +130,17 @@ public class PaymentRequestTestBridge {
mOnMinimalUIReadyPtr = onMinimalUIReadyPtr; mOnMinimalUIReadyPtr = onMinimalUIReadyPtr;
} }
@Override
public void onPaymentUiServiceCreated(PaymentUiServiceTestInterface uiService) {
assert uiService != null;
PaymentRequestTestBridge.sUiService = uiService;
}
@Override
public void onClosed() {
PaymentRequestTestBridge.sUiService = null;
}
@Override @Override
public void onCanMakePaymentCalled() { public void onCanMakePaymentCalled() {
nativeResolvePaymentRequestObserverCallback(mOnCanMakePaymentCalledPtr); nativeResolvePaymentRequestObserverCallback(mOnCanMakePaymentCalledPtr);
...@@ -229,27 +243,27 @@ public class PaymentRequestTestBridge { ...@@ -229,27 +243,27 @@ public class PaymentRequestTestBridge {
@CalledByNative @CalledByNative
private static WebContents getPaymentHandlerWebContentsForTest() { private static WebContents getPaymentHandlerWebContentsForTest() {
return ChromePaymentRequestService.getPaymentHandlerWebContentsForTest(); return sUiService.getPaymentHandlerWebContentsForTest();
} }
@CalledByNative @CalledByNative
private static boolean clickPaymentHandlerSecurityIconForTest() { private static boolean clickPaymentHandlerSecurityIconForTest() {
return ChromePaymentRequestService.clickPaymentHandlerSecurityIconForTest(); return sUiService.clickPaymentHandlerSecurityIconForTest();
} }
@CalledByNative @CalledByNative
private static boolean clickPaymentHandlerCloseButtonForTest() { private static boolean clickPaymentHandlerCloseButtonForTest() {
return ChromePaymentRequestService.clickPaymentHandlerCloseButtonForTest(); return sUiService.clickPaymentHandlerCloseButtonForTest();
} }
@CalledByNative @CalledByNative
private static boolean confirmMinimalUIForTest() { private static boolean confirmMinimalUIForTest() {
return ChromePaymentRequestService.confirmMinimalUIForTest(); return sUiService.confirmMinimalUIForTest();
} }
@CalledByNative @CalledByNative
private static boolean dismissMinimalUIForTest() { private static boolean dismissMinimalUIForTest() {
return ChromePaymentRequestService.dismissMinimalUIForTest(); return sUiService.dismissMinimalUIForTest();
} }
@CalledByNative @CalledByNative
......
...@@ -135,6 +135,7 @@ android_library("all_java") { ...@@ -135,6 +135,7 @@ android_library("all_java") {
"java/src/org/chromium/components/payments/PaymentRequestSpec.java", "java/src/org/chromium/components/payments/PaymentRequestSpec.java",
"java/src/org/chromium/components/payments/PaymentResponseHelperInterface.java", "java/src/org/chromium/components/payments/PaymentResponseHelperInterface.java",
"java/src/org/chromium/components/payments/PaymentUIsObserver.java", "java/src/org/chromium/components/payments/PaymentUIsObserver.java",
"java/src/org/chromium/components/payments/PaymentUiServiceTestInterface.java",
"java/src/org/chromium/components/payments/PaymentValidator.java", "java/src/org/chromium/components/payments/PaymentValidator.java",
"java/src/org/chromium/components/payments/SkipToGPayHelper.java", "java/src/org/chromium/components/payments/SkipToGPayHelper.java",
"java/src/org/chromium/components/payments/SupportedDelegations.java", "java/src/org/chromium/components/payments/SupportedDelegations.java",
......
...@@ -154,6 +154,8 @@ public class PaymentRequestService ...@@ -154,6 +154,8 @@ public class PaymentRequestService
void onAbortCalled(); void onAbortCalled();
void onCompleteCalled(); void onCompleteCalled();
void onMinimalUIReady(); void onMinimalUIReady();
void onPaymentUiServiceCreated(PaymentUiServiceTestInterface uiService);
void onClosed();
} }
/** /**
...@@ -989,6 +991,10 @@ public class PaymentRequestService ...@@ -989,6 +991,10 @@ public class PaymentRequestService
} }
mOnClosedListener.run(); mOnClosedListener.run();
if (sNativeObserverForTest != null) {
sNativeObserverForTest.onClosed();
}
} }
/** @return An observer for the payment request service, if any; otherwise, null. */ /** @return An observer for the payment request service, if any; otherwise, null. */
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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;
import org.chromium.content_public.browser.WebContents;
/** The interface of PaymentUiService that provides testing methods. */
public interface PaymentUiServiceTestInterface {
/**
* Get the WebContents of the Payment Handler; return null if nonexistent.
*
* @return The WebContents of the Payment Handler.
*/
WebContents getPaymentHandlerWebContentsForTest();
/**
* Clicks the security icon of the Payment Handler; return false if failed.
*
* @return Whether the click is successful.
*/
boolean clickPaymentHandlerSecurityIconForTest();
/**
* Simulates a click on the close button of the Payment Handler; return
* false if failed.
*
* @return Whether the click is successful.
*/
boolean clickPaymentHandlerCloseButtonForTest();
/**
* Confirms payment in minimal UI.
*
* @return Whether the payment was confirmed successfully.
*/
boolean confirmMinimalUIForTest();
/**
* Dismisses the minimal UI.
*
* @return Whether the dismissal was successful.
*/
boolean dismissMinimalUIForTest();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment