Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/password_manager/chrome_password_manager_client_unittest.cc

Issue 1415533013: Fix password manager internals renderer reporting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix failing test Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_manager.h"
23 #include "components/password_manager/core/browser/log_receiver.h" 24 #include "components/password_manager/core/browser/log_receiver.h"
25 #include "components/password_manager/core/browser/log_router.h"
24 #include "components/password_manager/core/browser/password_manager_internals_se rvice.h" 26 #include "components/password_manager/core/browser/password_manager_internals_se rvice.h"
25 #include "components/password_manager/core/common/credential_manager_types.h" 27 #include "components/password_manager/core/common/credential_manager_types.h"
26 #include "components/password_manager/core/common/password_manager_pref_names.h" 28 #include "components/password_manager/core/common/password_manager_pref_names.h"
27 #include "components/password_manager/core/common/password_manager_switches.h" 29 #include "components/password_manager/core/common/password_manager_switches.h"
28 #include "components/syncable_prefs/testing_pref_service_syncable.h" 30 #include "components/syncable_prefs/testing_pref_service_syncable.h"
29 #include "components/version_info/version_info.h" 31 #include "components/version_info/version_info.h"
30 #include "content/public/browser/browser_context.h" 32 #include "content/public/browser/browser_context.h"
31 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
32 #include "content/public/test/mock_render_process_host.h" 34 #include "content/public/test/mock_render_process_host.h"
33 #include "testing/gmock/include/gmock/gmock.h" 35 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
35 37
36 using content::BrowserContext; 38 using content::BrowserContext;
37 using content::WebContents; 39 using content::WebContents;
38 using testing::Return; 40 using testing::Return;
39 using testing::_; 41 using testing::_;
40 42
41 namespace { 43 namespace {
42 44
43 const char kPasswordManagerSettingsBehaviourChangeFieldTrialName[] = 45 const char kPasswordManagerSettingsBehaviourChangeFieldTrialName[] =
44 "PasswordManagerSettingsBehaviourChange"; 46 "PasswordManagerSettingsBehaviourChange";
45 const char kPasswordManagerSettingsBehaviourChangeEnabledGroupName[] = 47 const char kPasswordManagerSettingsBehaviourChangeEnabledGroupName[] =
46 "PasswordManagerSettingsBehaviourChange.Active"; 48 "PasswordManagerSettingsBehaviourChange.Active";
47 const char kPasswordManagerSettingsBehaviourChangeDisabledGroupName[] = 49 const char kPasswordManagerSettingsBehaviourChangeDisabledGroupName[] =
48 "PasswordManagerSettingsBehaviourChange.NotActive"; 50 "PasswordManagerSettingsBehaviourChange.NotActive";
49 51
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 52 // TODO(vabr): Get rid of the mocked client in the client's own test, see
58 // http://crbug.com/474577. 53 // http://crbug.com/474577.
59 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { 54 class MockChromePasswordManagerClient : public ChromePasswordManagerClient {
60 public: 55 public:
61 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); 56 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool());
62 57
63 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) 58 explicit MockChromePasswordManagerClient(content::WebContents* web_contents)
64 : ChromePasswordManagerClient(web_contents, nullptr) { 59 : ChromePasswordManagerClient(web_contents, nullptr) {
65 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors()) 60 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors())
66 .WillByDefault(testing::Return(false)); 61 .WillByDefault(testing::Return(false));
67 } 62 }
68 ~MockChromePasswordManagerClient() override {} 63 ~MockChromePasswordManagerClient() override {}
69 64
70 private: 65 private:
71 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); 66 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient);
72 }; 67 };
73 68
69 class DummyLogReceiver : public password_manager::LogReceiver {
70 public:
71 DummyLogReceiver() = default;
72
73 void LogSavePasswordProgress(const std::string& text) override {}
74
75 private:
76 DISALLOW_COPY_AND_ASSIGN(DummyLogReceiver);
77 };
78
74 } // namespace 79 } // namespace
75 80
76 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { 81 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
77 public: 82 public:
78 ChromePasswordManagerClientTest() 83 ChromePasswordManagerClientTest() : field_trial_list_(nullptr) {}
79 : service_(nullptr), field_trial_list_(nullptr) {}
80 void SetUp() override; 84 void SetUp() override;
81 85
82 syncable_prefs::TestingPrefServiceSyncable* prefs() { 86 syncable_prefs::TestingPrefServiceSyncable* prefs() {
83 return profile()->GetTestingPrefService(); 87 return profile()->GetTestingPrefService();
84 } 88 }
85 89
86 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup( 90 void EnforcePasswordManagerSettingsBehaviourChangeExperimentGroup(
87 const char* name) { 91 const char* name) {
88 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( 92 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
89 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name)); 93 kPasswordManagerSettingsBehaviourChangeFieldTrialName, name));
90 } 94 }
91 95
92 protected: 96 protected:
93 ChromePasswordManagerClient* GetClient(); 97 ChromePasswordManagerClient* GetClient();
94 98
95 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then 99 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
96 // copies its argument into |activation_flag| and returns true. Otherwise 100 // copies its argument into |activation_flag| and returns true. Otherwise
97 // returns false. 101 // returns false.
98 bool WasLoggingActivationMessageSent(bool* activation_flag); 102 bool WasLoggingActivationMessageSent(bool* activation_flag);
99 103
100 password_manager::PasswordManagerInternalsService* service_;
101
102 testing::StrictMock<MockLogReceiver> receiver_;
103 TestingPrefServiceSimple prefs_; 104 TestingPrefServiceSimple prefs_;
104 base::FieldTrialList field_trial_list_; 105 base::FieldTrialList field_trial_list_;
105 }; 106 };
106 107
107 void ChromePasswordManagerClientTest::SetUp() { 108 void ChromePasswordManagerClientTest::SetUp() {
108 ChromeRenderViewHostTestHarness::SetUp(); 109 ChromeRenderViewHostTestHarness::SetUp();
109 prefs_.registry()->RegisterBooleanPref( 110 prefs_.registry()->RegisterBooleanPref(
110 password_manager::prefs::kPasswordManagerSavingEnabled, true); 111 password_manager::prefs::kPasswordManagerSavingEnabled, true);
111 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( 112 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
112 web_contents(), nullptr); 113 web_contents(), nullptr);
113 service_ = password_manager::PasswordManagerInternalsServiceFactory::
114 GetForBrowserContext(profile());
115 ASSERT_TRUE(service_);
116 } 114 }
117 115
118 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { 116 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
119 return ChromePasswordManagerClient::FromWebContents(web_contents()); 117 return ChromePasswordManagerClient::FromWebContents(web_contents());
120 } 118 }
121 119
122 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( 120 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent(
123 bool* activation_flag) { 121 bool* activation_flag) {
124 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID; 122 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID;
125 const IPC::Message* message = 123 const IPC::Message* message =
126 process()->sink().GetFirstMessageMatching(kMsgID); 124 process()->sink().GetFirstMessageMatching(kMsgID);
127 if (!message) 125 if (!message)
128 return false; 126 return false;
129 base::Tuple<bool> param; 127 base::Tuple<bool> param;
130 AutofillMsg_SetLoggingState::Read(message, &param); 128 AutofillMsg_SetLoggingState::Read(message, &param);
131 *activation_flag = base::get<0>(param); 129 *activation_flag = base::get<0>(param);
132 process()->sink().ClearMessages(); 130 process()->sink().ClearMessages();
133 return true; 131 return true;
134 } 132 }
135 133
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) { 134 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) {
172 ChromePasswordManagerClient* client = GetClient(); 135 bool logging_active = true;
173 bool logging_active = false; 136 // Ensure the existence of a driver, which will send the IPCs we listen for
137 // below.
138 NavigateAndCommit(GURL("about:blank"));
174 139
175 // Initially, the logging should be off, so no IPC messages. 140 // Initially, the logging should be off, so no IPC messages.
176 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); 141 EXPECT_TRUE(!WasLoggingActivationMessageSent(&logging_active) ||
142 !logging_active)
143 << "logging_active=" << logging_active;
177 144
178 service_->RegisterReceiver(&receiver_); 145 DummyLogReceiver log_receiver;
179 EXPECT_TRUE(client->IsLoggingActive()); 146 password_manager::LogRouter* log_router = password_manager::
147 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile());
148 EXPECT_EQ(std::string(), log_router->RegisterReceiver(&log_receiver));
180 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); 149 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
181 EXPECT_TRUE(logging_active); 150 EXPECT_TRUE(logging_active);
182 151
183 service_->UnregisterReceiver(&receiver_); 152 log_router->UnregisterReceiver(&log_receiver);
184 EXPECT_FALSE(client->IsLoggingActive());
185 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); 153 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
186 EXPECT_FALSE(logging_active); 154 EXPECT_FALSE(logging_active);
187 } 155 }
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 156
220 TEST_F(ChromePasswordManagerClientTest, 157 TEST_F(ChromePasswordManagerClientTest,
221 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { 158 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) {
222 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); 159 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
223 } 160 }
224 161
225 TEST_F(ChromePasswordManagerClientTest, 162 TEST_F(ChromePasswordManagerClientTest,
226 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { 163 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) {
227 base::CommandLine::ForCurrentProcess()->AppendSwitch( 164 base::CommandLine::ForCurrentProcess()->AppendSwitch(
228 password_manager::switches::kEnableAutomaticPasswordSaving); 165 password_manager::switches::kEnableAutomaticPasswordSaving);
229 if (chrome::GetChannel() == version_info::Channel::UNKNOWN) 166 if (chrome::GetChannel() == version_info::Channel::UNKNOWN)
230 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); 167 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled());
231 else 168 else
232 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); 169 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
233 } 170 }
234 171
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) { 172 TEST_F(ChromePasswordManagerClientTest, GetPasswordSyncState) {
248 ChromePasswordManagerClient* client = GetClient(); 173 ChromePasswordManagerClient* client = GetClient();
249 174
250 ProfileSyncServiceMock* mock_sync_service = 175 ProfileSyncServiceMock* mock_sync_service =
251 static_cast<ProfileSyncServiceMock*>( 176 static_cast<ProfileSyncServiceMock*>(
252 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 177 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
253 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService)); 178 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService));
254 179
255 syncer::ModelTypeSet active_types; 180 syncer::ModelTypeSet active_types;
256 active_types.Put(syncer::PASSWORDS); 181 active_types.Put(syncer::PASSWORDS);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 EXPECT_EQ(GURL::EmptyGURL(), GetClient()->GetLastCommittedEntryURL()); 322 EXPECT_EQ(GURL::EmptyGURL(), GetClient()->GetLastCommittedEntryURL());
398 } 323 }
399 324
400 TEST_F(ChromePasswordManagerClientTest, GetLastCommittedEntryURL) { 325 TEST_F(ChromePasswordManagerClientTest, GetLastCommittedEntryURL) {
401 GURL kUrl( 326 GURL kUrl(
402 "https://accounts.google.com/ServiceLogin?continue=" 327 "https://accounts.google.com/ServiceLogin?continue="
403 "https://passwords.google.com/settings"); 328 "https://passwords.google.com/settings");
404 NavigateAndCommit(kUrl); 329 NavigateAndCommit(kUrl);
405 EXPECT_EQ(kUrl, GetClient()->GetLastCommittedEntryURL()); 330 EXPECT_EQ(kUrl, GetClient()->GetLastCommittedEntryURL());
406 } 331 }
332
333 TEST_F(ChromePasswordManagerClientTest, WebUINoLogging) {
334 // Make sure that logging is active.
335 password_manager::LogRouter* log_router = password_manager::
336 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile());
337 DummyLogReceiver log_receiver;
338 EXPECT_EQ(std::string(), log_router->RegisterReceiver(&log_receiver));
339
340 // But then navigate to a WebUI, there the logging should not be active.
341 NavigateAndCommit(GURL("about:password-manager-internals"));
342 EXPECT_FALSE(GetClient()->GetLogManager()->IsLoggingActive());
343
344 log_router->UnregisterReceiver(&log_receiver);
345 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698