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