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

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
47 // TODO(vabr): Get rid of the mocked client in the client's own test, see
48 // http://crbug.com/474577.
42 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { 49 class MockChromePasswordManagerClient : public ChromePasswordManagerClient {
43 public: 50 public:
51 MOCK_CONST_METHOD0(IsPasswordManagementEnabledForCurrentPage, bool());
52 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool());
44 MOCK_CONST_METHOD2(IsSyncAccountCredential, 53 MOCK_CONST_METHOD2(IsSyncAccountCredential,
45 bool(const std::string& username, 54 bool(const std::string& username,
46 const std::string& origin)); 55 const std::string& origin));
47 56
48 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) 57 explicit MockChromePasswordManagerClient(content::WebContents* web_contents)
49 : ChromePasswordManagerClient(web_contents, nullptr) {} 58 : ChromePasswordManagerClient(web_contents, nullptr) {
59 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors())
60 .WillByDefault(testing::Return(false));
61 ON_CALL(*this, IsPasswordManagementEnabledForCurrentPage())
62 .WillByDefault(testing::Return(true));
63 }
50 ~MockChromePasswordManagerClient() override {} 64 ~MockChromePasswordManagerClient() override {}
51 65
52 private: 66 private:
53 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); 67 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient);
54 }; 68 };
55 69
56 } // namespace 70 } // namespace
57 71
58 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { 72 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
59 public: 73 public:
60 ChromePasswordManagerClientTest(); 74 ChromePasswordManagerClientTest();
61 75
62 virtual void SetUp() override; 76 virtual void SetUp() override;
63 77
78 TestingPrefServiceSyncable* prefs() {
79 return profile()->GetTestingPrefService();
80 }
81
64 protected: 82 protected:
65 ChromePasswordManagerClient* GetClient(); 83 ChromePasswordManagerClient* GetClient();
66 84
67 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then 85 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
68 // copies its argument into |activation_flag| and returns true. Otherwise 86 // copies its argument into |activation_flag| and returns true. Otherwise
69 // returns false. 87 // returns false.
70 bool WasLoggingActivationMessageSent(bool* activation_flag); 88 bool WasLoggingActivationMessageSent(bool* activation_flag);
71 89
72 password_manager::PasswordManagerInternalsService* service_; 90 password_manager::PasswordManagerInternalsService* service_;
73 91
74 testing::StrictMock<MockLogReceiver> receiver_; 92 testing::StrictMock<MockLogReceiver> receiver_;
93 TestingPrefServiceSimple prefs_;
75 }; 94 };
76 95
77 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest() 96 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest()
78 : service_(nullptr) { 97 : service_(nullptr) {
79 } 98 }
80 99
81 void ChromePasswordManagerClientTest::SetUp() { 100 void ChromePasswordManagerClientTest::SetUp() {
82 ChromeRenderViewHostTestHarness::SetUp(); 101 ChromeRenderViewHostTestHarness::SetUp();
102 prefs_.registry()->RegisterBooleanPref(
103 password_manager::prefs::kPasswordManagerSavingEnabled, true);
83 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( 104 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
84 web_contents(), nullptr); 105 web_contents(), nullptr);
85 service_ = password_manager::PasswordManagerInternalsServiceFactory:: 106 service_ = password_manager::PasswordManagerInternalsServiceFactory::
86 GetForBrowserContext(profile()); 107 GetForBrowserContext(profile());
87 ASSERT_TRUE(service_); 108 ASSERT_TRUE(service_);
88 } 109 }
89 110
90 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { 111 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
91 return ChromePasswordManagerClient::FromWebContents(web_contents()); 112 return ChromePasswordManagerClient::FromWebContents(web_contents());
92 } 113 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 command_line->AppendSwitch( 286 command_line->AppendSwitch(
266 password_manager::switches::kDisallowAutofillSyncCredential); 287 password_manager::switches::kDisallowAutofillSyncCredential);
267 client.reset(new MockChromePasswordManagerClient(web_contents())); 288 client.reset(new MockChromePasswordManagerClient(web_contents()));
268 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) 289 EXPECT_CALL(*client, IsSyncAccountCredential(_, _))
269 .WillRepeatedly(Return(true)); 290 .WillRepeatedly(Return(true));
270 NavigateAndCommit(GURL("https://accounts.google.com/Login")); 291 NavigateAndCommit(GURL("https://accounts.google.com/Login"));
271 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); 292 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
272 } 293 }
273 294
274 TEST_F(ChromePasswordManagerClientTest, 295 TEST_F(ChromePasswordManagerClientTest,
275 IsPasswordManagerEnabledForCurrentPage) { 296 IsPasswordManagementEnabledForCurrentPage) {
276 ChromePasswordManagerClient* client = GetClient(); 297 ChromePasswordManagerClient* client = GetClient();
277 NavigateAndCommit( 298 NavigateAndCommit(
278 GURL("https://accounts.google.com/ServiceLogin?continue=" 299 GURL("https://accounts.google.com/ServiceLogin?continue="
279 "https://passwords.google.com/settings&rart=123")); 300 "https://passwords.google.com/settings&rart=123"));
280 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 301 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
281 302
282 // Password site is inaccesible via HTTP, but because of HSTS the following 303 // Password site is inaccesible via HTTP, but because of HSTS the following
283 // link should still continue to https://passwords.google.com. 304 // link should still continue to https://passwords.google.com.
284 NavigateAndCommit( 305 NavigateAndCommit(
285 GURL("https://accounts.google.com/ServiceLogin?continue=" 306 GURL("https://accounts.google.com/ServiceLogin?continue="
286 "http://passwords.google.com/settings&rart=123")); 307 "http://passwords.google.com/settings&rart=123"));
287 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 308 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
309 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
288 310
289 // Specifying default port still passes. 311 // Specifying default port still passes.
290 NavigateAndCommit( 312 NavigateAndCommit(
291 GURL("https://accounts.google.com/ServiceLogin?continue=" 313 GURL("https://accounts.google.com/ServiceLogin?continue="
292 "https://passwords.google.com:443/settings&rart=123")); 314 "https://passwords.google.com:443/settings&rart=123"));
293 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 315 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
316 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
294 317
295 // Encoded URL is considered the same. 318 // Encoded URL is considered the same.
296 NavigateAndCommit( 319 NavigateAndCommit(
297 GURL("https://accounts.google.com/ServiceLogin?continue=" 320 GURL("https://accounts.google.com/ServiceLogin?continue="
298 "https://passwords.%67oogle.com/settings&rart=123")); 321 "https://passwords.%67oogle.com/settings&rart=123"));
299 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 322 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
323 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
300 324
301 // Make sure testing sites are disabled as well. 325 // Make sure testing sites are disabled as well.
302 NavigateAndCommit( 326 NavigateAndCommit(
303 GURL("https://accounts.google.com/Login?continue=" 327 GURL("https://accounts.google.com/Login?continue="
304 "https://passwords-ac-testing.corp.google.com/settings&rart=456")); 328 "https://passwords-ac-testing.corp.google.com/settings&rart=456"));
305 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage()); 329 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
330 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage());
306 331
307 // Fully qualified domain name is considered a different hostname by GURL. 332 // 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 333 // 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 334 // verification on the server. This test is simply documentation of this
310 // behavior. 335 // behavior.
311 NavigateAndCommit( 336 NavigateAndCommit(
312 GURL("https://accounts.google.com/ServiceLogin?continue=" 337 GURL("https://accounts.google.com/ServiceLogin?continue="
313 "https://passwords.google.com./settings&rart=123")); 338 "https://passwords.google.com./settings&rart=123"));
314 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 339 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
315 340
316 // Not a transactional reauth page. 341 // Not a transactional reauth page.
317 NavigateAndCommit( 342 NavigateAndCommit(
318 GURL("https://accounts.google.com/ServiceLogin?continue=" 343 GURL("https://accounts.google.com/ServiceLogin?continue="
319 "https://passwords.google.com/settings")); 344 "https://passwords.google.com/settings"));
320 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 345 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
321 346
322 // Should be enabled for other transactional reauth pages. 347 // Should be enabled for other transactional reauth pages.
323 NavigateAndCommit( 348 NavigateAndCommit(
324 GURL("https://accounts.google.com/ServiceLogin?continue=" 349 GURL("https://accounts.google.com/ServiceLogin?continue="
325 "https://mail.google.com&rart=234")); 350 "https://mail.google.com&rart=234"));
326 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 351 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
327 352
328 // Reauth pages are only on accounts.google.com 353 // Reauth pages are only on accounts.google.com
329 NavigateAndCommit( 354 NavigateAndCommit(
330 GURL("https://other.site.com/ServiceLogin?continue=" 355 GURL("https://other.site.com/ServiceLogin?continue="
331 "https://passwords.google.com&rart=234")); 356 "https://passwords.google.com&rart=234"));
332 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage()); 357 EXPECT_TRUE(client->IsPasswordManagementEnabledForCurrentPage());
333 } 358 }
334 359
335 TEST_F(ChromePasswordManagerClientTest, IsPasswordSyncEnabled) { 360 TEST_F(ChromePasswordManagerClientTest, IsPasswordSyncEnabled) {
336 ChromePasswordManagerClient* client = GetClient(); 361 ChromePasswordManagerClient* client = GetClient();
337 362
338 ProfileSyncServiceMock* mock_sync_service = 363 ProfileSyncServiceMock* mock_sync_service =
339 static_cast<ProfileSyncServiceMock*>( 364 static_cast<ProfileSyncServiceMock*>(
340 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 365 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
341 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService)); 366 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService));
342 367
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 403
379 // Again, without a custom passphrase. 404 // Again, without a custom passphrase.
380 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase()) 405 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase())
381 .WillRepeatedly(Return(false)); 406 .WillRepeatedly(Return(false));
382 407
383 EXPECT_FALSE( 408 EXPECT_FALSE(
384 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE)); 409 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE));
385 EXPECT_FALSE(client->IsPasswordSyncEnabled( 410 EXPECT_FALSE(client->IsPasswordSyncEnabled(
386 password_manager::WITHOUT_CUSTOM_PASSPHRASE)); 411 password_manager::WITHOUT_CUSTOM_PASSPHRASE));
387 } 412 }
413
414 TEST_F(ChromePasswordManagerClientTest, IsOffTheRecordTest) {
415 ChromePasswordManagerClient* client = GetClient();
416
417 profile()->ForceIncognito(true);
418 EXPECT_TRUE(client->IsOffTheRecord());
419
420 profile()->ForceIncognito(false);
421 EXPECT_FALSE(client->IsOffTheRecord());
422 }
423
424 TEST_F(ChromePasswordManagerClientTest,
425 SavingDependsOnManagerEnabledPreference) {
426 // Test that saving passwords depends on the password manager enabled
427 // preference.
428 ChromePasswordManagerClient* client = GetClient();
429 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
430 new base::FundamentalValue(true));
431 EXPECT_TRUE(client->IsSavingEnabledForCurrentPage());
432 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
433 new base::FundamentalValue(false));
434 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
435 }
436
437 TEST_F(ChromePasswordManagerClientTest, IsSavingEnabledForCurrentPageTest) {
438 scoped_ptr<MockChromePasswordManagerClient> client(
439 new MockChromePasswordManagerClient(web_contents()));
440 // Functionality disabled if there is SSL errors.
441 EXPECT_CALL(*client, DidLastPageLoadEncounterSSLErrors())
442 .WillRepeatedly(Return(true));
443 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
444
445 // Functionality disabled if there are SSL errors and the manager itself is
446 // disabled.
447 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
448 new base::FundamentalValue(false));
449 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
450
451 // Functionality disabled if there are no SSL errorsm, but the manager itself
452 // is disabled.
453 EXPECT_CALL(*client, DidLastPageLoadEncounterSSLErrors())
454 .WillRepeatedly(Return(false));
455 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
456 new base::FundamentalValue(false));
457 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
458
459 // Functionality enabled if there are no SSL errors and the manager is
460 // enabled.
461 EXPECT_CALL(*client, DidLastPageLoadEncounterSSLErrors())
462 .WillRepeatedly(Return(false));
463 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
464 new base::FundamentalValue(true));
465 EXPECT_TRUE(client->IsSavingEnabledForCurrentPage());
466
467 // Functionality disabled in Incognito mode.
468 profile()->ForceIncognito(true);
469 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
470
471 // Functionality disabled in Incognito mode also when manager itself is
472 // enabled.
473 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled,
474 new base::FundamentalValue(true));
475 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage());
476 profile()->ForceIncognito(false);
477 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698