OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/mock_password_store_service.h" |
| 9 #include "chrome/browser/password_manager/password_store_factory.h" |
| 10 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 11 #include "chrome/browser/ui/webui/options/password_manager_handler.h" |
| 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "components/password_manager/core/browser/mock_password_store.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 ScopedVector<autofill::PasswordForm>&, |
| 47 bool) override {} |
| 48 void SetPasswordExceptionList( |
| 49 const ScopedVector<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); |
| 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_; |
| 74 delete dummy_handler_; |
| 75 } |
| 76 |
| 77 void SetUp() override { |
| 78 UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", 0); |
| 79 UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", 0, |
| 80 3); |
| 81 PasswordStoreFactory::GetInstance()->SetTestingFactory( |
| 82 &profile_, MockPasswordStoreService::Build); |
| 83 dummy_handler_ = new DummyPasswordManagerHandler(&profile_); |
| 84 handler_ = new options::PasswordManagerHandler( |
| 85 new MockPasswordManagerPresenter(dummy_handler_)); |
| 86 handler_->set_web_ui(&web_ui_); |
| 87 } |
| 88 |
| 89 MockPasswordManagerPresenter* GetPresenter() { |
| 90 return static_cast<MockPasswordManagerPresenter*>( |
| 91 handler_->password_manager_presenter_.get()); |
| 92 } |
| 93 |
| 94 MockPasswordStore* GetPasswordStore() { |
| 95 return static_cast<MockPasswordStore*>(GetPresenter()->GetPasswordStore()); |
| 96 } |
| 97 |
| 98 void ExportPassword() { handler_->HandlePasswordExport(NULL); } |
| 99 |
| 100 void ImportPassword() { |
| 101 autofill::PasswordForm form(MakeForm()); |
| 102 std::vector<autofill::PasswordForm> passwordForms; |
| 103 passwordForms.push_back(form); |
| 104 passwordForms.push_back(form); |
| 105 handler_->ImportPasswordFileRead( |
| 106 password_manager::PasswordImporter::SUCCESS, passwordForms); |
| 107 } |
| 108 |
| 109 autofill::PasswordForm MakeForm() { |
| 110 autofill::PasswordForm form; |
| 111 GURL origin("http://test.com"); |
| 112 form.origin = origin; |
| 113 form.username_element = base::ASCIIToUTF16("Email"); |
| 114 form.username_value = base::ASCIIToUTF16("test@test.com"); |
| 115 form.password_element = base::ASCIIToUTF16("Password"); |
| 116 form.password_value = base::ASCIIToUTF16("abcdef"); |
| 117 return form; |
| 118 } |
| 119 |
| 120 private: |
| 121 content::TestBrowserThreadBundle thread_bundle_; |
| 122 TestingProfile profile_; |
| 123 options::PasswordManagerHandler* handler_; |
| 124 DummyPasswordManagerHandler* dummy_handler_; |
| 125 content::TestWebUI web_ui_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); |
| 128 }; |
| 129 |
| 130 MATCHER_P(FormMatches, form, "") { |
| 131 return form.signon_realm == arg.signon_realm && form.origin == arg.origin && |
| 132 form.action == arg.action && |
| 133 form.username_element == arg.username_element && |
| 134 form.password_element == arg.password_element && |
| 135 form.new_password_element == arg.new_password_element && |
| 136 form.submit_element == arg.submit_element; |
| 137 } |
| 138 |
| 139 TEST_F(PasswordManagerHandlerTest, PasswordImport) { |
| 140 autofill::PasswordForm form(MakeForm()); |
| 141 base::HistogramBase* imported_passwords = |
| 142 base::StatisticsRecorder::FindHistogram( |
| 143 "PasswordManager.ImportedPasswordsPerUserInCSV"); |
| 144 base::HistogramBase* import_result_enum = |
| 145 base::StatisticsRecorder::FindHistogram( |
| 146 "PasswordManager.ImportPasswordFromCSVResult"); |
| 147 scoped_ptr<base::HistogramSamples> imported_count_samples( |
| 148 imported_passwords->SnapshotSamples()); |
| 149 scoped_ptr<base::HistogramSamples> import_result_enum_samples( |
| 150 import_result_enum->SnapshotSamples()); |
| 151 EXPECT_CALL(*GetPasswordStore(), AddLogin(FormMatches(form))).Times(2); |
| 152 ImportPassword(); |
| 153 EXPECT_EQ(imported_count_samples->TotalCount(), 1); |
| 154 EXPECT_EQ(import_result_enum_samples->GetCount(0), 1); |
| 155 } |
| 156 |
| 157 TEST_F(PasswordManagerHandlerTest, PasswordExport) { |
| 158 EXPECT_CALL(*GetPresenter(), IsUserAuthenticated()); |
| 159 ExportPassword(); |
| 160 } |
OLD | NEW |