Index: chrome/browser/ui/webui/options/password_manager_handler_unittest.cc |
diff --git a/chrome/browser/ui/webui/options/password_manager_handler_unittest.cc b/chrome/browser/ui/webui/options/password_manager_handler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..01ec66af086539c507ae31b074d542ad40164cfc |
--- /dev/null |
+++ b/chrome/browser/ui/webui/options/password_manager_handler_unittest.cc |
@@ -0,0 +1,205 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/metrics/histogram.h" |
+#include "base/metrics/statistics_recorder.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "chrome/browser/password_manager/password_store_factory.h" |
+#include "chrome/browser/ui/passwords/password_manager_presenter.h" |
+#include "chrome/browser/ui/webui/options/password_manager_handler.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "components/password_manager/core/browser/mock_password_store.h" |
+#include "components/password_manager/core/browser/password_manager_test_utils.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "content/public/test/test_web_ui.h" |
+#include "content/public/test/web_contents_tester.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using password_manager::MockPasswordStore; |
+ |
+class CallbackTestWebUI : public content::TestWebUI { |
+ public: |
+ explicit CallbackTestWebUI() {} |
+ ~CallbackTestWebUI() override {} |
+ void RegisterMessageCallback(const std::string& message, |
+ const MessageCallback& callback) override; |
+ void ProcessWebUIMessage(const GURL& source_url, |
+ const std::string& message, |
+ const base::ListValue& args) override; |
+ |
+ private: |
+ std::map<std::string, MessageCallback> message_callbacks_; |
+}; |
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.
|
+void CallbackTestWebUI::RegisterMessageCallback( |
+ const std::string& message, |
+ const MessageCallback& callback) { |
+ message_callbacks_.insert(std::make_pair(message, callback)); |
+} |
+void CallbackTestWebUI::ProcessWebUIMessage(const GURL& source_url, |
+ const std::string& message, |
+ const base::ListValue& args) { |
+ std::map<std::string, MessageCallback>::const_iterator callback = |
+ message_callbacks_.find(message); |
+ if (callback != message_callbacks_.end()) { |
+ callback->second.Run(&args); |
+ } |
+} |
+ |
+class TestPasswordManagerHandler : public options::PasswordManagerHandler { |
+ public: |
+ TestPasswordManagerHandler(scoped_ptr<PasswordManagerPresenter> presenter, |
+ CallbackTestWebUI* web_ui) |
+ : PasswordManagerHandler(std::move(presenter)) { |
+ set_web_ui(web_ui); |
+ } |
+ ~TestPasswordManagerHandler() override {} |
+}; |
+ |
+class MockPasswordManagerPresenter : public PasswordManagerPresenter { |
+ public: |
+ explicit MockPasswordManagerPresenter(PasswordUIView* handler) |
+ : PasswordManagerPresenter(handler) {} |
+ ~MockPasswordManagerPresenter() override {} |
+ |
+ MOCK_METHOD0(IsUserAuthenticated, bool()); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); |
+}; |
+ |
+class DummyPasswordManagerHandler : public PasswordUIView { |
+ public: |
+ explicit DummyPasswordManagerHandler() : password_manager_presenter_(this) { |
+ PasswordStoreFactory::GetInstance()->SetTestingFactory( |
+ &profile_, password_manager::BuildPasswordStore<content::BrowserContext, |
+ MockPasswordStore>); |
+ password_manager_presenter_.Initialize(); |
+ } |
+ ~DummyPasswordManagerHandler() override {} |
+ Profile* GetProfile() override; |
+ |
+ void ShowPassword(size_t, |
+ const std::string&, |
+ const std::string&, |
+ const base::string16&) override {} |
+ void SetPasswordList(const std::vector<scoped_ptr<autofill::PasswordForm>>&, |
+ bool) override {} |
+ void SetPasswordExceptionList( |
+ const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} |
+ |
+#if !defined(OS_ANDROID) |
+ gfx::NativeWindow GetNativeWindow() const override; |
+#endif |
+ private: |
+ TestingProfile profile_; |
+ PasswordManagerPresenter password_manager_presenter_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); |
+}; |
+ |
+#if !defined(OS_ANDROID) |
+gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { |
+ return NULL; |
+} |
+#endif |
+Profile* DummyPasswordManagerHandler::GetProfile() { |
+ return &profile_; |
+} |
+ |
+class PasswordManagerHandlerTest : public testing::Test { |
+ protected: |
+ PasswordManagerHandlerTest() { |
+ UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", 0); |
+ UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", 0, |
+ 4); |
+ PasswordStoreFactory::GetInstance()->SetTestingFactory( |
+ &profile_, password_manager::BuildPasswordStore<content::BrowserContext, |
+ MockPasswordStore>); |
+ dummy_handler_.reset(new DummyPasswordManagerHandler()); |
+ presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get()); |
+ web_ui_.set_web_contents( |
+ content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); |
+ handler_.reset(new TestPasswordManagerHandler( |
+ make_scoped_ptr(presenter_raw_), &web_ui_)); |
+ handler_->RegisterMessages(); |
+ } |
+ ~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.
|
+ |
+ MockPasswordStore* GetPasswordStore() { |
+ return static_cast<MockPasswordStore*>( |
+ PasswordStoreFactory::GetForProfile(&profile_, |
+ ServiceAccessType::EXPLICIT_ACCESS) |
+ .get()); |
+ } |
+ |
+ void ExportPassword() { |
+ 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.
|
+ 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.
|
+ } |
+ |
+ void ImportPassword() { |
+ autofill::PasswordForm form(MakeForm()); |
+ std::vector<autofill::PasswordForm> passwordForms; |
+ passwordForms.push_back(form); |
+ passwordForms.push_back(form); |
+ handler_->ImportPasswordFileRead( |
+ password_manager::PasswordImporter::SUCCESS, passwordForms); |
+ } |
+ |
+ autofill::PasswordForm MakeForm() { |
+ autofill::PasswordForm form; |
+ GURL origin("http://test.com"); |
+ form.origin = origin; |
+ form.username_element = base::ASCIIToUTF16("Email"); |
+ form.username_value = base::ASCIIToUTF16("test@test.com"); |
+ form.password_element = base::ASCIIToUTF16("Password"); |
+ form.password_value = base::ASCIIToUTF16("abcdef"); |
+ return form; |
+ } |
+ |
+ PasswordManagerPresenter* presenter_raw_; |
+ CallbackTestWebUI web_ui_; |
+ |
+ private: |
+ content::TestBrowserThreadBundle thread_bundle_; |
+ TestingProfile profile_; |
+ scoped_ptr<DummyPasswordManagerHandler> dummy_handler_; |
+ scoped_ptr<TestPasswordManagerHandler> handler_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); |
+}; |
+ |
+MATCHER_P(FormMatches, form, "") { |
+ return form.signon_realm == arg.signon_realm && form.origin == arg.origin && |
+ form.action == arg.action && |
+ form.username_element == arg.username_element && |
+ form.password_element == arg.password_element && |
+ form.new_password_element == arg.new_password_element && |
+ form.submit_element == arg.submit_element; |
+} |
+ |
+TEST_F(PasswordManagerHandlerTest, PasswordImport) { |
+ autofill::PasswordForm form(MakeForm()); |
+ base::HistogramBase* imported_passwords = |
+ base::StatisticsRecorder::FindHistogram( |
+ "PasswordManager.ImportedPasswordsPerUserInCSV"); |
+ base::HistogramBase* import_result_enum = |
+ base::StatisticsRecorder::FindHistogram( |
+ "PasswordManager.ImportPasswordFromCSVResult"); |
+ scoped_ptr<base::HistogramSamples> imported_count_samples( |
+ imported_passwords->SnapshotSamples()); |
+ scoped_ptr<base::HistogramSamples> import_result_enum_samples( |
+ import_result_enum->SnapshotSamples()); |
+ EXPECT_CALL(*GetPasswordStore(), AddLogin(FormMatches(form))).Times(2); |
+ ImportPassword(); |
+ EXPECT_EQ(imported_count_samples->TotalCount(), 1); |
+ EXPECT_EQ(import_result_enum_samples->GetCount(0), 1); |
+} |
+ |
+TEST_F(PasswordManagerHandlerTest, PasswordExport) { |
+ EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), |
+ IsUserAuthenticated()); |
+ ExportPassword(); |
+} |