OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/password_manager/chrome_password_manager_client.h" | 5 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/sync/profile_sync_service_factory.h" | 14 #include "chrome/browser/sync/profile_sync_service_factory.h" |
15 #include "chrome/browser/sync/profile_sync_service_mock.h" | 15 #include "chrome/browser/sync/profile_sync_service_mock.h" |
16 #include "chrome/common/channel_info.h" | 16 #include "chrome/common/channel_info.h" |
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
19 #include "components/autofill/content/common/autofill_messages.h" | 19 #include "components/autofill/content/common/autofill_messages.h" |
20 #include "components/password_manager/content/browser/password_manager_internals
_service_factory.h" | 20 #include "components/password_manager/content/browser/password_manager_internals
_service_factory.h" |
21 #include "components/password_manager/content/common/credential_manager_messages
.h" | 21 #include "components/password_manager/content/common/credential_manager_messages
.h" |
22 #include "components/password_manager/core/browser/credentials_filter.h" | 22 #include "components/password_manager/core/browser/credentials_filter.h" |
23 #include "components/password_manager/core/browser/log_receiver.h" | |
24 #include "components/password_manager/core/browser/password_manager_internals_se
rvice.h" | |
25 #include "components/password_manager/core/common/credential_manager_types.h" | 23 #include "components/password_manager/core/common/credential_manager_types.h" |
26 #include "components/password_manager/core/common/password_manager_pref_names.h" | 24 #include "components/password_manager/core/common/password_manager_pref_names.h" |
27 #include "components/password_manager/core/common/password_manager_switches.h" | 25 #include "components/password_manager/core/common/password_manager_switches.h" |
28 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 26 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
29 #include "components/version_info/version_info.h" | 27 #include "components/version_info/version_info.h" |
30 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
32 #include "content/public/test/mock_render_process_host.h" | 30 #include "content/public/test/mock_render_process_host.h" |
33 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
35 | 33 |
36 using content::BrowserContext; | 34 using content::BrowserContext; |
37 using content::WebContents; | 35 using content::WebContents; |
38 using testing::Return; | 36 using testing::Return; |
39 using testing::_; | 37 using testing::_; |
40 | 38 |
41 namespace { | 39 namespace { |
42 | 40 |
43 const char kPasswordManagerSettingsBehaviourChangeFieldTrialName[] = | 41 const char kPasswordManagerSettingsBehaviourChangeFieldTrialName[] = |
44 "PasswordManagerSettingsBehaviourChange"; | 42 "PasswordManagerSettingsBehaviourChange"; |
45 const char kPasswordManagerSettingsBehaviourChangeEnabledGroupName[] = | 43 const char kPasswordManagerSettingsBehaviourChangeEnabledGroupName[] = |
46 "PasswordManagerSettingsBehaviourChange.Active"; | 44 "PasswordManagerSettingsBehaviourChange.Active"; |
47 const char kPasswordManagerSettingsBehaviourChangeDisabledGroupName[] = | 45 const char kPasswordManagerSettingsBehaviourChangeDisabledGroupName[] = |
48 "PasswordManagerSettingsBehaviourChange.NotActive"; | 46 "PasswordManagerSettingsBehaviourChange.NotActive"; |
49 | 47 |
50 const char kTestText[] = "abcd1234"; | |
51 | |
52 class MockLogReceiver : public password_manager::LogReceiver { | |
53 public: | |
54 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); | |
55 }; | |
56 | |
57 // TODO(vabr): Get rid of the mocked client in the client's own test, see | 48 // TODO(vabr): Get rid of the mocked client in the client's own test, see |
58 // http://crbug.com/474577. | 49 // http://crbug.com/474577. |
59 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { | 50 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { |
60 public: | 51 public: |
61 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); | 52 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); |
62 | 53 |
63 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) | 54 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) |
64 : ChromePasswordManagerClient(web_contents, nullptr) { | 55 : ChromePasswordManagerClient(web_contents, nullptr) { |
65 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors()) | 56 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors()) |
66 .WillByDefault(testing::Return(false)); | 57 .WillByDefault(testing::Return(false)); |
67 } | 58 } |
68 ~MockChromePasswordManagerClient() override {} | 59 ~MockChromePasswordManagerClient() override {} |
69 | 60 |
70 private: | 61 private: |
71 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); | 62 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); |
72 }; | 63 }; |
73 | 64 |
74 } // namespace | 65 } // namespace |
75 | 66 |
76 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 67 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
77 public: | 68 public: |
78 ChromePasswordManagerClientTest() | 69 ChromePasswordManagerClientTest() : field_trial_list_(nullptr) {} |
79 : service_(nullptr), field_trial_list_(nullptr) {} | |
80 void SetUp() override; | 70 void SetUp() override; |
81 | 71 |
82 syncable_prefs::TestingPrefServiceSyncable* prefs() { | 72 syncable_prefs::TestingPrefServiceSyncable* prefs() { |
83 return profile()->GetTestingPrefService(); | 73 return profile()->GetTestingPrefService(); |
84 } | 74 } |
85 | 75 |
86 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( | 76 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( |
87 const char* name) { | 77 const char* name) { |
88 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 78 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
89 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); | 79 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); |
90 } | 80 } |
91 | 81 |
92 protected: | 82 protected: |
93 ChromePasswordManagerClient* GetClient(); | 83 ChromePasswordManagerClient* GetClient(); |
94 | 84 |
95 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then | 85 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then |
96 // copies its argument into |activation_flag| and returns true. Otherwise | 86 // copies its argument into |activation_flag| and returns true. Otherwise |
97 // returns false. | 87 // returns false. |
98 bool WasLoggingActivationMessageSent(bool* activation_flag); | 88 bool WasLoggingActivationMessageSent(bool* activation_flag); |
99 | 89 |
100 password_manager::PasswordManagerInternalsService* service_; | |
101 | |
102 testing::StrictMock<MockLogReceiver> receiver_; | |
103 TestingPrefServiceSimple prefs_; | 90 TestingPrefServiceSimple prefs_; |
104 base::FieldTrialList field_trial_list_; | 91 base::FieldTrialList field_trial_list_; |
105 }; | 92 }; |
106 | 93 |
107 void ChromePasswordManagerClientTest::SetUp() { | 94 void ChromePasswordManagerClientTest::SetUp() { |
108 ChromeRenderViewHostTestHarness::SetUp(); | 95 ChromeRenderViewHostTestHarness::SetUp(); |
109 prefs_.registry()->RegisterBooleanPref( | 96 prefs_.registry()->RegisterBooleanPref( |
110 password_manager::prefs::kPasswordManagerSavingEnabled, true); | 97 password_manager::prefs::kPasswordManagerSavingEnabled, true); |
111 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( | 98 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( |
112 web_contents(), nullptr); | 99 web_contents(), nullptr); |
113 service_ = password_manager::PasswordManagerInternalsServiceFactory:: | |
114 GetForBrowserContext(profile()); | |
115 ASSERT_TRUE(service_); | |
116 } | 100 } |
117 | 101 |
118 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { | 102 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { |
119 return ChromePasswordManagerClient::FromWebContents(web_contents()); | 103 return ChromePasswordManagerClient::FromWebContents(web_contents()); |
120 } | 104 } |
121 | 105 |
122 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( | 106 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( |
123 bool* activation_flag) { | 107 bool* activation_flag) { |
124 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID; | 108 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID; |
125 const IPC::Message* message = | 109 const IPC::Message* message = |
126 process()->sink().GetFirstMessageMatching(kMsgID); | 110 process()->sink().GetFirstMessageMatching(kMsgID); |
127 if (!message) | 111 if (!message) |
128 return false; | 112 return false; |
129 base::Tuple<bool> param; | 113 base::Tuple<bool> param; |
130 AutofillMsg_SetLoggingState::Read(message, ¶m); | 114 AutofillMsg_SetLoggingState::Read(message, ¶m); |
131 *activation_flag = base::get<0>(param); | 115 *activation_flag = base::get<0>(param); |
132 process()->sink().ClearMessages(); | 116 process()->sink().ClearMessages(); |
133 return true; | 117 return true; |
134 } | 118 } |
135 | 119 |
136 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) { | |
137 ChromePasswordManagerClient* client = GetClient(); | |
138 | |
139 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0); | |
140 // Before attaching the receiver, no text should be passed. | |
141 client->LogSavePasswordProgress(kTestText); | |
142 EXPECT_FALSE(client->IsLoggingActive()); | |
143 } | |
144 | |
145 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) { | |
146 ChromePasswordManagerClient* client = GetClient(); | |
147 EXPECT_FALSE(client->IsLoggingActive()); | |
148 | |
149 // After attaching the logger, text should be passed. | |
150 service_->RegisterReceiver(&receiver_); | |
151 EXPECT_TRUE(client->IsLoggingActive()); | |
152 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); | |
153 client->LogSavePasswordProgress(kTestText); | |
154 service_->UnregisterReceiver(&receiver_); | |
155 EXPECT_FALSE(client->IsLoggingActive()); | |
156 } | |
157 | |
158 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachReceiver) { | |
159 ChromePasswordManagerClient* client = GetClient(); | |
160 | |
161 service_->RegisterReceiver(&receiver_); | |
162 EXPECT_TRUE(client->IsLoggingActive()); | |
163 service_->UnregisterReceiver(&receiver_); | |
164 EXPECT_FALSE(client->IsLoggingActive()); | |
165 | |
166 // After detaching the logger, no text should be passed. | |
167 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0); | |
168 client->LogSavePasswordProgress(kTestText); | |
169 } | |
170 | |
171 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { | 120 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { |
172 ChromePasswordManagerClient* client = GetClient(); | 121 ChromePasswordManagerClient* client = GetClient(); |
173 bool logging_active = false; | 122 bool logging_active = false; |
| 123 // Ensure the existence of a driver, which will send the IPCs we listen for |
| 124 // below. |
| 125 NavigateAndCommit(GURL("about:blank")); |
174 | 126 |
175 // Initially, the logging should be off, so no IPC messages. | 127 // Initially, the logging should be off, so no IPC messages. |
176 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); | 128 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); |
177 | 129 |
178 service_->RegisterReceiver(&receiver_); | 130 client->NotifyDriversAboutLoggingAvailability(true); |
179 EXPECT_TRUE(client->IsLoggingActive()); | |
180 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | 131 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); |
181 EXPECT_TRUE(logging_active); | 132 EXPECT_TRUE(logging_active); |
182 | 133 |
183 service_->UnregisterReceiver(&receiver_); | 134 client->NotifyDriversAboutLoggingAvailability(false); |
184 EXPECT_FALSE(client->IsLoggingActive()); | |
185 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | 135 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); |
186 EXPECT_FALSE(logging_active); | 136 EXPECT_FALSE(logging_active); |
187 } | 137 } |
188 | |
189 TEST_F(ChromePasswordManagerClientTest, AnswerToPingsAboutLoggingState_Active) { | |
190 service_->RegisterReceiver(&receiver_); | |
191 | |
192 process()->sink().ClearMessages(); | |
193 | |
194 // Ping the client for logging activity update. | |
195 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0); | |
196 static_cast<content::WebContentsObserver*>(GetClient())->OnMessageReceived( | |
197 msg, web_contents()->GetMainFrame()); | |
198 | |
199 bool logging_active = false; | |
200 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | |
201 EXPECT_TRUE(logging_active); | |
202 | |
203 service_->UnregisterReceiver(&receiver_); | |
204 } | |
205 | |
206 TEST_F(ChromePasswordManagerClientTest, | |
207 AnswerToPingsAboutLoggingState_Inactive) { | |
208 process()->sink().ClearMessages(); | |
209 | |
210 // Ping the client for logging activity update. | |
211 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0); | |
212 static_cast<content::WebContentsObserver*>(GetClient())->OnMessageReceived( | |
213 msg, web_contents()->GetMainFrame()); | |
214 | |
215 bool logging_active = true; | |
216 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | |
217 EXPECT_FALSE(logging_active); | |
218 } | |
219 | 138 |
220 TEST_F(ChromePasswordManagerClientTest, | 139 TEST_F(ChromePasswordManagerClientTest, |
221 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { | 140 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { |
222 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 141 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
223 } | 142 } |
224 | 143 |
225 TEST_F(ChromePasswordManagerClientTest, | 144 TEST_F(ChromePasswordManagerClientTest, |
226 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { | 145 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { |
227 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 146 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
228 password_manager::switches::kEnableAutomaticPasswordSaving); | 147 password_manager::switches::kEnableAutomaticPasswordSaving); |
229 if (chrome::GetChannel() == version_info::Channel::UNKNOWN) | 148 if (chrome::GetChannel() == version_info::Channel::UNKNOWN) |
230 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 149 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
231 else | 150 else |
232 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 151 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
233 } | 152 } |
234 | 153 |
235 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) { | |
236 ChromePasswordManagerClient* client = GetClient(); | |
237 service_->RegisterReceiver(&receiver_); | |
238 EXPECT_TRUE(client->IsLoggingActive()); | |
239 | |
240 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); | |
241 client->LogSavePasswordProgress(kTestText); | |
242 | |
243 service_->UnregisterReceiver(&receiver_); | |
244 EXPECT_FALSE(client->IsLoggingActive()); | |
245 } | |
246 | |
247 TEST_F(ChromePasswordManagerClientTest, GetPasswordSyncState) { | 154 TEST_F(ChromePasswordManagerClientTest, GetPasswordSyncState) { |
248 ChromePasswordManagerClient* client = GetClient(); | 155 ChromePasswordManagerClient* client = GetClient(); |
249 | 156 |
250 ProfileSyncServiceMock* mock_sync_service = | 157 ProfileSyncServiceMock* mock_sync_service = |
251 static_cast<ProfileSyncServiceMock*>( | 158 static_cast<ProfileSyncServiceMock*>( |
252 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 159 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
253 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService)); | 160 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService)); |
254 | 161 |
255 syncer::ModelTypeSet active_types; | 162 syncer::ModelTypeSet active_types; |
256 active_types.Put(syncer::PASSWORDS); | 163 active_types.Put(syncer::PASSWORDS); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 EXPECT_EQ(GURL::EmptyGURL(), GetClient()->GetLastCommittedEntryURL()); | 304 EXPECT_EQ(GURL::EmptyGURL(), GetClient()->GetLastCommittedEntryURL()); |
398 } | 305 } |
399 | 306 |
400 TEST_F(ChromePasswordManagerClientTest, GetLastCommittedEntryURL) { | 307 TEST_F(ChromePasswordManagerClientTest, GetLastCommittedEntryURL) { |
401 GURL kUrl( | 308 GURL kUrl( |
402 "https://accounts.google.com/ServiceLogin?continue=" | 309 "https://accounts.google.com/ServiceLogin?continue=" |
403 "https://passwords.google.com/settings"); | 310 "https://passwords.google.com/settings"); |
404 NavigateAndCommit(kUrl); | 311 NavigateAndCommit(kUrl); |
405 EXPECT_EQ(kUrl, GetClient()->GetLastCommittedEntryURL()); | 312 EXPECT_EQ(kUrl, GetClient()->GetLastCommittedEntryURL()); |
406 } | 313 } |
| 314 |
| 315 TEST_F(ChromePasswordManagerClientTest, GetLogRouter) { |
| 316 EXPECT_TRUE(GetClient()->GetLogRouter()); |
| 317 } |
| 318 |
| 319 TEST_F(ChromePasswordManagerClientTest, GetLogRouter_WebUI) { |
| 320 // No router for WebUI. |
| 321 NavigateAndCommit(GURL("about:password-manager-internals")); |
| 322 EXPECT_FALSE(GetClient()->GetLogRouter()); |
| 323 } |
OLD | NEW |