OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "chrome/browser/browser_process.h" | 6 #include "chrome/browser/browser_process.h" |
7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/browser/ui/browser_tabstrip.h" | 8 #include "chrome/browser/ui/browser_tabstrip.h" |
9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
11 #include "chrome/test/base/in_process_browser_test.h" | 11 #include "chrome/test/base/in_process_browser_test.h" |
12 #include "chrome/test/base/testing_pref_service_syncable.h" | 12 #include "chrome/test/base/testing_pref_service_syncable.h" |
13 #include "components/autofill/content/browser/content_autofill_driver.h" | 13 #include "components/autofill/content/browser/content_autofill_driver.h" |
14 #include "components/autofill/core/browser/autofill_manager.h" | 14 #include "components/autofill/core/browser/autofill_manager.h" |
15 #include "components/autofill/core/browser/test_autofill_manager_delegate.h" | 15 #include "components/autofill/core/browser/test_autofill_client.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/page_navigator.h" | 17 #include "content/public/browser/page_navigator.h" |
18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
19 #include "content/public/browser/web_contents_observer.h" | 19 #include "content/public/browser/web_contents_observer.h" |
20 #include "content/public/common/url_constants.h" | 20 #include "content/public/common/url_constants.h" |
21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "ui/gfx/rect.h" | 24 #include "ui/gfx/rect.h" |
25 | 25 |
26 namespace autofill { | 26 namespace autofill { |
27 namespace { | 27 namespace { |
28 | 28 |
29 class MockAutofillManagerDelegate | 29 class MockAutofillClient : public TestAutofillClient { |
30 : public autofill::TestAutofillManagerDelegate { | |
31 public: | 30 public: |
32 MockAutofillManagerDelegate() {} | 31 MockAutofillClient() {} |
33 virtual ~MockAutofillManagerDelegate() {} | 32 virtual ~MockAutofillClient() {} |
34 | 33 |
35 virtual PrefService* GetPrefs() { return &prefs_; } | 34 virtual PrefService* GetPrefs() { return &prefs_; } |
36 | 35 |
37 user_prefs::PrefRegistrySyncable* GetPrefRegistry() { | 36 user_prefs::PrefRegistrySyncable* GetPrefRegistry() { |
38 return prefs_.registry(); | 37 return prefs_.registry(); |
39 } | 38 } |
40 | 39 |
41 MOCK_METHOD7(ShowAutofillPopup, | 40 MOCK_METHOD7(ShowAutofillPopup, |
42 void(const gfx::RectF& element_bounds, | 41 void(const gfx::RectF& element_bounds, |
43 base::i18n::TextDirection text_direction, | 42 base::i18n::TextDirection text_direction, |
44 const std::vector<base::string16>& values, | 43 const std::vector<base::string16>& values, |
45 const std::vector<base::string16>& labels, | 44 const std::vector<base::string16>& labels, |
46 const std::vector<base::string16>& icons, | 45 const std::vector<base::string16>& icons, |
47 const std::vector<int>& identifiers, | 46 const std::vector<int>& identifiers, |
48 base::WeakPtr<AutofillPopupDelegate> delegate)); | 47 base::WeakPtr<AutofillPopupDelegate> delegate)); |
49 | 48 |
50 MOCK_METHOD0(HideAutofillPopup, void()); | 49 MOCK_METHOD0(HideAutofillPopup, void()); |
51 | 50 |
52 private: | 51 private: |
53 TestingPrefServiceSyncable prefs_; | 52 TestingPrefServiceSyncable prefs_; |
54 | 53 |
55 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); | 54 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient); |
56 }; | 55 }; |
57 | 56 |
58 // Subclass ContentAutofillDriver so we can create an ContentAutofillDriver | 57 // Subclass ContentAutofillDriver so we can create an ContentAutofillDriver |
59 // instance. | 58 // instance. |
60 class TestContentAutofillDriver : public ContentAutofillDriver { | 59 class TestContentAutofillDriver : public ContentAutofillDriver { |
61 public: | 60 public: |
62 TestContentAutofillDriver(content::WebContents* web_contents, | 61 TestContentAutofillDriver(content::WebContents* web_contents, |
63 AutofillManagerDelegate* delegate) | 62 AutofillClient* client) |
64 : ContentAutofillDriver( | 63 : ContentAutofillDriver( |
65 web_contents, | 64 web_contents, |
66 delegate, | 65 client, |
67 g_browser_process->GetApplicationLocale(), | 66 g_browser_process->GetApplicationLocale(), |
68 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {} | 67 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {} |
69 virtual ~TestContentAutofillDriver() {} | 68 virtual ~TestContentAutofillDriver() {} |
70 | 69 |
71 private: | 70 private: |
72 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver); | 71 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver); |
73 }; | 72 }; |
74 | 73 |
75 } // namespace | 74 } // namespace |
76 | 75 |
77 class ContentAutofillDriverBrowserTest : public InProcessBrowserTest, | 76 class ContentAutofillDriverBrowserTest : public InProcessBrowserTest, |
78 public content::WebContentsObserver { | 77 public content::WebContentsObserver { |
79 public: | 78 public: |
80 ContentAutofillDriverBrowserTest() {} | 79 ContentAutofillDriverBrowserTest() {} |
81 virtual ~ContentAutofillDriverBrowserTest() {} | 80 virtual ~ContentAutofillDriverBrowserTest() {} |
82 | 81 |
83 virtual void SetUpOnMainThread() OVERRIDE { | 82 virtual void SetUpOnMainThread() OVERRIDE { |
84 content::WebContents* web_contents = | 83 content::WebContents* web_contents = |
85 browser()->tab_strip_model()->GetActiveWebContents(); | 84 browser()->tab_strip_model()->GetActiveWebContents(); |
86 ASSERT_TRUE(web_contents != NULL); | 85 ASSERT_TRUE(web_contents != NULL); |
87 Observe(web_contents); | 86 Observe(web_contents); |
88 AutofillManager::RegisterProfilePrefs(manager_delegate_.GetPrefRegistry()); | 87 AutofillManager::RegisterProfilePrefs(autofill_client_.GetPrefRegistry()); |
89 | 88 |
90 autofill_driver_.reset( | 89 autofill_driver_.reset( |
91 new TestContentAutofillDriver(web_contents, &manager_delegate_)); | 90 new TestContentAutofillDriver(web_contents, &autofill_client_)); |
92 } | 91 } |
93 | 92 |
94 // Normally the WebContents will automatically delete the driver, but here | 93 // Normally the WebContents will automatically delete the driver, but here |
95 // the driver is owned by this test, so we have to manually destroy. | 94 // the driver is owned by this test, so we have to manually destroy. |
96 virtual void WebContentsDestroyed() OVERRIDE { | 95 virtual void WebContentsDestroyed() OVERRIDE { |
97 autofill_driver_.reset(); | 96 autofill_driver_.reset(); |
98 } | 97 } |
99 | 98 |
100 virtual void WasHidden() OVERRIDE { | 99 virtual void WasHidden() OVERRIDE { |
101 if (!web_contents_hidden_callback_.is_null()) | 100 if (!web_contents_hidden_callback_.is_null()) |
102 web_contents_hidden_callback_.Run(); | 101 web_contents_hidden_callback_.Run(); |
103 } | 102 } |
104 | 103 |
105 virtual void NavigationEntryCommitted( | 104 virtual void NavigationEntryCommitted( |
106 const content::LoadCommittedDetails& load_details) OVERRIDE { | 105 const content::LoadCommittedDetails& load_details) OVERRIDE { |
107 if (!nav_entry_committed_callback_.is_null()) | 106 if (!nav_entry_committed_callback_.is_null()) |
108 nav_entry_committed_callback_.Run(); | 107 nav_entry_committed_callback_.Run(); |
109 } | 108 } |
110 | 109 |
111 protected: | 110 protected: |
112 content::WebContents* web_contents_; | 111 content::WebContents* web_contents_; |
113 | 112 |
114 base::Closure web_contents_hidden_callback_; | 113 base::Closure web_contents_hidden_callback_; |
115 base::Closure nav_entry_committed_callback_; | 114 base::Closure nav_entry_committed_callback_; |
116 | 115 |
117 testing::NiceMock<MockAutofillManagerDelegate> manager_delegate_; | 116 testing::NiceMock<MockAutofillClient> autofill_client_; |
118 scoped_ptr<TestContentAutofillDriver> autofill_driver_; | 117 scoped_ptr<TestContentAutofillDriver> autofill_driver_; |
119 }; | 118 }; |
120 | 119 |
121 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest, | 120 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest, |
122 SwitchTabAndHideAutofillPopup) { | 121 SwitchTabAndHideAutofillPopup) { |
123 // Notification is different on platforms. On linux this will be called twice, | 122 // Notification is different on platforms. On linux this will be called twice, |
124 // while on windows only once. | 123 // while on windows only once. |
125 EXPECT_CALL(manager_delegate_, HideAutofillPopup()) | 124 EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(testing::AtLeast(1)); |
126 .Times(testing::AtLeast(1)); | |
127 | 125 |
128 scoped_refptr<content::MessageLoopRunner> runner = | 126 scoped_refptr<content::MessageLoopRunner> runner = |
129 new content::MessageLoopRunner; | 127 new content::MessageLoopRunner; |
130 web_contents_hidden_callback_ = runner->QuitClosure(); | 128 web_contents_hidden_callback_ = runner->QuitClosure(); |
131 chrome::AddSelectedTabWithURL(browser(), | 129 chrome::AddSelectedTabWithURL(browser(), |
132 GURL(content::kAboutBlankURL), | 130 GURL(content::kAboutBlankURL), |
133 content::PAGE_TRANSITION_AUTO_TOPLEVEL); | 131 content::PAGE_TRANSITION_AUTO_TOPLEVEL); |
134 runner->Run(); | 132 runner->Run(); |
135 web_contents_hidden_callback_.Reset(); | 133 web_contents_hidden_callback_.Reset(); |
136 } | 134 } |
137 | 135 |
138 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest, | 136 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest, |
139 TestPageNavigationHidingAutofillPopup) { | 137 TestPageNavigationHidingAutofillPopup) { |
140 // Notification is different on platforms. On linux this will be called twice, | 138 // Notification is different on platforms. On linux this will be called twice, |
141 // while on windows only once. | 139 // while on windows only once. |
142 EXPECT_CALL(manager_delegate_, HideAutofillPopup()) | 140 EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(testing::AtLeast(1)); |
143 .Times(testing::AtLeast(1)); | |
144 | 141 |
145 scoped_refptr<content::MessageLoopRunner> runner = | 142 scoped_refptr<content::MessageLoopRunner> runner = |
146 new content::MessageLoopRunner; | 143 new content::MessageLoopRunner; |
147 nav_entry_committed_callback_ = runner->QuitClosure(); | 144 nav_entry_committed_callback_ = runner->QuitClosure(); |
148 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIBookmarksURL), | 145 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIBookmarksURL), |
149 content::Referrer(), | 146 content::Referrer(), |
150 CURRENT_TAB, | 147 CURRENT_TAB, |
151 content::PAGE_TRANSITION_TYPED, | 148 content::PAGE_TRANSITION_TYPED, |
152 false)); | 149 false)); |
153 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIAboutURL), | 150 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIAboutURL), |
154 content::Referrer(), | 151 content::Referrer(), |
155 CURRENT_TAB, | 152 CURRENT_TAB, |
156 content::PAGE_TRANSITION_TYPED, | 153 content::PAGE_TRANSITION_TYPED, |
157 false)); | 154 false)); |
158 runner->Run(); | 155 runner->Run(); |
159 nav_entry_committed_callback_.Reset(); | 156 nav_entry_committed_callback_.Reset(); |
160 } | 157 } |
161 | 158 |
162 } // namespace autofill | 159 } // namespace autofill |
OLD | NEW |