OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/metrics/histogram.h" | |
6 #include "base/metrics/statistics_recorder.h" | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "chrome/browser/password_manager/password_store_factory.h" | |
9 #include "chrome/browser/ui/passwords/password_manager_presenter.h" | |
10 #include "chrome/browser/ui/webui/options/password_manager_handler.h" | |
11 #include "chrome/test/base/testing_profile.h" | |
12 #include "components/password_manager/core/browser/mock_password_store.h" | |
13 #include "components/password_manager/core/browser/password_manager_test_utils.h " | |
14 #include "content/public/test/test_browser_thread_bundle.h" | |
15 #include "content/public/test/test_web_ui.h" | |
16 #include "content/public/test/web_contents_tester.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using password_manager::MockPasswordStore; | |
21 | |
22 class MockPasswordManagerPresenter : public PasswordManagerPresenter { | |
23 public: | |
24 explicit MockPasswordManagerPresenter(PasswordUIView* handler) | |
25 : PasswordManagerPresenter(handler) {} | |
26 ~MockPasswordManagerPresenter() override {} | |
27 | |
28 MOCK_METHOD0(IsUserAuthenticated, bool()); | |
29 | |
30 private: | |
31 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); | |
32 }; | |
33 | |
34 class DummyPasswordManagerHandler : public PasswordUIView { | |
35 public: | |
36 explicit DummyPasswordManagerHandler() : password_manager_presenter_(this) { | |
37 PasswordStoreFactory::GetInstance()->SetTestingFactory( | |
38 &profile_, password_manager::BuildPasswordStore<content::BrowserContext, | |
39 MockPasswordStore>); | |
40 password_manager_presenter_.Initialize(); | |
41 } | |
42 ~DummyPasswordManagerHandler() override {} | |
43 Profile* GetProfile() override; | |
44 | |
45 void ShowPassword(size_t, | |
46 const std::string&, | |
47 const std::string&, | |
48 const base::string16&) override {} | |
49 void SetPasswordList(const std::vector<scoped_ptr<autofill::PasswordForm>>&, | |
50 bool) override {} | |
51 void SetPasswordExceptionList( | |
52 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} | |
53 | |
54 #if !defined(OS_ANDROID) | |
55 gfx::NativeWindow GetNativeWindow() const override; | |
56 #endif | |
57 private: | |
58 TestingProfile profile_; | |
59 PasswordManagerPresenter password_manager_presenter_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); | |
62 }; | |
63 | |
64 #if !defined(OS_ANDROID) | |
65 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { | |
66 return NULL; | |
67 } | |
68 #endif | |
69 Profile* DummyPasswordManagerHandler::GetProfile() { | |
70 return &profile_; | |
71 } | |
72 | |
73 class PasswordManagerHandlerTest : public testing::Test { | |
74 protected: | |
75 PasswordManagerHandlerTest() { | |
76 UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", 0); | |
77 UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", 0, | |
78 4); | |
79 PasswordStoreFactory::GetInstance()->SetTestingFactory( | |
80 &profile_, password_manager::BuildPasswordStore<content::BrowserContext, | |
81 MockPasswordStore>); | |
82 dummy_handler_ = new DummyPasswordManagerHandler(); | |
83 presenter_.reset(new MockPasswordManagerPresenter(dummy_handler_)); | |
vabr (Chromium)
2016/03/16 17:48:28
Looks like presenter_ is only used within the cons
xunlu
2016/03/18 21:15:30
Done.
| |
84 presenter_raw_ = presenter_.get(); | |
85 handler_ = new options::PasswordManagerHandler(std::move(presenter_)); | |
86 web_ui_.set_web_contents( | |
87 content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); | |
88 handler_->set_web_ui(&web_ui_); | |
89 } | |
90 ~PasswordManagerHandlerTest() override { | |
91 delete handler_; | |
92 delete dummy_handler_; | |
93 } | |
94 | |
95 MockPasswordStore* GetPasswordStore() { | |
96 return static_cast<MockPasswordStore*>( | |
97 PasswordStoreFactory::GetForProfile(&profile_, | |
98 ServiceAccessType::EXPLICIT_ACCESS) | |
99 .get()); | |
100 } | |
101 | |
102 void ExportPassword() { handler_->HandlePasswordExport(NULL); } | |
103 | |
104 void ImportPassword() { | |
105 autofill::PasswordForm form(MakeForm()); | |
106 std::vector<autofill::PasswordForm> passwordForms; | |
107 passwordForms.push_back(form); | |
108 passwordForms.push_back(form); | |
109 handler_->ImportPasswordFileRead( | |
110 password_manager::PasswordImporter::SUCCESS, passwordForms); | |
111 } | |
112 | |
113 autofill::PasswordForm MakeForm() { | |
114 autofill::PasswordForm form; | |
115 GURL origin("http://test.com"); | |
116 form.origin = origin; | |
117 form.username_element = base::ASCIIToUTF16("Email"); | |
118 form.username_value = base::ASCIIToUTF16("test@test.com"); | |
119 form.password_element = base::ASCIIToUTF16("Password"); | |
120 form.password_value = base::ASCIIToUTF16("abcdef"); | |
121 return form; | |
122 } | |
123 | |
124 PasswordManagerPresenter* presenter_raw_; | |
125 | |
126 private: | |
127 content::TestBrowserThreadBundle thread_bundle_; | |
128 TestingProfile profile_; | |
129 scoped_ptr<PasswordManagerPresenter> presenter_; | |
130 options::PasswordManagerHandler* handler_; | |
131 DummyPasswordManagerHandler* dummy_handler_; | |
132 content::TestWebUI web_ui_; | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); | |
135 }; | |
136 | |
137 MATCHER_P(FormMatches, form, "") { | |
138 return form.signon_realm == arg.signon_realm && form.origin == arg.origin && | |
139 form.action == arg.action && | |
140 form.username_element == arg.username_element && | |
141 form.password_element == arg.password_element && | |
142 form.new_password_element == arg.new_password_element && | |
143 form.submit_element == arg.submit_element; | |
144 } | |
145 | |
146 TEST_F(PasswordManagerHandlerTest, PasswordImport) { | |
147 autofill::PasswordForm form(MakeForm()); | |
148 base::HistogramBase* imported_passwords = | |
149 base::StatisticsRecorder::FindHistogram( | |
150 "PasswordManager.ImportedPasswordsPerUserInCSV"); | |
151 base::HistogramBase* import_result_enum = | |
152 base::StatisticsRecorder::FindHistogram( | |
153 "PasswordManager.ImportPasswordFromCSVResult"); | |
154 scoped_ptr<base::HistogramSamples> imported_count_samples( | |
155 imported_passwords->SnapshotSamples()); | |
156 scoped_ptr<base::HistogramSamples> import_result_enum_samples( | |
157 import_result_enum->SnapshotSamples()); | |
158 EXPECT_CALL(*GetPasswordStore(), AddLogin(FormMatches(form))).Times(2); | |
159 ImportPassword(); | |
160 EXPECT_EQ(imported_count_samples->TotalCount(), 1); | |
161 EXPECT_EQ(import_result_enum_samples->GetCount(0), 1); | |
162 } | |
163 | |
164 TEST_F(PasswordManagerHandlerTest, PasswordExport) { | |
165 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), | |
166 IsUserAuthenticated()); | |
167 ExportPassword(); | |
168 } | |
OLD | NEW |