Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / webdata / autofill_table_unittest.cc
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.
4
5 #include <vector>
6
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/guid.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"
25
26 using base::ASCIIToUTF16;
27 using base::Time;
28 using base::TimeDelta;
29
30 namespace autofill {
31
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());
36 }
37
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: {
42       os << "ADD";
43       break;
44     }
45     case AutofillChange::UPDATE: {
46       os << "UPDATE";
47       break;
48     }
49     case AutofillChange::REMOVE: {
50       os << "REMOVE";
51       break;
52     }
53   }
54   return os << " " << change.key();
55 }
56
57 namespace {
58
59 typedef std::set<AutofillEntry,
60     bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet;
61 typedef AutofillEntrySet::iterator AutofillEntrySetIterator;
62
63 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) {
64   int compVal = a.key().name().compare(b.key().name());
65   if (compVal != 0)
66     return compVal < 0;
67
68   compVal = a.key().value().compare(b.key().value());
69   if (compVal != 0)
70     return compVal < 0;
71
72   if (a.date_created() != b.date_created())
73     return a.date_created() < b.date_created();
74
75   return a.date_last_used() < b.date_last_used();
76 }
77
78 AutofillEntry MakeAutofillEntry(const char* name,
79                                 const char* value,
80                                 time_t date_created,
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));
87 }
88
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());
93   size_t count = 0;
94   for (AutofillEntrySet::const_iterator it = actual.begin();
95        it != actual.end(); ++it) {
96     count += expected.count(*it);
97   }
98   EXPECT_EQ(actual.size(), count);
99 }
100
101 int GetAutofillEntryCount(const base::string16& name,
102                           const base::string16& value,
103                           WebDatabase* db) {
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);
108   s.Step();
109   return s.ColumnInt(0);
110 }
111
112 }  // namespace
113
114 class AutofillTableTest : public testing::Test {
115  public:
116   AutofillTableTest() {}
117   ~AutofillTableTest() override {}
118
119  protected:
120   void SetUp() override {
121 #if defined(OS_MACOSX)
122     OSCrypt::UseMockKeychain(true);
123 #endif
124     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
125     file_ = temp_dir_.path().AppendASCII("TestWebDatabase");
126
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_));
131   }
132
133   base::FilePath file_;
134   base::ScopedTempDir temp_dir_;
135   scoped_ptr<AutofillTable> table_;
136   scoped_ptr<WebDatabase> db_;
137
138  private:
139   DISALLOW_COPY_AND_ASSIGN(AutofillTableTest);
140 };
141
142 TEST_F(AutofillTableTest, Autofill) {
143   Time t1 = Time::Now();
144
145   // Simulate the submission of a handful of entries in a field called "Name",
146   // some more often than others.
147   AutofillChangeList changes;
148   FormFieldData field;
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));
161   }
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));
166   }
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));
172   }
173
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()));
177
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()));
182
183   EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("Favorite Color"),
184                                      ASCIIToUTF16("Green"), db_.get()));
185
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());
193   if (v.size() == 3) {
194     EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
195     EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]);
196     EXPECT_EQ(ASCIIToUTF16("Superman"), v[2]);
197   }
198
199   // If we query again limiting the list size to 1, we should only get the most
200   // frequent entry.
201   EXPECT_TRUE(table_->GetFormValuesForElementName(
202       ASCIIToUTF16("Name"), base::string16(), &v, 1));
203   EXPECT_EQ(1U, v.size());
204   if (v.size() == 1) {
205     EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
206   }
207
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());
213   if (v.size() == 2) {
214     EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]);
215     EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]);
216   }
217
218   // Removing all elements since the beginning of this function should remove
219   // everything from the database.
220   changes.clear();
221   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, Time(), &changes));
222
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"))),
236   };
237   EXPECT_EQ(arraysize(kExpectedChanges), changes.size());
238   for (size_t i = 0; i < arraysize(kExpectedChanges); ++i) {
239     EXPECT_EQ(kExpectedChanges[i], changes[i]);
240   }
241
242   EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"),
243                                      ASCIIToUTF16("Clark Kent"), db_.get()));
244
245   EXPECT_TRUE(table_->GetFormValuesForElementName(
246       ASCIIToUTF16("Name"), base::string16(), &v, 6));
247   EXPECT_EQ(0U, v.size());
248
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));
263
264   // They should be stored normally as the DB layer does not check for empty
265   // values.
266   v.clear();
267   EXPECT_TRUE(table_->GetFormValuesForElementName(
268       ASCIIToUTF16("blank"), base::string16(), &v, 10));
269   EXPECT_EQ(4U, v.size());
270 }
271
272 TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) {
273   TimeDelta one_day(TimeDelta::FromDays(1));
274   Time t1 = Time::Now();
275   Time t2 = t1 + one_day;
276
277   AutofillChangeList changes;
278   FormFieldData field;
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));
283
284   changes.clear();
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"))),
290             changes[0]);
291   changes.clear();
292
293   EXPECT_TRUE(
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"))),
299             changes[0]);
300 }
301
302 TEST_F(AutofillTableTest, Autofill_AddChanges) {
303   TimeDelta one_day(TimeDelta::FromDays(1));
304   Time t1 = Time::Now();
305   Time t2 = t1 + one_day;
306
307   AutofillChangeList changes;
308   FormFieldData field;
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"))),
316             changes[0]);
317
318   changes.clear();
319   EXPECT_TRUE(
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"))),
325             changes[0]);
326 }
327
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));
333
334   EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"),
335                                      db_.get()));
336
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]);
341 }
342
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));
348
349   EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"),
350                                      db_.get()));
351
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]);
356 }
357
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));
363
364   Time date_created, date_last_used;
365   ASSERT_TRUE(table_->GetAutofillTimestamps(ASCIIToUTF16("foo"),
366                                             ASCIIToUTF16("bar"),
367                                             &date_created,
368                                             &date_last_used));
369   EXPECT_EQ(Time::FromTimeT(1), date_created);
370   EXPECT_EQ(Time::FromTimeT(2), date_last_used);
371 }
372
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));
380
381   EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar0"),
382                                      db_.get()));
383   EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar1"),
384                                      db_.get()));
385 }
386
387 TEST_F(AutofillTableTest, Autofill_UpdateReplace) {
388   AutofillChangeList changes;
389   // Add a form field.  This will be replaced.
390   FormFieldData field;
391   field.name = ASCIIToUTF16("Name");
392   field.value = ASCIIToUTF16("Superman");
393   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
394
395   AutofillEntry entry(MakeAutofillEntry("Name", "Superman", 1, 2));
396   std::vector<AutofillEntry> entries;
397   entries.push_back(entry);
398   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
399
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]);
404 }
405
406 TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) {
407   Time t = Time::Now();
408   AutofillEntry existing(
409       MakeAutofillEntry("Name", "Superman", t.ToTimeT(), -1));
410
411   AutofillChangeList changes;
412   // Add a form field.  This will NOT be replaced.
413   FormFieldData field;
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));
421
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(),
426                                     all_entries.end(),
427                                     CompareAutofillEntries);
428   EXPECT_EQ(1U, expected_entries.count(existing));
429   EXPECT_EQ(1U, expected_entries.count(entry));
430 }
431
432 TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) {
433   Time t = Time::Now();
434
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;
439   FormFieldData field;
440   field.name = ASCIIToUTF16("firstname");
441   field.value = ASCIIToUTF16("Joe");
442   elements.push_back(field);
443
444   field.name = ASCIIToUTF16("firstname");
445   field.value = ASCIIToUTF16("Jane");
446   elements.push_back(field);
447
448   field.name = ASCIIToUTF16("lastname");
449   field.value = ASCIIToUTF16("Smith");
450   elements.push_back(field);
451
452   field.name = ASCIIToUTF16("lastname");
453   field.value = ASCIIToUTF16("Jones");
454   elements.push_back(field);
455
456   std::vector<AutofillChange> changes;
457   table_->AddFormFieldValuesTime(elements, &changes, t);
458
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"))));
466
467   std::vector<AutofillEntry> all_entries;
468   ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
469   ASSERT_EQ(2U, all_entries.size());
470 }
471
472 TEST_F(AutofillTableTest,
473        Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore) {
474   // Add an entry used only before the targetted range.
475   AutofillChangeList changes;
476   FormFieldData field;
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)));
489
490   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
491
492   changes.clear();
493   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(51),
494                                                      base::Time::FromTimeT(60),
495                                                      &changes));
496   EXPECT_TRUE(changes.empty());
497   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
498 }
499
500 TEST_F(AutofillTableTest,
501        Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter) {
502   // Add an entry used only after the targetted range.
503   AutofillChangeList changes;
504   FormFieldData field;
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)));
517
518   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
519
520   changes.clear();
521   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
522                                                      base::Time::FromTimeT(50),
523                                                      &changes));
524   EXPECT_TRUE(changes.empty());
525   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
526 }
527
528 TEST_F(AutofillTableTest,
529        Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring) {
530   // Add an entry used entirely during the targetted range.
531   AutofillChangeList changes;
532   FormFieldData field;
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)));
545
546   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
547
548   changes.clear();
549   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(10),
550                                                      base::Time::FromTimeT(51),
551                                                      &changes));
552   ASSERT_EQ(1U, changes.size());
553   EXPECT_EQ(AutofillChange(AutofillChange::REMOVE,
554                            AutofillKey(field.name, field.value)),
555             changes[0]);
556   EXPECT_EQ(0, GetAutofillEntryCount(field.name, field.value, db_.get()));
557 }
558
559 TEST_F(AutofillTableTest,
560        Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring) {
561   // Add an entry used both before and during the targetted range.
562   AutofillChangeList changes;
563   FormFieldData field;
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)));
576
577   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
578
579   changes.clear();
580   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
581                                                      base::Time::FromTimeT(60),
582                                                      &changes));
583   ASSERT_EQ(1U, changes.size());
584   EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
585                            AutofillKey(field.name, field.value)),
586             changes[0]);
587   EXPECT_EQ(4, GetAutofillEntryCount(field.name, field.value, db_.get()));
588   base::Time date_created, date_last_used;
589   EXPECT_TRUE(
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);
594 }
595
596 TEST_F(AutofillTableTest,
597        Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter) {
598   // Add an entry used both during and after the targetted range.
599   AutofillChangeList changes;
600   FormFieldData field;
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)));
613
614   EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get()));
615
616   changes.clear();
617   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
618                                                      base::Time::FromTimeT(80),
619                                                      &changes));
620   ASSERT_EQ(1U, changes.size());
621   EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
622                            AutofillKey(field.name, field.value)),
623             changes[0]);
624   EXPECT_EQ(2, GetAutofillEntryCount(field.name, field.value, db_.get()));
625   base::Time date_created, date_last_used;
626   EXPECT_TRUE(
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);
631 }
632
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");
653
654   Time pre_creation_time = Time::Now();
655   EXPECT_TRUE(table_->AddAutofillProfile(home_profile));
656   Time post_creation_time = Time::Now();
657
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());
671   delete db_profile;
672
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"));
680
681   pre_creation_time = Time::Now();
682   EXPECT_TRUE(table_->AddAutofillProfile(billing_profile));
683   post_creation_time = Time::Now();
684
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());
696   delete db_profile;
697
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());
715   delete db_profile;
716
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());
751   delete db_profile;
752
753   // Remove the 'Billing' profile.
754   EXPECT_TRUE(table_->RemoveAutofillProfile(billing_profile.guid()));
755   EXPECT_FALSE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
756 }
757
758 TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) {
759   AutofillProfile p;
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);
766
767   EXPECT_TRUE(table_->AddAutofillProfile(p));
768
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));
773   delete db_profile;
774
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));
783   delete db_profile;
784
785   // Delete values.
786   set_values.clear();
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));
793   delete db_profile;
794 }
795
796 TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) {
797   AutofillProfile p;
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);
804
805   EXPECT_TRUE(table_->AddAutofillProfile(p));
806
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));
811   delete db_profile;
812
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));
821   delete db_profile;
822
823   // Delete values.
824   set_values.clear();
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));
831   delete db_profile;
832 }
833
834 TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) {
835   AutofillProfile p;
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);
842
843   EXPECT_TRUE(table_->AddAutofillProfile(p));
844
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));
849   delete db_profile;
850
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));
859   delete db_profile;
860
861   // Delete values.
862   set_values.clear();
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));
869   delete db_profile;
870 }
871
872 TEST_F(AutofillTableTest, AutofillProfileTrash) {
873   std::vector<std::string> guids;
874   table_->GetAutofillProfilesInTrash(&guids);
875   EXPECT_TRUE(guids.empty());
876
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]);
885
886   ASSERT_TRUE(table_->EmptyAutofillProfilesTrash());
887   ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
888   EXPECT_TRUE(guids.empty());
889 }
890
891 TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) {
892   std::vector<std::string> guids;
893   table_->GetAutofillProfilesInTrash(&guids);
894   EXPECT_TRUE(guids.empty());
895
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"));
906
907   // Mark this profile as in the trash.  This stops |AddAutofillProfile| from
908   // adding it.
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);
914
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(),
921                                                         &added_profile));
922   ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile);
923   delete added_profile;
924
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;
936
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;
949
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);
956 }
957
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"));
968
969   Time pre_creation_time = Time::Now();
970   EXPECT_TRUE(table_->AddCreditCard(work_creditcard));
971   Time post_creation_time = Time::Now();
972
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;
988
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"));
998
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;
1015
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;
1035
1036   // Remove the 'Target' credit card.
1037   EXPECT_TRUE(table_->RemoveCreditCard(target_creditcard.guid()));
1038   EXPECT_FALSE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
1039 }
1040
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);
1058
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());
1067
1068   // Get the profile.
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());
1079
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);
1084
1085   // Get the 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());
1095
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());
1104
1105   // Finally, call into |UpdateAutofillProfile()| without changing the
1106   // profile.  The modification date should not change.
1107   table_->UpdateAutofillProfile(profile);
1108
1109   // Get the 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());
1119 }
1120
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);
1129
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());
1138
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());
1150
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);
1155
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());
1166
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());
1175
1176   // Finally, call into |UpdateCreditCard()| without changing the credit card.
1177   // The modification date should not change.
1178   table_->UpdateCreditCard(credit_card);
1179
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());
1190 }
1191
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);
1208
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());
1217
1218   // Get the profile.
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());
1229
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);
1234
1235   // Get the 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());
1245 }
1246
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);
1255
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());
1264
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());
1276
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);
1281
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());
1292 }
1293
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);"));
1321
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());
1359
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());
1386
1387   // Remove all remaining entries.
1388   table_->RemoveAutofillDataModifiedBetween(
1389       Time(), Time(),
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());
1406 }
1407
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', "
1425       "       37);"));
1426
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));
1460
1461   // Remove all origin URLS.
1462   profiles.clear();
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));
1491 }
1492
1493 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_NoResults) {
1494   std::vector<AutofillEntry> entries;
1495   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1496
1497   EXPECT_EQ(0U, entries.size());
1498 }
1499
1500 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) {
1501   AutofillChangeList changes;
1502   std::map<std::string, std::vector<Time> > name_value_times_map;
1503
1504   time_t start = 0;
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));
1515
1516   AutofillEntrySet expected_entries(CompareAutofillEntries);
1517   AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1518   AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back());
1519
1520   expected_entries.insert(ae1);
1521
1522   std::vector<AutofillEntry> entries;
1523   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1524   AutofillEntrySet entry_set(entries.begin(), entries.end(),
1525                              CompareAutofillEntries);
1526
1527   CompareAutofillEntrySets(entry_set, expected_entries);
1528 }
1529
1530 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) {
1531   AutofillChangeList changes;
1532   std::map<std::string, std::vector<Time> > name_value_times_map;
1533   time_t start = 0;
1534
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));
1545
1546   ++start;
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));
1556
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());
1562
1563   expected_entries.insert(ae1);
1564   expected_entries.insert(ae2);
1565
1566   std::vector<AutofillEntry> entries;
1567   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1568   AutofillEntrySet entry_set(entries.begin(), entries.end(),
1569                              CompareAutofillEntries);
1570
1571   CompareAutofillEntrySets(entry_set, expected_entries);
1572 }
1573
1574 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) {
1575   AutofillChangeList changes;
1576   std::map<std::string, std::vector<Time> > name_value_times_map;
1577
1578   std::vector<Time> timestamps;
1579   time_t start = 0;
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));
1587   }
1588
1589   std::string key("NameSuperman");
1590   name_value_times_map.insert(
1591       std::pair<std::string, std::vector<Time> >(key, timestamps));
1592
1593   AutofillEntrySet expected_entries(CompareAutofillEntries);
1594   AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1595   AutofillEntry ae1(ak1, timestamps.front(), timestamps.back());
1596
1597   expected_entries.insert(ae1);
1598
1599   std::vector<AutofillEntry> entries;
1600   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
1601   AutofillEntrySet entry_set(entries.begin(), entries.end(),
1602                              CompareAutofillEntries);
1603
1604   CompareAutofillEntrySets(entry_set, expected_entries);
1605 }
1606
1607 }  // namespace autofill