OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/password_manager/mock_password_store.h" | 10 #include "chrome/browser/password_manager/mock_password_store.h" |
11 #include "chrome/browser/password_manager/password_manager.h" | 11 #include "chrome/browser/password_manager/password_manager.h" |
12 #include "chrome/browser/password_manager/password_manager_delegate.h" | 12 #include "chrome/browser/password_manager/password_manager_delegate.h" |
13 #include "chrome/browser/password_manager/password_manager_driver.h" | 13 #include "chrome/browser/password_manager/password_manager_driver.h" |
14 #include "chrome/browser/password_manager/password_store.h" | 14 #include "chrome/browser/password_manager/password_store.h" |
15 #include "chrome/browser/password_manager/password_store_factory.h" | 15 #include "chrome/browser/password_manager/password_store_factory.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
19 #include "chrome/test/base/testing_pref_service_syncable.h" | 19 #include "chrome/test/base/testing_pref_service_syncable.h" |
20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
21 #include "content/public/browser/navigation_details.h" | 21 #include "content/public/browser/navigation_details.h" |
22 #include "content/public/common/frame_navigate_params.h" | 22 #include "content/public/common/frame_navigate_params.h" |
23 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
| 27 class PasswordGenerationManager; |
| 28 |
27 using autofill::PasswordForm; | 29 using autofill::PasswordForm; |
28 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
29 using testing::_; | 31 using testing::_; |
30 using testing::DoAll; | 32 using testing::DoAll; |
31 using testing::Exactly; | 33 using testing::Exactly; |
32 using testing::Return; | 34 using testing::Return; |
33 using testing::WithArg; | 35 using testing::WithArg; |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 class MockPasswordManagerDelegate : public PasswordManagerDelegate { | 39 class MockPasswordManagerDelegate : public PasswordManagerDelegate { |
38 public: | 40 public: |
39 MOCK_METHOD1(AddSavePasswordInfoBarIfPermitted, void(PasswordFormManager*)); | 41 MOCK_METHOD1(AddSavePasswordInfoBarIfPermitted, void(PasswordFormManager*)); |
40 MOCK_METHOD0(GetProfile, Profile*()); | 42 MOCK_METHOD0(GetProfile, Profile*()); |
41 MOCK_METHOD0(GetDriver, PasswordManagerDriver*()); | 43 MOCK_METHOD0(GetDriver, PasswordManagerDriver*()); |
42 }; | 44 }; |
43 | 45 |
44 class MockPasswordManagerDriver : public PasswordManagerDriver { | 46 class MockPasswordManagerDriver : public PasswordManagerDriver { |
45 public: | 47 public: |
46 MOCK_METHOD1(FillPasswordForm, void(const autofill::PasswordFormFillData&)); | 48 MOCK_METHOD1(FillPasswordForm, void(const autofill::PasswordFormFillData&)); |
47 MOCK_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); | 49 MOCK_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); |
| 50 MOCK_METHOD0(GetPasswordGenerationManager, PasswordGenerationManager*()); |
| 51 MOCK_METHOD0(GetPasswordManager, PasswordManager*()); |
48 }; | 52 }; |
49 | 53 |
50 ACTION_P(InvokeConsumer, forms) { | 54 ACTION_P(InvokeConsumer, forms) { |
51 arg0->OnGetPasswordStoreResults(forms); | 55 arg0->OnGetPasswordStoreResults(forms); |
52 } | 56 } |
53 | 57 |
54 ACTION_P(SaveToScopedPtr, scoped) { | 58 ACTION_P(SaveToScopedPtr, scoped) { |
55 scoped->reset(arg0); | 59 scoped->reset(arg0); |
56 } | 60 } |
57 | 61 |
58 class TestPasswordManager : public PasswordManager { | 62 class TestPasswordManager : public PasswordManager { |
59 public: | 63 public: |
60 TestPasswordManager(content::WebContents* contents, | 64 TestPasswordManager(content::WebContents* contents, |
61 PasswordManagerDelegate* delegate) | 65 PasswordManagerDelegate* delegate) |
62 : PasswordManager(contents, delegate) {} | 66 : PasswordManager(contents, delegate) {} |
63 virtual ~TestPasswordManager() {} | 67 virtual ~TestPasswordManager() {} |
64 | 68 |
65 virtual void OnPasswordFormSubmitted(const PasswordForm& form) OVERRIDE { | 69 virtual void OnPasswordFormSubmitted(const PasswordForm& form) OVERRIDE { |
66 PasswordManager::OnPasswordFormSubmitted(form); | 70 PasswordManager::OnPasswordFormSubmitted(form); |
67 } | 71 } |
68 | 72 |
69 static TestPasswordManager* CreateForWebContentsAndDelegate( | |
70 content::WebContents* contents, | |
71 PasswordManagerDelegate* delegate) { | |
72 TestPasswordManager* tpm = new TestPasswordManager(contents, delegate); | |
73 contents->SetUserData(UserDataKey(), tpm); | |
74 return tpm; | |
75 } | |
76 | |
77 private: | 73 private: |
78 DISALLOW_COPY_AND_ASSIGN(TestPasswordManager); | 74 DISALLOW_COPY_AND_ASSIGN(TestPasswordManager); |
79 }; | 75 }; |
80 | 76 |
81 } // namespace | 77 } // namespace |
82 | 78 |
83 class PasswordManagerTest : public ChromeRenderViewHostTestHarness { | 79 class PasswordManagerTest : public ChromeRenderViewHostTestHarness { |
84 protected: | 80 protected: |
85 virtual void SetUp() { | 81 virtual void SetUp() { |
86 ChromeRenderViewHostTestHarness::SetUp(); | 82 ChromeRenderViewHostTestHarness::SetUp(); |
87 store_ = static_cast<MockPasswordStore*>( | 83 store_ = static_cast<MockPasswordStore*>( |
88 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( | 84 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( |
89 profile(), MockPasswordStore::Build).get()); | 85 profile(), MockPasswordStore::Build).get()); |
90 | 86 |
91 EXPECT_CALL(delegate_, GetProfile()).WillRepeatedly(Return(profile())); | 87 EXPECT_CALL(delegate_, GetProfile()).WillRepeatedly(Return(profile())); |
92 EXPECT_CALL(delegate_, GetDriver()).WillRepeatedly(Return(&driver_)); | 88 EXPECT_CALL(delegate_, GetDriver()).WillRepeatedly(Return(&driver_)); |
93 manager_ = TestPasswordManager::CreateForWebContentsAndDelegate( | 89 |
94 web_contents(), &delegate_); | 90 manager_.reset(new TestPasswordManager(web_contents(), &delegate_)); |
| 91 |
95 EXPECT_CALL(driver_, DidLastPageLoadEncounterSSLErrors()) | 92 EXPECT_CALL(driver_, DidLastPageLoadEncounterSSLErrors()) |
96 .WillRepeatedly(Return(false)); | 93 .WillRepeatedly(Return(false)); |
| 94 EXPECT_CALL(driver_, GetPasswordGenerationManager()) |
| 95 .WillRepeatedly(Return(static_cast<PasswordGenerationManager*>(NULL))); |
| 96 EXPECT_CALL(driver_, GetPasswordManager()) |
| 97 .WillRepeatedly(Return(manager_.get())); |
97 } | 98 } |
98 | 99 |
99 virtual void TearDown() { | 100 virtual void TearDown() { |
100 store_ = NULL; | 101 store_ = NULL; |
| 102 |
| 103 // Destroy the PasswordManager before tearing down the Profile to avoid |
| 104 // crashes due to prefs accesses. |
| 105 manager_.reset(); |
101 ChromeRenderViewHostTestHarness::TearDown(); | 106 ChromeRenderViewHostTestHarness::TearDown(); |
102 } | 107 } |
103 | 108 |
104 PasswordForm MakeSimpleForm() { | 109 PasswordForm MakeSimpleForm() { |
105 PasswordForm form; | 110 PasswordForm form; |
106 form.origin = GURL("http://www.google.com/a/LoginAuth"); | 111 form.origin = GURL("http://www.google.com/a/LoginAuth"); |
107 form.action = GURL("http://www.google.com/a/Login"); | 112 form.action = GURL("http://www.google.com/a/Login"); |
108 form.username_element = ASCIIToUTF16("Email"); | 113 form.username_element = ASCIIToUTF16("Email"); |
109 form.password_element = ASCIIToUTF16("Passwd"); | 114 form.password_element = ASCIIToUTF16("Passwd"); |
110 form.username_value = ASCIIToUTF16("google"); | 115 form.username_value = ASCIIToUTF16("google"); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 return false; | 168 return false; |
164 if (lhs.password_autocomplete_set != rhs.password_autocomplete_set) | 169 if (lhs.password_autocomplete_set != rhs.password_autocomplete_set) |
165 return false; | 170 return false; |
166 if (lhs.submit_element != rhs.submit_element) | 171 if (lhs.submit_element != rhs.submit_element) |
167 return false; | 172 return false; |
168 if (lhs.signon_realm != rhs.signon_realm) | 173 if (lhs.signon_realm != rhs.signon_realm) |
169 return false; | 174 return false; |
170 return true; | 175 return true; |
171 } | 176 } |
172 | 177 |
173 TestPasswordManager* manager() { | 178 TestPasswordManager* manager() { return manager_.get(); } |
174 return manager_; | |
175 } | |
176 | 179 |
177 void OnPasswordFormSubmitted(const autofill::PasswordForm& form) { | 180 void OnPasswordFormSubmitted(const autofill::PasswordForm& form) { |
178 manager()->OnPasswordFormSubmitted(form); | 181 manager()->OnPasswordFormSubmitted(form); |
179 } | 182 } |
180 | 183 |
181 PasswordManager::PasswordSubmittedCallback SubmissionCallback() { | 184 PasswordManager::PasswordSubmittedCallback SubmissionCallback() { |
182 return base::Bind(&PasswordManagerTest::FormSubmitted, | 185 return base::Bind(&PasswordManagerTest::FormSubmitted, |
183 base::Unretained(this)); | 186 base::Unretained(this)); |
184 } | 187 } |
185 | 188 |
186 void FormSubmitted(const autofill::PasswordForm& form) { | 189 void FormSubmitted(const autofill::PasswordForm& form) { |
187 submitted_form_ = form; | 190 submitted_form_ = form; |
188 } | 191 } |
189 | 192 |
190 scoped_refptr<MockPasswordStore> store_; | 193 scoped_refptr<MockPasswordStore> store_; |
191 TestPasswordManager* manager_; | 194 MockPasswordManagerDelegate delegate_; |
192 MockPasswordManagerDelegate delegate_; // Owned by manager_. | |
193 MockPasswordManagerDriver driver_; | 195 MockPasswordManagerDriver driver_; |
| 196 scoped_ptr<TestPasswordManager> manager_; |
194 PasswordForm submitted_form_; | 197 PasswordForm submitted_form_; |
195 }; | 198 }; |
196 | 199 |
197 MATCHER_P(FormMatches, form, "") { | 200 MATCHER_P(FormMatches, form, "") { |
198 return form.signon_realm == arg.signon_realm && | 201 return form.signon_realm == arg.signon_realm && |
199 form.origin == arg.origin && | 202 form.origin == arg.origin && |
200 form.action == arg.action && | 203 form.action == arg.action && |
201 form.username_element == arg.username_element && | 204 form.username_element == arg.username_element && |
202 form.password_element == arg.password_element && | 205 form.password_element == arg.password_element && |
203 form.password_autocomplete_set == | 206 form.password_autocomplete_set == |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 manager()->ProvisionallySavePassword(login_form); | 599 manager()->ProvisionallySavePassword(login_form); |
597 | 600 |
598 PasswordForm failed_login_form(MakeTwitterFailedLoginForm()); | 601 PasswordForm failed_login_form(MakeTwitterFailedLoginForm()); |
599 observed.clear(); | 602 observed.clear(); |
600 observed.push_back(failed_login_form); | 603 observed.push_back(failed_login_form); |
601 // A PasswordForm appears, and is visible in the layout: | 604 // A PasswordForm appears, and is visible in the layout: |
602 // No expected calls to the PasswordStore... | 605 // No expected calls to the PasswordStore... |
603 manager()->OnPasswordFormsParsed(observed); | 606 manager()->OnPasswordFormsParsed(observed); |
604 manager()->OnPasswordFormsRendered(observed); | 607 manager()->OnPasswordFormsRendered(observed); |
605 } | 608 } |
OLD | NEW |