| 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 "chrome/browser/ui/webui/options/password_manager_handler.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/metrics/histogram_macros.h" | |
| 9 #include "base/metrics/statistics_recorder.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "chrome/browser/password_manager/password_store_factory.h" | |
| 12 #include "chrome/browser/ui/passwords/password_manager_presenter.h" | |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 14 #include "chrome/test/base/testing_profile.h" | |
| 15 #include "components/password_manager/core/browser/mock_password_store.h" | |
| 16 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | |
| 17 #include "content/public/test/test_web_ui.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "ui/shell_dialogs/select_file_dialog.h" | |
| 21 #include "ui/shell_dialogs/select_file_dialog_factory.h" | |
| 22 #include "ui/shell_dialogs/select_file_policy.h" | |
| 23 | |
| 24 using password_manager::MockPasswordStore; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 class TestSelectFileDialogFactory final : public ui::SelectFileDialogFactory { | |
| 29 public: | |
| 30 TestSelectFileDialogFactory() {} | |
| 31 ~TestSelectFileDialogFactory() override {} | |
| 32 ui::SelectFileDialog* Create(ui::SelectFileDialog::Listener* listener, | |
| 33 ui::SelectFilePolicy* policy) override { | |
| 34 delete policy; // Ignore the policy, replace it with a test one. | |
| 35 return new TestSelectFileDialog(listener, new TestSelectFilePolicy); | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 class TestSelectFilePolicy : public ui::SelectFilePolicy { | |
| 40 public: | |
| 41 bool CanOpenSelectFileDialog() override { return true; } | |
| 42 void SelectFileDenied() override {} | |
| 43 }; | |
| 44 | |
| 45 class TestSelectFileDialog : public ui::SelectFileDialog { | |
| 46 public: | |
| 47 TestSelectFileDialog(Listener* listener, ui::SelectFilePolicy* policy) | |
| 48 : ui::SelectFileDialog(listener, policy) {} | |
| 49 | |
| 50 protected: | |
| 51 void SelectFileImpl(Type type, | |
| 52 const base::string16& title, | |
| 53 const base::FilePath& default_path, | |
| 54 const FileTypeInfo* file_types, | |
| 55 int file_type_index, | |
| 56 const base::FilePath::StringType& default_extension, | |
| 57 gfx::NativeWindow owning_window, | |
| 58 void* params) override { | |
| 59 listener_->FileSelected(default_path, file_type_index, params); | |
| 60 } | |
| 61 bool IsRunning(gfx::NativeWindow owning_window) const override { | |
| 62 return false; | |
| 63 } | |
| 64 void ListenerDestroyed() override {} | |
| 65 bool HasMultipleFileTypeChoicesImpl() override { return false; } | |
| 66 ~TestSelectFileDialog() override {} | |
| 67 }; | |
| 68 }; | |
| 69 | |
| 70 class CallbackTestWebUI : public content::TestWebUI { | |
| 71 public: | |
| 72 CallbackTestWebUI() {} | |
| 73 ~CallbackTestWebUI() override {} | |
| 74 void RegisterMessageCallback(const std::string& message, | |
| 75 const MessageCallback& callback) override; | |
| 76 void ProcessWebUIMessage(const GURL& source_url, | |
| 77 const std::string& message, | |
| 78 const base::ListValue& args) override; | |
| 79 | |
| 80 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); | |
| 81 | |
| 82 private: | |
| 83 std::map<std::string, MessageCallback> message_callbacks_; | |
| 84 }; | |
| 85 | |
| 86 void CallbackTestWebUI::RegisterMessageCallback( | |
| 87 const std::string& message, | |
| 88 const MessageCallback& callback) { | |
| 89 message_callbacks_.insert(std::make_pair(message, callback)); | |
| 90 } | |
| 91 | |
| 92 void CallbackTestWebUI::ProcessWebUIMessage(const GURL& source_url, | |
| 93 const std::string& message, | |
| 94 const base::ListValue& args) { | |
| 95 std::map<std::string, MessageCallback>::const_iterator callback = | |
| 96 message_callbacks_.find(message); | |
| 97 if (callback != message_callbacks_.end()) { | |
| 98 callback->second.Run(&args); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 class TestPasswordManagerHandler : public options::PasswordManagerHandler { | |
| 103 public: | |
| 104 TestPasswordManagerHandler( | |
| 105 std::unique_ptr<PasswordManagerPresenter> presenter, | |
| 106 CallbackTestWebUI* web_ui) | |
| 107 : PasswordManagerHandler(std::move(presenter)) { | |
| 108 set_web_ui(web_ui); | |
| 109 } | |
| 110 ~TestPasswordManagerHandler() override {} | |
| 111 #if !defined(OS_ANDROID) | |
| 112 gfx::NativeWindow GetNativeWindow() const override; | |
| 113 #endif | |
| 114 | |
| 115 MOCK_METHOD3(FileSelected, void(const base::FilePath& path, int, void*)); | |
| 116 }; | |
| 117 #if !defined(OS_ANDROID) | |
| 118 gfx::NativeWindow TestPasswordManagerHandler::GetNativeWindow() const { | |
| 119 return NULL; | |
| 120 } | |
| 121 #endif | |
| 122 | |
| 123 class MockPasswordManagerPresenter : public PasswordManagerPresenter { | |
| 124 public: | |
| 125 explicit MockPasswordManagerPresenter(PasswordUIView* handler) | |
| 126 : PasswordManagerPresenter(handler) {} | |
| 127 ~MockPasswordManagerPresenter() override {} | |
| 128 | |
| 129 MOCK_METHOD0(IsUserAuthenticated, bool()); | |
| 130 | |
| 131 private: | |
| 132 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); | |
| 133 }; | |
| 134 | |
| 135 class DummyPasswordManagerHandler : public PasswordUIView { | |
| 136 public: | |
| 137 explicit DummyPasswordManagerHandler(Profile* profile) | |
| 138 : profile_(profile), password_manager_presenter_(this) { | |
| 139 password_manager_presenter_.Initialize(); | |
| 140 } | |
| 141 ~DummyPasswordManagerHandler() override {} | |
| 142 Profile* GetProfile() override; | |
| 143 | |
| 144 void ShowPassword(size_t, | |
| 145 const std::string&, | |
| 146 const std::string&, | |
| 147 const base::string16&) override {} | |
| 148 void SetPasswordList( | |
| 149 const std::vector<std::unique_ptr<autofill::PasswordForm>>&) override {} | |
| 150 void SetPasswordExceptionList( | |
| 151 const std::vector<std::unique_ptr<autofill::PasswordForm>>&) override {} | |
| 152 | |
| 153 #if !defined(OS_ANDROID) | |
| 154 gfx::NativeWindow GetNativeWindow() const override; | |
| 155 #endif | |
| 156 private: | |
| 157 Profile* profile_; | |
| 158 PasswordManagerPresenter password_manager_presenter_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); | |
| 161 }; | |
| 162 | |
| 163 #if !defined(OS_ANDROID) | |
| 164 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { | |
| 165 return NULL; | |
| 166 } | |
| 167 #endif | |
| 168 | |
| 169 Profile* DummyPasswordManagerHandler::GetProfile() { | |
| 170 return profile_; | |
| 171 } | |
| 172 | |
| 173 } // namespace | |
| 174 | |
| 175 class PasswordManagerHandlerTest : public ChromeRenderViewHostTestHarness { | |
| 176 protected: | |
| 177 PasswordManagerHandlerTest() {} | |
| 178 ~PasswordManagerHandlerTest() override {} | |
| 179 | |
| 180 void SetUp() override { | |
| 181 ChromeRenderViewHostTestHarness::SetUp(); | |
| 182 dummy_handler_.reset(new DummyPasswordManagerHandler(profile())); | |
| 183 presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get()); | |
| 184 handler_.reset(new TestPasswordManagerHandler( | |
| 185 base::WrapUnique(presenter_raw_), &web_ui_)); | |
| 186 handler_->RegisterMessages(); | |
| 187 ui::SelectFileDialog::SetFactory(new TestSelectFileDialogFactory); | |
| 188 handler_->InitializeHandler(); | |
| 189 web_ui_.set_web_contents(web_contents()); | |
| 190 } | |
| 191 | |
| 192 void TearDown() override { | |
| 193 handler_.reset(); | |
| 194 dummy_handler_.reset(); | |
| 195 ChromeRenderViewHostTestHarness::TearDown(); | |
| 196 } | |
| 197 | |
| 198 void ExportPassword() { | |
| 199 base::ListValue tmp; | |
| 200 web_ui_.ProcessWebUIMessage(GURL(), "exportPassword", tmp); | |
| 201 } | |
| 202 | |
| 203 void ImportPassword() { | |
| 204 base::ListValue tmp; | |
| 205 web_ui_.ProcessWebUIMessage(GURL(), "importPassword", tmp); | |
| 206 } | |
| 207 | |
| 208 PasswordManagerPresenter* presenter_raw_; | |
| 209 CallbackTestWebUI web_ui_; | |
| 210 std::unique_ptr<DummyPasswordManagerHandler> dummy_handler_; | |
| 211 std::unique_ptr<TestPasswordManagerHandler> handler_; | |
| 212 | |
| 213 private: | |
| 214 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); | |
| 215 }; | |
| 216 | |
| 217 MATCHER(IsEmptyPath, "") { | |
| 218 return arg.empty(); | |
| 219 } | |
| 220 | |
| 221 TEST_F(PasswordManagerHandlerTest, PasswordImport) { | |
| 222 EXPECT_CALL(web_ui_, GetWebContents()) | |
| 223 .WillRepeatedly(testing::Return(web_contents())); | |
| 224 EXPECT_CALL( | |
| 225 *handler_, | |
| 226 FileSelected(IsEmptyPath(), 1, | |
| 227 reinterpret_cast<void*>( | |
| 228 TestPasswordManagerHandler::IMPORT_FILE_SELECTED))); | |
| 229 ImportPassword(); | |
| 230 } | |
| 231 | |
| 232 TEST_F(PasswordManagerHandlerTest, PasswordExport) { | |
| 233 const base::FilePath file_path; | |
| 234 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), | |
| 235 IsUserAuthenticated()) | |
| 236 .Times(testing::AtLeast(1)) | |
| 237 .WillRepeatedly(testing::Return(true)); | |
| 238 EXPECT_CALL( | |
| 239 *handler_, | |
| 240 FileSelected(IsEmptyPath(), 1, | |
| 241 reinterpret_cast<void*>( | |
| 242 TestPasswordManagerHandler::EXPORT_FILE_SELECTED))); | |
| 243 ExportPassword(); | |
| 244 } | |
| OLD | NEW |