| 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 #import "ios/chrome/browser/passwords/password_controller.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "components/password_manager/core/browser/log_manager.h" |
| 12 #include "components/password_manager/core/browser/password_form_manager.h" |
| 13 #include "components/password_manager/core/browser/stub_password_manager_client.
h" |
| 14 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 15 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 17 #import "ios/web/public/test/test_web_state.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" |
| 21 |
| 22 using testing::_; |
| 23 using testing::Return; |
| 24 |
| 25 class MockWebState : public web::TestWebState { |
| 26 public: |
| 27 MOCK_CONST_METHOD0(GetBrowserState, web::BrowserState*(void)); |
| 28 }; |
| 29 |
| 30 class MockPasswordManagerClient |
| 31 : public password_manager::StubPasswordManagerClient { |
| 32 public: |
| 33 // |form_manager| stays owned by the mock. |
| 34 MOCK_METHOD3(PromptUserToSaveOrUpdatePasswordPtr, |
| 35 void(password_manager::PasswordFormManager* form_manager, |
| 36 password_manager::CredentialSourceType type, |
| 37 bool update_password)); |
| 38 MOCK_CONST_METHOD0(GetLogManager, password_manager::LogManager*(void)); |
| 39 |
| 40 // Workaround for scoped_ptr<> lacking a copy constructor. |
| 41 bool PromptUserToSaveOrUpdatePassword( |
| 42 scoped_ptr<password_manager::PasswordFormManager> manager, |
| 43 password_manager::CredentialSourceType type, |
| 44 bool update_password) override { |
| 45 PromptUserToSaveOrUpdatePasswordPtr(manager.get(), type, update_password); |
| 46 return false; |
| 47 } |
| 48 }; |
| 49 |
| 50 class MockLogManager : public password_manager::LogManager { |
| 51 public: |
| 52 MOCK_CONST_METHOD1(LogSavePasswordProgress, void(const std::string& text)); |
| 53 MOCK_CONST_METHOD0(IsLoggingActive, bool(void)); |
| 54 |
| 55 // Methods not important for testing. |
| 56 void OnLogRouterAvailabilityChanged(bool router_can_be_used) override {} |
| 57 void SetSuspended(bool suspended) override {} |
| 58 }; |
| 59 |
| 60 TEST(PasswordControllerTest, SaveOnNonHTMLLandingPage) { |
| 61 // Create the PasswordController with a MockPasswordManagerClient. |
| 62 TestChromeBrowserState::Builder builder; |
| 63 auto pref_service = |
| 64 make_scoped_ptr(new syncable_prefs::TestingPrefServiceSyncable); |
| 65 pref_service->registry()->RegisterBooleanPref( |
| 66 password_manager::prefs::kPasswordManagerSavingEnabled, true); |
| 67 builder.SetPrefService(std::move(pref_service)); |
| 68 scoped_ptr<TestChromeBrowserState> browser_state(builder.Build()); |
| 69 MockWebState web_state; |
| 70 ON_CALL(web_state, GetBrowserState()) |
| 71 .WillByDefault(testing::Return(browser_state.get())); |
| 72 auto client = make_scoped_ptr(new MockPasswordManagerClient); |
| 73 MockPasswordManagerClient* weak_client = client.get(); |
| 74 base::scoped_nsobject<PasswordController> passwordController = |
| 75 [[PasswordController alloc] initWithWebState:&web_state |
| 76 passwordsUiDelegate:nil |
| 77 client:std::move(client)]; |
| 78 |
| 79 // Use a mock LogManager to detect that OnPasswordFormsRendered has been |
| 80 // called. TODO(crbug.com/598672): this is a hack, we should modularize the |
| 81 // code better to allow proper unit-testing. |
| 82 MockLogManager log_manager; |
| 83 EXPECT_CALL(log_manager, IsLoggingActive()).WillRepeatedly(Return(true)); |
| 84 EXPECT_CALL(log_manager, |
| 85 LogSavePasswordProgress( |
| 86 "Message: \"PasswordManager::OnPasswordFormsRendered\"\n")); |
| 87 EXPECT_CALL(log_manager, |
| 88 LogSavePasswordProgress(testing::Ne( |
| 89 "Message: \"PasswordManager::OnPasswordFormsRendered\"\n"))) |
| 90 .Times(testing::AnyNumber()); |
| 91 EXPECT_CALL(*weak_client, GetLogManager()) |
| 92 .WillRepeatedly(Return(&log_manager)); |
| 93 |
| 94 web_state.SetContentIsHTML(false); |
| 95 web_state.SetCurrentURL(GURL("https://example.com")); |
| 96 [passwordController webStateDidLoadPage:&web_state]; |
| 97 } |
| OLD | NEW |