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
3 // found in the LICENSE file.
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/autofill/risk_util.h"
10 #include "chrome/browser/ui/autofill/card_unmask_prompt_view_tester.h"
11 #include "chrome/browser/ui/autofill/create_card_unmask_prompt_view.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "components/autofill/core/browser/autofill_test_utils.h"
16 #include "components/autofill/core/browser/card_unmask_delegate.h"
17 #include "components/autofill/core/browser/ui/card_unmask_prompt_controller_impl.h"
18 #include "components/user_prefs/user_prefs.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_utils.h"
26 class TestCardUnmaskDelegate
: public CardUnmaskDelegate
{
28 TestCardUnmaskDelegate() : weak_factory_(this) {}
30 virtual ~TestCardUnmaskDelegate() {}
32 // CardUnmaskDelegate implementation.
33 void OnUnmaskResponse(const UnmaskResponse
& response
) override
{
36 void OnUnmaskPromptClosed() override
{}
38 base::WeakPtr
<TestCardUnmaskDelegate
> GetWeakPtr() {
39 return weak_factory_
.GetWeakPtr();
42 UnmaskResponse
response() { return response_
; }
45 UnmaskResponse response_
;
47 base::WeakPtrFactory
<TestCardUnmaskDelegate
> weak_factory_
;
49 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskDelegate
);
52 class TestCardUnmaskPromptController
: public CardUnmaskPromptControllerImpl
{
54 TestCardUnmaskPromptController(
55 content::WebContents
* contents
,
56 scoped_refptr
<content::MessageLoopRunner
> runner
)
57 : CardUnmaskPromptControllerImpl(base::Bind(&LoadRiskData
, 0, contents
),
58 user_prefs::UserPrefs::Get(contents
->GetBrowserContext()), false),
60 weak_factory_(this) {}
62 // CardUnmaskPromptControllerImpl implementation.
63 base::TimeDelta
GetSuccessMessageDuration() const override
{
64 return base::TimeDelta::FromMilliseconds(10);
67 void LoadRiskFingerprint() override
{
68 OnDidLoadRiskFingerprint("risk_data");
71 base::WeakPtr
<TestCardUnmaskPromptController
> GetWeakPtr() {
72 return weak_factory_
.GetWeakPtr();
75 using CardUnmaskPromptControllerImpl::view
;
78 scoped_refptr
<content::MessageLoopRunner
> runner_
;
79 base::WeakPtrFactory
<TestCardUnmaskPromptController
> weak_factory_
;
81 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskPromptController
);
84 class CardUnmaskPromptViewBrowserTest
: public InProcessBrowserTest
{
86 CardUnmaskPromptViewBrowserTest() : InProcessBrowserTest() {}
88 ~CardUnmaskPromptViewBrowserTest() override
{}
90 void SetUpOnMainThread() override
{
91 runner_
= new content::MessageLoopRunner
;
92 contents_
= browser()->tab_strip_model()->GetActiveWebContents();
93 controller_
.reset(new TestCardUnmaskPromptController(contents_
, runner_
));
94 delegate_
.reset(new TestCardUnmaskDelegate());
97 void FreeDelegate() { delegate_
.reset(); }
99 content::WebContents
* contents() { return contents_
; }
100 TestCardUnmaskPromptController
* controller() { return controller_
.get(); }
101 TestCardUnmaskDelegate
* delegate() { return delegate_
.get(); }
104 // This member must outlive the controller.
105 scoped_refptr
<content::MessageLoopRunner
> runner_
;
108 content::WebContents
* contents_
;
109 scoped_ptr
<TestCardUnmaskPromptController
> controller_
;
110 scoped_ptr
<TestCardUnmaskDelegate
> delegate_
;
112 DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViewBrowserTest
);
115 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest
, DisplayUI
) {
116 controller()->ShowPrompt(CreateCardUnmaskPromptView(controller(), contents()),
117 test::GetMaskedServerCard(),
118 delegate()->GetWeakPtr());
121 // TODO(bondd): bring up on Mac.
122 #if !defined(OS_MACOSX)
123 // Makes sure the user can close the dialog while the verification success
124 // message is showing.
125 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest
,
126 EarlyCloseAfterSuccess
) {
127 controller()->ShowPrompt(CreateCardUnmaskPromptView(controller(), contents()),
128 test::GetMaskedServerCard(),
129 delegate()->GetWeakPtr());
130 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"),
131 base::ASCIIToUTF16("10"),
132 base::ASCIIToUTF16("19"), false);
133 EXPECT_EQ(base::ASCIIToUTF16("123"), delegate()->response().cvc
);
134 controller()->OnVerificationResult(AutofillClient::SUCCESS
);
136 // Simulate the user clicking [x] before the "Success!" message disappears.
137 CardUnmaskPromptViewTester::For(controller()->view())->Close();
138 // Wait a little while; there should be no crash.
139 base::MessageLoop::current()->task_runner()->PostDelayedTask(
140 FROM_HERE
, base::Bind(&content::MessageLoopRunner::Quit
,
141 base::Unretained(runner_
.get())),
142 2 * controller()->GetSuccessMessageDuration());
147 // Makes sure the tab can be closed while the dialog is showing.
148 // https://crbug.com/484376
149 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest
,
150 CloseTabWhileDialogShowing
) {
151 controller()->ShowPrompt(CreateCardUnmaskPromptView(controller(), contents()),
152 test::GetMaskedServerCard(),
153 delegate()->GetWeakPtr());
154 // Simulate AutofillManager (the delegate in production code) being destroyed
155 // before CardUnmaskPromptViewBridge::OnConstrainedWindowClosed() is called.
157 browser()->tab_strip_model()->GetActiveWebContents()->Close();
159 content::RunAllPendingInMessageLoop();
164 } // namespace autofill