Home | History | Annotate | Download | only in webdata
      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/file_util.h"
      8 #include "base/files/scoped_temp_dir.h"
      9 #include "base/guid.h"
     10 #include "base/path_service.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "base/strings/string_util.h"
     13 #include "base/strings/utf_string_conversions.h"
     14 #include "base/time/time.h"
     15 #include "components/autofill/core/browser/autofill_profile.h"
     16 #include "components/autofill/core/browser/autofill_type.h"
     17 #include "components/autofill/core/browser/credit_card.h"
     18 #include "components/autofill/core/browser/webdata/autofill_change.h"
     19 #include "components/autofill/core/browser/webdata/autofill_entry.h"
     20 #include "components/autofill/core/browser/webdata/autofill_table.h"
     21 #include "components/autofill/core/common/form_field_data.h"
     22 #include "components/webdata/common/web_database.h"
     23 #include "components/webdata/encryptor/encryptor.h"
     24 #include "sql/statement.h"
     25 #include "testing/gtest/include/gtest/gtest.h"
     26 
     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 << UTF16ToASCII(key.name()) << ", " << UTF16ToASCII(key.value());
     35 }
     36 
     37 // So we can compare AutofillChanges with EXPECT_EQ().
     38 std::ostream& operator<<(std::ostream& os, const AutofillChange& change) {
     39   switch (change.type()) {
     40     case AutofillChange::ADD: {
     41       os << "ADD";
     42       break;
     43     }
     44     case AutofillChange::UPDATE: {
     45       os << "UPDATE";
     46       break;
     47     }
     48     case AutofillChange::REMOVE: {
     49       os << "REMOVE";
     50       break;
     51     }
     52   }
     53   return os << " " << change.key();
     54 }
     55 
     56 namespace {
     57 
     58 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) {
     59   std::set<Time> timestamps1(a.timestamps().begin(), a.timestamps().end());
     60   std::set<Time> timestamps2(b.timestamps().begin(), b.timestamps().end());
     61 
     62   int compVal = a.key().name().compare(b.key().name());
     63   if (compVal != 0) {
     64     return compVal < 0;
     65   }
     66 
     67   compVal = a.key().value().compare(b.key().value());
     68   if (compVal != 0) {
     69     return compVal < 0;
     70   }
     71 
     72   if (timestamps1.size() != timestamps2.size()) {
     73     return timestamps1.size() < timestamps2.size();
     74   }
     75 
     76   std::set<Time>::iterator it;
     77   for (it = timestamps1.begin(); it != timestamps1.end(); it++) {
     78     timestamps2.erase(*it);
     79   }
     80 
     81   return !timestamps2.empty();
     82 }
     83 
     84 }  // anonymous namespace
     85 
     86 class AutofillTableTest : public testing::Test {
     87  public:
     88   AutofillTableTest() {}
     89   virtual ~AutofillTableTest() {}
     90 
     91  protected:
     92   typedef std::set<AutofillEntry,
     93     bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet;
     94   typedef std::set<AutofillEntry, bool (*)(const AutofillEntry&,
     95     const AutofillEntry&)>::iterator AutofillEntrySetIterator;
     96 
     97   virtual void SetUp() {
     98 #if defined(OS_MACOSX)
     99     Encryptor::UseMockKeychain(true);
    100 #endif
    101     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    102     file_ = temp_dir_.path().AppendASCII("TestWebDatabase");
    103 
    104     table_.reset(new AutofillTable("en-US"));
    105     db_.reset(new WebDatabase);
    106     db_->AddTable(table_.get());
    107     ASSERT_EQ(sql::INIT_OK, db_->Init(file_));
    108   }
    109 
    110   static AutofillEntry MakeAutofillEntry(const char* name,
    111                                          const char* value,
    112                                          time_t timestamp0,
    113                                          time_t timestamp1) {
    114     std::vector<Time> timestamps;
    115     if (timestamp0 >= 0)
    116       timestamps.push_back(Time::FromTimeT(timestamp0));
    117     if (timestamp1 >= 0)
    118       timestamps.push_back(Time::FromTimeT(timestamp1));
    119     return AutofillEntry(
    120         AutofillKey(ASCIIToUTF16(name), ASCIIToUTF16(value)), timestamps);
    121   }
    122 
    123   base::FilePath file_;
    124   base::ScopedTempDir temp_dir_;
    125   scoped_ptr<AutofillTable> table_;
    126   scoped_ptr<WebDatabase> db_;
    127 
    128  private:
    129   DISALLOW_COPY_AND_ASSIGN(AutofillTableTest);
    130 };
    131 
    132 TEST_F(AutofillTableTest, Autofill) {
    133   Time t1 = Time::Now();
    134 
    135   // Simulate the submission of a handful of entries in a field called "Name",
    136   // some more often than others.
    137   AutofillChangeList changes;
    138   FormFieldData field;
    139   field.name = ASCIIToUTF16("Name");
    140   field.value = ASCIIToUTF16("Superman");
    141   base::Time now = base::Time::Now();
    142   base::TimeDelta two_seconds = base::TimeDelta::FromSeconds(2);
    143   EXPECT_FALSE(table_->HasFormElements());
    144   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
    145   EXPECT_TRUE(table_->HasFormElements());
    146   std::vector<base::string16> v;
    147   for (int i = 0; i < 5; i++) {
    148     field.value = ASCIIToUTF16("Clark Kent");
    149     EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
    150                                               now + i * two_seconds));
    151   }
    152   for (int i = 0; i < 3; i++) {
    153     field.value = ASCIIToUTF16("Clark Sutter");
    154     EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
    155                                               now + i * two_seconds));
    156   }
    157   for (int i = 0; i < 2; i++) {
    158     field.name = ASCIIToUTF16("Favorite Color");
    159     field.value = ASCIIToUTF16("Green");
    160     EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
    161                                               now + i * two_seconds));
    162   }
    163 
    164   int count = 0;
    165   int64 pair_id = 0;
    166 
    167   // We have added the name Clark Kent 5 times, so count should be 5 and pair_id
    168   // should be somthing non-zero.
    169   field.name = ASCIIToUTF16("Name");
    170   field.value = ASCIIToUTF16("Clark Kent");
    171   EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count));
    172   EXPECT_EQ(5, count);
    173   EXPECT_NE(0, pair_id);
    174 
    175   // Storing in the data base should be case sensitive, so there should be no
    176   // database entry for clark kent lowercase.
    177   field.value = ASCIIToUTF16("clark kent");
    178   EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count));
    179   EXPECT_EQ(0, count);
    180 
    181   field.name = ASCIIToUTF16("Favorite Color");
    182   field.value = ASCIIToUTF16("Green");
    183   EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count));
    184   EXPECT_EQ(2, count);
    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 expected_changes[] = {
    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(expected_changes), changes.size());
    238   for (size_t i = 0; i < arraysize(expected_changes); i++) {
    239     EXPECT_EQ(expected_changes[i], changes[i]);
    240   }
    241 
    242   field.name = ASCIIToUTF16("Name");
    243   field.value = ASCIIToUTF16("Clark Kent");
    244   EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count));
    245   EXPECT_EQ(0, count);
    246 
    247   EXPECT_TRUE(table_->GetFormValuesForElementName(
    248       ASCIIToUTF16("Name"), base::string16(), &v, 6));
    249   EXPECT_EQ(0U, v.size());
    250 
    251   // Now add some values with empty strings.
    252   const base::string16 kValue = ASCIIToUTF16("  toto   ");
    253   field.name = ASCIIToUTF16("blank");
    254   field.value = base::string16();
    255   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
    256   field.name = ASCIIToUTF16("blank");
    257   field.value = ASCIIToUTF16(" ");
    258   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
    259   field.name = ASCIIToUTF16("blank");
    260   field.value = ASCIIToUTF16("      ");
    261   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
    262   field.name = ASCIIToUTF16("blank");
    263   field.value = kValue;
    264   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
    265 
    266   // They should be stored normally as the DB layer does not check for empty
    267   // values.
    268   v.clear();
    269   EXPECT_TRUE(table_->GetFormValuesForElementName(
    270       ASCIIToUTF16("blank"), base::string16(), &v, 10));
    271   EXPECT_EQ(4U, v.size());
    272 
    273   // Now we'll check that ClearAutofillEmptyValueElements() works as expected.
    274   table_->ClearAutofillEmptyValueElements();
    275 
    276   v.clear();
    277   EXPECT_TRUE(table_->GetFormValuesForElementName(
    278       ASCIIToUTF16("blank"), base::string16(), &v, 10));
    279   ASSERT_EQ(1U, v.size());
    280 
    281   EXPECT_EQ(kValue, v[0]);
    282 }
    283 
    284 TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) {
    285   TimeDelta one_day(TimeDelta::FromDays(1));
    286   Time t1 = Time::Now();
    287   Time t2 = t1 + one_day;
    288 
    289   AutofillChangeList changes;
    290   FormFieldData field;
    291   field.name = ASCIIToUTF16("Name");
    292   field.value = ASCIIToUTF16("Superman");
    293   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1));
    294   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t2));
    295 
    296   changes.clear();
    297   EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, t2, &changes));
    298   ASSERT_EQ(1U, changes.size());
    299   EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
    300                            AutofillKey(ASCIIToUTF16("Name"),
    301                                        ASCIIToUTF16("Superman"))),
    302             changes[0]);
    303   changes.clear();
    304 
    305   EXPECT_TRUE(
    306       table_->RemoveFormElementsAddedBetween(t2, t2 + one_day, &changes));
    307   ASSERT_EQ(1U, changes.size());
    308   EXPECT_EQ(AutofillChange(AutofillChange::REMOVE,
    309                            AutofillKey(ASCIIToUTF16("Name"),
    310                                        ASCIIToUTF16("Superman"))),
    311             changes[0]);
    312 }
    313 
    314 TEST_F(AutofillTableTest, Autofill_AddChanges) {
    315   TimeDelta one_day(TimeDelta::FromDays(1));
    316   Time t1 = Time::Now();
    317   Time t2 = t1 + one_day;
    318 
    319   AutofillChangeList changes;
    320   FormFieldData field;
    321   field.name = ASCIIToUTF16("Name");
    322   field.value = ASCIIToUTF16("Superman");
    323   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1));
    324   ASSERT_EQ(1U, changes.size());
    325   EXPECT_EQ(AutofillChange(AutofillChange::ADD,
    326                            AutofillKey(ASCIIToUTF16("Name"),
    327                                        ASCIIToUTF16("Superman"))),
    328             changes[0]);
    329 
    330   changes.clear();
    331   EXPECT_TRUE(
    332       table_->AddFormFieldValueTime(field, &changes, t2));
    333   ASSERT_EQ(1U, changes.size());
    334   EXPECT_EQ(AutofillChange(AutofillChange::UPDATE,
    335                            AutofillKey(ASCIIToUTF16("Name"),
    336                                        ASCIIToUTF16("Superman"))),
    337             changes[0]);
    338 }
    339 
    340 TEST_F(AutofillTableTest, Autofill_UpdateOneWithOneTimestamp) {
    341   AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, -1));
    342   std::vector<AutofillEntry> entries;
    343   entries.push_back(entry);
    344   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
    345 
    346   FormFieldData field;
    347   field.name = ASCIIToUTF16("foo");
    348   field.value = ASCIIToUTF16("bar");
    349   int64 pair_id;
    350   int count;
    351   ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count));
    352   EXPECT_LE(0, pair_id);
    353   EXPECT_EQ(1, count);
    354 
    355   std::vector<AutofillEntry> all_entries;
    356   ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
    357   ASSERT_EQ(1U, all_entries.size());
    358   EXPECT_TRUE(entry == all_entries[0]);
    359 }
    360 
    361 TEST_F(AutofillTableTest, Autofill_UpdateOneWithTwoTimestamps) {
    362   AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2));
    363   std::vector<AutofillEntry> entries;
    364   entries.push_back(entry);
    365   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
    366 
    367   FormFieldData field;
    368   field.name = ASCIIToUTF16("foo");
    369   field.value = ASCIIToUTF16("bar");
    370   int64 pair_id;
    371   int count;
    372   ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count));
    373   EXPECT_LE(0, pair_id);
    374   EXPECT_EQ(2, count);
    375 
    376   std::vector<AutofillEntry> all_entries;
    377   ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
    378   ASSERT_EQ(1U, all_entries.size());
    379   EXPECT_TRUE(entry == all_entries[0]);
    380 }
    381 
    382 TEST_F(AutofillTableTest, Autofill_GetAutofillTimestamps) {
    383   AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2));
    384   std::vector<AutofillEntry> entries;
    385   entries.push_back(entry);
    386   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
    387 
    388   std::vector<Time> timestamps;
    389   ASSERT_TRUE(table_->GetAutofillTimestamps(ASCIIToUTF16("foo"),
    390                                             ASCIIToUTF16("bar"),
    391                                             &timestamps));
    392   ASSERT_EQ(2U, timestamps.size());
    393   EXPECT_TRUE(Time::FromTimeT(1) == timestamps[0]);
    394   EXPECT_TRUE(Time::FromTimeT(2) == timestamps[1]);
    395 }
    396 
    397 TEST_F(AutofillTableTest, Autofill_UpdateTwo) {
    398   AutofillEntry entry0(MakeAutofillEntry("foo", "bar0", 1, -1));
    399   AutofillEntry entry1(MakeAutofillEntry("foo", "bar1", 2, 3));
    400   std::vector<AutofillEntry> entries;
    401   entries.push_back(entry0);
    402   entries.push_back(entry1);
    403   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
    404 
    405   FormFieldData field0;
    406   field0.name = ASCIIToUTF16("foo");
    407   field0.value = ASCIIToUTF16("bar0");
    408   int64 pair_id;
    409   int count;
    410   ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field0, &pair_id, &count));
    411   EXPECT_LE(0, pair_id);
    412   EXPECT_EQ(1, count);
    413 
    414   FormFieldData field1;
    415   field1.name = ASCIIToUTF16("foo");
    416   field1.value = ASCIIToUTF16("bar1");
    417   ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field1, &pair_id, &count));
    418   EXPECT_LE(0, pair_id);
    419   EXPECT_EQ(2, count);
    420 }
    421 
    422 TEST_F(AutofillTableTest, Autofill_UpdateReplace) {
    423   AutofillChangeList changes;
    424   // Add a form field.  This will be replaced.
    425   FormFieldData field;
    426   field.name = ASCIIToUTF16("Name");
    427   field.value = ASCIIToUTF16("Superman");
    428   EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
    429 
    430   AutofillEntry entry(MakeAutofillEntry("Name", "Superman", 1, 2));
    431   std::vector<AutofillEntry> entries;
    432   entries.push_back(entry);
    433   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
    434 
    435   std::vector<AutofillEntry> all_entries;
    436   ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
    437   ASSERT_EQ(1U, all_entries.size());
    438   EXPECT_TRUE(entry == all_entries[0]);
    439 }
    440 
    441 TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) {
    442   Time t = Time::Now();
    443   AutofillEntry existing(
    444       MakeAutofillEntry("Name", "Superman", t.ToTimeT(), -1));
    445 
    446   AutofillChangeList changes;
    447   // Add a form field.  This will NOT be replaced.
    448   FormFieldData field;
    449   field.name = existing.key().name();
    450   field.value = existing.key().value();
    451   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t));
    452   AutofillEntry entry(MakeAutofillEntry("Name", "Clark Kent", 1, 2));
    453   std::vector<AutofillEntry> entries;
    454   entries.push_back(entry);
    455   ASSERT_TRUE(table_->UpdateAutofillEntries(entries));
    456 
    457   std::vector<AutofillEntry> all_entries;
    458   ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
    459   ASSERT_EQ(2U, all_entries.size());
    460   AutofillEntrySet expected_entries(all_entries.begin(),
    461                                     all_entries.end(),
    462                                     CompareAutofillEntries);
    463   EXPECT_EQ(1U, expected_entries.count(existing));
    464   EXPECT_EQ(1U, expected_entries.count(entry));
    465 }
    466 
    467 TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) {
    468   Time t = Time::Now();
    469 
    470   // Add multiple values for "firstname" and "lastname" names.  Test that only
    471   // first value of each gets added. Related to security issue:
    472   // http://crbug.com/51727.
    473   std::vector<FormFieldData> elements;
    474   FormFieldData field;
    475   field.name = ASCIIToUTF16("firstname");
    476   field.value = ASCIIToUTF16("Joe");
    477   elements.push_back(field);
    478 
    479   field.name = ASCIIToUTF16("firstname");
    480   field.value = ASCIIToUTF16("Jane");
    481   elements.push_back(field);
    482 
    483   field.name = ASCIIToUTF16("lastname");
    484   field.value = ASCIIToUTF16("Smith");
    485   elements.push_back(field);
    486 
    487   field.name = ASCIIToUTF16("lastname");
    488   field.value = ASCIIToUTF16("Jones");
    489   elements.push_back(field);
    490 
    491   std::vector<AutofillChange> changes;
    492   table_->AddFormFieldValuesTime(elements, &changes, t);
    493 
    494   ASSERT_EQ(2U, changes.size());
    495   EXPECT_EQ(changes[0], AutofillChange(AutofillChange::ADD,
    496                                        AutofillKey(ASCIIToUTF16("firstname"),
    497                                        ASCIIToUTF16("Joe"))));
    498   EXPECT_EQ(changes[1], AutofillChange(AutofillChange::ADD,
    499                                        AutofillKey(ASCIIToUTF16("lastname"),
    500                                        ASCIIToUTF16("Smith"))));
    501 
    502   std::vector<AutofillEntry> all_entries;
    503   ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries));
    504   ASSERT_EQ(2U, all_entries.size());
    505 }
    506 
    507 TEST_F(AutofillTableTest, AutofillProfile) {
    508   // Add a 'Home' profile.
    509   AutofillProfile home_profile;
    510   home_profile.set_origin(std::string());
    511   home_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
    512   home_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
    513   home_profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
    514   home_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js (at) smith.xyz"));
    515   home_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google"));
    516   home_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way"));
    517   home_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5"));
    518   home_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
    519   home_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
    520   home_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
    521   home_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
    522   home_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567"));
    523 
    524   Time pre_creation_time = Time::Now();
    525   EXPECT_TRUE(table_->AddAutofillProfile(home_profile));
    526   Time post_creation_time = Time::Now();
    527 
    528   // Get the 'Home' profile.
    529   AutofillProfile* db_profile;
    530   ASSERT_TRUE(table_->GetAutofillProfile(home_profile.guid(), &db_profile));
    531   EXPECT_EQ(home_profile, *db_profile);
    532   sql::Statement s_home(db_->GetSQLConnection()->GetUniqueStatement(
    533       "SELECT date_modified "
    534       "FROM autofill_profiles WHERE guid=?"));
    535   s_home.BindString(0, home_profile.guid());
    536   ASSERT_TRUE(s_home.is_valid());
    537   ASSERT_TRUE(s_home.Step());
    538   EXPECT_GE(s_home.ColumnInt64(0), pre_creation_time.ToTimeT());
    539   EXPECT_LE(s_home.ColumnInt64(0), post_creation_time.ToTimeT());
    540   EXPECT_FALSE(s_home.Step());
    541   delete db_profile;
    542 
    543   // Add a 'Billing' profile.
    544   AutofillProfile billing_profile = home_profile;
    545   billing_profile.set_guid(base::GenerateGUID());
    546   billing_profile.set_origin("https://www.example.com/");
    547   billing_profile.SetRawInfo(ADDRESS_HOME_LINE1,
    548                              ASCIIToUTF16("5678 Bottom Street"));
    549   billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("suite 3"));
    550 
    551   pre_creation_time = Time::Now();
    552   EXPECT_TRUE(table_->AddAutofillProfile(billing_profile));
    553   post_creation_time = Time::Now();
    554 
    555   // Get the 'Billing' profile.
    556   ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
    557   EXPECT_EQ(billing_profile, *db_profile);
    558   sql::Statement s_billing(db_->GetSQLConnection()->GetUniqueStatement(
    559       "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
    560   s_billing.BindString(0, billing_profile.guid());
    561   ASSERT_TRUE(s_billing.is_valid());
    562   ASSERT_TRUE(s_billing.Step());
    563   EXPECT_GE(s_billing.ColumnInt64(0), pre_creation_time.ToTimeT());
    564   EXPECT_LE(s_billing.ColumnInt64(0), post_creation_time.ToTimeT());
    565   EXPECT_FALSE(s_billing.Step());
    566   delete db_profile;
    567 
    568   // Update the 'Billing' profile, name only.
    569   billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane"));
    570   Time pre_modification_time = Time::Now();
    571   EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile));
    572   Time post_modification_time = Time::Now();
    573   ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
    574   EXPECT_EQ(billing_profile, *db_profile);
    575   sql::Statement s_billing_updated(db_->GetSQLConnection()->GetUniqueStatement(
    576       "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
    577   s_billing_updated.BindString(0, billing_profile.guid());
    578   ASSERT_TRUE(s_billing_updated.is_valid());
    579   ASSERT_TRUE(s_billing_updated.Step());
    580   EXPECT_GE(s_billing_updated.ColumnInt64(0),
    581             pre_modification_time.ToTimeT());
    582   EXPECT_LE(s_billing_updated.ColumnInt64(0),
    583             post_modification_time.ToTimeT());
    584   EXPECT_FALSE(s_billing_updated.Step());
    585   delete db_profile;
    586 
    587   // Update the 'Billing' profile.
    588   billing_profile.set_origin("Chrome settings");
    589   billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Janice"));
    590   billing_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("C."));
    591   billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Joplin"));
    592   billing_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("jane (at) singer.com"));
    593   billing_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Indy"));
    594   billing_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("Open Road"));
    595   billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("Route 66"));
    596   billing_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("NFA"));
    597   billing_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("NY"));
    598   billing_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("10011"));
    599   billing_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
    600   billing_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
    601                              ASCIIToUTF16("18181230000"));
    602   Time pre_modification_time_2 = Time::Now();
    603   EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile));
    604   Time post_modification_time_2 = Time::Now();
    605   ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
    606   EXPECT_EQ(billing_profile, *db_profile);
    607   sql::Statement s_billing_updated_2(
    608       db_->GetSQLConnection()->GetUniqueStatement(
    609           "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
    610   s_billing_updated_2.BindString(0, billing_profile.guid());
    611   ASSERT_TRUE(s_billing_updated_2.is_valid());
    612   ASSERT_TRUE(s_billing_updated_2.Step());
    613   EXPECT_GE(s_billing_updated_2.ColumnInt64(0),
    614             pre_modification_time_2.ToTimeT());
    615   EXPECT_LE(s_billing_updated_2.ColumnInt64(0),
    616             post_modification_time_2.ToTimeT());
    617   EXPECT_FALSE(s_billing_updated_2.Step());
    618   delete db_profile;
    619 
    620   // Remove the 'Billing' profile.
    621   EXPECT_TRUE(table_->RemoveAutofillProfile(billing_profile.guid()));
    622   EXPECT_FALSE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile));
    623 }
    624 
    625 TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) {
    626   AutofillProfile p;
    627   const base::string16 kJohnDoe(ASCIIToUTF16("John Doe"));
    628   const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe"));
    629   std::vector<base::string16> set_values;
    630   set_values.push_back(kJohnDoe);
    631   set_values.push_back(kJohnPDoe);
    632   p.SetRawMultiInfo(NAME_FULL, set_values);
    633 
    634   EXPECT_TRUE(table_->AddAutofillProfile(p));
    635 
    636   AutofillProfile* db_profile;
    637   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    638   EXPECT_EQ(p, *db_profile);
    639   EXPECT_EQ(0, p.Compare(*db_profile));
    640   delete db_profile;
    641 
    642   // Update the values.
    643   const base::string16 kNoOne(ASCIIToUTF16("No One"));
    644   set_values[1] = kNoOne;
    645   p.SetRawMultiInfo(NAME_FULL, set_values);
    646   EXPECT_TRUE(table_->UpdateAutofillProfile(p));
    647   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    648   EXPECT_EQ(p, *db_profile);
    649   EXPECT_EQ(0, p.Compare(*db_profile));
    650   delete db_profile;
    651 
    652   // Delete values.
    653   set_values.clear();
    654   p.SetRawMultiInfo(NAME_FULL, set_values);
    655   EXPECT_TRUE(table_->UpdateAutofillProfile(p));
    656   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    657   EXPECT_EQ(p, *db_profile);
    658   EXPECT_EQ(0, p.Compare(*db_profile));
    659   EXPECT_EQ(base::string16(), db_profile->GetRawInfo(NAME_FULL));
    660   delete db_profile;
    661 }
    662 
    663 TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) {
    664   AutofillProfile p;
    665   const base::string16 kJohnDoe(ASCIIToUTF16("john (at) doe.com"));
    666   const base::string16 kJohnPDoe(ASCIIToUTF16("john_p (at) doe.com"));
    667   std::vector<base::string16> set_values;
    668   set_values.push_back(kJohnDoe);
    669   set_values.push_back(kJohnPDoe);
    670   p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
    671 
    672   EXPECT_TRUE(table_->AddAutofillProfile(p));
    673 
    674   AutofillProfile* db_profile;
    675   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    676   EXPECT_EQ(p, *db_profile);
    677   EXPECT_EQ(0, p.Compare(*db_profile));
    678   delete db_profile;
    679 
    680   // Update the values.
    681   const base::string16 kNoOne(ASCIIToUTF16("no (at) one.com"));
    682   set_values[1] = kNoOne;
    683   p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
    684   EXPECT_TRUE(table_->UpdateAutofillProfile(p));
    685   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    686   EXPECT_EQ(p, *db_profile);
    687   EXPECT_EQ(0, p.Compare(*db_profile));
    688   delete db_profile;
    689 
    690   // Delete values.
    691   set_values.clear();
    692   p.SetRawMultiInfo(EMAIL_ADDRESS, set_values);
    693   EXPECT_TRUE(table_->UpdateAutofillProfile(p));
    694   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    695   EXPECT_EQ(p, *db_profile);
    696   EXPECT_EQ(0, p.Compare(*db_profile));
    697   EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS));
    698   delete db_profile;
    699 }
    700 
    701 TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) {
    702   AutofillProfile p;
    703   const base::string16 kJohnDoe(ASCIIToUTF16("4151112222"));
    704   const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333"));
    705   std::vector<base::string16> set_values;
    706   set_values.push_back(kJohnDoe);
    707   set_values.push_back(kJohnPDoe);
    708   p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
    709 
    710   EXPECT_TRUE(table_->AddAutofillProfile(p));
    711 
    712   AutofillProfile* db_profile;
    713   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    714   EXPECT_EQ(p, *db_profile);
    715   EXPECT_EQ(0, p.Compare(*db_profile));
    716   delete db_profile;
    717 
    718   // Update the values.
    719   const base::string16 kNoOne(ASCIIToUTF16("4151110000"));
    720   set_values[1] = kNoOne;
    721   p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
    722   EXPECT_TRUE(table_->UpdateAutofillProfile(p));
    723   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    724   EXPECT_EQ(p, *db_profile);
    725   EXPECT_EQ(0, p.Compare(*db_profile));
    726   delete db_profile;
    727 
    728   // Delete values.
    729   set_values.clear();
    730   p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values);
    731   EXPECT_TRUE(table_->UpdateAutofillProfile(p));
    732   ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile));
    733   EXPECT_EQ(p, *db_profile);
    734   EXPECT_EQ(0, p.Compare(*db_profile));
    735   EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS));
    736   delete db_profile;
    737 }
    738 
    739 TEST_F(AutofillTableTest, AutofillProfileTrash) {
    740   std::vector<std::string> guids;
    741   table_->GetAutofillProfilesInTrash(&guids);
    742   EXPECT_TRUE(guids.empty());
    743 
    744   ASSERT_TRUE(table_->AddAutofillGUIDToTrash(
    745       "00000000-0000-0000-0000-000000000000"));
    746   ASSERT_TRUE(table_->AddAutofillGUIDToTrash(
    747       "00000000-0000-0000-0000-000000000001"));
    748   ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
    749   EXPECT_EQ(2UL, guids.size());
    750   EXPECT_EQ("00000000-0000-0000-0000-000000000000", guids[0]);
    751   EXPECT_EQ("00000000-0000-0000-0000-000000000001", guids[1]);
    752 
    753   ASSERT_TRUE(table_->EmptyAutofillProfilesTrash());
    754   ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
    755   EXPECT_TRUE(guids.empty());
    756 }
    757 
    758 TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) {
    759   std::vector<std::string> guids;
    760   table_->GetAutofillProfilesInTrash(&guids);
    761   EXPECT_TRUE(guids.empty());
    762 
    763   AutofillProfile profile;
    764   profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
    765   profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
    766   profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
    767   profile.SetRawInfo(EMAIL_ADDRESS,ASCIIToUTF16("js (at) smith.xyz"));
    768   profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1 Main St"));
    769   profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
    770   profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
    771   profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
    772   profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
    773 
    774   // Mark this profile as in the trash.  This stops |AddAutofillProfile| from
    775   // adding it.
    776   EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid()));
    777   EXPECT_TRUE(table_->AddAutofillProfile(profile));
    778   AutofillProfile* added_profile = NULL;
    779   EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &added_profile));
    780   EXPECT_EQ(static_cast<AutofillProfile*>(NULL), added_profile);
    781 
    782   // Add the profile for real this time.
    783   EXPECT_TRUE(table_->EmptyAutofillProfilesTrash());
    784   EXPECT_TRUE(table_->GetAutofillProfilesInTrash(&guids));
    785   EXPECT_TRUE(guids.empty());
    786   EXPECT_TRUE(table_->AddAutofillProfile(profile));
    787   EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(),
    788                                                         &added_profile));
    789   ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile);
    790   delete added_profile;
    791 
    792   // Mark this profile as in the trash.  This stops |UpdateAutofillProfileMulti|
    793   // from updating it.  In normal operation a profile should not be both in the
    794   // trash and in the profiles table simultaneously.
    795   EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid()));
    796   profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane"));
    797   EXPECT_TRUE(table_->UpdateAutofillProfile(profile));
    798   AutofillProfile* updated_profile = NULL;
    799   EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &updated_profile));
    800   ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile);
    801   EXPECT_EQ(ASCIIToUTF16("John"), updated_profile->GetRawInfo(NAME_FIRST));
    802   delete updated_profile;
    803 
    804   // Try to delete the trashed profile.  This stops |RemoveAutofillProfile| from
    805   // deleting it.  In normal operation deletion is done by migration step, and
    806   // removal from trash is done by |WebDataService|.  |RemoveAutofillProfile|
    807   // does remove the item from the trash if it is found however, so that if
    808   // other clients remove it (via Sync say) then it is gone and doesn't need to
    809   // be processed further by |WebDataService|.
    810   EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid()));
    811   AutofillProfile* removed_profile = NULL;
    812   EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &removed_profile));
    813   EXPECT_FALSE(table_->IsAutofillGUIDInTrash(profile.guid()));
    814   ASSERT_NE(static_cast<AutofillProfile*>(NULL), removed_profile);
    815   delete removed_profile;
    816 
    817   // Check that emptying the trash now allows removal to occur.
    818   EXPECT_TRUE(table_->EmptyAutofillProfilesTrash());
    819   EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid()));
    820   removed_profile = NULL;
    821   EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &removed_profile));
    822   EXPECT_EQ(static_cast<AutofillProfile*>(NULL), removed_profile);
    823 }
    824 
    825 TEST_F(AutofillTableTest, CreditCard) {
    826   // Add a 'Work' credit card.
    827   CreditCard work_creditcard;
    828   work_creditcard.set_origin("https://www.example.com/");
    829   work_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
    830   work_creditcard.SetRawInfo(CREDIT_CARD_NUMBER,
    831                              ASCIIToUTF16("1234567890123456"));
    832   work_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04"));
    833   work_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR,
    834                              ASCIIToUTF16("2013"));
    835 
    836   Time pre_creation_time = Time::Now();
    837   EXPECT_TRUE(table_->AddCreditCard(work_creditcard));
    838   Time post_creation_time = Time::Now();
    839 
    840   // Get the 'Work' credit card.
    841   CreditCard* db_creditcard;
    842   ASSERT_TRUE(table_->GetCreditCard(work_creditcard.guid(), &db_creditcard));
    843   EXPECT_EQ(work_creditcard, *db_creditcard);
    844   sql::Statement s_work(db_->GetSQLConnection()->GetUniqueStatement(
    845       "SELECT guid, name_on_card, expiration_month, expiration_year, "
    846       "card_number_encrypted, date_modified "
    847       "FROM credit_cards WHERE guid=?"));
    848   s_work.BindString(0, work_creditcard.guid());
    849   ASSERT_TRUE(s_work.is_valid());
    850   ASSERT_TRUE(s_work.Step());
    851   EXPECT_GE(s_work.ColumnInt64(5), pre_creation_time.ToTimeT());
    852   EXPECT_LE(s_work.ColumnInt64(5), post_creation_time.ToTimeT());
    853   EXPECT_FALSE(s_work.Step());
    854   delete db_creditcard;
    855 
    856   // Add a 'Target' credit card.
    857   CreditCard target_creditcard;
    858   target_creditcard.set_origin(std::string());
    859   target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
    860   target_creditcard.SetRawInfo(CREDIT_CARD_NUMBER,
    861                                ASCIIToUTF16("1111222233334444"));
    862   target_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("06"));
    863   target_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR,
    864                                ASCIIToUTF16("2012"));
    865 
    866   pre_creation_time = Time::Now();
    867   EXPECT_TRUE(table_->AddCreditCard(target_creditcard));
    868   post_creation_time = Time::Now();
    869   ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
    870   EXPECT_EQ(target_creditcard, *db_creditcard);
    871   sql::Statement s_target(db_->GetSQLConnection()->GetUniqueStatement(
    872       "SELECT guid, name_on_card, expiration_month, expiration_year, "
    873       "card_number_encrypted, date_modified "
    874       "FROM credit_cards WHERE guid=?"));
    875   s_target.BindString(0, target_creditcard.guid());
    876   ASSERT_TRUE(s_target.is_valid());
    877   ASSERT_TRUE(s_target.Step());
    878   EXPECT_GE(s_target.ColumnInt64(5), pre_creation_time.ToTimeT());
    879   EXPECT_LE(s_target.ColumnInt64(5), post_creation_time.ToTimeT());
    880   EXPECT_FALSE(s_target.Step());
    881   delete db_creditcard;
    882 
    883   // Update the 'Target' credit card.
    884   target_creditcard.set_origin("Interactive Autofill dialog");
    885   target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Charles Grady"));
    886   Time pre_modification_time = Time::Now();
    887   EXPECT_TRUE(table_->UpdateCreditCard(target_creditcard));
    888   Time post_modification_time = Time::Now();
    889   ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
    890   EXPECT_EQ(target_creditcard, *db_creditcard);
    891   sql::Statement s_target_updated(db_->GetSQLConnection()->GetUniqueStatement(
    892       "SELECT guid, name_on_card, expiration_month, expiration_year, "
    893       "card_number_encrypted, date_modified "
    894       "FROM credit_cards WHERE guid=?"));
    895   s_target_updated.BindString(0, target_creditcard.guid());
    896   ASSERT_TRUE(s_target_updated.is_valid());
    897   ASSERT_TRUE(s_target_updated.Step());
    898   EXPECT_GE(s_target_updated.ColumnInt64(5), pre_modification_time.ToTimeT());
    899   EXPECT_LE(s_target_updated.ColumnInt64(5), post_modification_time.ToTimeT());
    900   EXPECT_FALSE(s_target_updated.Step());
    901   delete db_creditcard;
    902 
    903   // Remove the 'Target' credit card.
    904   EXPECT_TRUE(table_->RemoveCreditCard(target_creditcard.guid()));
    905   EXPECT_FALSE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard));
    906 }
    907 
    908 TEST_F(AutofillTableTest, UpdateAutofillProfile) {
    909   // Add a profile to the db.
    910   AutofillProfile profile;
    911   profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
    912   profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
    913   profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
    914   profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js (at) example.com"));
    915   profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google"));
    916   profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way"));
    917   profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5"));
    918   profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
    919   profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
    920   profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
    921   profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
    922   profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567"));
    923   table_->AddAutofillProfile(profile);
    924 
    925   // Set a mocked value for the profile's creation time.
    926   const time_t mock_creation_date = Time::Now().ToTimeT() - 13;
    927   sql::Statement s_mock_creation_date(
    928       db_->GetSQLConnection()->GetUniqueStatement(
    929           "UPDATE autofill_profiles SET date_modified = ?"));
    930   ASSERT_TRUE(s_mock_creation_date.is_valid());
    931   s_mock_creation_date.BindInt64(0, mock_creation_date);
    932   ASSERT_TRUE(s_mock_creation_date.Run());
    933 
    934   // Get the profile.
    935   AutofillProfile* tmp_profile;
    936   ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
    937   scoped_ptr<AutofillProfile> db_profile(tmp_profile);
    938   EXPECT_EQ(profile, *db_profile);
    939   sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
    940       "SELECT date_modified FROM autofill_profiles"));
    941   ASSERT_TRUE(s_original.is_valid());
    942   ASSERT_TRUE(s_original.Step());
    943   EXPECT_EQ(mock_creation_date, s_original.ColumnInt64(0));
    944   EXPECT_FALSE(s_original.Step());
    945 
    946   // Now, update the profile and save the update to the database.
    947   // The modification date should change to reflect the update.
    948   profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js (at) smith.xyz"));
    949   table_->UpdateAutofillProfile(profile);
    950 
    951   // Get the profile.
    952   ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
    953   db_profile.reset(tmp_profile);
    954   EXPECT_EQ(profile, *db_profile);
    955   sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
    956       "SELECT date_modified FROM autofill_profiles"));
    957   ASSERT_TRUE(s_updated.is_valid());
    958   ASSERT_TRUE(s_updated.Step());
    959   EXPECT_LT(mock_creation_date, s_updated.ColumnInt64(0));
    960   EXPECT_FALSE(s_updated.Step());
    961 
    962   // Set a mocked value for the profile's modification time.
    963   const time_t mock_modification_date = Time::Now().ToTimeT() - 7;
    964   sql::Statement s_mock_modification_date(
    965       db_->GetSQLConnection()->GetUniqueStatement(
    966           "UPDATE autofill_profiles SET date_modified = ?"));
    967   ASSERT_TRUE(s_mock_modification_date.is_valid());
    968   s_mock_modification_date.BindInt64(0, mock_modification_date);
    969   ASSERT_TRUE(s_mock_modification_date.Run());
    970 
    971   // Finally, call into |UpdateAutofillProfile()| without changing the
    972   // profile.  The modification date should not change.
    973   table_->UpdateAutofillProfile(profile);
    974 
    975   // Get the profile.
    976   ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
    977   db_profile.reset(tmp_profile);
    978   EXPECT_EQ(profile, *db_profile);
    979   sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement(
    980       "SELECT date_modified FROM autofill_profiles"));
    981   ASSERT_TRUE(s_unchanged.is_valid());
    982   ASSERT_TRUE(s_unchanged.Step());
    983   EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0));
    984   EXPECT_FALSE(s_unchanged.Step());
    985 }
    986 
    987 TEST_F(AutofillTableTest, UpdateCreditCard) {
    988   // Add a credit card to the db.
    989   CreditCard credit_card;
    990   credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
    991   credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456"));
    992   credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04"));
    993   credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013"));
    994   table_->AddCreditCard(credit_card);
    995 
    996   // Set a mocked value for the credit card's creation time.
    997   const time_t mock_creation_date = Time::Now().ToTimeT() - 13;
    998   sql::Statement s_mock_creation_date(
    999       db_->GetSQLConnection()->GetUniqueStatement(
   1000           "UPDATE credit_cards SET date_modified = ?"));
   1001   ASSERT_TRUE(s_mock_creation_date.is_valid());
   1002   s_mock_creation_date.BindInt64(0, mock_creation_date);
   1003   ASSERT_TRUE(s_mock_creation_date.Run());
   1004 
   1005   // Get the credit card.
   1006   CreditCard* tmp_credit_card;
   1007   ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
   1008   scoped_ptr<CreditCard> db_credit_card(tmp_credit_card);
   1009   EXPECT_EQ(credit_card, *db_credit_card);
   1010   sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
   1011       "SELECT date_modified FROM credit_cards"));
   1012   ASSERT_TRUE(s_original.is_valid());
   1013   ASSERT_TRUE(s_original.Step());
   1014   EXPECT_EQ(mock_creation_date, s_original.ColumnInt64(0));
   1015   EXPECT_FALSE(s_original.Step());
   1016 
   1017   // Now, update the credit card and save the update to the database.
   1018   // The modification date should change to reflect the update.
   1019   credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("01"));
   1020   table_->UpdateCreditCard(credit_card);
   1021 
   1022   // Get the credit card.
   1023   ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
   1024   db_credit_card.reset(tmp_credit_card);
   1025   EXPECT_EQ(credit_card, *db_credit_card);
   1026   sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
   1027       "SELECT date_modified FROM credit_cards"));
   1028   ASSERT_TRUE(s_updated.is_valid());
   1029   ASSERT_TRUE(s_updated.Step());
   1030   EXPECT_LT(mock_creation_date, s_updated.ColumnInt64(0));
   1031   EXPECT_FALSE(s_updated.Step());
   1032 
   1033   // Set a mocked value for the credit card's modification time.
   1034   const time_t mock_modification_date = Time::Now().ToTimeT() - 7;
   1035   sql::Statement s_mock_modification_date(
   1036       db_->GetSQLConnection()->GetUniqueStatement(
   1037           "UPDATE credit_cards SET date_modified = ?"));
   1038   ASSERT_TRUE(s_mock_modification_date.is_valid());
   1039   s_mock_modification_date.BindInt64(0, mock_modification_date);
   1040   ASSERT_TRUE(s_mock_modification_date.Run());
   1041 
   1042   // Finally, call into |UpdateCreditCard()| without changing the credit card.
   1043   // The modification date should not change.
   1044   table_->UpdateCreditCard(credit_card);
   1045 
   1046   // Get the credit card.
   1047   ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
   1048   db_credit_card.reset(tmp_credit_card);
   1049   EXPECT_EQ(credit_card, *db_credit_card);
   1050   sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement(
   1051       "SELECT date_modified FROM credit_cards"));
   1052   ASSERT_TRUE(s_unchanged.is_valid());
   1053   ASSERT_TRUE(s_unchanged.Step());
   1054   EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0));
   1055   EXPECT_FALSE(s_unchanged.Step());
   1056 }
   1057 
   1058 TEST_F(AutofillTableTest, UpdateProfileOriginOnly) {
   1059   // Add a profile to the db.
   1060   AutofillProfile profile;
   1061   profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
   1062   profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q."));
   1063   profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith"));
   1064   profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js (at) example.com"));
   1065   profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google"));
   1066   profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way"));
   1067   profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5"));
   1068   profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles"));
   1069   profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA"));
   1070   profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025"));
   1071   profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
   1072   profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567"));
   1073   table_->AddAutofillProfile(profile);
   1074 
   1075   // Set a mocked value for the profile's creation time.
   1076   const time_t mock_creation_date = Time::Now().ToTimeT() - 13;
   1077   sql::Statement s_mock_creation_date(
   1078       db_->GetSQLConnection()->GetUniqueStatement(
   1079           "UPDATE autofill_profiles SET date_modified = ?"));
   1080   ASSERT_TRUE(s_mock_creation_date.is_valid());
   1081   s_mock_creation_date.BindInt64(0, mock_creation_date);
   1082   ASSERT_TRUE(s_mock_creation_date.Run());
   1083 
   1084   // Get the profile.
   1085   AutofillProfile* tmp_profile;
   1086   ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
   1087   scoped_ptr<AutofillProfile> db_profile(tmp_profile);
   1088   EXPECT_EQ(profile, *db_profile);
   1089   sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
   1090       "SELECT date_modified FROM autofill_profiles"));
   1091   ASSERT_TRUE(s_original.is_valid());
   1092   ASSERT_TRUE(s_original.Step());
   1093   EXPECT_EQ(mock_creation_date, s_original.ColumnInt64(0));
   1094   EXPECT_FALSE(s_original.Step());
   1095 
   1096   // Now, update just the profile's origin and save the update to the database.
   1097   // The modification date should change to reflect the update.
   1098   profile.set_origin("https://www.example.com/");
   1099   table_->UpdateAutofillProfile(profile);
   1100 
   1101   // Get the profile.
   1102   ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile));
   1103   db_profile.reset(tmp_profile);
   1104   EXPECT_EQ(profile, *db_profile);
   1105   sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
   1106       "SELECT date_modified FROM autofill_profiles"));
   1107   ASSERT_TRUE(s_updated.is_valid());
   1108   ASSERT_TRUE(s_updated.Step());
   1109   EXPECT_LT(mock_creation_date, s_updated.ColumnInt64(0));
   1110   EXPECT_FALSE(s_updated.Step());
   1111 }
   1112 
   1113 TEST_F(AutofillTableTest, UpdateCreditCardOriginOnly) {
   1114   // Add a credit card to the db.
   1115   CreditCard credit_card;
   1116   credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance"));
   1117   credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456"));
   1118   credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04"));
   1119   credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013"));
   1120   table_->AddCreditCard(credit_card);
   1121 
   1122   // Set a mocked value for the credit card's creation time.
   1123   const time_t mock_creation_date = Time::Now().ToTimeT() - 13;
   1124   sql::Statement s_mock_creation_date(
   1125       db_->GetSQLConnection()->GetUniqueStatement(
   1126           "UPDATE credit_cards SET date_modified = ?"));
   1127   ASSERT_TRUE(s_mock_creation_date.is_valid());
   1128   s_mock_creation_date.BindInt64(0, mock_creation_date);
   1129   ASSERT_TRUE(s_mock_creation_date.Run());
   1130 
   1131   // Get the credit card.
   1132   CreditCard* tmp_credit_card;
   1133   ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
   1134   scoped_ptr<CreditCard> db_credit_card(tmp_credit_card);
   1135   EXPECT_EQ(credit_card, *db_credit_card);
   1136   sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement(
   1137       "SELECT date_modified FROM credit_cards"));
   1138   ASSERT_TRUE(s_original.is_valid());
   1139   ASSERT_TRUE(s_original.Step());
   1140   EXPECT_EQ(mock_creation_date, s_original.ColumnInt64(0));
   1141   EXPECT_FALSE(s_original.Step());
   1142 
   1143   // Now, update just the credit card's origin and save the update to the
   1144   // database.  The modification date should change to reflect the update.
   1145   credit_card.set_origin("https://www.example.com/");
   1146   table_->UpdateCreditCard(credit_card);
   1147 
   1148   // Get the credit card.
   1149   ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card));
   1150   db_credit_card.reset(tmp_credit_card);
   1151   EXPECT_EQ(credit_card, *db_credit_card);
   1152   sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement(
   1153       "SELECT date_modified FROM credit_cards"));
   1154   ASSERT_TRUE(s_updated.is_valid());
   1155   ASSERT_TRUE(s_updated.Step());
   1156   EXPECT_LT(mock_creation_date, s_updated.ColumnInt64(0));
   1157   EXPECT_FALSE(s_updated.Step());
   1158 }
   1159 
   1160 TEST_F(AutofillTableTest, RemoveAutofillDataModifiedBetween) {
   1161   // Populate the autofill_profiles and credit_cards tables.
   1162   ASSERT_TRUE(db_->GetSQLConnection()->Execute(
   1163       "INSERT INTO autofill_profiles (guid, date_modified) "
   1164       "VALUES('00000000-0000-0000-0000-000000000000', 11);"
   1165       "INSERT INTO autofill_profiles (guid, date_modified) "
   1166       "VALUES('00000000-0000-0000-0000-000000000001', 21);"
   1167       "INSERT INTO autofill_profiles (guid, date_modified) "
   1168       "VALUES('00000000-0000-0000-0000-000000000002', 31);"
   1169       "INSERT INTO autofill_profiles (guid, date_modified) "
   1170       "VALUES('00000000-0000-0000-0000-000000000003', 41);"
   1171       "INSERT INTO autofill_profiles (guid, date_modified) "
   1172       "VALUES('00000000-0000-0000-0000-000000000004', 51);"
   1173       "INSERT INTO autofill_profiles (guid, date_modified) "
   1174       "VALUES('00000000-0000-0000-0000-000000000005', 61);"
   1175       "INSERT INTO credit_cards (guid, date_modified) "
   1176       "VALUES('00000000-0000-0000-0000-000000000006', 17);"
   1177       "INSERT INTO credit_cards (guid, date_modified) "
   1178       "VALUES('00000000-0000-0000-0000-000000000007', 27);"
   1179       "INSERT INTO credit_cards (guid, date_modified) "
   1180       "VALUES('00000000-0000-0000-0000-000000000008', 37);"
   1181       "INSERT INTO credit_cards (guid, date_modified) "
   1182       "VALUES('00000000-0000-0000-0000-000000000009', 47);"
   1183       "INSERT INTO credit_cards (guid, date_modified) "
   1184       "VALUES('00000000-0000-0000-0000-000000000010', 57);"
   1185       "INSERT INTO credit_cards (guid, date_modified) "
   1186       "VALUES('00000000-0000-0000-0000-000000000011', 67);"));
   1187 
   1188   // Remove all entries modified in the bounded time range [17,41).
   1189   std::vector<std::string> profile_guids;
   1190   std::vector<std::string> credit_card_guids;
   1191   table_->RemoveAutofillDataModifiedBetween(
   1192       Time::FromTimeT(17), Time::FromTimeT(41),
   1193       &profile_guids, &credit_card_guids);
   1194   ASSERT_EQ(2UL, profile_guids.size());
   1195   EXPECT_EQ("00000000-0000-0000-0000-000000000001", profile_guids[0]);
   1196   EXPECT_EQ("00000000-0000-0000-0000-000000000002", profile_guids[1]);
   1197   sql::Statement s_autofill_profiles_bounded(
   1198       db_->GetSQLConnection()->GetUniqueStatement(
   1199           "SELECT date_modified FROM autofill_profiles"));
   1200   ASSERT_TRUE(s_autofill_profiles_bounded.is_valid());
   1201   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1202   EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0));
   1203   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1204   EXPECT_EQ(41, s_autofill_profiles_bounded.ColumnInt64(0));
   1205   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1206   EXPECT_EQ(51, s_autofill_profiles_bounded.ColumnInt64(0));
   1207   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1208   EXPECT_EQ(61, s_autofill_profiles_bounded.ColumnInt64(0));
   1209   EXPECT_FALSE(s_autofill_profiles_bounded.Step());
   1210   ASSERT_EQ(3UL, credit_card_guids.size());
   1211   EXPECT_EQ("00000000-0000-0000-0000-000000000006", credit_card_guids[0]);
   1212   EXPECT_EQ("00000000-0000-0000-0000-000000000007", credit_card_guids[1]);
   1213   EXPECT_EQ("00000000-0000-0000-0000-000000000008", credit_card_guids[2]);
   1214   sql::Statement s_credit_cards_bounded(
   1215       db_->GetSQLConnection()->GetUniqueStatement(
   1216           "SELECT date_modified FROM credit_cards"));
   1217   ASSERT_TRUE(s_credit_cards_bounded.is_valid());
   1218   ASSERT_TRUE(s_credit_cards_bounded.Step());
   1219   EXPECT_EQ(47, s_credit_cards_bounded.ColumnInt64(0));
   1220   ASSERT_TRUE(s_credit_cards_bounded.Step());
   1221   EXPECT_EQ(57, s_credit_cards_bounded.ColumnInt64(0));
   1222   ASSERT_TRUE(s_credit_cards_bounded.Step());
   1223   EXPECT_EQ(67, s_credit_cards_bounded.ColumnInt64(0));
   1224   EXPECT_FALSE(s_credit_cards_bounded.Step());
   1225 
   1226   // Remove all entries modified on or after time 51 (unbounded range).
   1227   table_->RemoveAutofillDataModifiedBetween(
   1228       Time::FromTimeT(51), Time(),
   1229       &profile_guids, &credit_card_guids);
   1230   ASSERT_EQ(2UL, profile_guids.size());
   1231   EXPECT_EQ("00000000-0000-0000-0000-000000000004", profile_guids[0]);
   1232   EXPECT_EQ("00000000-0000-0000-0000-000000000005", profile_guids[1]);
   1233   sql::Statement s_autofill_profiles_unbounded(
   1234       db_->GetSQLConnection()->GetUniqueStatement(
   1235           "SELECT date_modified FROM autofill_profiles"));
   1236   ASSERT_TRUE(s_autofill_profiles_unbounded.is_valid());
   1237   ASSERT_TRUE(s_autofill_profiles_unbounded.Step());
   1238   EXPECT_EQ(11, s_autofill_profiles_unbounded.ColumnInt64(0));
   1239   ASSERT_TRUE(s_autofill_profiles_unbounded.Step());
   1240   EXPECT_EQ(41, s_autofill_profiles_unbounded.ColumnInt64(0));
   1241   EXPECT_FALSE(s_autofill_profiles_unbounded.Step());
   1242   ASSERT_EQ(2UL, credit_card_guids.size());
   1243   EXPECT_EQ("00000000-0000-0000-0000-000000000010", credit_card_guids[0]);
   1244   EXPECT_EQ("00000000-0000-0000-0000-000000000011", credit_card_guids[1]);
   1245   sql::Statement s_credit_cards_unbounded(
   1246       db_->GetSQLConnection()->GetUniqueStatement(
   1247           "SELECT date_modified FROM credit_cards"));
   1248   ASSERT_TRUE(s_credit_cards_unbounded.is_valid());
   1249   ASSERT_TRUE(s_credit_cards_unbounded.Step());
   1250   EXPECT_EQ(47, s_credit_cards_unbounded.ColumnInt64(0));
   1251   EXPECT_FALSE(s_credit_cards_unbounded.Step());
   1252 
   1253   // Remove all remaining entries.
   1254   table_->RemoveAutofillDataModifiedBetween(
   1255       Time(), Time(),
   1256       &profile_guids, &credit_card_guids);
   1257   ASSERT_EQ(2UL, profile_guids.size());
   1258   EXPECT_EQ("00000000-0000-0000-0000-000000000000", profile_guids[0]);
   1259   EXPECT_EQ("00000000-0000-0000-0000-000000000003", profile_guids[1]);
   1260   sql::Statement s_autofill_profiles_empty(
   1261       db_->GetSQLConnection()->GetUniqueStatement(
   1262           "SELECT date_modified FROM autofill_profiles"));
   1263   ASSERT_TRUE(s_autofill_profiles_empty.is_valid());
   1264   EXPECT_FALSE(s_autofill_profiles_empty.Step());
   1265   ASSERT_EQ(1UL, credit_card_guids.size());
   1266   EXPECT_EQ("00000000-0000-0000-0000-000000000009", credit_card_guids[0]);
   1267   sql::Statement s_credit_cards_empty(
   1268       db_->GetSQLConnection()->GetUniqueStatement(
   1269           "SELECT date_modified FROM credit_cards"));
   1270   ASSERT_TRUE(s_credit_cards_empty.is_valid());
   1271   EXPECT_FALSE(s_credit_cards_empty.Step());
   1272 }
   1273 
   1274 TEST_F(AutofillTableTest, RemoveOriginURLsModifiedBetween) {
   1275   // Populate the autofill_profiles and credit_cards tables.
   1276   ASSERT_TRUE(db_->GetSQLConnection()->Execute(
   1277       "INSERT INTO autofill_profiles (guid, origin, date_modified) "
   1278       "VALUES('00000000-0000-0000-0000-000000000000', '', 11);"
   1279       "INSERT INTO autofill_profiles (guid, origin, date_modified) "
   1280       "VALUES('00000000-0000-0000-0000-000000000001', "
   1281       "       'https://www.example.com/', 21);"
   1282       "INSERT INTO autofill_profiles (guid, origin, date_modified) "
   1283       "VALUES('00000000-0000-0000-0000-000000000002', 'Chrome settings', 31);"
   1284       "INSERT INTO credit_cards (guid, origin, date_modified) "
   1285       "VALUES('00000000-0000-0000-0000-000000000003', '', 17);"
   1286       "INSERT INTO credit_cards (guid, origin, date_modified) "
   1287       "VALUES('00000000-0000-0000-0000-000000000004', "
   1288       "       'https://www.example.com/', 27);"
   1289       "INSERT INTO credit_cards (guid, origin, date_modified) "
   1290       "VALUES('00000000-0000-0000-0000-000000000005', 'Chrome settings', "
   1291       "       37);"));
   1292 
   1293   // Remove all origin URLs set in the bounded time range [21,27).
   1294   ScopedVector<AutofillProfile> profiles;
   1295   table_->RemoveOriginURLsModifiedBetween(
   1296       Time::FromTimeT(21), Time::FromTimeT(27), &profiles);
   1297   ASSERT_EQ(1UL, profiles.size());
   1298   EXPECT_EQ("00000000-0000-0000-0000-000000000001", profiles[0]->guid());
   1299   sql::Statement s_autofill_profiles_bounded(
   1300       db_->GetSQLConnection()->GetUniqueStatement(
   1301           "SELECT date_modified, origin FROM autofill_profiles"));
   1302   ASSERT_TRUE(s_autofill_profiles_bounded.is_valid());
   1303   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1304   EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0));
   1305   EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1));
   1306   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1307   EXPECT_EQ(21, s_autofill_profiles_bounded.ColumnInt64(0));
   1308   EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1));
   1309   ASSERT_TRUE(s_autofill_profiles_bounded.Step());
   1310   EXPECT_EQ(31, s_autofill_profiles_bounded.ColumnInt64(0));
   1311   EXPECT_EQ("Chrome settings", s_autofill_profiles_bounded.ColumnString(1));
   1312   sql::Statement s_credit_cards_bounded(
   1313       db_->GetSQLConnection()->GetUniqueStatement(
   1314           "SELECT date_modified, origin FROM credit_cards"));
   1315   ASSERT_TRUE(s_credit_cards_bounded.is_valid());
   1316   ASSERT_TRUE(s_credit_cards_bounded.Step());
   1317   EXPECT_EQ(17, s_credit_cards_bounded.ColumnInt64(0));
   1318   EXPECT_EQ(std::string(), s_credit_cards_bounded.ColumnString(1));
   1319   ASSERT_TRUE(s_credit_cards_bounded.Step());
   1320   EXPECT_EQ(27, s_credit_cards_bounded.ColumnInt64(0));
   1321   EXPECT_EQ("https://www.example.com/",
   1322             s_credit_cards_bounded.ColumnString(1));
   1323   ASSERT_TRUE(s_credit_cards_bounded.Step());
   1324   EXPECT_EQ(37, s_credit_cards_bounded.ColumnInt64(0));
   1325   EXPECT_EQ("Chrome settings", s_credit_cards_bounded.ColumnString(1));
   1326 
   1327   // Remove all origin URLS.
   1328   profiles.clear();
   1329   table_->RemoveOriginURLsModifiedBetween(Time(), Time(), &profiles);
   1330   EXPECT_EQ(0UL, profiles.size());
   1331   sql::Statement s_autofill_profiles_all(
   1332       db_->GetSQLConnection()->GetUniqueStatement(
   1333           "SELECT date_modified, origin FROM autofill_profiles"));
   1334   ASSERT_TRUE(s_autofill_profiles_all.is_valid());
   1335   ASSERT_TRUE(s_autofill_profiles_all.Step());
   1336   EXPECT_EQ(11, s_autofill_profiles_all.ColumnInt64(0));
   1337   EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1));
   1338   ASSERT_TRUE(s_autofill_profiles_all.Step());
   1339   EXPECT_EQ(21, s_autofill_profiles_all.ColumnInt64(0));
   1340   EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1));
   1341   ASSERT_TRUE(s_autofill_profiles_all.Step());
   1342   EXPECT_EQ(31, s_autofill_profiles_all.ColumnInt64(0));
   1343   EXPECT_EQ("Chrome settings", s_autofill_profiles_all.ColumnString(1));
   1344   sql::Statement s_credit_cards_all(
   1345       db_->GetSQLConnection()->GetUniqueStatement(
   1346           "SELECT date_modified, origin FROM credit_cards"));
   1347   ASSERT_TRUE(s_credit_cards_all.is_valid());
   1348   ASSERT_TRUE(s_credit_cards_all.Step());
   1349   EXPECT_EQ(17, s_credit_cards_all.ColumnInt64(0));
   1350   EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1));
   1351   ASSERT_TRUE(s_credit_cards_all.Step());
   1352   EXPECT_EQ(27, s_credit_cards_all.ColumnInt64(0));
   1353   EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1));
   1354   ASSERT_TRUE(s_credit_cards_all.Step());
   1355   EXPECT_EQ(37, s_credit_cards_all.ColumnInt64(0));
   1356   EXPECT_EQ("Chrome settings", s_credit_cards_all.ColumnString(1));
   1357 }
   1358 
   1359 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_NoResults) {
   1360   std::vector<AutofillEntry> entries;
   1361   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
   1362 
   1363   EXPECT_EQ(0U, entries.size());
   1364 }
   1365 
   1366 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) {
   1367   AutofillChangeList changes;
   1368   std::map<std::string, std::vector<Time> > name_value_times_map;
   1369 
   1370   time_t start = 0;
   1371   std::vector<Time> timestamps1;
   1372   FormFieldData field;
   1373   field.name = ASCIIToUTF16("Name");
   1374   field.value = ASCIIToUTF16("Superman");
   1375   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
   1376                                             Time::FromTimeT(start)));
   1377   timestamps1.push_back(Time::FromTimeT(start));
   1378   std::string key1("NameSuperman");
   1379   name_value_times_map.insert(std::pair<std::string,
   1380     std::vector<Time> > (key1, timestamps1));
   1381 
   1382   AutofillEntrySet expected_entries(CompareAutofillEntries);
   1383   AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
   1384   AutofillEntry ae1(ak1, timestamps1);
   1385 
   1386   expected_entries.insert(ae1);
   1387 
   1388   std::vector<AutofillEntry> entries;
   1389   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
   1390   AutofillEntrySet entry_set(entries.begin(), entries.end(),
   1391     CompareAutofillEntries);
   1392 
   1393   // make sure the lists of entries match
   1394   ASSERT_EQ(expected_entries.size(), entry_set.size());
   1395   AutofillEntrySetIterator it;
   1396   for (it = entry_set.begin(); it != entry_set.end(); it++) {
   1397     expected_entries.erase(*it);
   1398   }
   1399 
   1400   EXPECT_EQ(0U, expected_entries.size());
   1401 }
   1402 
   1403 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) {
   1404   AutofillChangeList changes;
   1405   std::map<std::string, std::vector<Time> > name_value_times_map;
   1406   time_t start = 0;
   1407 
   1408   std::vector<Time> timestamps1;
   1409   FormFieldData field;
   1410   field.name = ASCIIToUTF16("Name");
   1411   field.value = ASCIIToUTF16("Superman");
   1412   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
   1413                                             Time::FromTimeT(start)));
   1414   timestamps1.push_back(Time::FromTimeT(start));
   1415   std::string key1("NameSuperman");
   1416   name_value_times_map.insert(std::pair<std::string,
   1417     std::vector<Time> > (key1, timestamps1));
   1418 
   1419   start++;
   1420   std::vector<Time> timestamps2;
   1421   field.name = ASCIIToUTF16("Name");
   1422   field.value = ASCIIToUTF16("Clark Kent");
   1423   EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
   1424                                             Time::FromTimeT(start)));
   1425   timestamps2.push_back(Time::FromTimeT(start));
   1426   std::string key2("NameClark Kent");
   1427   name_value_times_map.insert(std::pair<std::string,
   1428     std::vector<Time> > (key2, timestamps2));
   1429 
   1430   AutofillEntrySet expected_entries(CompareAutofillEntries);
   1431   AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
   1432   AutofillKey ak2(ASCIIToUTF16("Name"), ASCIIToUTF16("Clark Kent"));
   1433   AutofillEntry ae1(ak1, timestamps1);
   1434   AutofillEntry ae2(ak2, timestamps2);
   1435 
   1436   expected_entries.insert(ae1);
   1437   expected_entries.insert(ae2);
   1438 
   1439   std::vector<AutofillEntry> entries;
   1440   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
   1441   AutofillEntrySet entry_set(entries.begin(), entries.end(),
   1442     CompareAutofillEntries);
   1443 
   1444   // make sure the lists of entries match
   1445   ASSERT_EQ(expected_entries.size(), entry_set.size());
   1446   AutofillEntrySetIterator it;
   1447   for (it = entry_set.begin(); it != entry_set.end(); it++) {
   1448     expected_entries.erase(*it);
   1449   }
   1450 
   1451   EXPECT_EQ(0U, expected_entries.size());
   1452 }
   1453 
   1454 TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) {
   1455   AutofillChangeList changes;
   1456   std::map<std::string, std::vector<Time> > name_value_times_map;
   1457 
   1458   time_t start = 0;
   1459   std::vector<Time> timestamps;
   1460   for (int i = 0; i < 2; i++) {
   1461     FormFieldData field;
   1462     field.name = ASCIIToUTF16("Name");
   1463     field.value = ASCIIToUTF16("Superman");
   1464     EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes,
   1465                                               Time::FromTimeT(start)));
   1466     timestamps.push_back(Time::FromTimeT(start));
   1467     start++;
   1468   }
   1469 
   1470   std::string key("NameSuperman");
   1471   name_value_times_map.insert(std::pair<std::string,
   1472       std::vector<Time> > (key, timestamps));
   1473 
   1474   AutofillEntrySet expected_entries(CompareAutofillEntries);
   1475   AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
   1476   AutofillEntry ae1(ak1, timestamps);
   1477 
   1478   expected_entries.insert(ae1);
   1479 
   1480   std::vector<AutofillEntry> entries;
   1481   ASSERT_TRUE(table_->GetAllAutofillEntries(&entries));
   1482   AutofillEntrySet entry_set(entries.begin(), entries.end(),
   1483     CompareAutofillEntries);
   1484 
   1485   // make sure the lists of entries match
   1486   ASSERT_EQ(expected_entries.size(), entry_set.size());
   1487   AutofillEntrySetIterator it;
   1488   for (it = entry_set.begin(); it != entry_set.end(); it++) {
   1489     expected_entries.erase(*it);
   1490   }
   1491 
   1492   EXPECT_EQ(0U, expected_entries.size());
   1493 }
   1494 
   1495 }  // namespace autofill
   1496