| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/payments/payment_request_interactive_uitest_ba
se.h" | 5 #include "chrome/browser/ui/views/payments/payment_request_interactive_uitest_ba
se.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string16.h" |
| 12 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/browser/ui/views/payments/test_chrome_payment_request_delegate.
h" | 16 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 17 #include "chrome/test/base/interactive_test_utils.h" |
| 15 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "components/payments/payment_request.h" | 19 #include "components/payments/payment_request.h" |
| 17 #include "components/payments/payment_request_web_contents_manager.h" | 20 #include "components/payments/payment_request_web_contents_manager.h" |
| 18 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 21 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 22 #include "content/public/test/browser_test_utils.h" | 25 #include "content/public/test/browser_test_utils.h" |
| 23 #include "services/service_manager/public/cpp/interface_registry.h" | 26 #include "services/service_manager/public/cpp/interface_registry.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "ui/base/test/ui_controls.h" |
| 29 #include "ui/views/controls/styled_label.h" |
| 25 | 30 |
| 26 namespace payments { | 31 namespace payments { |
| 27 | 32 |
| 28 PaymentRequestInteractiveTestBase::PaymentRequestInteractiveTestBase( | 33 PaymentRequestInteractiveTestBase::PaymentRequestInteractiveTestBase( |
| 29 const std::string& test_file_path) | 34 const std::string& test_file_path) |
| 30 : test_file_path_(test_file_path) {} | 35 : test_file_path_(test_file_path), |
| 36 delegate_(nullptr) {} |
| 31 PaymentRequestInteractiveTestBase::~PaymentRequestInteractiveTestBase() {} | 37 PaymentRequestInteractiveTestBase::~PaymentRequestInteractiveTestBase() {} |
| 32 | 38 |
| 33 void PaymentRequestInteractiveTestBase::SetUpCommandLine( | 39 void PaymentRequestInteractiveTestBase::SetUpCommandLine( |
| 34 base::CommandLine* command_line) { | 40 base::CommandLine* command_line) { |
| 35 InProcessBrowserTest::SetUpCommandLine(command_line); | 41 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 36 command_line->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); | 42 command_line->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); |
| 37 } | 43 } |
| 38 | 44 |
| 39 void PaymentRequestInteractiveTestBase::SetUpOnMainThread() { | 45 void PaymentRequestInteractiveTestBase::SetUpOnMainThread() { |
| 40 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( | 46 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 56 registry->AddInterface(base::Bind( | 62 registry->AddInterface(base::Bind( |
| 57 &PaymentRequestInteractiveTestBase::CreatePaymentRequestForTest, | 63 &PaymentRequestInteractiveTestBase::CreatePaymentRequestForTest, |
| 58 base::Unretained(this), web_contents)); | 64 base::Unretained(this), web_contents)); |
| 59 } | 65 } |
| 60 | 66 |
| 61 void PaymentRequestInteractiveTestBase::OnDialogOpened() { | 67 void PaymentRequestInteractiveTestBase::OnDialogOpened() { |
| 62 if (event_observer_) | 68 if (event_observer_) |
| 63 event_observer_->Observe(DialogEvent::DIALOG_OPENED); | 69 event_observer_->Observe(DialogEvent::DIALOG_OPENED); |
| 64 } | 70 } |
| 65 | 71 |
| 72 void PaymentRequestInteractiveTestBase::OnOrderSummaryOpened() { |
| 73 if (event_observer_) |
| 74 event_observer_->Observe(DialogEvent::ORDER_SUMMARY_OPENED); |
| 75 } |
| 76 |
| 66 void PaymentRequestInteractiveTestBase::OnWidgetDestroyed( | 77 void PaymentRequestInteractiveTestBase::OnWidgetDestroyed( |
| 67 views::Widget* widget) { | 78 views::Widget* widget) { |
| 68 if (event_observer_) | 79 if (event_observer_) |
| 69 event_observer_->Observe(DialogEvent::DIALOG_CLOSED); | 80 event_observer_->Observe(DialogEvent::DIALOG_CLOSED); |
| 70 } | 81 } |
| 71 | 82 |
| 72 void PaymentRequestInteractiveTestBase::InvokePaymentRequestUI() { | 83 void PaymentRequestInteractiveTestBase::InvokePaymentRequestUI() { |
| 73 event_observer_.reset(new DialogEventObserver(DialogEvent::DIALOG_OPENED)); | 84 ResetEventObserver(DialogEvent::DIALOG_OPENED); |
| 74 | 85 |
| 75 content::WebContents* web_contents = GetActiveWebContents(); | 86 content::WebContents* web_contents = GetActiveWebContents(); |
| 76 const std::string click_buy_button_js = | 87 const std::string click_buy_button_js = |
| 77 "(function() { document.getElementById('buy').click(); })();"; | 88 "(function() { document.getElementById('buy').click(); })();"; |
| 78 ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js)); | 89 ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js)); |
| 79 | 90 |
| 80 event_observer_->Wait(); | 91 WaitForObservedEvent(); |
| 81 | 92 |
| 82 // The web-modal dialog should be open. | 93 // The web-modal dialog should be open. |
| 83 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 94 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 84 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); | 95 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); |
| 85 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive()); | 96 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive()); |
| 86 } | 97 } |
| 87 | 98 |
| 99 void PaymentRequestInteractiveTestBase::OpenOrderSummaryScreen() { |
| 100 ResetEventObserver(DialogEvent::ORDER_SUMMARY_OPENED); |
| 101 |
| 102 ClickOnDialogView(DialogViewID::PAYMENT_SHEET_SUMMARY_SECTION); |
| 103 |
| 104 WaitForObservedEvent(); |
| 105 } |
| 106 |
| 88 content::WebContents* | 107 content::WebContents* |
| 89 PaymentRequestInteractiveTestBase::GetActiveWebContents() { | 108 PaymentRequestInteractiveTestBase::GetActiveWebContents() { |
| 90 return browser()->tab_strip_model()->GetActiveWebContents(); | 109 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 91 } | 110 } |
| 92 | 111 |
| 93 const std::vector<PaymentRequest*> | 112 const std::vector<PaymentRequest*> |
| 94 PaymentRequestInteractiveTestBase::GetPaymentRequests( | 113 PaymentRequestInteractiveTestBase::GetPaymentRequests( |
| 95 content::WebContents* web_contents) { | 114 content::WebContents* web_contents) { |
| 96 PaymentRequestWebContentsManager* manager = | 115 PaymentRequestWebContentsManager* manager = |
| 97 PaymentRequestWebContentsManager::GetOrCreateForWebContents(web_contents); | 116 PaymentRequestWebContentsManager::GetOrCreateForWebContents(web_contents); |
| 98 if (!manager) | 117 if (!manager) |
| 99 return std::vector<PaymentRequest*>(); | 118 return std::vector<PaymentRequest*>(); |
| 100 | 119 |
| 101 std::vector<PaymentRequest*> payment_requests_ptrs; | 120 std::vector<PaymentRequest*> payment_requests_ptrs; |
| 102 for (const auto& p : manager->payment_requests_) | 121 for (const auto& p : manager->payment_requests_) |
| 103 payment_requests_ptrs.push_back(p.first); | 122 payment_requests_ptrs.push_back(p.first); |
| 104 return payment_requests_ptrs; | 123 return payment_requests_ptrs; |
| 105 } | 124 } |
| 106 | 125 |
| 107 void PaymentRequestInteractiveTestBase::CreatePaymentRequestForTest( | 126 void PaymentRequestInteractiveTestBase::CreatePaymentRequestForTest( |
| 108 content::WebContents* web_contents, | 127 content::WebContents* web_contents, |
| 109 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { | 128 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) { |
| 110 DCHECK(web_contents); | 129 DCHECK(web_contents); |
| 130 std::unique_ptr<TestChromePaymentRequestDelegate> delegate = |
| 131 base::MakeUnique<TestChromePaymentRequestDelegate>( |
| 132 web_contents, this /* observer */, this /* widget_observer */); |
| 133 delegate_ = delegate.get(); |
| 111 PaymentRequestWebContentsManager::GetOrCreateForWebContents(web_contents) | 134 PaymentRequestWebContentsManager::GetOrCreateForWebContents(web_contents) |
| 112 ->CreatePaymentRequest( | 135 ->CreatePaymentRequest(web_contents, std::move(delegate), |
| 113 web_contents, | 136 std::move(request)); |
| 114 base::MakeUnique<TestChromePaymentRequestDelegate>( | 137 } |
| 115 web_contents, this /* observer */, this /* widget_observer */), | 138 |
| 116 std::move(request)); | 139 void PaymentRequestInteractiveTestBase::ClickOnDialogView( |
| 140 DialogViewID view_id) { |
| 141 views::View* view = |
| 142 delegate_->dialog_view()->GetViewByID(static_cast<int>(view_id)); |
| 143 DCHECK(view); |
| 144 base::RunLoop run_loop; |
| 145 ui_test_utils::MoveMouseToCenterAndPress( |
| 146 view, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP, |
| 147 run_loop.QuitClosure()); |
| 148 run_loop.Run(); |
| 149 } |
| 150 |
| 151 const base::string16& PaymentRequestInteractiveTestBase::GetStyledLabelText( |
| 152 DialogViewID view_id) { |
| 153 views::View* view = dialog_view()->GetViewByID(static_cast<int>(view_id)); |
| 154 DCHECK(view); |
| 155 return static_cast<views::StyledLabel*>(view)->text(); |
| 117 } | 156 } |
| 118 | 157 |
| 119 PaymentRequestInteractiveTestBase::DialogEventObserver::DialogEventObserver( | 158 PaymentRequestInteractiveTestBase::DialogEventObserver::DialogEventObserver( |
| 120 PaymentRequestInteractiveTestBase::DialogEvent event) | 159 PaymentRequestInteractiveTestBase::DialogEvent event) |
| 121 : event_(event), seen_(false) {} | 160 : event_(event), seen_(false) {} |
| 122 PaymentRequestInteractiveTestBase::DialogEventObserver::~DialogEventObserver() { | 161 PaymentRequestInteractiveTestBase::DialogEventObserver::~DialogEventObserver() { |
| 123 } | 162 } |
| 124 | 163 |
| 125 void PaymentRequestInteractiveTestBase::DialogEventObserver::Wait() { | 164 void PaymentRequestInteractiveTestBase::DialogEventObserver::Wait() { |
| 126 if (seen_) | 165 if (seen_) |
| 127 return; | 166 return; |
| 128 | 167 |
| 129 DCHECK(!run_loop_.running()); | 168 DCHECK(!run_loop_.running()); |
| 130 run_loop_.Run(); | 169 run_loop_.Run(); |
| 131 } | 170 } |
| 132 | 171 |
| 133 void PaymentRequestInteractiveTestBase::DialogEventObserver::Observe( | 172 void PaymentRequestInteractiveTestBase::DialogEventObserver::Observe( |
| 134 PaymentRequestInteractiveTestBase::DialogEvent event) { | 173 PaymentRequestInteractiveTestBase::DialogEvent event) { |
| 135 if (seen_) | 174 if (seen_) |
| 136 return; | 175 return; |
| 137 | 176 |
| 138 DCHECK_EQ(event_, event); | 177 DCHECK_EQ(event_, event); |
| 139 seen_ = true; | 178 seen_ = true; |
| 140 if (run_loop_.running()) | 179 if (run_loop_.running()) |
| 141 run_loop_.Quit(); | 180 run_loop_.Quit(); |
| 142 } | 181 } |
| 143 | 182 |
| 144 void PaymentRequestInteractiveTestBase::ResetEventObserver(DialogEvent event) { | 183 void PaymentRequestInteractiveTestBase::ResetEventObserver(DialogEvent event) { |
| 145 event_observer_.reset(new DialogEventObserver(event)); | 184 event_observer_ = base::MakeUnique<DialogEventObserver>(event); |
| 146 } | 185 } |
| 147 | 186 |
| 148 void PaymentRequestInteractiveTestBase::WaitForObservedEvent() { | 187 void PaymentRequestInteractiveTestBase::WaitForObservedEvent() { |
| 149 event_observer_->Wait(); | 188 event_observer_->Wait(); |
| 150 } | 189 } |
| 151 | 190 |
| 152 } // namespace payments | 191 } // namespace payments |
| OLD | NEW |