Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/ui/webui/options/password_manager_handler_unittest.cc

Issue 1193143003: Enable import/export of passwords into/from Password Manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 CallbackTestWebUI : public content::TestWebUI {
23 public:
24 explicit CallbackTestWebUI() {}
25 ~CallbackTestWebUI() override {}
26 void RegisterMessageCallback(const std::string& message,
27 const MessageCallback& callback) override;
28 void ProcessWebUIMessage(const GURL& source_url,
29 const std::string& message,
30 const base::ListValue& args) override;
31
32 private:
33 std::map<std::string, MessageCallback> message_callbacks_;
34 };
vabr (Chromium) 2016/03/21 14:01:30 nit: Please add one blank line to separate the cla
xunlu 2016/03/22 07:17:15 Done.
35 void CallbackTestWebUI::RegisterMessageCallback(
36 const std::string& message,
37 const MessageCallback& callback) {
38 message_callbacks_.insert(std::make_pair(message, callback));
39 }
40 void CallbackTestWebUI::ProcessWebUIMessage(const GURL& source_url,
41 const std::string& message,
42 const base::ListValue& args) {
43 std::map<std::string, MessageCallback>::const_iterator callback =
44 message_callbacks_.find(message);
45 if (callback != message_callbacks_.end()) {
46 callback->second.Run(&args);
47 }
48 }
49
50 class TestPasswordManagerHandler : public options::PasswordManagerHandler {
51 public:
52 TestPasswordManagerHandler(scoped_ptr<PasswordManagerPresenter> presenter,
53 CallbackTestWebUI* web_ui)
54 : PasswordManagerHandler(std::move(presenter)) {
55 set_web_ui(web_ui);
56 }
57 ~TestPasswordManagerHandler() override {}
58 };
59
60 class MockPasswordManagerPresenter : public PasswordManagerPresenter {
61 public:
62 explicit MockPasswordManagerPresenter(PasswordUIView* handler)
63 : PasswordManagerPresenter(handler) {}
64 ~MockPasswordManagerPresenter() override {}
65
66 MOCK_METHOD0(IsUserAuthenticated, bool());
67
68 private:
69 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter);
70 };
71
72 class DummyPasswordManagerHandler : public PasswordUIView {
73 public:
74 explicit DummyPasswordManagerHandler() : password_manager_presenter_(this) {
75 PasswordStoreFactory::GetInstance()->SetTestingFactory(
76 &profile_, password_manager::BuildPasswordStore<content::BrowserContext,
77 MockPasswordStore>);
78 password_manager_presenter_.Initialize();
79 }
80 ~DummyPasswordManagerHandler() override {}
81 Profile* GetProfile() override;
82
83 void ShowPassword(size_t,
84 const std::string&,
85 const std::string&,
86 const base::string16&) override {}
87 void SetPasswordList(const std::vector<scoped_ptr<autofill::PasswordForm>>&,
88 bool) override {}
89 void SetPasswordExceptionList(
90 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {}
91
92 #if !defined(OS_ANDROID)
93 gfx::NativeWindow GetNativeWindow() const override;
94 #endif
95 private:
96 TestingProfile profile_;
97 PasswordManagerPresenter password_manager_presenter_;
98
99 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler);
100 };
101
102 #if !defined(OS_ANDROID)
103 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const {
104 return NULL;
105 }
106 #endif
107 Profile* DummyPasswordManagerHandler::GetProfile() {
108 return &profile_;
109 }
110
111 class PasswordManagerHandlerTest : public testing::Test {
112 protected:
113 PasswordManagerHandlerTest() {
114 UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", 0);
115 UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", 0,
116 4);
117 PasswordStoreFactory::GetInstance()->SetTestingFactory(
118 &profile_, password_manager::BuildPasswordStore<content::BrowserContext,
119 MockPasswordStore>);
120 dummy_handler_.reset(new DummyPasswordManagerHandler());
121 presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get());
122 web_ui_.set_web_contents(
123 content::WebContentsTester::CreateTestWebContents(&profile_, NULL));
124 handler_.reset(new TestPasswordManagerHandler(
125 make_scoped_ptr(presenter_raw_), &web_ui_));
126 handler_->RegisterMessages();
127 }
128 ~PasswordManagerHandlerTest() override {}
vabr (Chromium) 2016/03/21 14:01:30 Nit: please add a blank line before the destructor
xunlu 2016/03/22 07:17:15 Done.
129
130 MockPasswordStore* GetPasswordStore() {
131 return static_cast<MockPasswordStore*>(
132 PasswordStoreFactory::GetForProfile(&profile_,
133 ServiceAccessType::EXPLICIT_ACCESS)
134 .get());
135 }
136
137 void ExportPassword() {
138 base::ListValue* tmp = new base::ListValue();
vabr (Chromium) 2016/03/21 14:01:30 No need to create ListValue on the heap, just crea
xunlu 2016/03/22 07:17:15 Done.
139 web_ui_.ProcessWebUIMessage(GURL(""), "exportPassword", *tmp);
vabr (Chromium) 2016/03/21 14:01:30 Please replace GURL("") with just GURL(), that's f
xunlu 2016/03/22 07:17:15 Done.
140 }
141
142 void ImportPassword() {
143 autofill::PasswordForm form(MakeForm());
144 std::vector<autofill::PasswordForm> passwordForms;
145 passwordForms.push_back(form);
146 passwordForms.push_back(form);
147 handler_->ImportPasswordFileRead(
148 password_manager::PasswordImporter::SUCCESS, passwordForms);
149 }
150
151 autofill::PasswordForm MakeForm() {
152 autofill::PasswordForm form;
153 GURL origin("http://test.com");
154 form.origin = origin;
155 form.username_element = base::ASCIIToUTF16("Email");
156 form.username_value = base::ASCIIToUTF16("test@test.com");
157 form.password_element = base::ASCIIToUTF16("Password");
158 form.password_value = base::ASCIIToUTF16("abcdef");
159 return form;
160 }
161
162 PasswordManagerPresenter* presenter_raw_;
163 CallbackTestWebUI web_ui_;
164
165 private:
166 content::TestBrowserThreadBundle thread_bundle_;
167 TestingProfile profile_;
168 scoped_ptr<DummyPasswordManagerHandler> dummy_handler_;
169 scoped_ptr<TestPasswordManagerHandler> handler_;
170
171 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest);
172 };
173
174 MATCHER_P(FormMatches, form, "") {
175 return form.signon_realm == arg.signon_realm && form.origin == arg.origin &&
176 form.action == arg.action &&
177 form.username_element == arg.username_element &&
178 form.password_element == arg.password_element &&
179 form.new_password_element == arg.new_password_element &&
180 form.submit_element == arg.submit_element;
181 }
182
183 TEST_F(PasswordManagerHandlerTest, PasswordImport) {
184 autofill::PasswordForm form(MakeForm());
185 base::HistogramBase* imported_passwords =
186 base::StatisticsRecorder::FindHistogram(
187 "PasswordManager.ImportedPasswordsPerUserInCSV");
188 base::HistogramBase* import_result_enum =
189 base::StatisticsRecorder::FindHistogram(
190 "PasswordManager.ImportPasswordFromCSVResult");
191 scoped_ptr<base::HistogramSamples> imported_count_samples(
192 imported_passwords->SnapshotSamples());
193 scoped_ptr<base::HistogramSamples> import_result_enum_samples(
194 import_result_enum->SnapshotSamples());
195 EXPECT_CALL(*GetPasswordStore(), AddLogin(FormMatches(form))).Times(2);
196 ImportPassword();
197 EXPECT_EQ(imported_count_samples->TotalCount(), 1);
198 EXPECT_EQ(import_result_enum_samples->GetCount(0), 1);
199 }
200
201 TEST_F(PasswordManagerHandlerTest, PasswordExport) {
202 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)),
203 IsUserAuthenticated());
204 ExportPassword();
205 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698