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