OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 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/browsing_data/autofill_counter.h" | 5 #include "chrome/browser/browsing_data/autofill_counter.h" |
6 | 6 |
7 #include "base/guid.h" | 7 #include "base/guid.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/web_data_service_factory.h" | 16 #include "chrome/browser/web_data_service_factory.h" |
16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "components/autofill/core/browser/autofill_profile.h" | 19 #include "components/autofill/core/browser/autofill_profile.h" |
19 #include "components/autofill/core/browser/autofill_type.h" | 20 #include "components/autofill/core/browser/autofill_type.h" |
20 #include "components/autofill/core/browser/credit_card.h" | 21 #include "components/autofill/core/browser/credit_card.h" |
21 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 23 #include "components/browsing_data/browsing_data_utils.h" |
| 24 #include "components/browsing_data/pref_names.h" |
22 #include "components/prefs/pref_service.h" | 25 #include "components/prefs/pref_service.h" |
23 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 class AutofillCounterTest : public InProcessBrowserTest { | 30 class AutofillCounterTest : public InProcessBrowserTest { |
28 public: | 31 public: |
29 AutofillCounterTest() {} | 32 AutofillCounterTest() {} |
30 ~AutofillCounterTest() override {} | 33 ~AutofillCounterTest() override {} |
31 | 34 |
32 void SetUpOnMainThread() override { | 35 void SetUpOnMainThread() override { |
33 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile( | 36 web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile( |
34 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); | 37 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); |
35 | 38 |
36 SetAutofillDeletionPref(true); | 39 SetAutofillDeletionPref(true); |
37 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 40 SetDeletionPeriodPref(browsing_data::EVERYTHING); |
38 } | 41 } |
39 | 42 |
40 // Autocomplete suggestions -------------------------------------------------- | 43 // Autocomplete suggestions -------------------------------------------------- |
41 | 44 |
42 void AddAutocompleteSuggestion(const std::string& name, | 45 void AddAutocompleteSuggestion(const std::string& name, |
43 const std::string& value) { | 46 const std::string& value) { |
44 autofill::FormFieldData field; | 47 autofill::FormFieldData field; |
45 field.name = base::ASCIIToUTF16(name); | 48 field.name = base::ASCIIToUTF16(name); |
46 field.value = base::ASCIIToUTF16(value); | 49 field.value = base::ASCIIToUTF16(value); |
47 | 50 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 130 |
128 run_loop_->Run(); | 131 run_loop_->Run(); |
129 } | 132 } |
130 | 133 |
131 // Other utils --------------------------------------------------------------- | 134 // Other utils --------------------------------------------------------------- |
132 | 135 |
133 void SetAutofillDeletionPref(bool value) { | 136 void SetAutofillDeletionPref(bool value) { |
134 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteFormData, value); | 137 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteFormData, value); |
135 } | 138 } |
136 | 139 |
137 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { | 140 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
138 browser()->profile()->GetPrefs()->SetInteger( | 141 browser()->profile()->GetPrefs()->SetInteger( |
139 prefs::kDeleteTimePeriod, static_cast<int>(period)); | 142 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); |
140 } | 143 } |
141 | 144 |
142 // Callback and result retrieval --------------------------------------------- | 145 // Callback and result retrieval --------------------------------------------- |
143 | 146 |
144 void WaitForCounting() { | 147 void WaitForCounting() { |
145 run_loop_.reset(new base::RunLoop()); | 148 run_loop_.reset(new base::RunLoop()); |
146 run_loop_->Run(); | 149 run_loop_->Run(); |
147 } | 150 } |
148 | 151 |
149 BrowsingDataCounter::ResultInt GetNumSuggestions() { | 152 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { |
150 DCHECK(finished_); | 153 DCHECK(finished_); |
151 return num_suggestions_; | 154 return num_suggestions_; |
152 } | 155 } |
153 | 156 |
154 BrowsingDataCounter::ResultInt GetNumCreditCards() { | 157 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() { |
155 DCHECK(finished_); | 158 DCHECK(finished_); |
156 return num_credit_cards_; | 159 return num_credit_cards_; |
157 } | 160 } |
158 | 161 |
159 BrowsingDataCounter::ResultInt GetNumAddresses() { | 162 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() { |
160 DCHECK(finished_); | 163 DCHECK(finished_); |
161 return num_addresses_; | 164 return num_addresses_; |
162 } | 165 } |
163 | 166 |
164 void Callback(std::unique_ptr<BrowsingDataCounter::Result> result) { | 167 void Callback( |
| 168 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
165 finished_ = result->Finished(); | 169 finished_ = result->Finished(); |
166 | 170 |
167 if (finished_) { | 171 if (finished_) { |
168 AutofillCounter::AutofillResult* autofill_result = | 172 AutofillCounter::AutofillResult* autofill_result = |
169 static_cast<AutofillCounter::AutofillResult*>(result.get()); | 173 static_cast<AutofillCounter::AutofillResult*>(result.get()); |
170 | 174 |
171 num_suggestions_ = autofill_result->Value(); | 175 num_suggestions_ = autofill_result->Value(); |
172 num_credit_cards_ = autofill_result->num_credit_cards(); | 176 num_credit_cards_ = autofill_result->num_credit_cards(); |
173 num_addresses_ = autofill_result->num_addresses(); | 177 num_addresses_ = autofill_result->num_addresses(); |
174 } | 178 } |
175 | 179 |
176 if (run_loop_ && finished_) | 180 if (run_loop_ && finished_) |
177 run_loop_->Quit(); | 181 run_loop_->Quit(); |
178 } | 182 } |
179 | 183 |
180 private: | 184 private: |
181 std::unique_ptr<base::RunLoop> run_loop_; | 185 std::unique_ptr<base::RunLoop> run_loop_; |
182 | 186 |
183 std::vector<std::string> credit_card_ids_; | 187 std::vector<std::string> credit_card_ids_; |
184 std::vector<std::string> address_ids_; | 188 std::vector<std::string> address_ids_; |
185 | 189 |
186 scoped_refptr<autofill::AutofillWebDataService> web_data_service_; | 190 scoped_refptr<autofill::AutofillWebDataService> web_data_service_; |
187 | 191 |
188 bool finished_; | 192 bool finished_; |
189 BrowsingDataCounter::ResultInt num_suggestions_; | 193 browsing_data::BrowsingDataCounter::ResultInt num_suggestions_; |
190 BrowsingDataCounter::ResultInt num_credit_cards_; | 194 browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_; |
191 BrowsingDataCounter::ResultInt num_addresses_; | 195 browsing_data::BrowsingDataCounter::ResultInt num_addresses_; |
192 | 196 |
193 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); | 197 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); |
194 }; | 198 }; |
195 | 199 |
196 // Tests that the counter does not count when the form data deletion preference | 200 // Tests that the counter does not count when the form data deletion preference |
197 // is false. | 201 // is false. |
198 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, PrefIsFalse) { | 202 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, PrefIsFalse) { |
199 SetAutofillDeletionPref(false); | 203 SetAutofillDeletionPref(false); |
200 | 204 |
201 AutofillCounter counter; | 205 Profile* profile = browser()->profile(); |
202 counter.Init(browser()->profile(), | 206 AutofillCounter counter(profile); |
203 base::Bind(&AutofillCounterTest::Callback, | 207 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
204 base::Unretained(this))); | 208 base::Unretained(this))); |
205 counter.Restart(); | 209 counter.Restart(); |
206 | 210 |
207 EXPECT_FALSE(counter.HasPendingQuery()); | 211 EXPECT_FALSE(counter.HasPendingQuery()); |
208 } | 212 } |
209 | 213 |
210 // Tests that we count the correct number of autocomplete suggestions. | 214 // Tests that we count the correct number of autocomplete suggestions. |
211 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { | 215 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { |
212 AutofillCounter counter; | 216 Profile* profile = browser()->profile(); |
213 counter.Init(browser()->profile(), | 217 AutofillCounter counter(profile); |
214 base::Bind(&AutofillCounterTest::Callback, | 218 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
215 base::Unretained(this))); | 219 base::Unretained(this))); |
216 counter.Restart(); | 220 counter.Restart(); |
217 WaitForCounting(); | 221 WaitForCounting(); |
218 EXPECT_EQ(0, GetNumSuggestions()); | 222 EXPECT_EQ(0, GetNumSuggestions()); |
219 | 223 |
220 AddAutocompleteSuggestion("email", "example@example.com"); | 224 AddAutocompleteSuggestion("email", "example@example.com"); |
221 counter.Restart(); | 225 counter.Restart(); |
222 WaitForCounting(); | 226 WaitForCounting(); |
223 EXPECT_EQ(1, GetNumSuggestions()); | 227 EXPECT_EQ(1, GetNumSuggestions()); |
224 | 228 |
225 AddAutocompleteSuggestion("tel", "+123456789"); | 229 AddAutocompleteSuggestion("tel", "+123456789"); |
(...skipping 12 matching lines...) Expand all Loading... |
238 EXPECT_EQ(2, GetNumSuggestions()); | 242 EXPECT_EQ(2, GetNumSuggestions()); |
239 | 243 |
240 ClearAutocompleteSuggestions(); | 244 ClearAutocompleteSuggestions(); |
241 counter.Restart(); | 245 counter.Restart(); |
242 WaitForCounting(); | 246 WaitForCounting(); |
243 EXPECT_EQ(0, GetNumSuggestions()); | 247 EXPECT_EQ(0, GetNumSuggestions()); |
244 } | 248 } |
245 | 249 |
246 // Tests that we count the correct number of credit cards. | 250 // Tests that we count the correct number of credit cards. |
247 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { | 251 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { |
248 AutofillCounter counter; | 252 Profile* profile = browser()->profile(); |
249 counter.Init(browser()->profile(), | 253 AutofillCounter counter(profile); |
250 base::Bind(&AutofillCounterTest::Callback, | 254 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
251 base::Unretained(this))); | 255 base::Unretained(this))); |
252 counter.Restart(); | 256 counter.Restart(); |
253 WaitForCounting(); | 257 WaitForCounting(); |
254 EXPECT_EQ(0, GetNumCreditCards()); | 258 EXPECT_EQ(0, GetNumCreditCards()); |
255 | 259 |
256 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 260 AddCreditCard("0000-0000-0000-0000", 1, 2015); |
257 counter.Restart(); | 261 counter.Restart(); |
258 WaitForCounting(); | 262 WaitForCounting(); |
259 EXPECT_EQ(1, GetNumCreditCards()); | 263 EXPECT_EQ(1, GetNumCreditCards()); |
260 | 264 |
261 AddCreditCard("0123-4567-8910-1112", 10, 2015); | 265 AddCreditCard("0123-4567-8910-1112", 10, 2015); |
(...skipping 12 matching lines...) Expand all Loading... |
274 EXPECT_EQ(2, GetNumCreditCards()); | 278 EXPECT_EQ(2, GetNumCreditCards()); |
275 | 279 |
276 ClearCreditCardsAndAddresses(); | 280 ClearCreditCardsAndAddresses(); |
277 counter.Restart(); | 281 counter.Restart(); |
278 WaitForCounting(); | 282 WaitForCounting(); |
279 EXPECT_EQ(0, GetNumCreditCards()); | 283 EXPECT_EQ(0, GetNumCreditCards()); |
280 } | 284 } |
281 | 285 |
282 // Tests that we count the correct number of addresses. | 286 // Tests that we count the correct number of addresses. |
283 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) { | 287 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) { |
284 AutofillCounter counter; | 288 Profile* profile = browser()->profile(); |
285 counter.Init(browser()->profile(), | 289 AutofillCounter counter(profile); |
286 base::Bind(&AutofillCounterTest::Callback, | 290 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
287 base::Unretained(this))); | 291 base::Unretained(this))); |
288 counter.Restart(); | 292 counter.Restart(); |
289 WaitForCounting(); | 293 WaitForCounting(); |
290 EXPECT_EQ(0, GetNumAddresses()); | 294 EXPECT_EQ(0, GetNumAddresses()); |
291 | 295 |
292 AddAddress("John", "Doe", "Main Street 12345"); | 296 AddAddress("John", "Doe", "Main Street 12345"); |
293 counter.Restart(); | 297 counter.Restart(); |
294 WaitForCounting(); | 298 WaitForCounting(); |
295 EXPECT_EQ(1, GetNumAddresses()); | 299 EXPECT_EQ(1, GetNumAddresses()); |
296 | 300 |
297 AddAddress("Jane", "Smith", "Main Street 12346"); | 301 AddAddress("Jane", "Smith", "Main Street 12346"); |
(...skipping 26 matching lines...) Expand all Loading... |
324 AddAutocompleteSuggestion("tel", "+987654321"); | 328 AddAutocompleteSuggestion("tel", "+987654321"); |
325 AddAutocompleteSuggestion("city", "Munich"); | 329 AddAutocompleteSuggestion("city", "Munich"); |
326 | 330 |
327 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 331 AddCreditCard("0000-0000-0000-0000", 1, 2015); |
328 AddCreditCard("1211-1098-7654-3210", 10, 2030); | 332 AddCreditCard("1211-1098-7654-3210", 10, 2030); |
329 | 333 |
330 AddAddress("John", "Doe", "Main Street 12345"); | 334 AddAddress("John", "Doe", "Main Street 12345"); |
331 AddAddress("Jane", "Smith", "Main Street 12346"); | 335 AddAddress("Jane", "Smith", "Main Street 12346"); |
332 AddAddress("John", "Smith", "Side Street 47"); | 336 AddAddress("John", "Smith", "Side Street 47"); |
333 | 337 |
334 AutofillCounter counter; | 338 Profile* profile = browser()->profile(); |
335 counter.Init(browser()->profile(), | 339 AutofillCounter counter(profile); |
336 base::Bind(&AutofillCounterTest::Callback, | 340 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
337 base::Unretained(this))); | 341 base::Unretained(this))); |
338 counter.Restart(); | 342 counter.Restart(); |
339 WaitForCounting(); | 343 WaitForCounting(); |
340 EXPECT_EQ(5, GetNumSuggestions()); | 344 EXPECT_EQ(5, GetNumSuggestions()); |
341 EXPECT_EQ(2, GetNumCreditCards()); | 345 EXPECT_EQ(2, GetNumCreditCards()); |
342 EXPECT_EQ(3, GetNumAddresses()); | 346 EXPECT_EQ(3, GetNumAddresses()); |
343 } | 347 } |
344 | 348 |
345 // Tests that the counting respects time ranges. | 349 // Tests that the counting respects time ranges. |
346 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) { | 350 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) { |
347 // This test makes time comparisons that are precise to a microsecond, but the | 351 // This test makes time comparisons that are precise to a microsecond, but the |
(...skipping 19 matching lines...) Expand all Loading... |
367 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | 371 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
368 base::Time time3 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); | 372 base::Time time3 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); |
369 | 373 |
370 AddAutocompleteSuggestion("tel", "+987654321"); | 374 AddAutocompleteSuggestion("tel", "+987654321"); |
371 AddCreditCard("1211-1098-7654-3210", 10, 2030); | 375 AddCreditCard("1211-1098-7654-3210", 10, 2030); |
372 WaitForDBThread(); | 376 WaitForDBThread(); |
373 | 377 |
374 // Test the results for different starting points. | 378 // Test the results for different starting points. |
375 struct TestCase { | 379 struct TestCase { |
376 const base::Time period_start; | 380 const base::Time period_start; |
377 const BrowsingDataCounter::ResultInt expected_num_suggestions; | 381 const browsing_data::BrowsingDataCounter::ResultInt |
378 const BrowsingDataCounter::ResultInt expected_num_credit_cards; | 382 expected_num_suggestions; |
379 const BrowsingDataCounter::ResultInt expected_num_addresses; | 383 const browsing_data::BrowsingDataCounter::ResultInt |
| 384 expected_num_credit_cards; |
| 385 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses; |
380 } test_cases[] = { | 386 } test_cases[] = { |
381 { base::Time(), 2, 3, 3}, | 387 { base::Time(), 2, 3, 3}, |
382 { time1, 2, 3, 3}, | 388 { time1, 2, 3, 3}, |
383 { time2, 1, 2, 2}, | 389 { time2, 1, 2, 2}, |
384 { time3, 1, 1, 0} | 390 { time3, 1, 1, 0} |
385 }; | 391 }; |
386 | 392 |
387 AutofillCounter counter; | 393 Profile* profile = browser()->profile(); |
388 counter.Init(browser()->profile(), | 394 AutofillCounter counter(profile); |
389 base::Bind(&AutofillCounterTest::Callback, | 395 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
390 base::Unretained(this))); | 396 base::Unretained(this))); |
391 | 397 |
392 for (const TestCase& test_case : test_cases) { | 398 for (const TestCase& test_case : test_cases) { |
393 counter.SetPeriodStartForTesting(test_case.period_start); | 399 counter.SetPeriodStartForTesting(test_case.period_start); |
394 counter.Restart(); | 400 counter.Restart(); |
395 WaitForCounting(); | 401 WaitForCounting(); |
396 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); | 402 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); |
397 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); | 403 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); |
398 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); | 404 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); |
399 } | 405 } |
400 } | 406 } |
401 | 407 |
402 } // namespace | 408 } // namespace |
OLD | NEW |