OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
vabr (Chromium)
2016/03/10 10:53:32
2016
xunlu
2016/03/16 07:23:58
Done.
| |
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 "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using password_manager::MockPasswordStore; | |
20 | |
21 class MockPasswordManagerPresenter : public PasswordManagerPresenter { | |
22 public: | |
23 explicit MockPasswordManagerPresenter(PasswordUIView* handler) | |
24 : PasswordManagerPresenter(handler) {} | |
25 ~MockPasswordManagerPresenter() override {} | |
26 | |
27 MOCK_METHOD0(IsUserAuthenticated, bool()); | |
28 | |
29 private: | |
30 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); | |
31 }; | |
32 | |
33 class DummyPasswordManagerHandler : public PasswordUIView { | |
34 public: | |
35 explicit DummyPasswordManagerHandler(Profile* profile) | |
36 : profile_(profile), password_manager_presenter_(this) { | |
37 password_manager_presenter_.Initialize(); | |
38 } | |
39 ~DummyPasswordManagerHandler() override {} | |
40 Profile* GetProfile() override; | |
41 | |
42 void ShowPassword(size_t, | |
43 const std::string&, | |
44 const std::string&, | |
45 const base::string16&) override {} | |
46 void SetPasswordList(const std::vector<scoped_ptr<autofill::PasswordForm>>&, | |
47 bool) override {} | |
48 void SetPasswordExceptionList( | |
49 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} | |
50 | |
51 #if !defined(OS_ANDROID) | |
52 gfx::NativeWindow GetNativeWindow() const override; | |
53 #endif | |
54 private: | |
55 Profile* profile_; | |
56 PasswordManagerPresenter password_manager_presenter_; | |
57 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); | |
vabr (Chromium)
2016/03/10 10:53:32
nit: Please add a blank line above this line.
xunlu
2016/03/16 07:23:58
Done.
| |
58 }; | |
59 | |
60 #if !defined(OS_ANDROID) | |
61 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { | |
62 return NULL; | |
63 } | |
64 #endif | |
65 Profile* DummyPasswordManagerHandler::GetProfile() { | |
66 return profile_; | |
67 } | |
68 | |
69 class PasswordManagerHandlerTest : public testing::Test { | |
70 protected: | |
71 PasswordManagerHandlerTest() {} | |
72 ~PasswordManagerHandlerTest() override { | |
73 delete handler_; | |
vabr (Chromium)
2016/03/10 10:53:32
Why not use scoped_ptr instead of the manual delet
xunlu
2016/03/16 07:23:58
Doing that would crash the test during the destruc
vabr (Chromium)
2016/03/16 17:48:28
Just looking at the code, I can tell that handler_
xunlu
2016/03/18 21:15:30
It's working fine now. Thanks for the tip!
| |
74 delete dummy_handler_; | |
75 } | |
76 | |
77 void SetUp() override { | |
78 UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", 0); | |
vabr (Chromium)
2016/03/10 10:53:32
Could this all happen in the constructor instead o
xunlu
2016/03/16 07:23:58
Done.
| |
79 UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", 0, | |
80 3); | |
81 PasswordStoreFactory::GetInstance()->SetTestingFactory( | |
82 &profile_, password_manager::BuildPasswordStore<content::BrowserContext, | |
83 MockPasswordStore>); | |
84 dummy_handler_ = new DummyPasswordManagerHandler(&profile_); | |
85 handler_ = new options::PasswordManagerHandler( | |
86 new MockPasswordManagerPresenter(dummy_handler_)); | |
87 handler_->set_web_ui(&web_ui_); | |
88 } | |
89 | |
90 MockPasswordManagerPresenter* GetPresenter() { | |
91 return static_cast<MockPasswordManagerPresenter*>( | |
92 handler_->password_manager_presenter_.get()); | |
vabr (Chromium)
2016/03/10 10:53:32
Why do not cache this in the test instead of getti
xunlu
2016/03/16 07:23:58
Done.
| |
93 } | |
94 | |
95 MockPasswordStore* GetPasswordStore() { | |
96 return static_cast<MockPasswordStore*>(GetPresenter()->GetPasswordStore()); | |
97 } | |
98 | |
99 void ExportPassword() { handler_->HandlePasswordExport(NULL); } | |
100 | |
101 void ImportPassword() { | |
102 autofill::PasswordForm form(MakeForm()); | |
103 std::vector<autofill::PasswordForm> passwordForms; | |
104 passwordForms.push_back(form); | |
105 passwordForms.push_back(form); | |
106 handler_->ImportPasswordFileRead( | |
107 password_manager::PasswordImporter::SUCCESS, passwordForms); | |
108 } | |
109 | |
110 autofill::PasswordForm MakeForm() { | |
111 autofill::PasswordForm form; | |
112 GURL origin("http://test.com"); | |
113 form.origin = origin; | |
114 form.username_element = base::ASCIIToUTF16("Email"); | |
115 form.username_value = base::ASCIIToUTF16("test@test.com"); | |
116 form.password_element = base::ASCIIToUTF16("Password"); | |
117 form.password_value = base::ASCIIToUTF16("abcdef"); | |
118 return form; | |
119 } | |
120 | |
121 private: | |
122 content::TestBrowserThreadBundle thread_bundle_; | |
123 TestingProfile profile_; | |
124 options::PasswordManagerHandler* handler_; | |
125 DummyPasswordManagerHandler* dummy_handler_; | |
126 content::TestWebUI web_ui_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); | |
129 }; | |
130 | |
131 MATCHER_P(FormMatches, form, "") { | |
132 return form.signon_realm == arg.signon_realm && form.origin == arg.origin && | |
133 form.action == arg.action && | |
134 form.username_element == arg.username_element && | |
135 form.password_element == arg.password_element && | |
136 form.new_password_element == arg.new_password_element && | |
137 form.submit_element == arg.submit_element; | |
138 } | |
139 | |
140 TEST_F(PasswordManagerHandlerTest, PasswordImport) { | |
141 autofill::PasswordForm form(MakeForm()); | |
142 base::HistogramBase* imported_passwords = | |
143 base::StatisticsRecorder::FindHistogram( | |
144 "PasswordManager.ImportedPasswordsPerUserInCSV"); | |
145 base::HistogramBase* import_result_enum = | |
146 base::StatisticsRecorder::FindHistogram( | |
147 "PasswordManager.ImportPasswordFromCSVResult"); | |
148 scoped_ptr<base::HistogramSamples> imported_count_samples( | |
149 imported_passwords->SnapshotSamples()); | |
150 scoped_ptr<base::HistogramSamples> import_result_enum_samples( | |
151 import_result_enum->SnapshotSamples()); | |
152 EXPECT_CALL(*GetPasswordStore(), AddLogin(FormMatches(form))).Times(2); | |
153 ImportPassword(); | |
154 EXPECT_EQ(imported_count_samples->TotalCount(), 1); | |
155 EXPECT_EQ(import_result_enum_samples->GetCount(0), 1); | |
156 } | |
157 | |
158 TEST_F(PasswordManagerHandlerTest, PasswordExport) { | |
159 EXPECT_CALL(*GetPresenter(), IsUserAuthenticated()); | |
160 ExportPassword(); | |
161 } | |
OLD | NEW |