Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / browsing_data / passwords_counter_browsertest.cc
blob32504bc17e8cc25c1f09e2213b507c45e097ca35
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/browsing_data/passwords_counter.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/password_manager/password_store_factory.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "components/autofill/core/common/password_form.h"
16 namespace {
18 using autofill::PasswordForm;
20 class PasswordsCounterTest : public InProcessBrowserTest {
21 public:
22 void AddLogin(const std::string& origin,
23 const std::string& username,
24 bool blacklisted) {
25 PasswordStoreFactory::GetForProfile(
26 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)->
27 AddLogin(CreateCredentials(origin, username, blacklisted));
28 base::RunLoop().RunUntilIdle();
31 void RemoveLogin(const std::string& origin,
32 const std::string& username,
33 bool blacklisted) {
34 PasswordStoreFactory::GetForProfile(
35 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)->
36 RemoveLogin(CreateCredentials(origin, username, blacklisted));
37 base::RunLoop().RunUntilIdle();
40 void SetPasswordsDeletionPref(bool value) {
41 browser()->profile()->GetPrefs()->SetBoolean(
42 prefs::kDeletePasswords, value);
45 void WaitForCounting() {
46 run_loop_.reset(new base::RunLoop());
47 run_loop_->Run();
50 uint32 GetResult() {
51 DCHECK(finished_);
52 return result_;
55 void Callback(bool finished, uint32 count) {
56 finished_ = finished;
57 result_ = count;
58 if (run_loop_ && finished)
59 run_loop_->Quit();
62 private:
63 PasswordForm CreateCredentials(const std::string& origin,
64 const std::string& username,
65 bool blacklisted) {
66 PasswordForm result;
67 result.signon_realm = origin;
68 result.origin = GURL(origin);
69 result.username_value = base::ASCIIToUTF16(username);
70 result.password_value = base::ASCIIToUTF16("hunter2");
71 result.blacklisted_by_user = blacklisted;
72 return result;
75 scoped_ptr<base::RunLoop> run_loop_;
77 bool finished_;
78 uint32 result_;
81 // Tests that the counter correctly counts each individual credential on
82 // the same domain.
83 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) {
84 SetPasswordsDeletionPref(true);
85 AddLogin("https://www.google.com", "user1", false);
86 AddLogin("https://www.google.com", "user2", false);
87 AddLogin("https://www.google.com", "user3", false);
88 AddLogin("https://www.chrome.com", "user1", false);
89 AddLogin("https://www.chrome.com", "user2", false);
91 PasswordsCounter counter;
92 counter.Init(browser()->profile(),
93 base::Bind(&PasswordsCounterTest::Callback,
94 base::Unretained(this)));
96 WaitForCounting();
97 EXPECT_EQ(5u, GetResult());
100 // Tests that the counter doesn't count blacklisted entries.
101 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) {
102 SetPasswordsDeletionPref(true);
103 AddLogin("https://www.google.com", "user1", false);
104 AddLogin("https://www.google.com", "user2", true);
105 AddLogin("https://www.chrome.com", "user3", true);
107 PasswordsCounter counter;
108 counter.Init(browser()->profile(),
109 base::Bind(&PasswordsCounterTest::Callback,
110 base::Unretained(this)));
112 WaitForCounting();
113 EXPECT_EQ(1u, GetResult());
116 // Tests that the counter starts counting automatically when the deletion
117 // pref changes to true.
118 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) {
119 SetPasswordsDeletionPref(false);
120 AddLogin("https://www.google.com", "user", false);
121 AddLogin("https://www.chrome.com", "user", false);
123 PasswordsCounter counter;
124 counter.Init(browser()->profile(),
125 base::Bind(&PasswordsCounterTest::Callback,
126 base::Unretained(this)));
127 SetPasswordsDeletionPref(true);
129 WaitForCounting();
130 EXPECT_EQ(2u, GetResult());
133 // Tests that the counter does not count passwords if the deletion
134 // preference is false.
135 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefIsFalse) {
136 SetPasswordsDeletionPref(false);
137 AddLogin("https://www.google.com", "user", false);
139 PasswordsCounter counter;
140 counter.Init(browser()->profile(),
141 base::Bind(&PasswordsCounterTest::Callback,
142 base::Unretained(this)));
144 EXPECT_FALSE(counter.cancelable_task_tracker()->HasTrackedTasks());
147 // Tests that the counter starts counting automatically when
148 // the password store changes.
149 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) {
150 SetPasswordsDeletionPref(true);
151 AddLogin("https://www.google.com", "user", false);
153 PasswordsCounter counter;
154 counter.Init(browser()->profile(),
155 base::Bind(&PasswordsCounterTest::Callback,
156 base::Unretained(this)));
158 WaitForCounting();
159 EXPECT_EQ(1u, GetResult());
161 AddLogin("https://www.chrome.com", "user", false);
162 WaitForCounting();
163 EXPECT_EQ(2u, GetResult());
165 RemoveLogin("https://www.chrome.com", "user", false);
166 WaitForCounting();
167 EXPECT_EQ(1u, GetResult());
170 } // namespace