| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/guid.h" | 6 #include "base/guid.h" |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/ui/autofill/card_unmask_prompt_view_tester.h" | 13 #include "chrome/browser/ui/autofill/card_unmask_prompt_view_tester.h" |
| 14 #include "chrome/browser/ui/autofill/create_card_unmask_prompt_view.h" | 14 #include "chrome/browser/ui/autofill/create_card_unmask_prompt_view.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/test/test_browser_dialog.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "components/autofill/core/browser/autofill_test_utils.h" | 19 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 19 #include "components/autofill/core/browser/card_unmask_delegate.h" | 20 #include "components/autofill/core/browser/card_unmask_delegate.h" |
| 20 #include "components/autofill/core/browser/ui/card_unmask_prompt_controller_impl
.h" | 21 #include "components/autofill/core/browser/ui/card_unmask_prompt_controller_impl
.h" |
| 21 #include "components/user_prefs/user_prefs.h" | 22 #include "components/user_prefs/user_prefs.h" |
| 22 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
| 25 #include "ui/base/test/user_interactive_test_case.h" | |
| 26 | 26 |
| 27 namespace autofill { | 27 namespace autofill { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 enum class CreditCardExpiry : uint8_t { EXPIRED, VALID }; | 31 // Forms of the dialog that can be invoked. |
| 32 constexpr const char kExpiryExpired[] = "expired"; |
| 33 constexpr const char kExpiryValid[] = "valid"; |
| 32 | 34 |
| 33 class TestCardUnmaskDelegate : public CardUnmaskDelegate { | 35 class TestCardUnmaskDelegate : public CardUnmaskDelegate { |
| 34 public: | 36 public: |
| 35 TestCardUnmaskDelegate() : weak_factory_(this) {} | 37 TestCardUnmaskDelegate() : weak_factory_(this) {} |
| 36 | 38 |
| 37 virtual ~TestCardUnmaskDelegate() {} | 39 virtual ~TestCardUnmaskDelegate() {} |
| 38 | 40 |
| 39 // CardUnmaskDelegate implementation. | 41 // CardUnmaskDelegate implementation. |
| 40 void OnUnmaskResponse(const UnmaskResponse& response) override { | 42 void OnUnmaskResponse(const UnmaskResponse& response) override { |
| 41 response_ = response; | 43 response_ = response; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 80 |
| 79 using CardUnmaskPromptControllerImpl::view; | 81 using CardUnmaskPromptControllerImpl::view; |
| 80 | 82 |
| 81 private: | 83 private: |
| 82 scoped_refptr<content::MessageLoopRunner> runner_; | 84 scoped_refptr<content::MessageLoopRunner> runner_; |
| 83 base::WeakPtrFactory<TestCardUnmaskPromptController> weak_factory_; | 85 base::WeakPtrFactory<TestCardUnmaskPromptController> weak_factory_; |
| 84 | 86 |
| 85 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskPromptController); | 87 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskPromptController); |
| 86 }; | 88 }; |
| 87 | 89 |
| 88 class CardUnmaskPromptViewBrowserTest : public InProcessBrowserTest { | 90 class CardUnmaskPromptViewBrowserTest : public DialogBrowserTest { |
| 89 public: | 91 public: |
| 90 CardUnmaskPromptViewBrowserTest() : InProcessBrowserTest() {} | 92 CardUnmaskPromptViewBrowserTest() {} |
| 91 | 93 |
| 92 ~CardUnmaskPromptViewBrowserTest() override {} | 94 ~CardUnmaskPromptViewBrowserTest() override {} |
| 93 | 95 |
| 96 // DialogBrowserTest: |
| 94 void SetUpOnMainThread() override { | 97 void SetUpOnMainThread() override { |
| 95 runner_ = new content::MessageLoopRunner; | 98 runner_ = new content::MessageLoopRunner; |
| 96 contents_ = browser()->tab_strip_model()->GetActiveWebContents(); | 99 contents_ = browser()->tab_strip_model()->GetActiveWebContents(); |
| 97 controller_.reset(new TestCardUnmaskPromptController(contents_, runner_)); | 100 controller_.reset(new TestCardUnmaskPromptController(contents_, runner_)); |
| 98 delegate_.reset(new TestCardUnmaskDelegate()); | 101 delegate_.reset(new TestCardUnmaskDelegate()); |
| 99 } | 102 } |
| 100 | 103 |
| 101 void ShowUI(CreditCardExpiry expired) { | 104 void ShowDialog(const std::string& name) override { |
| 102 CardUnmaskPromptView* dialog = | 105 CardUnmaskPromptView* dialog = |
| 103 CreateCardUnmaskPromptView(controller(), contents()); | 106 CreateCardUnmaskPromptView(controller(), contents()); |
| 104 CreditCard card = (expired == CreditCardExpiry::EXPIRED) | 107 EXPECT_TRUE(name == kExpiryExpired || name == kExpiryValid); |
| 108 CreditCard card = (name == kExpiryExpired) |
| 105 ? test::GetMaskedServerCard() | 109 ? test::GetMaskedServerCard() |
| 106 : test::GetMaskedServerCardAmex(); | 110 : test::GetMaskedServerCardAmex(); |
| 107 controller()->ShowPrompt(dialog, card, AutofillClient::UNMASK_FOR_AUTOFILL, | 111 controller()->ShowPrompt(dialog, card, AutofillClient::UNMASK_FOR_AUTOFILL, |
| 108 delegate()->GetWeakPtr()); | 112 delegate()->GetWeakPtr()); |
| 109 } | 113 } |
| 110 | 114 |
| 111 void FreeDelegate() { delegate_.reset(); } | 115 void FreeDelegate() { delegate_.reset(); } |
| 112 | 116 |
| 113 content::WebContents* contents() { return contents_; } | 117 content::WebContents* contents() { return contents_; } |
| 114 TestCardUnmaskPromptController* controller() { return controller_.get(); } | 118 TestCardUnmaskPromptController* controller() { return controller_.get(); } |
| 115 TestCardUnmaskDelegate* delegate() { return delegate_.get(); } | 119 TestCardUnmaskDelegate* delegate() { return delegate_.get(); } |
| 116 | 120 |
| 117 protected: | 121 protected: |
| 118 // This member must outlive the controller. | 122 // This member must outlive the controller. |
| 119 scoped_refptr<content::MessageLoopRunner> runner_; | 123 scoped_refptr<content::MessageLoopRunner> runner_; |
| 120 | 124 |
| 121 private: | 125 private: |
| 122 content::WebContents* contents_; | 126 content::WebContents* contents_; |
| 123 std::unique_ptr<TestCardUnmaskPromptController> controller_; | 127 std::unique_ptr<TestCardUnmaskPromptController> controller_; |
| 124 std::unique_ptr<TestCardUnmaskDelegate> delegate_; | 128 std::unique_ptr<TestCardUnmaskDelegate> delegate_; |
| 125 | 129 |
| 126 DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViewBrowserTest); | 130 DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViewBrowserTest); |
| 127 }; | 131 }; |
| 128 | 132 |
| 129 // Permanently disabled test used to invoke the UI for the card unmask prompt | 133 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, InvokeDialog_expired) { |
| 130 // with an expired credit card, which shows additional month/year controls. | 134 RunDialog(); |
| 131 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, | |
| 132 DISABLED_InvokeExpired) { | |
| 133 ShowUI(CreditCardExpiry::EXPIRED); | |
| 134 ::test::RunTestInteractively(); | |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Permanently disabled test used to invoke the UI for the card unmask prompt | 137 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, InvokeDialog_valid) { |
| 138 // with a valid credit card, which only shows the CCV Textfield. | 138 RunDialog(); |
| 139 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, DISABLED_InvokeValid) { | |
| 140 ShowUI(CreditCardExpiry::VALID); | |
| 141 ::test::RunTestInteractively(); | |
| 142 } | 139 } |
| 143 | 140 |
| 144 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, DisplayUI) { | 141 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, DisplayUI) { |
| 145 ShowUI(CreditCardExpiry::EXPIRED); | 142 ShowDialog(kExpiryExpired); |
| 146 } | 143 } |
| 147 | 144 |
| 148 // TODO(bondd): bring up on Mac. | 145 // TODO(bondd): bring up on Mac. |
| 149 #if !defined(OS_MACOSX) | 146 #if !defined(OS_MACOSX) |
| 150 // Makes sure the user can close the dialog while the verification success | 147 // Makes sure the user can close the dialog while the verification success |
| 151 // message is showing. | 148 // message is showing. |
| 152 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, | 149 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, |
| 153 EarlyCloseAfterSuccess) { | 150 EarlyCloseAfterSuccess) { |
| 154 ShowUI(CreditCardExpiry::EXPIRED); | 151 ShowDialog(kExpiryExpired); |
| 155 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | 152 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), |
| 156 base::ASCIIToUTF16("10"), | 153 base::ASCIIToUTF16("10"), |
| 157 base::ASCIIToUTF16("19"), false); | 154 base::ASCIIToUTF16("19"), false); |
| 158 EXPECT_EQ(base::ASCIIToUTF16("123"), delegate()->response().cvc); | 155 EXPECT_EQ(base::ASCIIToUTF16("123"), delegate()->response().cvc); |
| 159 controller()->OnVerificationResult(AutofillClient::SUCCESS); | 156 controller()->OnVerificationResult(AutofillClient::SUCCESS); |
| 160 | 157 |
| 161 // Simulate the user clicking [x] before the "Success!" message disappears. | 158 // Simulate the user clicking [x] before the "Success!" message disappears. |
| 162 CardUnmaskPromptViewTester::For(controller()->view())->Close(); | 159 CardUnmaskPromptViewTester::For(controller()->view())->Close(); |
| 163 // Wait a little while; there should be no crash. | 160 // Wait a little while; there should be no crash. |
| 164 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 161 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 165 FROM_HERE, base::Bind(&content::MessageLoopRunner::Quit, | 162 FROM_HERE, base::Bind(&content::MessageLoopRunner::Quit, |
| 166 base::Unretained(runner_.get())), | 163 base::Unretained(runner_.get())), |
| 167 2 * controller()->GetSuccessMessageDuration()); | 164 2 * controller()->GetSuccessMessageDuration()); |
| 168 runner_->Run(); | 165 runner_->Run(); |
| 169 } | 166 } |
| 170 #endif | 167 #endif |
| 171 | 168 |
| 172 // Makes sure the tab can be closed while the dialog is showing. | 169 // Makes sure the tab can be closed while the dialog is showing. |
| 173 // https://crbug.com/484376 | 170 // https://crbug.com/484376 |
| 174 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, | 171 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, |
| 175 CloseTabWhileDialogShowing) { | 172 CloseTabWhileDialogShowing) { |
| 176 ShowUI(CreditCardExpiry::EXPIRED); | 173 ShowDialog(kExpiryExpired); |
| 177 // Simulate AutofillManager (the delegate in production code) being destroyed | 174 // Simulate AutofillManager (the delegate in production code) being destroyed |
| 178 // before CardUnmaskPromptViewBridge::OnConstrainedWindowClosed() is called. | 175 // before CardUnmaskPromptViewBridge::OnConstrainedWindowClosed() is called. |
| 179 FreeDelegate(); | 176 FreeDelegate(); |
| 180 browser()->tab_strip_model()->GetActiveWebContents()->Close(); | 177 browser()->tab_strip_model()->GetActiveWebContents()->Close(); |
| 181 | 178 |
| 182 content::RunAllPendingInMessageLoop(); | 179 content::RunAllPendingInMessageLoop(); |
| 183 } | 180 } |
| 184 | 181 |
| 185 } // namespace | 182 } // namespace |
| 186 | 183 |
| 187 } // namespace autofill | 184 } // namespace autofill |
| OLD | NEW |