Commit 0287293f authored by danakj's avatar danakj Committed by Commit Bot

Make PaymentRequestBrowserTestBase more compatible with Android

Replace the mojo injection to change how PaymentRequests are made
by instead injecting a factory in C++. Future work will do a similar
injection into the Android Java code to change PaymentRequest behaviour
in similar ways.

Use content test helpers to navigate instead of chrome ui desktop-only
helpers.

R=rouslan@chromium.org

Bug: 961849
Change-Id: Id4859416e7457686cc35c9c578ee3a4954fe80e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1680861
Commit-Queue: danakj <danakj@chromium.org>
Reviewed-by: default avatarDanyao Wang <danyao@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#673091}
parent 9e8e33c6
...@@ -8,13 +8,32 @@ ...@@ -8,13 +8,32 @@
#include <utility> #include <utility>
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h"
#include "chrome/browser/payments/chrome_payment_request_delegate.h" #include "chrome/browser/payments/chrome_payment_request_delegate.h"
#include "components/payments/content/payment_request_web_contents_manager.h" #include "components/payments/content/payment_request_web_contents_manager.h"
namespace payments { namespace payments {
namespace {
using PaymentRequestFactoryCallback =
base::RepeatingCallback<void(mojom::PaymentRequestRequest request,
content::RenderFrameHost* render_frame_host)>;
PaymentRequestFactoryCallback& GetTestingFactoryCallback() {
static base::NoDestructor<PaymentRequestFactoryCallback> callback;
return *callback;
}
} // namespace
void CreatePaymentRequest(mojom::PaymentRequestRequest request, void CreatePaymentRequest(mojom::PaymentRequestRequest request,
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
if (GetTestingFactoryCallback()) {
return GetTestingFactoryCallback().Run(std::move(request),
render_frame_host);
}
content::WebContents* web_contents = content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host); content::WebContents::FromRenderFrameHost(render_frame_host);
if (!web_contents) if (!web_contents)
...@@ -27,4 +46,9 @@ void CreatePaymentRequest(mojom::PaymentRequestRequest request, ...@@ -27,4 +46,9 @@ void CreatePaymentRequest(mojom::PaymentRequestRequest request,
/*observer_for_testing=*/nullptr); /*observer_for_testing=*/nullptr);
} }
void SetPaymentRequestFactoryForTesting(
PaymentRequestFactoryCallback factory_callback) {
GetTestingFactoryCallback() = std::move(factory_callback);
}
} // namespace payments } // namespace payments
...@@ -20,6 +20,13 @@ namespace payments { ...@@ -20,6 +20,13 @@ namespace payments {
void CreatePaymentRequest(mojom::PaymentRequestRequest request, void CreatePaymentRequest(mojom::PaymentRequestRequest request,
content::RenderFrameHost* render_frame_host); content::RenderFrameHost* render_frame_host);
// Unit tests should clean up this factory override by giving a null callback so
// as to not interfere with other tests.
void SetPaymentRequestFactoryForTesting(
base::RepeatingCallback<void(mojom::PaymentRequestRequest request,
content::RenderFrameHost* render_frame_host)>
factory_callback);
} // namespace payments } // namespace payments
#endif // CHROME_BROWSER_PAYMENTS_PAYMENT_REQUEST_FACTORY_H_ #endif // CHROME_BROWSER_PAYMENTS_PAYMENT_REQUEST_FACTORY_H_
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h" #include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/payments/payment_request_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
...@@ -25,7 +26,6 @@ ...@@ -25,7 +26,6 @@
#include "chrome/browser/ui/views/payments/validating_combobox.h" #include "chrome/browser/ui/views/payments/validating_combobox.h"
#include "chrome/browser/ui/views/payments/validating_textfield.h" #include "chrome/browser/ui/views/payments/validating_textfield.h"
#include "chrome/browser/ui/views/payments/view_stack.h" #include "chrome/browser/ui/views/payments/view_stack.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h" #include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/credit_card.h" #include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
...@@ -96,13 +96,14 @@ void PaymentRequestBrowserTestBase::SetUpOnMainThread() { ...@@ -96,13 +96,14 @@ void PaymentRequestBrowserTestBase::SetUpOnMainThread() {
https_server_->ServeFilesFromSourceDirectory("components/test/data/payments"); https_server_->ServeFilesFromSourceDirectory("components/test/data/payments");
https_server_->StartAcceptingConnections(); https_server_->StartAcceptingConnections();
Observe(GetActiveWebContents());
// Starting now, PaymentRequest Mojo messages sent by the renderer will // Starting now, PaymentRequest Mojo messages sent by the renderer will
// create PaymentRequest objects via this test's CreatePaymentRequestForTest, // create PaymentRequest objects via this test's CreatePaymentRequestForTest,
// allowing the test to inject itself as a dialog observer. // allowing the test to inject itself as a dialog observer.
Observe(GetActiveWebContents()); payments::SetPaymentRequestFactoryForTesting(base::BindRepeating(
registry_.AddInterface<payments::mojom::PaymentRequest>( &PaymentRequestBrowserTestBase::CreatePaymentRequestForTest,
base::Bind(&PaymentRequestBrowserTestBase::CreatePaymentRequestForTest, base::Unretained(this)));
base::Unretained(this)));
// Set a test sync service so that all types of cards work. // Set a test sync service so that all types of cards work.
GetDataManager()->SetSyncServiceForTest(&sync_service_); GetDataManager()->SetSyncServiceForTest(&sync_service_);
...@@ -112,12 +113,17 @@ void PaymentRequestBrowserTestBase::SetUpOnMainThread() { ...@@ -112,12 +113,17 @@ void PaymentRequestBrowserTestBase::SetUpOnMainThread() {
} }
void PaymentRequestBrowserTestBase::NavigateTo(const std::string& file_path) { void PaymentRequestBrowserTestBase::NavigateTo(const std::string& file_path) {
bool result;
if (file_path.find("data:") == 0U) { if (file_path.find("data:") == 0U) {
ui_test_utils::NavigateToURL(browser(), GURL(file_path)); result = content::NavigateToURL(GetActiveWebContents(), GURL(file_path));
} else { } else {
ui_test_utils::NavigateToURL(browser(), result = content::NavigateToURL(GetActiveWebContents(),
https_server()->GetURL("a.com", file_path)); https_server()->GetURL("a.com", file_path));
} }
// TODO(rouslan): Some tests actually expect navigation to fail. They could
// use a different method and then NavigateTo() could ensure navigation
// succeeded in the common case.
ignore_result(result);
} }
void PaymentRequestBrowserTestBase::SetIncognito() { void PaymentRequestBrowserTestBase::SetIncognito() {
......
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