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