OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/guid.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "chrome/browser/ui/autofill/card_unmask_prompt_controller_impl.h" | |
8 #include "chrome/browser/ui/autofill/card_unmask_prompt_view_tester.h" | |
9 #include "chrome/browser/ui/browser.h" | |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
11 #include "chrome/test/base/in_process_browser_test.h" | |
12 #include "components/autofill/core/browser/card_unmask_delegate.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "content/public/test/test_utils.h" | |
15 | |
16 namespace autofill { | |
17 | |
18 namespace { | |
19 | |
20 class TestCardUnmaskDelegate : public CardUnmaskDelegate { | |
21 public: | |
22 TestCardUnmaskDelegate() : weak_factory_(this) {} | |
23 | |
24 virtual ~TestCardUnmaskDelegate() {} | |
bondd
2015/02/24 00:24:33
scoped_ptr complains if destructor is non-virtual.
| |
25 | |
26 // CardUnmaskDelegate implementation. | |
27 void OnUnmaskResponse(const UnmaskResponse& response) override {} | |
28 void OnUnmaskPromptClosed() override {} | |
29 | |
30 base::WeakPtr<TestCardUnmaskDelegate> GetWeakPtr() { | |
31 return weak_factory_.GetWeakPtr(); | |
32 } | |
33 | |
34 private: | |
35 base::WeakPtrFactory<TestCardUnmaskDelegate> weak_factory_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskDelegate); | |
38 }; | |
39 | |
40 class TestCardUnmaskPromptController : public CardUnmaskPromptControllerImpl { | |
41 public: | |
42 TestCardUnmaskPromptController( | |
43 content::WebContents* contents, | |
44 scoped_refptr<content::MessageLoopRunner> runner) | |
45 : CardUnmaskPromptControllerImpl(contents), | |
46 runner_(runner), | |
47 weak_factory_(this) {} | |
48 | |
49 // CardUnmaskPromptControllerImpl implementation. | |
50 void OnDidLoadRiskFingerprint(const std::string& risk_data) override { | |
51 CardUnmaskPromptControllerImpl::OnDidLoadRiskFingerprint(risk_data); | |
52 | |
53 // Call Quit() from here rather than from OnUnmaskDialogClosed(). | |
54 // FingerprintDataLoader starts several async tasks that take a while to | |
55 // complete. If Quit() is called before FingerprintDataLoader is all done | |
56 // then LeakTracker will detect that some resources have not been freed | |
57 // and cause the browser test to fail. This is not a real leak though - | |
58 // normally the async tasks would complete and encounter weak callbacks. | |
59 runner_->Quit(); | |
60 } | |
61 | |
62 void RunMessageLoop() { runner_->Run(); } | |
63 | |
64 scoped_ptr<CardUnmaskPromptViewTester> GetViewTester() { | |
65 return CardUnmaskPromptViewTester::For(view()).Pass(); | |
66 } | |
67 | |
68 base::WeakPtr<TestCardUnmaskPromptController> GetWeakPtr() { | |
69 return weak_factory_.GetWeakPtr(); | |
70 } | |
71 | |
72 private: | |
73 scoped_refptr<content::MessageLoopRunner> runner_; | |
74 base::WeakPtrFactory<TestCardUnmaskPromptController> weak_factory_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskPromptController); | |
77 }; | |
78 | |
79 class CardUnmaskPromptViewBrowserTest : public InProcessBrowserTest { | |
80 public: | |
81 CardUnmaskPromptViewBrowserTest() : InProcessBrowserTest() {} | |
82 | |
83 ~CardUnmaskPromptViewBrowserTest() override {} | |
84 | |
85 void SetUpOnMainThread() override { | |
86 runner_ = new content::MessageLoopRunner; | |
87 controller_.reset(new TestCardUnmaskPromptController( | |
88 browser()->tab_strip_model()->GetActiveWebContents(), runner_)); | |
89 delegate_.reset(new TestCardUnmaskDelegate()); | |
90 } | |
91 | |
92 base::WeakPtr<TestCardUnmaskPromptController> controller() { | |
93 return controller_->GetWeakPtr(); | |
94 } | |
95 | |
96 base::WeakPtr<TestCardUnmaskDelegate> delegate() { | |
97 return delegate_->GetWeakPtr(); | |
98 } | |
bondd
2015/02/24 00:24:33
Made these WeakPtrs to automatically detect if sta
Evan Stade
2015/02/24 22:19:22
not sure I follow, the lifetime of |controller_| a
bondd
2015/02/24 23:51:52
Yep you're right. Fixed.
| |
99 | |
100 private: | |
101 // This member must outlive the controller. | |
102 scoped_refptr<content::MessageLoopRunner> runner_; | |
103 | |
104 scoped_ptr<TestCardUnmaskPromptController> controller_; | |
bondd
2015/02/24 00:24:33
Comment "The controller owns itself" was wrong; fi
| |
105 scoped_ptr<TestCardUnmaskDelegate> delegate_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViewBrowserTest); | |
108 }; | |
109 | |
110 IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, DisplayUI) { | |
111 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/"); | |
112 | |
113 controller()->ShowPrompt(credit_card, delegate()); | |
114 controller()->RunMessageLoop(); | |
115 } | |
116 | |
117 } // namespace | |
118 | |
119 } // namespace autofill | |
OLD | NEW |