Chromium Code Reviews| 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 namespace { | |
| 26 class TestSelectFileDialogFactory final : public ui::SelectFileDialogFactory { | |
| 27 public: | |
| 28 TestSelectFileDialogFactory() {} | |
| 29 ~TestSelectFileDialogFactory() override {} | |
| 30 ui::SelectFileDialog* Create(ui::SelectFileDialog::Listener* listener, | |
| 31 ui::SelectFilePolicy* policy) override { | |
| 32 return new TestSelectFileDialog(listener, new TestSelectFilePolicy); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 class TestSelectFilePolicy : public ui::SelectFilePolicy { | |
| 37 public: | |
| 38 bool CanOpenSelectFileDialog() override { return true; } | |
| 39 void SelectFileDenied() override {} | |
| 40 }; | |
| 41 | |
| 42 class TestSelectFileDialog : public ui::SelectFileDialog { | |
| 43 public: | |
| 44 TestSelectFileDialog(Listener* listener, ui::SelectFilePolicy* policy) | |
| 45 : ui::SelectFileDialog(listener, policy) {} | |
| 46 | |
| 47 protected: | |
| 48 void SelectFileImpl(Type type, | |
| 49 const base::string16& title, | |
| 50 const base::FilePath& default_path, | |
| 51 const FileTypeInfo* file_types, | |
| 52 int file_type_index, | |
| 53 const base::FilePath::StringType& default_extension, | |
| 54 gfx::NativeWindow owning_window, | |
| 55 void* params) override { | |
| 56 listener_->FileSelected(default_path, file_type_index, params); | |
| 57 } | |
| 58 bool IsRunning(gfx::NativeWindow owning_window) const override { | |
| 59 return false; | |
| 60 } | |
| 61 void ListenerDestroyed() override {} | |
| 62 bool HasMultipleFileTypeChoicesImpl() override { return false; } | |
| 63 ~TestSelectFileDialog() override {} | |
| 64 }; | |
| 65 }; | |
| 66 | |
| 67 class CallbackTestWebUI : public content::TestWebUI { | |
| 68 public: | |
| 69 CallbackTestWebUI() {} | |
| 70 ~CallbackTestWebUI() override {} | |
| 71 void RegisterMessageCallback(const std::string& message, | |
| 72 const MessageCallback& callback) override; | |
| 73 void ProcessWebUIMessage(const GURL& source_url, | |
| 74 const std::string& message, | |
| 75 const base::ListValue& args) override; | |
| 76 | |
| 77 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); | |
| 78 | |
| 79 private: | |
| 80 std::map<std::string, MessageCallback> message_callbacks_; | |
| 81 }; | |
| 82 | |
| 83 void CallbackTestWebUI::RegisterMessageCallback( | |
| 84 const std::string& message, | |
| 85 const MessageCallback& callback) { | |
| 86 message_callbacks_.insert(std::make_pair(message, callback)); | |
| 87 } | |
| 88 | |
| 89 void CallbackTestWebUI::ProcessWebUIMessage(const GURL& source_url, | |
| 90 const std::string& message, | |
| 91 const base::ListValue& args) { | |
| 92 std::map<std::string, MessageCallback>::const_iterator callback = | |
| 93 message_callbacks_.find(message); | |
| 94 if (callback != message_callbacks_.end()) { | |
| 95 callback->second.Run(&args); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 class TestPasswordManagerHandler : public options::PasswordManagerHandler { | |
| 100 public: | |
| 101 TestPasswordManagerHandler(scoped_ptr<PasswordManagerPresenter> presenter, | |
| 102 CallbackTestWebUI* web_ui) | |
| 103 : PasswordManagerHandler(std::move(presenter)) { | |
| 104 set_web_ui(web_ui); | |
| 105 } | |
| 106 ~TestPasswordManagerHandler() override {} | |
| 107 #if !defined(OS_ANDROID) | |
| 108 gfx::NativeWindow GetNativeWindow() const override; | |
| 109 #endif | |
| 110 | |
| 111 MOCK_METHOD3(FileSelected, void(const base::FilePath& path, int, void*)); | |
| 112 }; | |
| 113 #if !defined(OS_ANDROID) | |
| 114 gfx::NativeWindow TestPasswordManagerHandler::GetNativeWindow() const { | |
| 115 return NULL; | |
| 116 } | |
| 117 #endif | |
| 118 | |
| 119 class MockPasswordManagerPresenter : public PasswordManagerPresenter { | |
| 120 public: | |
| 121 explicit MockPasswordManagerPresenter(PasswordUIView* handler) | |
| 122 : PasswordManagerPresenter(handler) {} | |
| 123 ~MockPasswordManagerPresenter() override {} | |
| 124 | |
| 125 MOCK_METHOD0(IsUserAuthenticated, bool()); | |
| 126 | |
| 127 private: | |
| 128 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); | |
| 129 }; | |
| 130 | |
| 131 class DummyPasswordManagerHandler : public PasswordUIView { | |
| 132 public: | |
| 133 DummyPasswordManagerHandler() : password_manager_presenter_(this) { | |
| 134 password_manager_presenter_.Initialize(); | |
| 135 } | |
| 136 ~DummyPasswordManagerHandler() override {} | |
| 137 Profile* GetProfile() override; | |
| 138 | |
| 139 void ShowPassword(size_t, | |
| 140 const std::string&, | |
| 141 const std::string&, | |
| 142 const base::string16&) override {} | |
| 143 void SetPasswordList( | |
| 144 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} | |
| 145 void SetPasswordExceptionList( | |
| 146 const std::vector<scoped_ptr<autofill::PasswordForm>>&) override {} | |
| 147 | |
| 148 #if !defined(OS_ANDROID) | |
| 149 gfx::NativeWindow GetNativeWindow() const override; | |
| 150 #endif | |
| 151 private: | |
| 152 TestingProfile profile_; | |
| 153 PasswordManagerPresenter password_manager_presenter_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); | |
| 156 }; | |
| 157 | |
| 158 #if !defined(OS_ANDROID) | |
| 159 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { | |
| 160 return NULL; | |
| 161 } | |
| 162 #endif | |
| 163 | |
| 164 Profile* DummyPasswordManagerHandler::GetProfile() { | |
| 165 return &profile_; | |
| 166 } | |
| 167 | |
| 168 } // namespace | |
| 169 | |
| 170 class PasswordManagerHandlerTest : public testing::Test { | |
| 171 protected: | |
| 172 PasswordManagerHandlerTest() { | |
| 173 dummy_handler_.reset(new DummyPasswordManagerHandler()); | |
| 174 presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get()); | |
| 175 web_contents_ = | |
| 176 content::WebContentsTester::CreateTestWebContents(&profile_, NULL); | |
| 177 web_ui_.set_web_contents(web_contents_); | |
| 178 handler_.reset(new TestPasswordManagerHandler( | |
| 179 make_scoped_ptr(presenter_raw_), &web_ui_)); | |
| 180 handler_->RegisterMessages(); | |
| 181 ui::SelectFileDialog::SetFactory(new TestSelectFileDialogFactory); | |
| 182 handler_->InitializeHandler(); | |
| 183 } | |
| 184 | |
| 185 ~PasswordManagerHandlerTest() override {} | |
| 186 | |
| 187 void ExportPassword() { | |
| 188 base::ListValue tmp; | |
| 189 web_ui_.ProcessWebUIMessage(GURL(), "exportPassword", tmp); | |
| 190 } | |
| 191 | |
| 192 void ImportPassword() { | |
| 193 base::ListValue tmp; | |
| 194 web_ui_.ProcessWebUIMessage(GURL(), "importPassword", tmp); | |
| 195 } | |
| 196 | |
| 197 PasswordManagerPresenter* presenter_raw_; | |
| 198 CallbackTestWebUI web_ui_; | |
| 199 content::WebContents* web_contents_; | |
| 200 scoped_ptr<DummyPasswordManagerHandler> dummy_handler_; | |
| 201 scoped_ptr<TestPasswordManagerHandler> handler_; | |
| 202 | |
| 203 private: | |
| 204 content::TestBrowserThreadBundle thread_bundle_; | |
| 205 TestingProfile profile_; | |
| 206 | |
| 207 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); | |
| 208 }; | |
| 209 | |
| 210 MATCHER(IsEmptyPath, "") { | |
| 211 return arg.empty(); | |
| 212 } | |
| 213 | |
| 214 TEST_F(PasswordManagerHandlerTest, PasswordImport) { | |
| 215 EXPECT_CALL(web_ui_, GetWebContents()) | |
| 216 .WillRepeatedly(testing::Return(web_contents_)); | |
| 217 EXPECT_CALL(*(handler_.get()), | |
|
Evan Stade
2016/04/15 20:52:42
do you need this many parens? In fact I think *han
xunlu
2016/04/18 02:09:13
Done.
| |
| 218 FileSelected(IsEmptyPath(), 1, reinterpret_cast<void*>(0))); | |
|
vabr (Chromium)
2016/04/12 08:55:39
Here and on line 228 you are referencing FileSelec
xunlu
2016/04/18 02:09:13
Done.
| |
| 219 ImportPassword(); | |
| 220 } | |
| 221 | |
| 222 TEST_F(PasswordManagerHandlerTest, PasswordExport) { | |
| 223 const base::FilePath file_path; | |
| 224 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), | |
|
Evan Stade
2016/04/15 20:52:42
do you need this many parens?
xunlu
2016/04/18 02:09:13
Casting is needed here, otherwise the code won't c
| |
| 225 IsUserAuthenticated()) | |
| 226 .WillRepeatedly(testing::Return(true)); | |
|
vabr (Chromium)
2016/04/12 08:55:39
Please also use Times(AtLeast(1)), to ensure that
xunlu
2016/04/18 02:09:13
Done.
| |
| 227 EXPECT_CALL(*(handler_.get()), | |
|
Evan Stade
2016/04/15 20:52:42
ditto
xunlu
2016/04/18 02:09:13
Done.
| |
| 228 FileSelected(IsEmptyPath(), 1, reinterpret_cast<void*>(1))); | |
| 229 ExportPassword(); | |
| 230 } | |
| OLD | NEW |