1 // Copyright 2013 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.
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "components/autofill/core/browser/autofill_profile.h"
15 #include "components/autofill/core/browser/autofill_type.h"
16 #include "components/autofill/core/browser/credit_card.h"
17 #include "components/autofill/core/browser/webdata/autofill_change.h"
18 #include "components/autofill/core/browser/webdata/autofill_entry.h"
19 #include "components/autofill/core/browser/webdata/autofill_table.h"
20 #include "components/autofill/core/common/form_field_data.h"
21 #include "components/os_crypt/os_crypt.h"
22 #include "components/webdata/common/web_database.h"
23 #include "sql/statement.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using base::ASCIIToUTF16;
28 using base::TimeDelta;
32 // So we can compare AutofillKeys with EXPECT_EQ().
33 std::ostream& operator<<(std::ostream& os, const AutofillKey& key) {
34 return os << base::UTF16ToASCII(key.name()) << ", "
35 << base::UTF16ToASCII(key.value());
38 // So we can compare AutofillChanges with EXPECT_EQ().
39 std::ostream& operator<<(std::ostream& os, const AutofillChange& change) {
40 switch (change.type()) {
41 case AutofillChange::ADD: {
45 case AutofillChange::UPDATE: {
49 case AutofillChange::REMOVE: {
54 return os << " " << change.key();
59 typedef std::set<AutofillEntry,
60 bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet;
61 typedef AutofillEntrySet::iterator AutofillEntrySetIterator;
63 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) {
64 int compVal = a.key().name().compare(b.key().name());
68 compVal = a.key().value().compare(b.key().value());
72 if (a.date_created() != b.date_created())
73 return a.date_created() < b.date_created();
75 return a.date_last_used() < b.date_last_used();
78 AutofillEntry MakeAutofillEntry(const char* name,
81 time_t date_last_used) {
82 if (date_last_used < 0)
83 date_last_used = date_created;
84 return AutofillEntry(AutofillKey(ASCIIToUTF16(name), ASCIIToUTF16(value)),
85 Time::FromTimeT(date_created),
86 Time::FromTimeT(date_last_used));
89 // Checks |actual| and |expected| contain the same elements.
90 void CompareAutofillEntrySets(const AutofillEntrySet& actual,
91 const AutofillEntrySet& expected) {
92 ASSERT_EQ(expected.size(), actual.size());
94 for (AutofillEntrySet::const_iterator it = actual.begin();
95 it != actual.end(); ++it) {
96 count += expected.count(*it);
98 EXPECT_EQ(actual.size(), count);
101 int GetAutofillEntryCount(const base::string16& name,
102 const base::string16& value,
104 sql::Statement s(db->GetSQLConnection()->GetUniqueStatement(
105 "SELECT count FROM autofill WHERE name = ? AND value = ?"));
106 s.BindString16(0, name);
107 s.BindString16(1, value);
109 return s.ColumnInt(0);
114 class AutofillTableTest : public testing::Test {
116 AutofillTableTest() {}
117 ~AutofillTableTest() override {}
120 void SetUp() override {
121 #if defined(OS_MACOSX)
122 OSCrypt::UseMockKeychain(true);
124 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
125 file_ = temp_dir_.path().AppendASCII("TestWebDatabase");
127 table_.reset(new AutofillTable("en-US"));
128 db_.reset(new WebDatabase);
129 db_->AddTable(table_.get());
130 ASSERT_EQ(sql::INIT_OK, db_->Init(file_));
133 base::FilePath file_;
134 base::ScopedTempDir temp_dir_;
135 scoped_ptr<AutofillTable> table_;
136 scoped_ptr<WebDatabase> db_;
139 DISALLOW_COPY_AND_ASSIGN(AutofillTableTest);
142 TEST_F(AutofillTableTest, Autofill) {
143 Time t1 = Time::Now();
145 // Simulate the submission of a handful of entries in a field called "Name",
146 // some more often than others.
147 AutofillChangeList changes;
149 field.name = ASCIIToUTF16("Name");
150 field.value = ASCIIToUTF16("Superman");
151 base::Time now = base::Time::Now();
152 base::TimeDelta two_seconds = base::TimeDelta::FromSeconds(2);
153 EXPECT_FALSE(table_->HasFormElements());
154 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
155 EXPECT_TRUE(table_->HasFormElements());
156 std::vector<base::string16> v;
157 for (int i = 0; i < 5; ++i) {
158 field.value = ASCIIToUTF16("Clark Kent");
159 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
160 now + i * two_seconds));
162 for (int i = 0; i < 3; ++i) {
163 field.value = ASCIIToUTF16("Clark Sutter");
164 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
165 now + i * two_seconds));
167 for (int i = 0; i < 2; ++i) {
168 field.name = ASCIIToUTF16("Favorite Color");
169 field.value = ASCIIToUTF16("Green");
170 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
171 now + i * two_seconds));
174 // We have added the name Clark Kent 5 times, so count should be 5.
175 EXPECT_EQ(5, GetAutofillEntryCount(ASCIIToUTF16("Name"),
176 ASCIIToUTF16("Clark Kent"), db_.get()));
178 // Storing in the data base should be case sensitive, so there should be no
179 // database entry for clark kent lowercase.
180 EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"),
181 ASCIIToUTF16("clark kent"), db_.get()));
183 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("Favorite Color"),
184 ASCIIToUTF16("Green"), db_.get()));
186 // This is meant to get a list of suggestions for Name. The empty prefix
187 // in the second argument means it should return all suggestions for a name
188 // no matter what they start with. The order that the names occur in the list
189 // should be decreasing order by count.
190 EXPECT_TRUE(table_->GetFormValuesForElementName(
191 ASCIIToUTF16("Name"), base::string16(), &v, 6));
192 EXPECT_EQ(3U, v.size());
194 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
195 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]);
196 EXPECT_EQ(ASCIIToUTF16("Superman"), v[2]);
199 // If we query again limiting the list size to 1, we should only get the most
201 EXPECT_TRUE(table_->GetFormValuesForElementName(
202 ASCIIToUTF16("Name"), base::string16(), &v, 1));
203 EXPECT_EQ(1U, v.size());
205 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
208 // Querying for suggestions given a prefix is case-insensitive, so the prefix
209 // "cLa" shoud get suggestions for both Clarks.
210 EXPECT_TRUE(table_->GetFormValuesForElementName(
211 ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v, 6));
212 EXPECT_EQ(2U, v.size());
214 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
215 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]);
218 // Removing all elements since the beginning of this function should remove
219 // everything from the database.
221 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, Time(), &changes));
223 const AutofillChange kExpectedChanges[] = {
224 AutofillChange(AutofillChange::REMOVE,
225 AutofillKey(ASCIIToUTF16("Name"),
226 ASCIIToUTF16("Superman"))),
227 AutofillChange(AutofillChange::REMOVE,
228 AutofillKey(ASCIIToUTF16("Name"),
229 ASCIIToUTF16("Clark Kent"))),
230 AutofillChange(AutofillChange::REMOVE,
231 AutofillKey(ASCIIToUTF16("Name"),
232 ASCIIToUTF16("Clark Sutter"))),
233 AutofillChange(AutofillChange::REMOVE,
234 AutofillKey(ASCIIToUTF16("Favorite Color"),
235 ASCIIToUTF16("Green"))),
237 EXPECT_EQ(arraysize(kExpectedChanges), changes.size());
238 for (size_t i = 0; i < arraysize(kExpectedChanges); ++i) {
239 EXPECT_EQ(kExpectedChanges[i], changes[i]);
242 EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"),
243 ASCIIToUTF16("Clark Kent"), db_.get()));
245 EXPECT_TRUE(table_->GetFormValuesForElementName(
246 ASCIIToUTF16("Name"), base::string16(), &v, 6));
247 EXPECT_EQ(0U, v.size());
249 // Now add some values with empty strings.
250 const base::string16 kValue = ASCIIToUTF16(" toto ");
251 field.name = ASCIIToUTF16("blank");
252 field.value = base::string16();
253 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
254 field.name = ASCIIToUTF16("blank");
255 field.value = ASCIIToUTF16(" ");
256 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
257 field.name = ASCIIToUTF16("blank");
258 field.value = ASCIIToUTF16(" ");
259 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
260 field.name = ASCIIToUTF16("blank");
261 field.value = kValue;
262 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
264 // They should be stored normally as the DB layer does not check for empty
267 EXPECT_TRUE(table_->GetFormValuesForElementName(
268 ASCIIToUTF16("blank"), base::string16(), &v, 10));
269 EXPECT_EQ(4U, v.size());
272 TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) {
273 TimeDelta one_day(TimeDelta::FromDays(1));
274 Time t1 = Time::Now();
275 Time t2 = t1 + one_day;
277 AutofillChangeList changes;
279 field.name = ASCIIToUTF16("Name");
280 field.value = ASCIIToUTF16("Superman");
281 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1));
282 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t2));
285 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, t2, &changes));
286 ASSERT_EQ(1U, changes.size());
287 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
288 AutofillKey(ASCIIToUTF16("Name"),
289 ASCIIToUTF16("Superman"))),
294 table_->RemoveFormElementsAddedBetween(t2, t2 + one_day, &changes));
295 ASSERT_EQ(1U, changes.size());
296 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE,
297 AutofillKey(ASCIIToUTF16("Name"),
298 ASCIIToUTF16("Superman"))),
302 TEST_F(AutofillTableTest, Autofill_AddChanges) {
303 TimeDelta one_day(TimeDelta::FromDays(1));
304 Time t1 = Time::Now();
305 Time t2 = t1 + one_day;
307 AutofillChangeList changes;
309 field.name = ASCIIToUTF16("Name");
310 field.value = ASCIIToUTF16("Superman");
311 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1));
312 ASSERT_EQ(1U, changes.size());
313 EXPECT_EQ(AutofillChange(AutofillChange::ADD,
314 AutofillKey(ASCIIToUTF16("Name"),
315 ASCIIToUTF16("Superman"))),
320 table_->AddFormFieldValueTime(field, &changes, t2));
321 ASSERT_EQ(1U, changes.size());
322 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
323 AutofillKey(ASCIIToUTF16("Name"),
324 ASCIIToUTF16("Superman"))),
328 TEST_F(AutofillTableTest, Autofill_UpdateOneWithOneTimestamp) {
329 AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, -1));
330 std::vector<AutofillEntry> entries;
331 entries.push_back(entry);
332 ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
334 EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"),
337 std::vector<AutofillEntry> all_entries;
338 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
339 ASSERT_EQ(1U, all_entries.size());
340 EXPECT_EQ(entry, all_entries[0]);
343 TEST_F(AutofillTableTest, Autofill_UpdateOneWithTwoTimestamps) {
344 AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2));
345 std::vector<AutofillEntry> entries;
346 entries.push_back(entry);
347 ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
349 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"),
352 std::vector<AutofillEntry> all_entries;
353 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
354 ASSERT_EQ(1U, all_entries.size());
355 EXPECT_EQ(entry, all_entries[0]);
358 TEST_F(AutofillTableTest, Autofill_GetAutofillTimestamps) {
359 AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2));
360 std::vector<AutofillEntry> entries;
361 entries.push_back(entry);
362 ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
364 Time date_created, date_last_used;
365 ASSERT_TRUE(table_->GetAutofillTimestamps(ASCIIToUTF16("foo"),
369 EXPECT_EQ(Time::FromTimeT(1), date_created);
370 EXPECT_EQ(Time::FromTimeT(2), date_last_used);
373 TEST_F(AutofillTableTest, Autofill_UpdateTwo) {
374 AutofillEntry entry0(MakeAutofillEntry("foo", "bar0", 1, -1));
375 AutofillEntry entry1(MakeAutofillEntry("foo", "bar1", 2, 3));
376 std::vector<AutofillEntry> entries;
377 entries.push_back(entry0);
378 entries.push_back(entry1);
379 ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
381 EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar0"),
383 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar1"),
387 TEST_F(AutofillTableTest, Autofill_UpdateReplace) {
388 AutofillChangeList changes;
389 // Add a form field. This will be replaced.
391 field.name = ASCIIToUTF16("Name");
392 field.value = ASCIIToUTF16("Superman");
393 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
395 AutofillEntry entry(MakeAutofillEntry("Name", "Superman", 1, 2));
396 std::vector<AutofillEntry> entries;
397 entries.push_back(entry);
398 ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
400 std::vector<AutofillEntry> all_entries;
401 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
402 ASSERT_EQ(1U, all_entries.size());
403 EXPECT_EQ(entry, all_entries[0]);
406 TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) {
407 Time t = Time::Now();
408 AutofillEntry existing(
409 MakeAutofillEntry("Name", "Superman", t.ToTimeT(), -1));
411 AutofillChangeList changes;
412 // Add a form field. This will NOT be replaced.
414 field.name = existing.key().name();
415 field.value = existing.key().value();
416 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t));
417 AutofillEntry entry(MakeAutofillEntry("Name", "Clark Kent", 1, 2));
418 std::vector<AutofillEntry> entries;
419 entries.push_back(entry);
420 ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
422 std::vector<AutofillEntry> all_entries;
423 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
424 ASSERT_EQ(2U, all_entries.size());
425 AutofillEntrySet expected_entries(all_entries.begin(),
427 CompareAutofillEntries);
428 EXPECT_EQ(1U, expected_entries.count(existing));
429 EXPECT_EQ(1U, expected_entries.count(entry));
432 TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) {
433 Time t = Time::Now();
435 // Add multiple values for "firstname" and "lastname" names. Test that only
436 // first value of each gets added. Related to security issue:
437 // http://crbug.com/51727.
438 std::vector<FormFieldData> elements;
440 field.name = ASCIIToUTF16("firstname");
441 field.value = ASCIIToUTF16("Joe");
442 elements.push_back(field);
444 field.name = ASCIIToUTF16("firstname");
445 field.value = ASCIIToUTF16("Jane");
446 elements.push_back(field);
448 field.name = ASCIIToUTF16("lastname");
449 field.value = ASCIIToUTF16("Smith");
450 elements.push_back(field);
452 field.name = ASCIIToUTF16("lastname");
453 field.value = ASCIIToUTF16("Jones");
454 elements.push_back(field);
456 std::vector<AutofillChange> changes;
457 table_->AddFormFieldValuesTime(elements, &changes, t);
459 ASSERT_EQ(2U, changes.size());
460 EXPECT_EQ(changes[0], AutofillChange(AutofillChange::ADD,
461 AutofillKey(ASCIIToUTF16("firstname"),
462 ASCIIToUTF16("Joe"))));
463 EXPECT_EQ(changes[1], AutofillChange(AutofillChange::ADD,
464 AutofillKey(ASCIIToUTF16("lastname"),
465 ASCIIToUTF16("Smith"))));
467 std::vector<AutofillEntry> all_entries;
468 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
469 ASSERT_EQ(2U, all_entries.size());
472 TEST_F(AutofillTableTest,
473 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore) {
474 // Add an entry used only before the targetted range.
475 AutofillChangeList changes;
477 field.name = ASCIIToUTF16("Name");
478 field.value = ASCIIToUTF16("Superman");
479 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
480 base::Time::FromTimeT(10)));
481 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
482 base::Time::FromTimeT(20)));
483 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
484 base::Time::FromTimeT(30)));
485 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
486 base::Time::FromTimeT(40)));
487 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
488 base::Time::FromTimeT(50)));
490 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
493 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(51),
494 base::Time::FromTimeT(60),
496 EXPECT_TRUE(changes.empty());
497 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
500 TEST_F(AutofillTableTest,
501 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter) {
502 // Add an entry used only after the targetted range.
503 AutofillChangeList changes;
505 field.name = ASCIIToUTF16("Name");
506 field.value = ASCIIToUTF16("Superman");
507 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
508 base::Time::FromTimeT(50)));
509 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
510 base::Time::FromTimeT(60)));
511 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
512 base::Time::FromTimeT(70)));
513 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
514 base::Time::FromTimeT(80)));
515 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
516 base::Time::FromTimeT(90)));
518 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
521 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
522 base::Time::FromTimeT(50),
524 EXPECT_TRUE(changes.empty());
525 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
528 TEST_F(AutofillTableTest,
529 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring) {
530 // Add an entry used entirely during the targetted range.
531 AutofillChangeList changes;
533 field.name = ASCIIToUTF16("Name");
534 field.value = ASCIIToUTF16("Superman");
535 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
536 base::Time::FromTimeT(10)));
537 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
538 base::Time::FromTimeT(20)));
539 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
540 base::Time::FromTimeT(30)));
541 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
542 base::Time::FromTimeT(40)));
543 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
544 base::Time::FromTimeT(50)));
546 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
549 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(10),
550 base::Time::FromTimeT(51),
552 ASSERT_EQ(1U, changes.size());
553 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE,
554 AutofillKey(field.name, field.value)),
556 EXPECT_EQ(0, GetAutofillEntryCount(field.name, field.value, db_.get()));
559 TEST_F(AutofillTableTest,
560 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring) {
561 // Add an entry used both before and during the targetted range.
562 AutofillChangeList changes;
564 field.name = ASCIIToUTF16("Name");
565 field.value = ASCIIToUTF16("Superman");
566 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
567 base::Time::FromTimeT(10)));
568 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
569 base::Time::FromTimeT(20)));
570 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
571 base::Time::FromTimeT(30)));
572 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
573 base::Time::FromTimeT(40)));
574 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
575 base::Time::FromTimeT(50)));
577 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
580 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
581 base::Time::FromTimeT(60),
583 ASSERT_EQ(1U, changes.size());
584 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
585 AutofillKey(field.name, field.value)),
587 EXPECT_EQ(4, GetAutofillEntryCount(field.name, field.value, db_.get()));
588 base::Time date_created, date_last_used;
590 table_->GetAutofillTimestamps(field.name, field.value,
591 &date_created, &date_last_used));
592 EXPECT_EQ(base::Time::FromTimeT(10), date_created);
593 EXPECT_EQ(base::Time::FromTimeT(39), date_last_used);
596 TEST_F(AutofillTableTest,
597 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter) {
598 // Add an entry used both during and after the targetted range.
599 AutofillChangeList changes;
601 field.name = ASCIIToUTF16("Name");
602 field.value = ASCIIToUTF16("Superman");
603 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
604 base::Time::FromTimeT(50)));
605 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
606 base::Time::FromTimeT(60)));
607 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
608 base::Time::FromTimeT(70)));
609 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
610 base::Time::FromTimeT(80)));
611 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
612 base::Time::FromTimeT(90)));
614 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
617 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
618 base::Time::FromTimeT(80),
620 ASSERT_EQ(1U, changes.size());
621 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
622 AutofillKey(field.name, field.value)),
624 EXPECT_EQ(2, GetAutofillEntryCount(field.name, field.value, db_.get()));
625 base::Time date_created, date_last_used;
627 table_->GetAutofillTimestamps(field.name, field.value,
628 &date_created, &date_last_used));
629 EXPECT_EQ(base::Time::FromTimeT(80), date_created);
630 EXPECT_EQ(base::Time::FromTimeT(90), date_last_used);
633 TEST_F(AutofillTableTest, AutofillProfile) {
634 // Add a 'Home' profile.
635 AutofillProfile home_profile;
636 home_profile.set_origin(std::string());
637 home_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
638 home_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
639 home_profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
640 home_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz"));
641 home_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google"));
642 home_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way"));
643 home_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5"));
644 home_profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
645 ASCIIToUTF16("Beverly Hills"));
646 home_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
647 home_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
648 home_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
649 home_profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, ASCIIToUTF16("MAGIC ###"));
650 home_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
651 home_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567"));
652 home_profile.set_language_code("en");
654 Time pre_creation_time = Time::Now();
655 EXPECT_TRUE(table_->AddAutofillProfile(home_profile));
656 Time post_creation_time = Time::Now();
658 // Get the 'Home' profile.
659 AutofillProfile* db_profile;
660 ASSERT_TRUE(table_->GetAutofillProfile(home_profile.guid(), &db_profile));
661 EXPECT_EQ(home_profile, *db_profile);
662 sql::Statement s_home(db_->GetSQLConnection()->GetUniqueStatement(
663 "SELECT date_modified "
664 "FROM autofill_profiles WHERE guid=?"));
665 s_home.BindString(0, home_profile.guid());
666 ASSERT_TRUE(s_home.is_valid());
667 ASSERT_TRUE(s_home.Step());
668 EXPECT_GE(s_home.ColumnInt64(0), pre_creation_time.ToTimeT());
669 EXPECT_LE(s_home.ColumnInt64(0), post_creation_time.ToTimeT());
670 EXPECT_FALSE(s_home.Step());
673 // Add a 'Billing' profile.
674 AutofillProfile billing_profile = home_profile;
675 billing_profile.set_guid(base::GenerateGUID());
676 billing_profile.set_origin("https://www.example.com/");
677 billing_profile.SetRawInfo(ADDRESS_HOME_LINE1,
678 ASCIIToUTF16("5678 Bottom Street"));
679 billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("suite 3"));
681 pre_creation_time = Time::Now();
682 EXPECT_TRUE(table_->AddAutofillProfile(billing_profile));
683 post_creation_time = Time::Now();
685 // Get the 'Billing' profile.
686 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
687 EXPECT_EQ(billing_profile, *db_profile);
688 sql::Statement s_billing(db_->GetSQLConnection()->GetUniqueStatement(
689 "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
690 s_billing.BindString(0, billing_profile.guid());
691 ASSERT_TRUE(s_billing.is_valid());
692 ASSERT_TRUE(s_billing.Step());
693 EXPECT_GE(s_billing.ColumnInt64(0), pre_creation_time.ToTimeT());
694 EXPECT_LE(s_billing.ColumnInt64(0), post_creation_time.ToTimeT());
695 EXPECT_FALSE(s_billing.Step());
698 // Update the 'Billing' profile, name only.
699 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane"));
700 Time pre_modification_time = Time::Now();
701 EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile));
702 Time post_modification_time = Time::Now();
703 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
704 EXPECT_EQ(billing_profile, *db_profile);
705 sql::Statement s_billing_updated(db_->GetSQLConnection()->GetUniqueStatement(
706 "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
707 s_billing_updated.BindString(0, billing_profile.guid());
708 ASSERT_TRUE(s_billing_updated.is_valid());
709 ASSERT_TRUE(s_billing_updated.Step());
710 EXPECT_GE(s_billing_updated.ColumnInt64(0),
711 pre_modification_time.ToTimeT());
712 EXPECT_LE(s_billing_updated.ColumnInt64(0),
713 post_modification_time.ToTimeT());
714 EXPECT_FALSE(s_billing_updated.Step());
717 // Update the 'Billing' profile.
718 billing_profile.set_origin("Chrome settings");
719 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Janice"));
720 billing_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("C."));
721 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Joplin"));
722 billing_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("jane@singer.com"));
723 billing_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Indy"));
724 billing_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("Open Road"));
725 billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("Route 66"));
726 billing_profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
727 ASCIIToUTF16("District 9"));
728 billing_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("NFA"));
729 billing_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("NY"));
730 billing_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("10011"));
731 billing_profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, ASCIIToUTF16("123456"));
732 billing_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
733 billing_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
734 ASCIIToUTF16("18181230000"));
735 Time pre_modification_time_2 = Time::Now();
736 EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile));
737 Time post_modification_time_2 = Time::Now();
738 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
739 EXPECT_EQ(billing_profile, *db_profile);
740 sql::Statement s_billing_updated_2(
741 db_->GetSQLConnection()->GetUniqueStatement(
742 "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
743 s_billing_updated_2.BindString(0, billing_profile.guid());
744 ASSERT_TRUE(s_billing_updated_2.is_valid());
745 ASSERT_TRUE(s_billing_updated_2.Step());
746 EXPECT_GE(s_billing_updated_2.ColumnInt64(0),
747 pre_modification_time_2.ToTimeT());
748 EXPECT_LE(s_billing_updated_2.ColumnInt64(0),
749 post_modification_time_2.ToTimeT());
750 EXPECT_FALSE(s_billing_updated_2.Step());
753 // Remove the 'Billing' profile.
754 EXPECT_TRUE(table_->RemoveAutofillProfile(billing_profile.guid()));
755 EXPECT_FALSE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
758 TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) {
760 const base::string16 kJohnDoe(ASCIIToUTF16("John Doe"));
761 const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe"));
762 std::vector<base::string16> set_values;
763 set_values.push_back(kJohnDoe);
764 set_values.push_back(kJohnPDoe);
765 p.SetRawMultiInfo(NAME_FULL, set_values);
767 EXPECT_TRUE(table_->AddAutofillProfile(p));
769 AutofillProfile* db_profile;
770 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
771 EXPECT_EQ(p, *db_profile);
772 EXPECT_EQ(0, p.Compare(*db_profile));
775 // Update the values.
776 const base::string16 kNoOne(ASCIIToUTF16("No One"));
777 set_values[1] = kNoOne;
778 p.SetRawMultiInfo(NAME_FULL, set_values);
779 EXPECT_TRUE(table_->UpdateAutofillProfile(p));
780 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
781 EXPECT_EQ(p, *db_profile);
782 EXPECT_EQ(0, p.Compare(*db_profile));
787 p.SetRawMultiInfo(NAME_FULL, set_values);
788 EXPECT_TRUE(table_->UpdateAutofillProfile(p));
789 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
790 EXPECT_EQ(p, *db_profile);
791 EXPECT_EQ(0, p.Compare(*db_profile));
792 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(NAME_FULL));
796 TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) {
798 const base::string16 kJohnDoe(ASCIIToUTF16("john@doe.com"));
799 const base::string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com"));
800 std::vector<base::string16> set_values;
801 set_values.push_back(kJohnDoe);
802 set_values.push_back(kJohnPDoe);
803 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
805 EXPECT_TRUE(table_->AddAutofillProfile(p));
807 AutofillProfile* db_profile;
808 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
809 EXPECT_EQ(p, *db_profile);
810 EXPECT_EQ(0, p.Compare(*db_profile));
813 // Update the values.
814 const base::string16 kNoOne(ASCIIToUTF16("no@one.com"));
815 set_values[1] = kNoOne;
816 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
817 EXPECT_TRUE(table_->UpdateAutofillProfile(p));
818 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
819 EXPECT_EQ(p, *db_profile);
820 EXPECT_EQ(0, p.Compare(*db_profile));
825 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
826 EXPECT_TRUE(table_->UpdateAutofillProfile(p));
827 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
828 EXPECT_EQ(p, *db_profile);
829 EXPECT_EQ(0, p.Compare(*db_profile));
830 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS));
834 TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) {
836 const base::string16 kJohnDoe(ASCIIToUTF16("4151112222"));
837 const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333"));
838 std::vector<base::string16> set_values;
839 set_values.push_back(kJohnDoe);
840 set_values.push_back(kJohnPDoe);
841 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
843 EXPECT_TRUE(table_->AddAutofillProfile(p));
845 AutofillProfile* db_profile;
846 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
847 EXPECT_EQ(p, *db_profile);
848 EXPECT_EQ(0, p.Compare(*db_profile));
851 // Update the values.
852 const base::string16 kNoOne(ASCIIToUTF16("4151110000"));
853 set_values[1] = kNoOne;
854 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
855 EXPECT_TRUE(table_->UpdateAutofillProfile(p));
856 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
857 EXPECT_EQ(p, *db_profile);
858 EXPECT_EQ(0, p.Compare(*db_profile));
863 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
864 EXPECT_TRUE(table_->UpdateAutofillProfile(p));
865 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
866 EXPECT_EQ(p, *db_profile);
867 EXPECT_EQ(0, p.Compare(*db_profile));
868 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS));
872 TEST_F(AutofillTableTest, AutofillProfileTrash) {
873 std::vector<std::string> guids;
874 table_->GetAutofillProfilesInTrash(&guids);
875 EXPECT_TRUE(guids.empty());
877 ASSERT_TRUE(table_->AddAutofillGUIDToTrash(
878 "00000000-0000-0000-0000-000000000000"));
879 ASSERT_TRUE(table_->AddAutofillGUIDToTrash(
880 "00000000-0000-0000-0000-000000000001"));
881 ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
882 EXPECT_EQ(2UL, guids.size());
883 EXPECT_EQ("00000000-0000-0000-0000-000000000000", guids[0]);
884 EXPECT_EQ("00000000-0000-0000-0000-000000000001", guids[1]);
886 ASSERT_TRUE(table_->EmptyAutofillProfilesTrash());
887 ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
888 EXPECT_TRUE(guids.empty());
891 TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) {
892 std::vector<std::string> guids;
893 table_->GetAutofillProfilesInTrash(&guids);
894 EXPECT_TRUE(guids.empty());
896 AutofillProfile profile;
897 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
898 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
899 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
900 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz"));
901 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1 Main St"));
902 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
903 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
904 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
905 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
907 // Mark this profile as in the trash. This stops |AddAutofillProfile| from
909 EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid()));
910 EXPECT_TRUE(table_->AddAutofillProfile(profile));
911 AutofillProfile* added_profile = NULL;
912 EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &added_profile));
913 EXPECT_EQ(static_cast<AutofillProfile*>(NULL), added_profile);
915 // Add the profile for real this time.
916 EXPECT_TRUE(table_->EmptyAutofillProfilesTrash());
917 EXPECT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
918 EXPECT_TRUE(guids.empty());
919 EXPECT_TRUE(table_->AddAutofillProfile(profile));
920 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(),
922 ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile);
923 delete added_profile;
925 // Mark this profile as in the trash. This stops |UpdateAutofillProfileMulti|
926 // from updating it. In normal operation a profile should not be both in the
927 // trash and in the profiles table simultaneously.
928 EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid()));
929 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane"));
930 EXPECT_TRUE(table_->UpdateAutofillProfile(profile));
931 AutofillProfile* updated_profile = NULL;
932 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &updated_profile));
933 ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile);
934 EXPECT_EQ(ASCIIToUTF16("John"), updated_profile->GetRawInfo(NAME_FIRST));
935 delete updated_profile;
937 // Try to delete the trashed profile. This stops |RemoveAutofillProfile| from
938 // deleting it. In normal operation deletion is done by migration step, and
939 // removal from trash is done by |WebDataService|. |RemoveAutofillProfile|
940 // does remove the item from the trash if it is found however, so that if
941 // other clients remove it (via Sync say) then it is gone and doesn't need to
942 // be processed further by |WebDataService|.
943 EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid()));
944 AutofillProfile* removed_profile = NULL;
945 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &removed_profile));
946 EXPECT_FALSE(table_->IsAutofillGUIDInTrash(profile.guid()));
947 ASSERT_NE(static_cast<AutofillProfile*>(NULL), removed_profile);
948 delete removed_profile;
950 // Check that emptying the trash now allows removal to occur.
951 EXPECT_TRUE(table_->EmptyAutofillProfilesTrash());
952 EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid()));
953 removed_profile = NULL;
954 EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &removed_profile));
955 EXPECT_EQ(static_cast<AutofillProfile*>(NULL), removed_profile);
958 TEST_F(AutofillTableTest, CreditCard) {
959 // Add a 'Work' credit card.
960 CreditCard work_creditcard;
961 work_creditcard.set_origin("https://www.example.com/");
962 work_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
963 work_creditcard.SetRawInfo(CREDIT_CARD_NUMBER,
964 ASCIIToUTF16("1234567890123456"));
965 work_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04"));
966 work_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR,
967 ASCIIToUTF16("2013"));
969 Time pre_creation_time = Time::Now();
970 EXPECT_TRUE(table_->AddCreditCard(work_creditcard));
971 Time post_creation_time = Time::Now();
973 // Get the 'Work' credit card.
974 CreditCard* db_creditcard;
975 ASSERT_TRUE(table_->GetCreditCard(work_creditcard.guid(), &db_creditcard));
976 EXPECT_EQ(work_creditcard, *db_creditcard);
977 sql::Statement s_work(db_->GetSQLConnection()->GetUniqueStatement(
978 "SELECT guid, name_on_card, expiration_month, expiration_year, "
979 "card_number_encrypted, date_modified "
980 "FROM credit_cards WHERE guid=?"));
981 s_work.BindString(0, work_creditcard.guid());
982 ASSERT_TRUE(s_work.is_valid());
983 ASSERT_TRUE(s_work.Step());
984 EXPECT_GE(s_work.ColumnInt64(5), pre_creation_time.ToTimeT());
985 EXPECT_LE(s_work.ColumnInt64(5), post_creation_time.ToTimeT());
986 EXPECT_FALSE(s_work.Step());
987 delete db_creditcard;
989 // Add a 'Target' credit card.
990 CreditCard target_creditcard;
991 target_creditcard.set_origin(std::string());
992 target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
993 target_creditcard.SetRawInfo(CREDIT_CARD_NUMBER,
994 ASCIIToUTF16("1111222233334444"));
995 target_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("06"));
996 target_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR,
997 ASCIIToUTF16("2012"));
999 pre_creation_time = Time::Now();
1000 EXPECT_TRUE(table_->AddCreditCard(target_creditcard));
1001 post_creation_time = Time::Now();
1002 ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
1003 EXPECT_EQ(target_creditcard, *db_creditcard);
1004 sql::Statement s_target(db_->GetSQLConnection()->GetUniqueStatement(
1005 "SELECT guid, name_on_card, expiration_month, expiration_year, "
1006 "card_number_encrypted, date_modified "
1007 "FROM credit_cards WHERE guid=?"));
1008 s_target.BindString(0, target_creditcard.guid());
1009 ASSERT_TRUE(s_target.is_valid());
1010 ASSERT_TRUE(s_target.Step());
1011 EXPECT_GE(s_target.ColumnInt64(5), pre_creation_time.ToTimeT());
1012 EXPECT_LE(s_target.ColumnInt64(5), post_creation_time.ToTimeT());
1013 EXPECT_FALSE(s_target.Step());
1014 delete db_creditcard;
1016 // Update the 'Target' credit card.
1017 target_creditcard.set_origin("Interactive Autofill dialog");
1018 target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Charles Grady"));
1019 Time pre_modification_time = Time::Now();
1020 EXPECT_TRUE(table_->UpdateCreditCard(target_creditcard));
1021 Time post_modification_time = Time::Now();
1022 ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
1023 EXPECT_EQ(target_creditcard, *db_creditcard);
1024 sql::Statement s_target_updated(db_->GetSQLConnection()->GetUniqueStatement(
1025 "SELECT guid, name_on_card, expiration_month, expiration_year, "
1026 "card_number_encrypted, date_modified "
1027 "FROM credit_cards WHERE guid=?"));
1028 s_target_updated.BindString(0, target_creditcard.guid());
1029 ASSERT_TRUE(s_target_updated.is_valid());
1030 ASSERT_TRUE(s_target_updated.Step());
1031 EXPECT_GE(s_target_updated.ColumnInt64(5), pre_modification_time.ToTimeT());
1032 EXPECT_LE(s_target_updated.ColumnInt64(5), post_modification_time.ToTimeT());
1033 EXPECT_FALSE(s_target_updated.Step());
1034 delete db_creditcard;
1036 // Remove the 'Target' credit card.
1037 EXPECT_TRUE(table_->RemoveCreditCard(target_creditcard.guid()));
1038 EXPECT_FALSE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
1041 TEST_F(AutofillTableTest, UpdateAutofillProfile) {
1042 // Add a profile to the db.
1043 AutofillProfile profile;
1044 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
1045 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
1046 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1047 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@example.com"));
1048 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google"));
1049 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way"));
1050 profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5"));
1051 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
1052 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1053 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
1054 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1055 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567"));
1056 profile.set_language_code("en");
1057 table_->AddAutofillProfile(profile);
1059 // Set a mocked value for the profile's creation time.
1060 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13;
1061 sql::Statement s_mock_creation_date(
1062 db_->GetSQLConnection()->GetUniqueStatement(
1063 "UPDATE autofill_profiles SET date_modified = ?"));
1064 ASSERT_TRUE(s_mock_creation_date.is_valid());
1065 s_mock_creation_date.BindInt64(0, kMockCreationDate);
1066 ASSERT_TRUE(s_mock_creation_date.Run());
1069 AutofillProfile* tmp_profile;
1070 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
1071 scoped_ptr<AutofillProfile> db_profile(tmp_profile);
1072 EXPECT_EQ(profile, *db_profile);
1073 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
1074 "SELECT date_modified FROM autofill_profiles"));
1075 ASSERT_TRUE(s_original.is_valid());
1076 ASSERT_TRUE(s_original.Step());
1077 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0));
1078 EXPECT_FALSE(s_original.Step());
1080 // Now, update the profile and save the update to the database.
1081 // The modification date should change to reflect the update.
1082 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz"));
1083 table_->UpdateAutofillProfile(profile);
1086 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
1087 db_profile.reset(tmp_profile);
1088 EXPECT_EQ(profile, *db_profile);
1089 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
1090 "SELECT date_modified FROM autofill_profiles"));
1091 ASSERT_TRUE(s_updated.is_valid());
1092 ASSERT_TRUE(s_updated.Step());
1093 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0));
1094 EXPECT_FALSE(s_updated.Step());
1096 // Set a mocked value for the profile's modification time.
1097 const time_t mock_modification_date = Time::Now().ToTimeT() - 7;
1098 sql::Statement s_mock_modification_date(
1099 db_->GetSQLConnection()->GetUniqueStatement(
1100 "UPDATE autofill_profiles SET date_modified = ?"));
1101 ASSERT_TRUE(s_mock_modification_date.is_valid());
1102 s_mock_modification_date.BindInt64(0, mock_modification_date);
1103 ASSERT_TRUE(s_mock_modification_date.Run());
1105 // Finally, call into |UpdateAutofillProfile()| without changing the
1106 // profile. The modification date should not change.
1107 table_->UpdateAutofillProfile(profile);
1110 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
1111 db_profile.reset(tmp_profile);
1112 EXPECT_EQ(profile, *db_profile);
1113 sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement(
1114 "SELECT date_modified FROM autofill_profiles"));
1115 ASSERT_TRUE(s_unchanged.is_valid());
1116 ASSERT_TRUE(s_unchanged.Step());
1117 EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0));
1118 EXPECT_FALSE(s_unchanged.Step());
1121 TEST_F(AutofillTableTest, UpdateCreditCard) {
1122 // Add a credit card to the db.
1123 CreditCard credit_card;
1124 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
1125 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456"));
1126 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04"));
1127 credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013"));
1128 table_->AddCreditCard(credit_card);
1130 // Set a mocked value for the credit card's creation time.
1131 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13;
1132 sql::Statement s_mock_creation_date(
1133 db_->GetSQLConnection()->GetUniqueStatement(
1134 "UPDATE credit_cards SET date_modified = ?"));
1135 ASSERT_TRUE(s_mock_creation_date.is_valid());
1136 s_mock_creation_date.BindInt64(0, kMockCreationDate);
1137 ASSERT_TRUE(s_mock_creation_date.Run());
1139 // Get the credit card.
1140 CreditCard* tmp_credit_card;
1141 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
1142 scoped_ptr<CreditCard> db_credit_card(tmp_credit_card);
1143 EXPECT_EQ(credit_card, *db_credit_card);
1144 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
1145 "SELECT date_modified FROM credit_cards"));
1146 ASSERT_TRUE(s_original.is_valid());
1147 ASSERT_TRUE(s_original.Step());
1148 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0));
1149 EXPECT_FALSE(s_original.Step());
1151 // Now, update the credit card and save the update to the database.
1152 // The modification date should change to reflect the update.
1153 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("01"));
1154 table_->UpdateCreditCard(credit_card);
1156 // Get the credit card.
1157 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
1158 db_credit_card.reset(tmp_credit_card);
1159 EXPECT_EQ(credit_card, *db_credit_card);
1160 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
1161 "SELECT date_modified FROM credit_cards"));
1162 ASSERT_TRUE(s_updated.is_valid());
1163 ASSERT_TRUE(s_updated.Step());
1164 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0));
1165 EXPECT_FALSE(s_updated.Step());
1167 // Set a mocked value for the credit card's modification time.
1168 const time_t mock_modification_date = Time::Now().ToTimeT() - 7;
1169 sql::Statement s_mock_modification_date(
1170 db_->GetSQLConnection()->GetUniqueStatement(
1171 "UPDATE credit_cards SET date_modified = ?"));
1172 ASSERT_TRUE(s_mock_modification_date.is_valid());
1173 s_mock_modification_date.BindInt64(0, mock_modification_date);
1174 ASSERT_TRUE(s_mock_modification_date.Run());
1176 // Finally, call into |UpdateCreditCard()| without changing the credit card.
1177 // The modification date should not change.
1178 table_->UpdateCreditCard(credit_card);
1180 // Get the credit card.
1181 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
1182 db_credit_card.reset(tmp_credit_card);
1183 EXPECT_EQ(credit_card, *db_credit_card);
1184 sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement(
1185 "SELECT date_modified FROM credit_cards"));
1186 ASSERT_TRUE(s_unchanged.is_valid());
1187 ASSERT_TRUE(s_unchanged.Step());
1188 EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0));
1189 EXPECT_FALSE(s_unchanged.Step());
1192 TEST_F(AutofillTableTest, UpdateProfileOriginOnly) {
1193 // Add a profile to the db.
1194 AutofillProfile profile;
1195 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
1196 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
1197 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
1198 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@example.com"));
1199 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google"));
1200 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way"));
1201 profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5"));
1202 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
1203 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
1204 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
1205 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1206 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567"));
1207 table_->AddAutofillProfile(profile);
1209 // Set a mocked value for the profile's creation time.
1210 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13;
1211 sql::Statement s_mock_creation_date(
1212 db_->GetSQLConnection()->GetUniqueStatement(
1213 "UPDATE autofill_profiles SET date_modified = ?"));
1214 ASSERT_TRUE(s_mock_creation_date.is_valid());
1215 s_mock_creation_date.BindInt64(0, kMockCreationDate);
1216 ASSERT_TRUE(s_mock_creation_date.Run());
1219 AutofillProfile* tmp_profile;
1220 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
1221 scoped_ptr<AutofillProfile> db_profile(tmp_profile);
1222 EXPECT_EQ(profile, *db_profile);
1223 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
1224 "SELECT date_modified FROM autofill_profiles"));
1225 ASSERT_TRUE(s_original.is_valid());
1226 ASSERT_TRUE(s_original.Step());
1227 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0));
1228 EXPECT_FALSE(s_original.Step());
1230 // Now, update just the profile's origin and save the update to the database.
1231 // The modification date should change to reflect the update.
1232 profile.set_origin("https://www.example.com/");
1233 table_->UpdateAutofillProfile(profile);
1236 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
1237 db_profile.reset(tmp_profile);
1238 EXPECT_EQ(profile, *db_profile);
1239 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
1240 "SELECT date_modified FROM autofill_profiles"));
1241 ASSERT_TRUE(s_updated.is_valid());
1242 ASSERT_TRUE(s_updated.Step());
1243 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0));
1244 EXPECT_FALSE(s_updated.Step());
1247 TEST_F(AutofillTableTest, UpdateCreditCardOriginOnly) {
1248 // Add a credit card to the db.
1249 CreditCard credit_card;
1250 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
1251 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456"));
1252 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04"));
1253 credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013"));
1254 table_->AddCreditCard(credit_card);
1256 // Set a mocked value for the credit card's creation time.
1257 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13;
1258 sql::Statement s_mock_creation_date(
1259 db_->GetSQLConnection()->GetUniqueStatement(
1260 "UPDATE credit_cards SET date_modified = ?"));
1261 ASSERT_TRUE(s_mock_creation_date.is_valid());
1262 s_mock_creation_date.BindInt64(0, kMockCreationDate);
1263 ASSERT_TRUE(s_mock_creation_date.Run());
1265 // Get the credit card.
1266 CreditCard* tmp_credit_card;
1267 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
1268 scoped_ptr<CreditCard> db_credit_card(tmp_credit_card);
1269 EXPECT_EQ(credit_card, *db_credit_card);
1270 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
1271 "SELECT date_modified FROM credit_cards"));
1272 ASSERT_TRUE(s_original.is_valid());
1273 ASSERT_TRUE(s_original.Step());
1274 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0));
1275 EXPECT_FALSE(s_original.Step());
1277 // Now, update just the credit card's origin and save the update to the
1278 // database. The modification date should change to reflect the update.
1279 credit_card.set_origin("https://www.example.com/");
1280 table_->UpdateCreditCard(credit_card);
1282 // Get the credit card.
1283 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
1284 db_credit_card.reset(tmp_credit_card);
1285 EXPECT_EQ(credit_card, *db_credit_card);
1286 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
1287 "SELECT date_modified FROM credit_cards"));
1288 ASSERT_TRUE(s_updated.is_valid());
1289 ASSERT_TRUE(s_updated.Step());
1290 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0));
1291 EXPECT_FALSE(s_updated.Step());
1294 TEST_F(AutofillTableTest, RemoveAutofillDataModifiedBetween) {
1295 // Populate the autofill_profiles and credit_cards tables.
1296 ASSERT_TRUE(db_->GetSQLConnection()->Execute(
1297 "INSERT INTO autofill_profiles (guid, date_modified) "
1298 "VALUES('00000000-0000-0000-0000-000000000000', 11);"
1299 "INSERT INTO autofill_profiles (guid, date_modified) "
1300 "VALUES('00000000-0000-0000-0000-000000000001', 21);"
1301 "INSERT INTO autofill_profiles (guid, date_modified) "
1302 "VALUES('00000000-0000-0000-0000-000000000002', 31);"
1303 "INSERT INTO autofill_profiles (guid, date_modified) "
1304 "VALUES('00000000-0000-0000-0000-000000000003', 41);"
1305 "INSERT INTO autofill_profiles (guid, date_modified) "
1306 "VALUES('00000000-0000-0000-0000-000000000004', 51);"
1307 "INSERT INTO autofill_profiles (guid, date_modified) "
1308 "VALUES('00000000-0000-0000-0000-000000000005', 61);"
1309 "INSERT INTO credit_cards (guid, date_modified) "
1310 "VALUES('00000000-0000-0000-0000-000000000006', 17);"
1311 "INSERT INTO credit_cards (guid, date_modified) "
1312 "VALUES('00000000-0000-0000-0000-000000000007', 27);"
1313 "INSERT INTO credit_cards (guid, date_modified) "
1314 "VALUES('00000000-0000-0000-0000-000000000008', 37);"
1315 "INSERT INTO credit_cards (guid, date_modified) "
1316 "VALUES('00000000-0000-0000-0000-000000000009', 47);"
1317 "INSERT INTO credit_cards (guid, date_modified) "
1318 "VALUES('00000000-0000-0000-0000-000000000010', 57);"
1319 "INSERT INTO credit_cards (guid, date_modified) "
1320 "VALUES('00000000-0000-0000-0000-000000000011', 67);"));
1322 // Remove all entries modified in the bounded time range [17,41).
1323 std::vector<std::string> profile_guids;
1324 std::vector<std::string> credit_card_guids;
1325 table_->RemoveAutofillDataModifiedBetween(
1326 Time::FromTimeT(17), Time::FromTimeT(41),
1327 &profile_guids, &credit_card_guids);
1328 ASSERT_EQ(2UL, profile_guids.size());
1329 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profile_guids[0]);
1330 EXPECT_EQ("00000000-0000-0000-0000-000000000002", profile_guids[1]);
1331 sql::Statement s_autofill_profiles_bounded(
1332 db_->GetSQLConnection()->GetUniqueStatement(
1333 "SELECT date_modified FROM autofill_profiles"));
1334 ASSERT_TRUE(s_autofill_profiles_bounded.is_valid());
1335 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1336 EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0));
1337 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1338 EXPECT_EQ(41, s_autofill_profiles_bounded.ColumnInt64(0));
1339 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1340 EXPECT_EQ(51, s_autofill_profiles_bounded.ColumnInt64(0));
1341 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1342 EXPECT_EQ(61, s_autofill_profiles_bounded.ColumnInt64(0));
1343 EXPECT_FALSE(s_autofill_profiles_bounded.Step());
1344 ASSERT_EQ(3UL, credit_card_guids.size());
1345 EXPECT_EQ("00000000-0000-0000-0000-000000000006", credit_card_guids[0]);
1346 EXPECT_EQ("00000000-0000-0000-0000-000000000007", credit_card_guids[1]);
1347 EXPECT_EQ("00000000-0000-0000-0000-000000000008", credit_card_guids[2]);
1348 sql::Statement s_credit_cards_bounded(
1349 db_->GetSQLConnection()->GetUniqueStatement(
1350 "SELECT date_modified FROM credit_cards"));
1351 ASSERT_TRUE(s_credit_cards_bounded.is_valid());
1352 ASSERT_TRUE(s_credit_cards_bounded.Step());
1353 EXPECT_EQ(47, s_credit_cards_bounded.ColumnInt64(0));
1354 ASSERT_TRUE(s_credit_cards_bounded.Step());
1355 EXPECT_EQ(57, s_credit_cards_bounded.ColumnInt64(0));
1356 ASSERT_TRUE(s_credit_cards_bounded.Step());
1357 EXPECT_EQ(67, s_credit_cards_bounded.ColumnInt64(0));
1358 EXPECT_FALSE(s_credit_cards_bounded.Step());
1360 // Remove all entries modified on or after time 51 (unbounded range).
1361 table_->RemoveAutofillDataModifiedBetween(
1362 Time::FromTimeT(51), Time(),
1363 &profile_guids, &credit_card_guids);
1364 ASSERT_EQ(2UL, profile_guids.size());
1365 EXPECT_EQ("00000000-0000-0000-0000-000000000004", profile_guids[0]);
1366 EXPECT_EQ("00000000-0000-0000-0000-000000000005", profile_guids[1]);
1367 sql::Statement s_autofill_profiles_unbounded(
1368 db_->GetSQLConnection()->GetUniqueStatement(
1369 "SELECT date_modified FROM autofill_profiles"));
1370 ASSERT_TRUE(s_autofill_profiles_unbounded.is_valid());
1371 ASSERT_TRUE(s_autofill_profiles_unbounded.Step());
1372 EXPECT_EQ(11, s_autofill_profiles_unbounded.ColumnInt64(0));
1373 ASSERT_TRUE(s_autofill_profiles_unbounded.Step());
1374 EXPECT_EQ(41, s_autofill_profiles_unbounded.ColumnInt64(0));
1375 EXPECT_FALSE(s_autofill_profiles_unbounded.Step());
1376 ASSERT_EQ(2UL, credit_card_guids.size());
1377 EXPECT_EQ("00000000-0000-0000-0000-000000000010", credit_card_guids[0]);
1378 EXPECT_EQ("00000000-0000-0000-0000-000000000011", credit_card_guids[1]);
1379 sql::Statement s_credit_cards_unbounded(
1380 db_->GetSQLConnection()->GetUniqueStatement(
1381 "SELECT date_modified FROM credit_cards"));
1382 ASSERT_TRUE(s_credit_cards_unbounded.is_valid());
1383 ASSERT_TRUE(s_credit_cards_unbounded.Step());
1384 EXPECT_EQ(47, s_credit_cards_unbounded.ColumnInt64(0));
1385 EXPECT_FALSE(s_credit_cards_unbounded.Step());
1387 // Remove all remaining entries.
1388 table_->RemoveAutofillDataModifiedBetween(
1390 &profile_guids, &credit_card_guids);
1391 ASSERT_EQ(2UL, profile_guids.size());
1392 EXPECT_EQ("00000000-0000-0000-0000-000000000000", profile_guids[0]);
1393 EXPECT_EQ("00000000-0000-0000-0000-000000000003", profile_guids[1]);
1394 sql::Statement s_autofill_profiles_empty(
1395 db_->GetSQLConnection()->GetUniqueStatement(
1396 "SELECT date_modified FROM autofill_profiles"));
1397 ASSERT_TRUE(s_autofill_profiles_empty.is_valid());
1398 EXPECT_FALSE(s_autofill_profiles_empty.Step());
1399 ASSERT_EQ(1UL, credit_card_guids.size());
1400 EXPECT_EQ("00000000-0000-0000-0000-000000000009", credit_card_guids[0]);
1401 sql::Statement s_credit_cards_empty(
1402 db_->GetSQLConnection()->GetUniqueStatement(
1403 "SELECT date_modified FROM credit_cards"));
1404 ASSERT_TRUE(s_credit_cards_empty.is_valid());
1405 EXPECT_FALSE(s_credit_cards_empty.Step());
1408 TEST_F(AutofillTableTest, RemoveOriginURLsModifiedBetween) {
1409 // Populate the autofill_profiles and credit_cards tables.
1410 ASSERT_TRUE(db_->GetSQLConnection()->Execute(
1411 "INSERT INTO autofill_profiles (guid, origin, date_modified) "
1412 "VALUES('00000000-0000-0000-0000-000000000000', '', 11);"
1413 "INSERT INTO autofill_profiles (guid, origin, date_modified) "
1414 "VALUES('00000000-0000-0000-0000-000000000001', "
1415 " 'https://www.example.com/', 21);"
1416 "INSERT INTO autofill_profiles (guid, origin, date_modified) "
1417 "VALUES('00000000-0000-0000-0000-000000000002', 'Chrome settings', 31);"
1418 "INSERT INTO credit_cards (guid, origin, date_modified) "
1419 "VALUES('00000000-0000-0000-0000-000000000003', '', 17);"
1420 "INSERT INTO credit_cards (guid, origin, date_modified) "
1421 "VALUES('00000000-0000-0000-0000-000000000004', "
1422 " 'https://www.example.com/', 27);"
1423 "INSERT INTO credit_cards (guid, origin, date_modified) "
1424 "VALUES('00000000-0000-0000-0000-000000000005', 'Chrome settings', "
1427 // Remove all origin URLs set in the bounded time range [21,27).
1428 ScopedVector<AutofillProfile> profiles;
1429 table_->RemoveOriginURLsModifiedBetween(
1430 Time::FromTimeT(21), Time::FromTimeT(27), &profiles);
1431 ASSERT_EQ(1UL, profiles.size());
1432 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profiles[0]->guid());
1433 sql::Statement s_autofill_profiles_bounded(
1434 db_->GetSQLConnection()->GetUniqueStatement(
1435 "SELECT date_modified, origin FROM autofill_profiles"));
1436 ASSERT_TRUE(s_autofill_profiles_bounded.is_valid());
1437 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1438 EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0));
1439 EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1));
1440 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1441 EXPECT_EQ(21, s_autofill_profiles_bounded.ColumnInt64(0));
1442 EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1));
1443 ASSERT_TRUE(s_autofill_profiles_bounded.Step());
1444 EXPECT_EQ(31, s_autofill_profiles_bounded.ColumnInt64(0));
1445 EXPECT_EQ("Chrome settings", s_autofill_profiles_bounded.ColumnString(1));
1446 sql::Statement s_credit_cards_bounded(
1447 db_->GetSQLConnection()->GetUniqueStatement(
1448 "SELECT date_modified, origin FROM credit_cards"));
1449 ASSERT_TRUE(s_credit_cards_bounded.is_valid());
1450 ASSERT_TRUE(s_credit_cards_bounded.Step());
1451 EXPECT_EQ(17, s_credit_cards_bounded.ColumnInt64(0));
1452 EXPECT_EQ(std::string(), s_credit_cards_bounded.ColumnString(1));
1453 ASSERT_TRUE(s_credit_cards_bounded.Step());
1454 EXPECT_EQ(27, s_credit_cards_bounded.ColumnInt64(0));
1455 EXPECT_EQ("https://www.example.com/",
1456 s_credit_cards_bounded.ColumnString(1));
1457 ASSERT_TRUE(s_credit_cards_bounded.Step());
1458 EXPECT_EQ(37, s_credit_cards_bounded.ColumnInt64(0));
1459 EXPECT_EQ("Chrome settings", s_credit_cards_bounded.ColumnString(1));
1461 // Remove all origin URLS.
1463 table_->RemoveOriginURLsModifiedBetween(Time(), Time(), &profiles);
1464 EXPECT_EQ(0UL, profiles.size());
1465 sql::Statement s_autofill_profiles_all(
1466 db_->GetSQLConnection()->GetUniqueStatement(
1467 "SELECT date_modified, origin FROM autofill_profiles"));
1468 ASSERT_TRUE(s_autofill_profiles_all.is_valid());
1469 ASSERT_TRUE(s_autofill_profiles_all.Step());
1470 EXPECT_EQ(11, s_autofill_profiles_all.ColumnInt64(0));
1471 EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1));
1472 ASSERT_TRUE(s_autofill_profiles_all.Step());
1473 EXPECT_EQ(21, s_autofill_profiles_all.ColumnInt64(0));
1474 EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1));
1475 ASSERT_TRUE(s_autofill_profiles_all.Step());
1476 EXPECT_EQ(31, s_autofill_profiles_all.ColumnInt64(0));
1477 EXPECT_EQ("Chrome settings", s_autofill_profiles_all.ColumnString(1));
1478 sql::Statement s_credit_cards_all(
1479 db_->GetSQLConnection()->GetUniqueStatement(
1480 "SELECT date_modified, origin FROM credit_cards"));
1481 ASSERT_TRUE(s_credit_cards_all.is_valid());
1482 ASSERT_TRUE(s_credit_cards_all.Step());
1483 EXPECT_EQ(17, s_credit_cards_all.ColumnInt64(0));
1484 EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1));
1485 ASSERT_TRUE(s_credit_cards_all.Step());
1486 EXPECT_EQ(27, s_credit_cards_all.ColumnInt64(0));
1487 EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1));
1488 ASSERT_TRUE(s_credit_cards_all.Step());
1489 EXPECT_EQ(37, s_credit_cards_all.ColumnInt64(0));
1490 EXPECT_EQ("Chrome settings", s_credit_cards_all.ColumnString(1));
1493 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_NoResults) {
1494 std::vector<AutofillEntry> entries;
1495 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1497 EXPECT_EQ(0U, entries.size());
1500 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) {
1501 AutofillChangeList changes;
1502 std::map<std::string, std::vector<Time> > name_value_times_map;
1505 std::vector<Time> timestamps1;
1506 FormFieldData field;
1507 field.name = ASCIIToUTF16("Name");
1508 field.value = ASCIIToUTF16("Superman");
1509 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
1510 Time::FromTimeT(start)));
1511 timestamps1.push_back(Time::FromTimeT(start));
1512 std::string key1("NameSuperman");
1513 name_value_times_map.insert(
1514 std::pair<std::string, std::vector<Time> >(key1, timestamps1));
1516 AutofillEntrySet expected_entries(CompareAutofillEntries);
1517 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1518 AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back());
1520 expected_entries.insert(ae1);
1522 std::vector<AutofillEntry> entries;
1523 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1524 AutofillEntrySet entry_set(entries.begin(), entries.end(),
1525 CompareAutofillEntries);
1527 CompareAutofillEntrySets(entry_set, expected_entries);
1530 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) {
1531 AutofillChangeList changes;
1532 std::map<std::string, std::vector<Time> > name_value_times_map;
1535 std::vector<Time> timestamps1;
1536 FormFieldData field;
1537 field.name = ASCIIToUTF16("Name");
1538 field.value = ASCIIToUTF16("Superman");
1539 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
1540 Time::FromTimeT(start)));
1541 timestamps1.push_back(Time::FromTimeT(start));
1542 std::string key1("NameSuperman");
1543 name_value_times_map.insert(
1544 std::pair<std::string, std::vector<Time> >(key1, timestamps1));
1547 std::vector<Time> timestamps2;
1548 field.name = ASCIIToUTF16("Name");
1549 field.value = ASCIIToUTF16("Clark Kent");
1550 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
1551 Time::FromTimeT(start)));
1552 timestamps2.push_back(Time::FromTimeT(start));
1553 std::string key2("NameClark Kent");
1554 name_value_times_map.insert(
1555 std::pair<std::string, std::vector<Time> >(key2, timestamps2));
1557 AutofillEntrySet expected_entries(CompareAutofillEntries);
1558 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1559 AutofillKey ak2(ASCIIToUTF16("Name"), ASCIIToUTF16("Clark Kent"));
1560 AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back());
1561 AutofillEntry ae2(ak2, timestamps2.front(), timestamps2.back());
1563 expected_entries.insert(ae1);
1564 expected_entries.insert(ae2);
1566 std::vector<AutofillEntry> entries;
1567 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1568 AutofillEntrySet entry_set(entries.begin(), entries.end(),
1569 CompareAutofillEntries);
1571 CompareAutofillEntrySets(entry_set, expected_entries);
1574 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) {
1575 AutofillChangeList changes;
1576 std::map<std::string, std::vector<Time> > name_value_times_map;
1578 std::vector<Time> timestamps;
1580 for (int i = 0; i < 2; ++i, ++start) {
1581 FormFieldData field;
1582 field.name = ASCIIToUTF16("Name");
1583 field.value = ASCIIToUTF16("Superman");
1584 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
1585 Time::FromTimeT(start)));
1586 timestamps.push_back(Time::FromTimeT(start));
1589 std::string key("NameSuperman");
1590 name_value_times_map.insert(
1591 std::pair<std::string, std::vector<Time> >(key, timestamps));
1593 AutofillEntrySet expected_entries(CompareAutofillEntries);
1594 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1595 AutofillEntry ae1(ak1, timestamps.front(), timestamps.back());
1597 expected_entries.insert(ae1);
1599 std::vector<AutofillEntry> entries;
1600 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1601 AutofillEntrySet entry_set(entries.begin(), entries.end(),
1602 CompareAutofillEntries);
1604 CompareAutofillEntrySets(entry_set, expected_entries);
1607 } // namespace autofill