Chromium Code Reviews| 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..f66dd933008acb3a0f1962a2a611a661c43a1f21 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/options/password_manager_handler_unittest.cc |
| @@ -0,0 +1,168 @@ |
| +// 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 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_ = new DummyPasswordManagerHandler(); |
| + 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.
|
| + presenter_raw_ = presenter_.get(); |
| + handler_ = new options::PasswordManagerHandler(std::move(presenter_)); |
| + web_ui_.set_web_contents( |
| + content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); |
| + handler_->set_web_ui(&web_ui_); |
| + } |
| + ~PasswordManagerHandlerTest() override { |
| + delete handler_; |
| + delete dummy_handler_; |
| + } |
| + |
| + MockPasswordStore* GetPasswordStore() { |
| + return static_cast<MockPasswordStore*>( |
| + PasswordStoreFactory::GetForProfile(&profile_, |
| + ServiceAccessType::EXPLICIT_ACCESS) |
| + .get()); |
| + } |
| + |
| + void ExportPassword() { handler_->HandlePasswordExport(NULL); } |
| + |
| + 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_; |
| + |
| + private: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + TestingProfile profile_; |
| + scoped_ptr<PasswordManagerPresenter> presenter_; |
| + options::PasswordManagerHandler* handler_; |
| + DummyPasswordManagerHandler* dummy_handler_; |
| + content::TestWebUI web_ui_; |
| + |
| + 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(); |
| +} |