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

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

Issue 1031153002: [Credential Management] Smart lock save Credentials bubble should not always pop up. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months 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/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/testing_pref_service.h"
8 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/sync/profile_sync_service_factory.h" 13 #include "chrome/browser/sync/profile_sync_service_factory.h"
11 #include "chrome/browser/sync/profile_sync_service_mock.h" 14 #include "chrome/browser/sync/profile_sync_service_mock.h"
12 #include "chrome/common/chrome_version_info.h" 15 #include "chrome/common/chrome_version_info.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
14 #include "chrome/test/base/testing_profile.h" 18 #include "chrome/test/base/testing_profile.h"
15 #include "components/autofill/content/common/autofill_messages.h" 19 #include "components/autofill/content/common/autofill_messages.h"
16 #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"
17 #include "components/password_manager/content/common/credential_manager_messages .h" 21 #include "components/password_manager/content/common/credential_manager_messages .h"
18 #include "components/password_manager/core/browser/log_receiver.h" 22 #include "components/password_manager/core/browser/log_receiver.h"
19 #include "components/password_manager/core/browser/password_manager_internals_se rvice.h" 23 #include "components/password_manager/core/browser/password_manager_internals_se rvice.h"
20 #include "components/password_manager/core/common/credential_manager_types.h" 24 #include "components/password_manager/core/common/credential_manager_types.h"
25 #include "components/password_manager/core/common/password_manager_pref_names.h"
21 #include "components/password_manager/core/common/password_manager_switches.h" 26 #include "components/password_manager/core/common/password_manager_switches.h"
22 #include "content/public/browser/browser_context.h" 27 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/mock_render_process_host.h" 29 #include "content/public/test/mock_render_process_host.h"
25 #include "testing/gmock/include/gmock/gmock.h" 30 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
27 32
28 using content::BrowserContext; 33 using content::BrowserContext;
29 using content::WebContents; 34 using content::WebContents;
30 using testing::Return; 35 using testing::Return;
31 using testing::_; 36 using testing::_;
32 37
33 namespace { 38 namespace {
34 39
35 const char kTestText[] = "abcd1234"; 40 const char kTestText[] = "abcd1234";
36 41
37 class MockLogReceiver : public password_manager::LogReceiver { 42 class MockLogReceiver : public password_manager::LogReceiver {
38 public: 43 public:
39 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); 44 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
40 }; 45 };
41 46
42 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { 47 class MockChromePasswordManagerClient : public ChromePasswordManagerClient {
vabr (Chromium) 2015/04/07 13:53:15 Could you please add the following comment? // TOD
melandory 2015/04/07 14:28:52 Yeah, I totally understand that. I tried not to us
43 public: 48 public:
49 MOCK_CONST_METHOD0(IsPasswordManagementEnabledForCurrentPage, bool());
50 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool());
44 MOCK_CONST_METHOD2(IsSyncAccountCredential, 51 MOCK_CONST_METHOD2(IsSyncAccountCredential,
45 bool(const std::string& username, 52 bool(const std::string& username,
46 const std::string& origin)); 53 const std::string& origin));
47 54
48 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) 55 explicit MockChromePasswordManagerClient(content::WebContents* web_contents)
49 : ChromePasswordManagerClient(web_contents, nullptr) {} 56 : ChromePasswordManagerClient(web_contents, nullptr) {
57 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors())
58 .WillByDefault(testing::Return(false));
59 ON_CALL(*this, IsPasswordManagementEnabledForCurrentPage())
60 .WillByDefault(testing::Return(true));
61 }
50 ~MockChromePasswordManagerClient() override {} 62 ~MockChromePasswordManagerClient() override {}
51 63
52 private: 64 private:
53 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); 65 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient);
54 }; 66 };
55 67
56 } // namespace 68 } // namespace
57 69
58 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { 70 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
59 public: 71 public:
60 ChromePasswordManagerClientTest(); 72 ChromePasswordManagerClientTest();
61 73
62 virtual void SetUp() override; 74 virtual void SetUp() override;
63 75
76 TestingPrefServiceSyncable* prefs() {
77 return profile()->GetTestingPrefService();
78 }
79
64 protected: 80 protected:
65 ChromePasswordManagerClient* GetClient(); 81 ChromePasswordManagerClient* GetClient();
66 82
67 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then 83 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
68 // copies its argument into |activation_flag| and returns true. Otherwise 84 // copies its argument into |activation_flag| and returns true. Otherwise
69 // returns false. 85 // returns false.
70 bool WasLoggingActivationMessageSent(bool* activation_flag); 86 bool WasLoggingActivationMessageSent(bool* activation_flag);
71 87
72 password_manager::PasswordManagerInternalsService* service_; 88 password_manager::PasswordManagerInternalsService* service_;
73 89
74 testing::StrictMock<MockLogReceiver> receiver_; 90 testing::StrictMock<MockLogReceiver> receiver_;
91 TestingPrefServiceSimple prefs_;
75 }; 92 };
76 93
77 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest() 94 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest()
78 : service_(nullptr) { 95 : service_(nullptr) {
79 } 96 }
80 97
81 void ChromePasswordManagerClientTest::SetUp() { 98 void ChromePasswordManagerClientTest::SetUp() {
82 ChromeRenderViewHostTestHarness::SetUp(); 99 ChromeRenderViewHostTestHarness::SetUp();
100 prefs_.registry()->RegisterBooleanPref(
101 password_manager::prefs::kPasswordManagerSavingEnabled, true);
83 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( 102 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
84 web_contents(), nullptr); 103 web_contents(), nullptr);
85 service_ = password_manager::PasswordManagerInternalsServiceFactory:: 104 service_ = password_manager::PasswordManagerInternalsServiceFactory::
86 GetForBrowserContext(profile()); 105 GetForBrowserContext(profile());
87 ASSERT_TRUE(service_); 106 ASSERT_TRUE(service_);
88 } 107 }
89 108
90 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { 109 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
91 return ChromePasswordManagerClient::FromWebContents(web_contents()); 110 return ChromePasswordManagerClient::FromWebContents(web_contents());
92 } 111 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 command_line->AppendSwitch( 284 command_line->AppendSwitch(
266 password_manager::switches::kDisallowAutofillSyncCredential); 285 password_manager::switches::kDisallowAutofillSyncCredential);
267 client.reset(new MockChromePasswordManagerClient(web_contents())); 286 client.reset(new MockChromePasswordManagerClient(web_contents()));
268 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) 287 EXPECT_CALL(*client, IsSyncAccountCredential(_, _))
269 .WillRepeatedly(Return(true)); 288 .WillRepeatedly(Return(true));
270 NavigateAndCommit(GURL("https://accounts.google.com/Login")); 289 NavigateAndCommit(GURL("https://accounts.google.com/Login"));
271 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); 290 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
272 } 291 }
273 292
274 TEST_F(ChromePasswordManagerClientTest, 293 TEST_F(ChromePasswordManagerClientTest,
275 IsPasswordManagerEnabledForCurrentPage) { 294 IsPasswordManagementEnabledForCurrentPage) {
276 ChromePasswordManagerClient* client = GetClient(); 295 ChromePasswordManagerClient* client = GetClient();
277 NavigateAndCommit( 296 NavigateAndCommit(
278 GURL("https://accounts.google.com/ServiceLogin?continue=" 297 GURL("https://accounts.google.com/ServiceLogin?continue="
279 "https://passwords.google.com/settings&rart=123")); 298 "https://passwords.google.com/settings&rart=123"));
280 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 299 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
281 300
282 // Password site is inaccesible via HTTP, but because of HSTS the following 301 // Password site is inaccesible via HTTP, but because of HSTS the following
283 // link should still continue to https://passwords.google.com. 302 // link should still continue to https://passwords.google.com.
284 NavigateAndCommit( 303 NavigateAndCommit(
285 GURL("https://accounts.google.com/ServiceLogin?continue=" 304 GURL("https://accounts.google.com/ServiceLogin?continue="
286 "http://passwords.google.com/settings&rart=123")); 305 "http://passwords.google.com/settings&rart=123"));
287 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 306 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
307 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
288 308
289 // Specifying default port still passes. 309 // Specifying default port still passes.
290 NavigateAndCommit( 310 NavigateAndCommit(
291 GURL("https://accounts.google.com/ServiceLogin?continue=" 311 GURL("https://accounts.google.com/ServiceLogin?continue="
292 "https://passwords.google.com:443/settings&rart=123")); 312 "https://passwords.google.com:443/settings&rart=123"));
293 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 313 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
314 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
294 315
295 // Encoded URL is considered the same. 316 // Encoded URL is considered the same.
296 NavigateAndCommit( 317 NavigateAndCommit(
297 GURL("https://accounts.google.com/ServiceLogin?continue=" 318 GURL("https://accounts.google.com/ServiceLogin?continue="
298 "https://passwords.%67oogle.com/settings&rart=123")); 319 "https://passwords.%67oogle.com/settings&rart=123"));
299 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 320 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
321 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
300 322
301 // Make sure testing sites are disabled as well. 323 // Make sure testing sites are disabled as well.
302 NavigateAndCommit( 324 NavigateAndCommit(
303 GURL("https://accounts.google.com/Login?continue=" 325 GURL("https://accounts.google.com/Login?continue="
304 "https://passwords-ac-testing.corp.google.com/settings&rart=456")); 326 "https://passwords-ac-testing.corp.google.com/settings&rart=456"));
305 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 327 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
328 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
306 329
307 // Fully qualified domain name is considered a different hostname by GURL. 330 // Fully qualified domain name is considered a different hostname by GURL.
308 // Ideally this would not be the case, but this quirk can be avoided by 331 // Ideally this would not be the case, but this quirk can be avoided by
309 // verification on the server. This test is simply documentation of this 332 // verification on the server. This test is simply documentation of this
310 // behavior. 333 // behavior.
311 NavigateAndCommit( 334 NavigateAndCommit(
312 GURL("https://accounts.google.com/ServiceLogin?continue=" 335 GURL("https://accounts.google.com/ServiceLogin?continue="
313 "https://passwords.google.com./settings&rart=123")); 336 "https://passwords.google.com./settings&rart=123"));
314 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 337 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
315 338
316 // Not a transactional reauth page. 339 // Not a transactional reauth page.
317 NavigateAndCommit( 340 NavigateAndCommit(
318 GURL("https://accounts.google.com/ServiceLogin?continue=" 341 GURL("https://accounts.google.com/ServiceLogin?continue="
319 "https://passwords.google.com/settings")); 342 "https://passwords.google.com/settings"));
320 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 343 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
321 344
322 // Should be enabled for other transactional reauth pages. 345 // Should be enabled for other transactional reauth pages.
323 NavigateAndCommit( 346 NavigateAndCommit(
324 GURL("https://accounts.google.com/ServiceLogin?continue=" 347 GURL("https://accounts.google.com/ServiceLogin?continue="
325 "https://mail.google.com&rart=234")); 348 "https://mail.google.com&rart=234"));
326 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 349 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
327 350
328 // Reauth pages are only on accounts.google.com 351 // Reauth pages are only on accounts.google.com
329 NavigateAndCommit( 352 NavigateAndCommit(
330 GURL("https://other.site.com/ServiceLogin?continue=" 353 GURL("https://other.site.com/ServiceLogin?continue="
331 "https://passwords.google.com&rart=234")); 354 "https://passwords.google.com&rart=234"));
332 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 355 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
333 } 356 }
334 357
335 TEST_F(ChromePasswordManagerClientTest, IsPasswordSyncEnabled) { 358 TEST_F(ChromePasswordManagerClientTest, IsPasswordSyncEnabled) {
336 ChromePasswordManagerClient* client = GetClient(); 359 ChromePasswordManagerClient* client = GetClient();
337 360
338 ProfileSyncServiceMock* mock_sync_service = 361 ProfileSyncServiceMock* mock_sync_service =
339 static_cast<ProfileSyncServiceMock*>( 362 static_cast<ProfileSyncServiceMock*>(
340 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 363 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
341 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService)); 364 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService));
342 365
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 401
379 // Again, without a custom passphrase. 402 // Again, without a custom passphrase.
380 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase()) 403 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase())
381 .WillRepeatedly(Return(false)); 404 .WillRepeatedly(Return(false));
382 405
383 EXPECT_FALSE( 406 EXPECT_FALSE(
384 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE)); 407 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE));
385 EXPECT_FALSE(client->IsPasswordSyncEnabled( 408 EXPECT_FALSE(client->IsPasswordSyncEnabled(
386 password_manager::WITHOUT_CUSTOM_PASSPHRASE)); 409 password_manager::WITHOUT_CUSTOM_PASSPHRASE));
387 } 410 }
411
412 TEST_F(ChromePasswordManagerClientTest, IsOffTheRecordTest) {
413 ChromePasswordManagerClient* client = GetClient();
414
415 profile()->ForceIncognito(true);
416 EXPECT_TRUE(client->IsOffTheRecord());
417
418 profile()->ForceIncognito(false);
419 EXPECT_FALSE(client->IsOffTheRecord());
420 }
421
422 TEST_F(ChromePasswordManagerClientTest,
423 SavingDependsOnManagerEnabledPreference) {
424 // Test that saving passwords depends on the password manager enabled
425 // preference.
426 ChromePasswordManagerClient* client = GetClient();
427 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
428 new base::FundamentalValue(true));
429 EXPECT_TRUE(client->IsSavingEnabledForCurrentPage());
430 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
431 new base::FundamentalValue(false));
432 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
433 }
434
435 TEST_F(ChromePasswordManagerClientTest, IsSavingEnabledForCurrentPageTest) {
436 scoped_ptr<MockChromePasswordManagerClient> client(
437 new MockChromePasswordManagerClient(web_contents()));
438 // Functionality disabled if there is SSL errors.
439 EXPECT_CALL(*client, DidLastPageLoadEncounterSSLErrors())
440 .WillRepeatedly(Return(true));
441 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
442 // Functionality disabled if there are SSL errors and the manager itself is
vabr (Chromium) 2015/04/07 13:53:15 nit: Please separate individual blocks of the test
melandory 2015/04/07 14:28:52 Done.
443 // disabled.
444 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
445 new base::FundamentalValue(false));
446 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
447 // Functionality disabled if there are no SSL errorsm, but the manager itself
448 // is
vabr (Chromium) 2015/04/07 13:53:15 nit: Please join the lines 448 and 449.
melandory 2015/04/07 14:28:52 Done.
449 // disabled.
450 EXPECT_CALL(*client, DidLastPageLoadEncounterSSLErrors())
451 .WillRepeatedly(Return(false));
452 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
453 new base::FundamentalValue(false));
454 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
455 // Functionality enabled if there are no SSL errors and the manager is
456 // enabled.
457 EXPECT_CALL(*client, DidLastPageLoadEncounterSSLErrors())
458 .WillRepeatedly(Return(false));
459 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
460 new base::FundamentalValue(true));
461 EXPECT_TRUE(client->IsSavingEnabledForCurrentPage());
462
463 // Functionality disabled in Incognito mode.
464 profile()->ForceIncognito(true);
465 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
466
467 // Functionality disabled in Incognito mode also when manager itself is
468 // enabled.
469 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
470 new base::FundamentalValue(true));
471 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
472 profile()->ForceIncognito(false);
473 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698