| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/payments/payment_request_interactive_uitest_ba
se.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/browser/ui/views/payments/test_chrome_payment_request_delegate.
h" |
| 15 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "components/payments/payment_request.h" |
| 17 #include "components/payments/payment_request_web_contents_manager.h" |
| 18 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 19 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/content_switches.h" |
| 22 #include "content/public/test/browser_test_utils.h" |
| 23 #include "services/service_manager/public/cpp/interface_registry.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace payments { |
| 27 |
| 28 PaymentRequestInteractiveTestBase::PaymentRequestInteractiveTestBase( |
| 29 const std::string& test_file_path) |
| 30 : test_file_path_(test_file_path) {} |
| 31 PaymentRequestInteractiveTestBase::~PaymentRequestInteractiveTestBase() {} |
| 32 |
| 33 void PaymentRequestInteractiveTestBase::SetUpCommandLine( |
| 34 base::CommandLine* command_line) { |
| 35 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 36 command_line->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); |
| 37 } |
| 38 |
| 39 void PaymentRequestInteractiveTestBase::SetUpOnMainThread() { |
| 40 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
| 41 net::EmbeddedTestServer::TYPE_HTTPS); |
| 42 ASSERT_TRUE(https_server_->InitializeAndListen()); |
| 43 https_server_->ServeFilesFromSourceDirectory("chrome/test/data/payments"); |
| 44 https_server_->StartAcceptingConnections(); |
| 45 |
| 46 GURL url = https_server()->GetURL(test_file_path_); |
| 47 ui_test_utils::NavigateToURL(browser(), url); |
| 48 |
| 49 // Starting now, PaymentRequest Mojo messages sent by the renderer will |
| 50 // create PaymentRequest objects via this test's CreatePaymentRequestForTest, |
| 51 // allowing the test to inject itself as a dialog observer. |
| 52 content::WebContents* web_contents = GetActiveWebContents(); |
| 53 service_manager::InterfaceRegistry* registry = |
| 54 web_contents->GetMainFrame()->GetInterfaceRegistry(); |
| 55 registry->RemoveInterface(payments::mojom::PaymentRequest::Name_); |
| 56 registry->AddInterface(base::Bind( |
| 57 &PaymentRequestInteractiveTestBase::CreatePaymentRequestForTest, |
| 58 base::Unretained(this), web_contents)); |
| 59 } |
| 60 |
| 61 void PaymentRequestInteractiveTestBase::OnDialogOpened() { |
| 62 if (event_observer_) |
| 63 event_observer_->Observe(DialogEvent::DIALOG_OPENED); |
| 64 } |
| 65 |
| 66 void PaymentRequestInteractiveTestBase::InvokePaymentRequestUI() { |
| 67 event_observer_.reset(new DialogEventObserver(DialogEvent::DIALOG_OPENED)); |
| 68 |
| 69 content::WebContents* web_contents = GetActiveWebContents(); |
| 70 const std::string click_buy_button_js = |
| 71 "(function() { document.getElementById('buy').click(); })();"; |
| 72 ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js)); |
| 73 |
| 74 event_observer_->Wait(); |
| 75 |
| 76 // The web-modal dialog should be open. |
| 77 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 78 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); |
| 79 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive()); |
| 80 } |
| 81 |
| 82 content::WebContents* |
| 83 PaymentRequestInteractiveTestBase::GetActiveWebContents() { |
| 84 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 85 } |
| 86 |
| 87 const std::vector<PaymentRequest*> |
| 88 PaymentRequestInteractiveTestBase::GetPaymentRequests( |
| 89 content::WebContents* web_contents) { |
| 90 PaymentRequestWebContentsManager* manager = |
| 91 PaymentRequestWebContentsManager::GetOrCreateForWebContents(web_contents); |
| 92 if (!manager) |
| 93 return std::vector<PaymentRequest*>(); |
| 94 |
| 95 std::vector<PaymentRequest*> payment_requests_ptrs; |
| 96 for (const auto& p : manager->payment_requests_) |
| 97 payment_requests_ptrs.push_back(p.first); |
| 98 return payment_requests_ptrs; |
| 99 } |
| 100 |
| 101 void PaymentRequestInteractiveTestBase::CreatePaymentRequestForTest( |
| 102 content::WebContents* web_contents, |
| 103 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { |
| 104 DCHECK(web_contents); |
| 105 PaymentRequestWebContentsManager::GetOrCreateForWebContents(web_contents) |
| 106 ->CreatePaymentRequest(web_contents, |
| 107 base::MakeUnique<TestChromePaymentRequestDelegate>( |
| 108 web_contents, this /* observer */), |
| 109 std::move(request)); |
| 110 } |
| 111 |
| 112 PaymentRequestInteractiveTestBase::DialogEventObserver::DialogEventObserver( |
| 113 PaymentRequestInteractiveTestBase::DialogEvent event) |
| 114 : event_(event), seen_(false) {} |
| 115 PaymentRequestInteractiveTestBase::DialogEventObserver::~DialogEventObserver() { |
| 116 } |
| 117 |
| 118 void PaymentRequestInteractiveTestBase::DialogEventObserver::Wait() { |
| 119 if (seen_) |
| 120 return; |
| 121 |
| 122 DCHECK(!run_loop_.running()); |
| 123 run_loop_.Run(); |
| 124 } |
| 125 |
| 126 void PaymentRequestInteractiveTestBase::DialogEventObserver::Observe( |
| 127 PaymentRequestInteractiveTestBase::DialogEvent event) { |
| 128 seen_ = true; |
| 129 if (event == event_ && run_loop_.running()) |
| 130 run_loop_.Quit(); |
| 131 } |
| 132 |
| 133 } // namespace payments |
| OLD | NEW |