OLD | NEW |
(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 #include "ui/shell_dialogs/select_file_dialog.h" |
| 20 #include "ui/shell_dialogs/select_file_dialog_factory.h" |
| 21 #include "ui/shell_dialogs/select_file_policy.h" |
| 22 |
| 23 using password_manager::MockPasswordStore; |
| 24 |
| 25 class TestSelectFileDialogFactory final : public ui::SelectFileDialogFactory { |
| 26 public: |
| 27 TestSelectFileDialogFactory() {} |
| 28 ~TestSelectFileDialogFactory() override {} |
| 29 ui::SelectFileDialog* Create(ui::SelectFileDialog::Listener* listener, |
| 30 ui::SelectFilePolicy* policy) override { |
| 31 return new TestSelectFileDialog(listener, new TestSelectFilePolicy); |
| 32 } |
| 33 |
| 34 private: |
| 35 class TestSelectFilePolicy : public ui::SelectFilePolicy { |
| 36 public: |
| 37 bool CanOpenSelectFileDialog() override { return true; } |
| 38 void SelectFileDenied() override {} |
| 39 }; |
| 40 |
| 41 class TestSelectFileDialog : public ui::SelectFileDialog { |
| 42 public: |
| 43 TestSelectFileDialog(Listener* listener, ui::SelectFilePolicy* policy) |
| 44 : ui::SelectFileDialog(listener, policy) {} |
| 45 |
| 46 protected: |
| 47 void SelectFileImpl(Type type, |
| 48 const base::string16& title, |
| 49 const base::FilePath& default_path, |
| 50 const FileTypeInfo* file_types, |
| 51 int file_type_index, |
| 52 const base::FilePath::StringType& default_extension, |
| 53 gfx::NativeWindow owning_window, |
| 54 void* params) override {} |
| 55 bool IsRunning(gfx::NativeWindow owning_window) const override { |
| 56 return false; |
| 57 } |
| 58 void ListenerDestroyed() override {} |
| 59 bool HasMultipleFileTypeChoicesImpl() override { return false; } |
| 60 ~TestSelectFileDialog() override {} |
| 61 }; |
| 62 }; |
| 63 |
| 64 class CallbackTestWebUI : public content::TestWebUI { |
| 65 public: |
| 66 CallbackTestWebUI() {} |
| 67 ~CallbackTestWebUI() override {} |
| 68 void RegisterMessageCallback(const std::string& message, |
| 69 const MessageCallback& callback) override; |
| 70 void ProcessWebUIMessage(const GURL& source_url, |
| 71 const std::string& message, |
| 72 const base::ListValue& args) override; |
| 73 |
| 74 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); |
| 75 |
| 76 private: |
| 77 std::map<std::string, MessageCallback> message_callbacks_; |
| 78 }; |
| 79 |
| 80 void CallbackTestWebUI::RegisterMessageCallback( |
| 81 const std::string& message, |
| 82 const MessageCallback& callback) { |
| 83 message_callbacks_.insert(std::make_pair(message, callback)); |
| 84 } |
| 85 |
| 86 void CallbackTestWebUI::ProcessWebUIMessage(const GURL& source_url, |
| 87 const std::string& message, |
| 88 const base::ListValue& args) { |
| 89 std::map<std::string, MessageCallback>::const_iterator callback = |
| 90 message_callbacks_.find(message); |
| 91 if (callback != message_callbacks_.end()) { |
| 92 callback->second.Run(&args); |
| 93 } |
| 94 } |
| 95 |
| 96 class TestPasswordManagerHandler : public options::PasswordManagerHandler { |
| 97 public: |
| 98 TestPasswordManagerHandler(scoped_ptr<PasswordManagerPresenter> presenter, |
| 99 CallbackTestWebUI* web_ui) |
| 100 : PasswordManagerHandler(std::move(presenter)) { |
| 101 set_web_ui(web_ui); |
| 102 } |
| 103 ~TestPasswordManagerHandler() override {} |
| 104 }; |
| 105 |
| 106 class MockPasswordManagerPresenter : public PasswordManagerPresenter { |
| 107 public: |
| 108 explicit MockPasswordManagerPresenter(PasswordUIView* handler) |
| 109 : PasswordManagerPresenter(handler) {} |
| 110 ~MockPasswordManagerPresenter() override {} |
| 111 |
| 112 MOCK_METHOD0(IsUserAuthenticated, bool()); |
| 113 |
| 114 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); |
| 116 }; |
| 117 |
| 118 class DummyPasswordManagerHandler : public PasswordUIView { |
| 119 public: |
| 120 DummyPasswordManagerHandler() : password_manager_presenter_(this) { |
| 121 password_manager_presenter_.Initialize(); |
| 122 } |
| 123 ~DummyPasswordManagerHandler() override {} |
| 124 Profile* GetProfile() override; |
| 125 |
| 126 void ShowPassword(size_t, |
| 127 const std::string&, |
| 128 const std::string&, |
| 129 const base::string16&) override {} |
| 130 void SetPasswordList( |
| 131 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} |
| 132 void SetPasswordExceptionList( |
| 133 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} |
| 134 |
| 135 #if !defined(OS_ANDROID) |
| 136 gfx::NativeWindow GetNativeWindow() const override; |
| 137 #endif |
| 138 private: |
| 139 TestingProfile profile_; |
| 140 PasswordManagerPresenter password_manager_presenter_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); |
| 143 }; |
| 144 |
| 145 #if !defined(OS_ANDROID) |
| 146 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { |
| 147 return NULL; |
| 148 } |
| 149 #endif |
| 150 |
| 151 Profile* DummyPasswordManagerHandler::GetProfile() { |
| 152 return &profile_; |
| 153 } |
| 154 |
| 155 class PasswordManagerHandlerTest : public testing::Test { |
| 156 protected: |
| 157 PasswordManagerHandlerTest() { |
| 158 UMA_HISTOGRAM_COUNTS("PasswordManager.ImportedPasswordsPerUserInCSV", 0); |
| 159 UMA_HISTOGRAM_ENUMERATION("PasswordManager.ImportPasswordFromCSVResult", 0, |
| 160 4); |
| 161 dummy_handler_.reset(new DummyPasswordManagerHandler()); |
| 162 presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get()); |
| 163 web_contents_ = |
| 164 content::WebContentsTester::CreateTestWebContents(&profile_, NULL); |
| 165 web_ui_.set_web_contents(web_contents_); |
| 166 handler_.reset(new TestPasswordManagerHandler( |
| 167 make_scoped_ptr(presenter_raw_), &web_ui_)); |
| 168 handler_->RegisterMessages(); |
| 169 ui::SelectFileDialog::SetFactory(new TestSelectFileDialogFactory); |
| 170 handler_->InitializeHandler(); |
| 171 } |
| 172 |
| 173 ~PasswordManagerHandlerTest() override {} |
| 174 |
| 175 void ExportPassword() { |
| 176 base::ListValue tmp; |
| 177 web_ui_.ProcessWebUIMessage(GURL(), "exportPassword", tmp); |
| 178 } |
| 179 |
| 180 void ImportPassword() { |
| 181 base::ListValue tmp; |
| 182 web_ui_.ProcessWebUIMessage(GURL(), "importPassword", tmp); |
| 183 } |
| 184 |
| 185 PasswordManagerPresenter* presenter_raw_; |
| 186 CallbackTestWebUI web_ui_; |
| 187 content::WebContents* web_contents_; |
| 188 |
| 189 private: |
| 190 content::TestBrowserThreadBundle thread_bundle_; |
| 191 TestingProfile profile_; |
| 192 scoped_ptr<DummyPasswordManagerHandler> dummy_handler_; |
| 193 scoped_ptr<TestPasswordManagerHandler> handler_; |
| 194 |
| 195 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); |
| 196 }; |
| 197 |
| 198 TEST_F(PasswordManagerHandlerTest, PasswordImport) { |
| 199 EXPECT_CALL(web_ui_, GetWebContents()) |
| 200 .Times(2) |
| 201 .WillOnce(testing::Return(web_contents_)) |
| 202 .WillOnce(testing::Return(web_contents_)); |
| 203 ImportPassword(); |
| 204 } |
| 205 |
| 206 TEST_F(PasswordManagerHandlerTest, PasswordExport) { |
| 207 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), |
| 208 IsUserAuthenticated()); |
| 209 ExportPassword(); |
| 210 } |
OLD | NEW |