| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/password_manager_handler.h" | 5 #include "chrome/browser/ui/webui/options/password_manager_handler.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/password_manager/password_store_factory.h" | 11 #include "chrome/browser/password_manager/password_store_factory.h" |
| 12 #include "chrome/browser/ui/passwords/password_manager_presenter.h" | 12 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 14 #include "components/password_manager/core/browser/mock_password_store.h" | 15 #include "components/password_manager/core/browser/mock_password_store.h" |
| 15 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 16 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "content/public/test/test_web_ui.h" | 17 #include "content/public/test/test_web_ui.h" |
| 18 #include "content/public/test/web_contents_tester.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "ui/shell_dialogs/select_file_dialog.h" | 20 #include "ui/shell_dialogs/select_file_dialog.h" |
| 22 #include "ui/shell_dialogs/select_file_dialog_factory.h" | 21 #include "ui/shell_dialogs/select_file_dialog_factory.h" |
| 23 #include "ui/shell_dialogs/select_file_policy.h" | 22 #include "ui/shell_dialogs/select_file_policy.h" |
| 24 | 23 |
| 25 using password_manager::MockPasswordStore; | 24 using password_manager::MockPasswordStore; |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 ~MockPasswordManagerPresenter() override {} | 127 ~MockPasswordManagerPresenter() override {} |
| 129 | 128 |
| 130 MOCK_METHOD0(IsUserAuthenticated, bool()); | 129 MOCK_METHOD0(IsUserAuthenticated, bool()); |
| 131 | 130 |
| 132 private: | 131 private: |
| 133 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); | 132 DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerPresenter); |
| 134 }; | 133 }; |
| 135 | 134 |
| 136 class DummyPasswordManagerHandler : public PasswordUIView { | 135 class DummyPasswordManagerHandler : public PasswordUIView { |
| 137 public: | 136 public: |
| 138 DummyPasswordManagerHandler() : password_manager_presenter_(this) { | 137 explicit DummyPasswordManagerHandler(Profile* profile) |
| 138 : profile_(profile), password_manager_presenter_(this) { |
| 139 password_manager_presenter_.Initialize(); | 139 password_manager_presenter_.Initialize(); |
| 140 } | 140 } |
| 141 ~DummyPasswordManagerHandler() override {} | 141 ~DummyPasswordManagerHandler() override {} |
| 142 Profile* GetProfile() override; | 142 Profile* GetProfile() override; |
| 143 | 143 |
| 144 void ShowPassword(size_t, | 144 void ShowPassword(size_t, |
| 145 const std::string&, | 145 const std::string&, |
| 146 const std::string&, | 146 const std::string&, |
| 147 const base::string16&) override {} | 147 const base::string16&) override {} |
| 148 void SetPasswordList( | 148 void SetPasswordList( |
| 149 const std::vector<std::unique_ptr<autofill::PasswordForm>>&) override {} | 149 const std::vector<std::unique_ptr<autofill::PasswordForm>>&) override {} |
| 150 void SetPasswordExceptionList( | 150 void SetPasswordExceptionList( |
| 151 const std::vector<std::unique_ptr<autofill::PasswordForm>>&) override {} | 151 const std::vector<std::unique_ptr<autofill::PasswordForm>>&) override {} |
| 152 | 152 |
| 153 #if !defined(OS_ANDROID) | 153 #if !defined(OS_ANDROID) |
| 154 gfx::NativeWindow GetNativeWindow() const override; | 154 gfx::NativeWindow GetNativeWindow() const override; |
| 155 #endif | 155 #endif |
| 156 private: | 156 private: |
| 157 TestingProfile profile_; | 157 Profile* profile_; |
| 158 PasswordManagerPresenter password_manager_presenter_; | 158 PasswordManagerPresenter password_manager_presenter_; |
| 159 | 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); | 160 DISALLOW_COPY_AND_ASSIGN(DummyPasswordManagerHandler); |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 #if !defined(OS_ANDROID) | 163 #if !defined(OS_ANDROID) |
| 164 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { | 164 gfx::NativeWindow DummyPasswordManagerHandler::GetNativeWindow() const { |
| 165 return NULL; | 165 return NULL; |
| 166 } | 166 } |
| 167 #endif | 167 #endif |
| 168 | 168 |
| 169 Profile* DummyPasswordManagerHandler::GetProfile() { | 169 Profile* DummyPasswordManagerHandler::GetProfile() { |
| 170 return &profile_; | 170 return profile_; |
| 171 } | 171 } |
| 172 | 172 |
| 173 } // namespace | 173 } // namespace |
| 174 | 174 |
| 175 class PasswordManagerHandlerTest : public testing::Test { | 175 class PasswordManagerHandlerTest : public ChromeRenderViewHostTestHarness { |
| 176 protected: | 176 protected: |
| 177 PasswordManagerHandlerTest() { | 177 PasswordManagerHandlerTest() {} |
| 178 dummy_handler_.reset(new DummyPasswordManagerHandler()); | 178 ~PasswordManagerHandlerTest() override {} |
| 179 |
| 180 void SetUp() override { |
| 181 ChromeRenderViewHostTestHarness::SetUp(); |
| 182 dummy_handler_.reset(new DummyPasswordManagerHandler(profile())); |
| 179 presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get()); | 183 presenter_raw_ = new MockPasswordManagerPresenter(dummy_handler_.get()); |
| 180 web_contents_ = | |
| 181 content::WebContentsTester::CreateTestWebContents(&profile_, NULL); | |
| 182 web_ui_.set_web_contents(web_contents_); | |
| 183 handler_.reset(new TestPasswordManagerHandler( | 184 handler_.reset(new TestPasswordManagerHandler( |
| 184 base::WrapUnique(presenter_raw_), &web_ui_)); | 185 base::WrapUnique(presenter_raw_), &web_ui_)); |
| 185 handler_->RegisterMessages(); | 186 handler_->RegisterMessages(); |
| 186 ui::SelectFileDialog::SetFactory(new TestSelectFileDialogFactory); | 187 ui::SelectFileDialog::SetFactory(new TestSelectFileDialogFactory); |
| 187 handler_->InitializeHandler(); | 188 handler_->InitializeHandler(); |
| 189 web_ui_.set_web_contents(web_contents()); |
| 188 } | 190 } |
| 189 | 191 |
| 190 ~PasswordManagerHandlerTest() override {} | 192 void TearDown() override { |
| 193 handler_.reset(); |
| 194 dummy_handler_.reset(); |
| 195 ChromeRenderViewHostTestHarness::TearDown(); |
| 196 } |
| 191 | 197 |
| 192 void ExportPassword() { | 198 void ExportPassword() { |
| 193 base::ListValue tmp; | 199 base::ListValue tmp; |
| 194 web_ui_.ProcessWebUIMessage(GURL(), "exportPassword", tmp); | 200 web_ui_.ProcessWebUIMessage(GURL(), "exportPassword", tmp); |
| 195 } | 201 } |
| 196 | 202 |
| 197 void ImportPassword() { | 203 void ImportPassword() { |
| 198 base::ListValue tmp; | 204 base::ListValue tmp; |
| 199 web_ui_.ProcessWebUIMessage(GURL(), "importPassword", tmp); | 205 web_ui_.ProcessWebUIMessage(GURL(), "importPassword", tmp); |
| 200 } | 206 } |
| 201 | 207 |
| 202 PasswordManagerPresenter* presenter_raw_; | 208 PasswordManagerPresenter* presenter_raw_; |
| 203 CallbackTestWebUI web_ui_; | 209 CallbackTestWebUI web_ui_; |
| 204 content::WebContents* web_contents_; | |
| 205 std::unique_ptr<DummyPasswordManagerHandler> dummy_handler_; | 210 std::unique_ptr<DummyPasswordManagerHandler> dummy_handler_; |
| 206 std::unique_ptr<TestPasswordManagerHandler> handler_; | 211 std::unique_ptr<TestPasswordManagerHandler> handler_; |
| 207 | 212 |
| 208 private: | 213 private: |
| 209 content::TestBrowserThreadBundle thread_bundle_; | |
| 210 TestingProfile profile_; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); | 214 DISALLOW_COPY_AND_ASSIGN(PasswordManagerHandlerTest); |
| 213 }; | 215 }; |
| 214 | 216 |
| 215 MATCHER(IsEmptyPath, "") { | 217 MATCHER(IsEmptyPath, "") { |
| 216 return arg.empty(); | 218 return arg.empty(); |
| 217 } | 219 } |
| 218 | 220 |
| 219 TEST_F(PasswordManagerHandlerTest, PasswordImport) { | 221 TEST_F(PasswordManagerHandlerTest, PasswordImport) { |
| 220 EXPECT_CALL(web_ui_, GetWebContents()) | 222 EXPECT_CALL(web_ui_, GetWebContents()) |
| 221 .WillRepeatedly(testing::Return(web_contents_)); | 223 .WillRepeatedly(testing::Return(web_contents())); |
| 222 EXPECT_CALL( | 224 EXPECT_CALL( |
| 223 *handler_, | 225 *handler_, |
| 224 FileSelected(IsEmptyPath(), 1, | 226 FileSelected(IsEmptyPath(), 1, |
| 225 reinterpret_cast<void*>( | 227 reinterpret_cast<void*>( |
| 226 TestPasswordManagerHandler::IMPORT_FILE_SELECTED))); | 228 TestPasswordManagerHandler::IMPORT_FILE_SELECTED))); |
| 227 ImportPassword(); | 229 ImportPassword(); |
| 228 } | 230 } |
| 229 | 231 |
| 230 TEST_F(PasswordManagerHandlerTest, PasswordExport) { | 232 TEST_F(PasswordManagerHandlerTest, PasswordExport) { |
| 231 const base::FilePath file_path; | 233 const base::FilePath file_path; |
| 232 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), | 234 EXPECT_CALL(*(static_cast<MockPasswordManagerPresenter*>(presenter_raw_)), |
| 233 IsUserAuthenticated()) | 235 IsUserAuthenticated()) |
| 234 .Times(testing::AtLeast(1)) | 236 .Times(testing::AtLeast(1)) |
| 235 .WillRepeatedly(testing::Return(true)); | 237 .WillRepeatedly(testing::Return(true)); |
| 236 EXPECT_CALL( | 238 EXPECT_CALL( |
| 237 *handler_, | 239 *handler_, |
| 238 FileSelected(IsEmptyPath(), 1, | 240 FileSelected(IsEmptyPath(), 1, |
| 239 reinterpret_cast<void*>( | 241 reinterpret_cast<void*>( |
| 240 TestPasswordManagerHandler::EXPORT_FILE_SELECTED))); | 242 TestPasswordManagerHandler::EXPORT_FILE_SELECTED))); |
| 241 ExportPassword(); | 243 ExportPassword(); |
| 242 } | 244 } |
| OLD | NEW |