Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: components/webdata/autofill/autofill_table_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698