blob: a5a74babb1d17f320187d8bbc6c4320eeae3c6a7 [file] [log] [blame]
[email protected]2609bc12010-01-24 08:32:551// Copyright (c) 2010 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
[email protected]42fe7bf22010-12-17 21:44:475#include <string>
6
[email protected]2609bc12010-01-24 08:32:557#include "base/basictypes.h"
8#include "base/message_loop.h"
[email protected]36c1ff9fd2010-06-03 20:08:599#include "base/ref_counted.h"
[email protected]2609bc12010-01-24 08:32:5510#include "base/scoped_ptr.h"
[email protected]be1ce6a72010-08-03 14:35:2211#include "base/utf_string_conversions.h"
[email protected]468327f2010-10-04 20:02:2912#include "chrome/browser/autofill/autofill_common_test.h"
[email protected]2609bc12010-01-24 08:32:5513#include "chrome/browser/autofill/autofill_profile.h"
[email protected]ce78c1f92010-04-09 02:23:2614#include "chrome/browser/autofill/form_structure.h"
[email protected]2609bc12010-01-24 08:32:5515#include "chrome/browser/autofill/personal_data_manager.h"
[email protected]ed7e6dd2010-10-12 02:02:4516#include "chrome/browser/browser_thread.h"
[email protected]ecbf2892010-07-16 01:51:4517#include "chrome/browser/password_manager/encryptor.h"
[email protected]3eb0d8f72010-12-15 23:38:2518#include "chrome/common/guid.h"
[email protected]2609bc12010-01-24 08:32:5519#include "chrome/common/notification_details.h"
20#include "chrome/common/notification_observer_mock.h"
21#include "chrome/common/notification_registrar.h"
[email protected]42fe7bf22010-12-17 21:44:4722#include "chrome/common/notification_source.h"
[email protected]2609bc12010-01-24 08:32:5523#include "chrome/common/notification_type.h"
[email protected]2609bc12010-01-24 08:32:5524#include "chrome/test/testing_profile.h"
[email protected]ce78c1f92010-04-09 02:23:2625#include "webkit/glue/form_data.h"
[email protected]2609bc12010-01-24 08:32:5526#include "testing/gmock/include/gmock/gmock.h"
27#include "testing/gtest/include/gtest/gtest.h"
28
[email protected]cea1d112010-07-01 00:57:3329using webkit_glue::FormData;
30
[email protected]2609bc12010-01-24 08:32:5531ACTION(QuitUIMessageLoop) {
[email protected]faec6312010-10-05 03:35:3432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2609bc12010-01-24 08:32:5533 MessageLoop::current()->Quit();
34}
35
36class PersonalDataLoadedObserverMock : public PersonalDataManager::Observer {
37 public:
38 PersonalDataLoadedObserverMock() {}
39 virtual ~PersonalDataLoadedObserverMock() {}
40
41 MOCK_METHOD0(OnPersonalDataLoaded, void());
42};
43
44class PersonalDataManagerTest : public testing::Test {
45 protected:
46 PersonalDataManagerTest()
[email protected]faec6312010-10-05 03:35:3447 : ui_thread_(BrowserThread::UI, &message_loop_),
48 db_thread_(BrowserThread::DB) {
[email protected]2609bc12010-01-24 08:32:5549 }
50
51 virtual void SetUp() {
52 db_thread_.Start();
53
54 profile_.reset(new TestingProfile);
55 profile_->CreateWebDataService(false);
56
[email protected]468327f2010-10-04 20:02:2957 autofill_test::DisableSystemServices(profile_.get());
[email protected]2609bc12010-01-24 08:32:5558 ResetPersonalDataManager();
59 }
60
61 virtual void TearDown() {
62 if (profile_.get())
63 profile_.reset(NULL);
64
65 db_thread_.Stop();
66 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask);
67 MessageLoop::current()->Run();
68 }
69
70 void ResetPersonalDataManager() {
[email protected]36c1ff9fd2010-06-03 20:08:5971 personal_data_ = new PersonalDataManager();
[email protected]e0976a72010-04-02 01:25:2472 personal_data_->Init(profile_.get());
[email protected]2609bc12010-01-24 08:32:5573 personal_data_->SetObserver(&personal_data_observer_);
74 }
75
[email protected]2609bc12010-01-24 08:32:5576 MessageLoopForUI message_loop_;
[email protected]faec6312010-10-05 03:35:3477 BrowserThread ui_thread_;
78 BrowserThread db_thread_;
[email protected]2609bc12010-01-24 08:32:5579 scoped_ptr<TestingProfile> profile_;
[email protected]36c1ff9fd2010-06-03 20:08:5980 scoped_refptr<PersonalDataManager> personal_data_;
[email protected]2609bc12010-01-24 08:32:5581 NotificationRegistrar registrar_;
82 NotificationObserverMock observer_;
83 PersonalDataLoadedObserverMock personal_data_observer_;
84};
85
[email protected]f11f6212010-01-25 20:47:2086// TODO(jhawkins): Test SetProfiles w/out a WebDataService in the profile.
[email protected]2609bc12010-01-24 08:32:5587TEST_F(PersonalDataManagerTest, SetProfiles) {
[email protected]e43783402010-11-04 19:45:0488 AutoFillProfile profile0;
[email protected]468327f2010-10-04 20:02:2989 autofill_test::SetProfileInfo(&profile0,
[email protected]280356aa2010-02-13 02:58:2090 "Billing", "Marion", "Mitchell", "Morrison",
[email protected]2609bc12010-01-24 08:32:5591 "[email protected]", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
92 "91601", "US", "12345678910", "01987654321");
93
[email protected]e43783402010-11-04 19:45:0494 AutoFillProfile profile1;
[email protected]468327f2010-10-04 20:02:2995 autofill_test::SetProfileInfo(&profile1,
[email protected]280356aa2010-02-13 02:58:2096 "Home", "Josephine", "Alicia", "Saenz",
[email protected]2609bc12010-01-24 08:32:5597 "[email protected]", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
98 "US", "19482937549", "13502849239");
99
[email protected]e43783402010-11-04 19:45:04100 AutoFillProfile profile2;
[email protected]468327f2010-10-04 20:02:29101 autofill_test::SetProfileInfo(&profile2,
[email protected]280356aa2010-02-13 02:58:20102 "Work", "Josephine", "Alicia", "Saenz",
[email protected]2609bc12010-01-24 08:32:55103 "[email protected]", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
104 "32801", "US", "19482937549", "13502849239");
105
106 // This will verify that the web database has been loaded and the notification
107 // sent out.
108 EXPECT_CALL(personal_data_observer_,
109 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
110
111 // The message loop will exit when the mock observer is notified.
112 MessageLoop::current()->Run();
113
[email protected]542e1992010-07-14 18:25:52114 // Add two test profiles to the database.
[email protected]2609bc12010-01-24 08:32:55115 std::vector<AutoFillProfile> update;
116 update.push_back(profile0);
117 update.push_back(profile1);
118 personal_data_->SetProfiles(&update);
119
[email protected]2609bc12010-01-24 08:32:55120 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
121 ASSERT_EQ(2U, results1.size());
[email protected]e43783402010-11-04 19:45:04122 EXPECT_EQ(0, profile0.Compare(*results1.at(0)));
123 EXPECT_EQ(0, profile1.Compare(*results1.at(1)));
[email protected]2609bc12010-01-24 08:32:55124
125 // Three operations in one:
126 // - Update profile0
127 // - Remove profile1
128 // - Add profile2
129 profile0.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("John"));
130 update.clear();
131 update.push_back(profile0);
132 update.push_back(profile2);
133 personal_data_->SetProfiles(&update);
134
[email protected]2609bc12010-01-24 08:32:55135 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
136 ASSERT_EQ(2U, results2.size());
[email protected]e43783402010-11-04 19:45:04137 EXPECT_EQ(0, profile0.Compare(*results2.at(0)));
138 EXPECT_EQ(0, profile2.Compare(*results2.at(1)));
[email protected]2609bc12010-01-24 08:32:55139
140 // Reset the PersonalDataManager. This tests that the personal data was saved
141 // to the web database, and that we can load the profiles from the web
142 // database.
143 ResetPersonalDataManager();
144
145 // This will verify that the web database has been loaded and the notification
146 // sent out.
147 EXPECT_CALL(personal_data_observer_,
148 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
149
150 // The message loop will exit when the PersonalDataLoadedObserver is notified.
151 MessageLoop::current()->Run();
152
153 // Verify that we've loaded the profiles from the web database.
154 const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles();
155 ASSERT_EQ(2U, results3.size());
[email protected]e43783402010-11-04 19:45:04156 EXPECT_EQ(0, profile0.Compare(*results3.at(0)));
157 EXPECT_EQ(0, profile2.Compare(*results3.at(1)));
[email protected]2609bc12010-01-24 08:32:55158}
[email protected]f11f6212010-01-25 20:47:20159
160// TODO(jhawkins): Test SetCreditCards w/out a WebDataService in the profile.
161TEST_F(PersonalDataManagerTest, SetCreditCards) {
[email protected]e43783402010-11-04 19:45:04162 CreditCard creditcard0;
[email protected]468327f2010-10-04 20:02:29163 autofill_test::SetCreditCardInfo(&creditcard0, "Corporate",
[email protected]2a13edd2010-10-26 22:52:54164 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
[email protected]f11f6212010-01-25 20:47:20165
[email protected]e43783402010-11-04 19:45:04166 CreditCard creditcard1;
[email protected]468327f2010-10-04 20:02:29167 autofill_test::SetCreditCardInfo(&creditcard1, "Personal",
[email protected]2a13edd2010-10-26 22:52:54168 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
[email protected]f11f6212010-01-25 20:47:20169
[email protected]e43783402010-11-04 19:45:04170 CreditCard creditcard2;
[email protected]468327f2010-10-04 20:02:29171 autofill_test::SetCreditCardInfo(&creditcard2, "Savings",
[email protected]2a13edd2010-10-26 22:52:54172 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015");
[email protected]f11f6212010-01-25 20:47:20173
174 // This will verify that the web database has been loaded and the notification
175 // sent out.
176 EXPECT_CALL(personal_data_observer_,
177 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
178
179 // The message loop will exit when the mock observer is notified.
180 MessageLoop::current()->Run();
181
[email protected]542e1992010-07-14 18:25:52182 // Add two test credit cards to the database.
[email protected]f11f6212010-01-25 20:47:20183 std::vector<CreditCard> update;
184 update.push_back(creditcard0);
185 update.push_back(creditcard1);
186 personal_data_->SetCreditCards(&update);
187
[email protected]f11f6212010-01-25 20:47:20188 const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
189 ASSERT_EQ(2U, results1.size());
[email protected]e43783402010-11-04 19:45:04190 EXPECT_EQ(0, creditcard0.Compare(*results1.at(0)));
191 EXPECT_EQ(0, creditcard1.Compare(*results1.at(1)));
[email protected]f11f6212010-01-25 20:47:20192
193 // Three operations in one:
194 // - Update creditcard0
195 // - Remove creditcard1
196 // - Add creditcard2
197 creditcard0.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe"));
198 update.clear();
199 update.push_back(creditcard0);
200 update.push_back(creditcard2);
201 personal_data_->SetCreditCards(&update);
202
[email protected]f11f6212010-01-25 20:47:20203 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
204 ASSERT_EQ(2U, results2.size());
205 EXPECT_EQ(creditcard0, *results2.at(0));
206 EXPECT_EQ(creditcard2, *results2.at(1));
[email protected]f11f6212010-01-25 20:47:20207
208 // Reset the PersonalDataManager. This tests that the personal data was saved
209 // to the web database, and that we can load the credit cards from the web
210 // database.
211 ResetPersonalDataManager();
212
213 // This will verify that the web database has been loaded and the notification
214 // sent out.
215 EXPECT_CALL(personal_data_observer_,
216 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
217
218 // The message loop will exit when the PersonalDataLoadedObserver is notified.
219 MessageLoop::current()->Run();
220
221 // Verify that we've loaded the credit cards from the web database.
222 const std::vector<CreditCard*>& results3 = personal_data_->credit_cards();
223 ASSERT_EQ(2U, results3.size());
224 EXPECT_EQ(creditcard0, *results3.at(0));
225 EXPECT_EQ(creditcard2, *results3.at(1));
226}
[email protected]e0976a72010-04-02 01:25:24227
[email protected]542e1992010-07-14 18:25:52228TEST_F(PersonalDataManagerTest, SetProfilesAndCreditCards) {
[email protected]e43783402010-11-04 19:45:04229 AutoFillProfile profile0;
[email protected]468327f2010-10-04 20:02:29230 autofill_test::SetProfileInfo(&profile0,
[email protected]542e1992010-07-14 18:25:52231 "Billing", "Marion", "Mitchell", "Morrison",
232 "[email protected]", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
233 "91601", "US", "12345678910", "01987654321");
234
[email protected]e43783402010-11-04 19:45:04235 AutoFillProfile profile1;
[email protected]468327f2010-10-04 20:02:29236 autofill_test::SetProfileInfo(&profile1,
[email protected]542e1992010-07-14 18:25:52237 "Home", "Josephine", "Alicia", "Saenz",
238 "[email protected]", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
239 "US", "19482937549", "13502849239");
240
[email protected]e43783402010-11-04 19:45:04241 CreditCard creditcard0;
[email protected]468327f2010-10-04 20:02:29242 autofill_test::SetCreditCardInfo(&creditcard0, "Corporate",
[email protected]2a13edd2010-10-26 22:52:54243 "John Dillinger", "423456789012" /* Visa */, "01", "2010");
[email protected]542e1992010-07-14 18:25:52244
[email protected]e43783402010-11-04 19:45:04245 CreditCard creditcard1;
[email protected]468327f2010-10-04 20:02:29246 autofill_test::SetCreditCardInfo(&creditcard1, "Personal",
[email protected]2a13edd2010-10-26 22:52:54247 "Bonnie Parker", "518765432109" /* Mastercard */, "12", "2012");
[email protected]542e1992010-07-14 18:25:52248
249 // This will verify that the web database has been loaded and the notification
250 // sent out.
251 EXPECT_CALL(
252 personal_data_observer_,
253 OnPersonalDataLoaded()).Times(2).WillRepeatedly(QuitUIMessageLoop());
254
255 // The message loop will exit when the mock observer is notified.
256 MessageLoop::current()->Run();
257
258 // Add two test profiles to the database.
259 std::vector<AutoFillProfile> update;
260 update.push_back(profile0);
261 update.push_back(profile1);
262 personal_data_->SetProfiles(&update);
263
[email protected]542e1992010-07-14 18:25:52264 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
265 ASSERT_EQ(2U, results1.size());
[email protected]e43783402010-11-04 19:45:04266 EXPECT_EQ(0, profile0.Compare(*results1.at(0)));
267 EXPECT_EQ(0, profile1.Compare(*results1.at(1)));
[email protected]542e1992010-07-14 18:25:52268
269 // Add two test credit cards to the database.
270 std::vector<CreditCard> update_cc;
271 update_cc.push_back(creditcard0);
272 update_cc.push_back(creditcard1);
273 personal_data_->SetCreditCards(&update_cc);
274
[email protected]542e1992010-07-14 18:25:52275
276 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
277 ASSERT_EQ(2U, results2.size());
278 EXPECT_EQ(creditcard0, *results2.at(0));
279 EXPECT_EQ(creditcard1, *results2.at(1));
280
[email protected]e43783402010-11-04 19:45:04281 // Determine uniqueness by inserting all of the GUIDs into a set and verifying
282 // the size of the set matches the number of GUIDs.
283 std::set<std::string> guids;
284 guids.insert(profile0.guid());
285 guids.insert(profile1.guid());
286 guids.insert(creditcard0.guid());
287 guids.insert(creditcard1.guid());
288 EXPECT_EQ(4U, guids.size());
[email protected]542e1992010-07-14 18:25:52289}
290
[email protected]7c543fd2010-07-24 02:44:59291// Test care for 50047. Makes sure that unique_ids_ is populated correctly on
292// load.
293TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
[email protected]e43783402010-11-04 19:45:04294 AutoFillProfile profile0;
[email protected]468327f2010-10-04 20:02:29295 autofill_test::SetProfileInfo(&profile0,
[email protected]7c543fd2010-07-24 02:44:59296 "", "y", "", "", "", "", "", "", "", "", "", "", "", "");
297
298 // This will verify that the web database has been loaded and the notification
299 // sent out.
300 EXPECT_CALL(personal_data_observer_,
301 OnPersonalDataLoaded()).WillRepeatedly(QuitUIMessageLoop());
302
303 // The message loop will exit when the mock observer is notified.
304 MessageLoop::current()->Run();
305
306 // Add the profile0 to the db.
307 std::vector<AutoFillProfile> update;
308 update.push_back(profile0);
309 personal_data_->SetProfiles(&update);
310
311 // Reset the PersonalDataManager. This recreates PersonalDataManager, which
312 // should populate unique_ids_.
313 ResetPersonalDataManager();
314
315 // The message loop will exit when the PersonalDataLoadedObserver is notified.
316 MessageLoop::current()->Run();
317
318 // Verify that we've loaded the profiles from the web database.
319 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
320 ASSERT_EQ(1U, results2.size());
321
322 // Add a new profile.
[email protected]e43783402010-11-04 19:45:04323 AutoFillProfile profile1;
[email protected]468327f2010-10-04 20:02:29324 autofill_test::SetProfileInfo(&profile1,
[email protected]7c543fd2010-07-24 02:44:59325 "", "y", "", "", "", "", "", "", "", "", "", "", "", "");
326 update.clear();
327 update.push_back(*results2[0]);
328 update.push_back(profile1);
329 personal_data_->SetProfiles(&update);
330
331 // Make sure the two profiles have different ids (and neither equal to 0,
332 // which is an invalid id).
333 const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles();
334 ASSERT_EQ(2U, results3.size());
[email protected]e43783402010-11-04 19:45:04335 EXPECT_NE(results3[0]->guid(), results3[1]->guid());
336 EXPECT_TRUE(guid::IsValidGUID(results3[0]->guid()));
337 EXPECT_TRUE(guid::IsValidGUID(results3[1]->guid()));
[email protected]7c543fd2010-07-24 02:44:59338}
339
[email protected]98a94ee2010-07-12 16:03:22340TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
[email protected]e43783402010-11-04 19:45:04341 AutoFillProfile profile0;
[email protected]468327f2010-10-04 20:02:29342 autofill_test::SetProfileInfo(&profile0,
[email protected]98a94ee2010-07-12 16:03:22343 "", "", "", "", "", "", "", "", "", "", "", "", "", "");
344
345 // This will verify that the web database has been loaded and the notification
346 // sent out.
347 EXPECT_CALL(personal_data_observer_,
348 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
349
350 // The message loop will exit when the mock observer is notified.
351 MessageLoop::current()->Run();
352
353 // Add the empty profile to the database.
354 std::vector<AutoFillProfile> update;
355 update.push_back(profile0);
356 personal_data_->SetProfiles(&update);
357
358 // Check the local store of profiles, not yet saved to the web database.
359 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
360 ASSERT_EQ(0U, results1.size());
361
362 // Reset the PersonalDataManager. This tests that the personal data was saved
363 // to the web database, and that we can load the profiles from the web
364 // database.
365 ResetPersonalDataManager();
366
367 // This will verify that the web database has been loaded and the notification
368 // sent out.
369 EXPECT_CALL(personal_data_observer_,
370 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
371
372 // The message loop will exit when the PersonalDataLoadedObserver is notified.
373 MessageLoop::current()->Run();
374
375 // Verify that we've loaded the profiles from the web database.
376 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
377 ASSERT_EQ(0U, results2.size());
378}
379
380TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
[email protected]e43783402010-11-04 19:45:04381 CreditCard creditcard0;
[email protected]2a13edd2010-10-26 22:52:54382 autofill_test::SetCreditCardInfo(&creditcard0, "", "", "", "", "");
[email protected]98a94ee2010-07-12 16:03:22383
384 // This will verify that the web database has been loaded and the notification
385 // sent out.
386 EXPECT_CALL(personal_data_observer_,
387 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
388
389 // The message loop will exit when the mock observer is notified.
390 MessageLoop::current()->Run();
391
392 // Add the empty credit card to the database.
393 std::vector<CreditCard> update;
394 update.push_back(creditcard0);
395 personal_data_->SetCreditCards(&update);
396
397 // Check the local store of credit cards, not yet saved to the web database.
398 const std::vector<CreditCard*>& results1 = personal_data_->credit_cards();
399 ASSERT_EQ(0U, results1.size());
400
401 // Reset the PersonalDataManager. This tests that the personal data was saved
402 // to the web database, and that we can load the credit cards from the web
403 // database.
404 ResetPersonalDataManager();
405
406 // This will verify that the web database has been loaded and the notification
407 // sent out.
408 EXPECT_CALL(personal_data_observer_,
409 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
410
411 // The message loop will exit when the PersonalDataLoadedObserver is notified.
412 MessageLoop::current()->Run();
413
414 // Verify that we've loaded the credit cards from the web database.
415 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
416 ASSERT_EQ(0U, results2.size());
417}
418
[email protected]e0976a72010-04-02 01:25:24419TEST_F(PersonalDataManagerTest, Refresh) {
[email protected]e43783402010-11-04 19:45:04420 AutoFillProfile profile0;
[email protected]468327f2010-10-04 20:02:29421 autofill_test::SetProfileInfo(&profile0,
[email protected]e0976a72010-04-02 01:25:24422 "Billing", "Marion", "Mitchell", "Morrison",
423 "[email protected]", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
424 "91601", "US", "12345678910", "01987654321");
425
[email protected]e43783402010-11-04 19:45:04426 AutoFillProfile profile1;
[email protected]468327f2010-10-04 20:02:29427 autofill_test::SetProfileInfo(&profile1,
[email protected]e0976a72010-04-02 01:25:24428 "Home", "Josephine", "Alicia", "Saenz",
429 "[email protected]", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
430 "US", "19482937549", "13502849239");
431
432 EXPECT_CALL(personal_data_observer_,
433 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
434
435 MessageLoop::current()->Run();
436
437 // Add the test profiles to the database.
438 std::vector<AutoFillProfile> update;
439 update.push_back(profile0);
440 update.push_back(profile1);
441 personal_data_->SetProfiles(&update);
442
[email protected]e43783402010-11-04 19:45:04443 // Labels depend on other profiles in the list - update labels manually.
[email protected]9f321442010-10-25 23:53:53444 std::vector<AutoFillProfile *> profile_pointers;
445 profile_pointers.push_back(&profile0);
446 profile_pointers.push_back(&profile1);
447 AutoFillProfile::AdjustInferredLabels(&profile_pointers);
[email protected]e0976a72010-04-02 01:25:24448
449 // Wait for the refresh.
450 EXPECT_CALL(personal_data_observer_,
451 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
452
453 MessageLoop::current()->Run();
454
455 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
456 ASSERT_EQ(2U, results1.size());
457 EXPECT_EQ(profile0, *results1.at(0));
458 EXPECT_EQ(profile1, *results1.at(1));
459
[email protected]61f25cd0e2010-11-07 05:23:50460 AutoFillProfile profile2;
461 autofill_test::SetProfileInfo(&profile2,
[email protected]e0976a72010-04-02 01:25:24462 "Work", "Josephine", "Alicia", "Saenz",
463 "[email protected]", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
464 "32801", "US", "19482937549", "13502849239");
465
[email protected]9f321442010-10-25 23:53:53466 // Adjust all labels.
[email protected]61f25cd0e2010-11-07 05:23:50467 profile_pointers.push_back(&profile2);
[email protected]9f321442010-10-25 23:53:53468 AutoFillProfile::AdjustInferredLabels(&profile_pointers);
469
[email protected]e0976a72010-04-02 01:25:24470 WebDataService* wds = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS);
471 ASSERT_TRUE(wds);
[email protected]61f25cd0e2010-11-07 05:23:50472 wds->AddAutoFillProfileGUID(profile2);
[email protected]e0976a72010-04-02 01:25:24473
474 personal_data_->Refresh();
475
476 // Wait for the refresh.
477 EXPECT_CALL(personal_data_observer_,
478 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
479
480 MessageLoop::current()->Run();
481
482 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
483 ASSERT_EQ(3U, results2.size());
484 EXPECT_EQ(profile0, *results2.at(0));
485 EXPECT_EQ(profile1, *results2.at(1));
[email protected]61f25cd0e2010-11-07 05:23:50486 EXPECT_EQ(profile2, *results2.at(2));
[email protected]e0976a72010-04-02 01:25:24487
[email protected]e43783402010-11-04 19:45:04488 wds->RemoveAutoFillProfileGUID(profile1.guid());
[email protected]61f25cd0e2010-11-07 05:23:50489 wds->RemoveAutoFillProfileGUID(profile2.guid());
[email protected]e0976a72010-04-02 01:25:24490
491 // Before telling the PDM to refresh, simulate an edit to one of the profiles
[email protected]a7b29dd2010-07-12 22:40:06492 // via a SetProfile update (this would happen if the AutoFill window was
[email protected]e0976a72010-04-02 01:25:24493 // open with a previous snapshot of the profiles, and something [e.g. sync]
494 // removed a profile from the browser. In this edge case, we will end up
495 // in a consistent state by dropping the write).
[email protected]61f25cd0e2010-11-07 05:23:50496 profile2.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("Jo"));
[email protected]e0976a72010-04-02 01:25:24497 update.clear();
498 update.push_back(profile0);
499 update.push_back(profile1);
[email protected]61f25cd0e2010-11-07 05:23:50500 update.push_back(profile2);
[email protected]e0976a72010-04-02 01:25:24501 personal_data_->SetProfiles(&update);
502
[email protected]d7da65d2010-11-11 19:26:05503 // Wait for the refresh.
[email protected]e0976a72010-04-02 01:25:24504 EXPECT_CALL(personal_data_observer_,
505 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
506
507 MessageLoop::current()->Run();
508
509 const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles();
510 ASSERT_EQ(1U, results3.size());
511 EXPECT_EQ(profile0, *results2.at(0));
512}
[email protected]83723f02010-04-07 17:33:10513
[email protected]ce78c1f92010-04-09 02:23:26514TEST_F(PersonalDataManagerTest, ImportFormData) {
[email protected]cea1d112010-07-01 00:57:33515 FormData form;
516 webkit_glue::FormField field;
[email protected]468327f2010-10-04 20:02:29517 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33518 "First name:", "first_name", "George", "text", &field);
519 form.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29520 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33521 "Last name:", "last_name", "Washington", "text", &field);
522 form.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29523 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33524 "Email:", "email", "[email protected]", "text", &field);
525 form.fields.push_back(field);
[email protected]37ff612e2011-02-03 15:54:52526 autofill_test::CreateTestFormField(
527 "Address:", "address1", "21 Laussat St", "text", &field);
528 form.fields.push_back(field);
529 autofill_test::CreateTestFormField(
530 "City:", "city", "San Francisco", "text", &field);
531 form.fields.push_back(field);
532 autofill_test::CreateTestFormField(
533 "State:", "state", "California", "text", &field);
534 form.fields.push_back(field);
535 autofill_test::CreateTestFormField(
536 "Zip:", "zip", "94102", "text", &field);
537 form.fields.push_back(field);
[email protected]ce78c1f92010-04-09 02:23:26538 FormStructure form_structure(form);
[email protected]09205efb2011-01-20 21:20:03539 std::vector<const FormStructure*> forms;
[email protected]ce78c1f92010-04-09 02:23:26540 forms.push_back(&form_structure);
[email protected]abcdf1a2011-02-04 23:12:24541 const CreditCard* imported_credit_card;
542 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
543 ASSERT_FALSE(imported_credit_card);
[email protected]ce78c1f92010-04-09 02:23:26544
[email protected]d7da65d2010-11-11 19:26:05545 // Wait for the refresh.
[email protected]ce78c1f92010-04-09 02:23:26546 EXPECT_CALL(personal_data_observer_,
547 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
548
549 MessageLoop::current()->Run();
550
[email protected]e43783402010-11-04 19:45:04551 AutoFillProfile expected;
[email protected]468327f2010-10-04 20:02:29552 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
[email protected]37ff612e2011-02-03 15:54:52553 "Washington", "[email protected]", NULL, "21 Laussat St", NULL,
554 "San Francisco", "California", "94102", NULL, NULL, NULL);
[email protected]ce78c1f92010-04-09 02:23:26555 const std::vector<AutoFillProfile*>& results = personal_data_->profiles();
556 ASSERT_EQ(1U, results.size());
[email protected]e43783402010-11-04 19:45:04557 EXPECT_EQ(0, expected.Compare(*results[0]));
[email protected]ce78c1f92010-04-09 02:23:26558}
559
[email protected]a2a1baf2011-02-03 20:22:00560TEST_F(PersonalDataManagerTest, ImportFormDataBadEmail) {
561 FormData form;
562 webkit_glue::FormField field;
563 autofill_test::CreateTestFormField(
564 "First name:", "first_name", "George", "text", &field);
565 form.fields.push_back(field);
566 autofill_test::CreateTestFormField(
567 "Last name:", "last_name", "Washington", "text", &field);
568 form.fields.push_back(field);
569 autofill_test::CreateTestFormField(
570 "Email:", "email", "bogus", "text", &field);
571 form.fields.push_back(field);
572 autofill_test::CreateTestFormField(
573 "Address:", "address1", "21 Laussat St", "text", &field);
574 form.fields.push_back(field);
575 autofill_test::CreateTestFormField(
576 "City:", "city", "San Francisco", "text", &field);
577 form.fields.push_back(field);
578 autofill_test::CreateTestFormField(
579 "State:", "state", "California", "text", &field);
580 form.fields.push_back(field);
581 autofill_test::CreateTestFormField(
582 "Zip:", "zip", "94102", "text", &field);
583 form.fields.push_back(field);
584 FormStructure form_structure(form);
585 std::vector<const FormStructure*> forms;
586 forms.push_back(&form_structure);
[email protected]abcdf1a2011-02-04 23:12:24587 const CreditCard* imported_credit_card;
588 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
589 ASSERT_FALSE(imported_credit_card);
[email protected]a2a1baf2011-02-03 20:22:00590
591 // Wait for the refresh.
592 EXPECT_CALL(personal_data_observer_,
593 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
594
595 MessageLoop::current()->Run();
596
597 AutoFillProfile expected;
598 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
599 "Washington", NULL, NULL, "21 Laussat St", NULL,
600 "San Francisco", "California", "94102", NULL, NULL, NULL);
601 const std::vector<AutoFillProfile*>& results = personal_data_->profiles();
602 ASSERT_EQ(1U, results.size());
603 EXPECT_EQ(0, expected.Compare(*results[0]));
604}
605
[email protected]204777552011-01-26 19:46:28606TEST_F(PersonalDataManagerTest, ImportFormDataNotEnoughFilledFields) {
[email protected]4fb058092010-11-24 23:52:25607 FormData form;
608 webkit_glue::FormField field;
609 autofill_test::CreateTestFormField(
610 "First name:", "first_name", "George", "text", &field);
611 form.fields.push_back(field);
612 autofill_test::CreateTestFormField(
613 "Last name:", "last_name", "Washington", "text", &field);
614 form.fields.push_back(field);
615 autofill_test::CreateTestFormField(
616 "Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
617 form.fields.push_back(field);
618 FormStructure form_structure(form);
[email protected]09205efb2011-01-20 21:20:03619 std::vector<const FormStructure*> forms;
[email protected]4fb058092010-11-24 23:52:25620 forms.push_back(&form_structure);
[email protected]abcdf1a2011-02-04 23:12:24621 const CreditCard* imported_credit_card;
622 EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
623 ASSERT_FALSE(imported_credit_card);
[email protected]4fb058092010-11-24 23:52:25624
625 // Wait for the refresh.
626 EXPECT_CALL(personal_data_observer_,
627 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
628
[email protected]204777552011-01-26 19:46:28629 MessageLoop::current()->Run();
630
[email protected]4fb058092010-11-24 23:52:25631 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles();
632 ASSERT_EQ(0U, profiles.size());
633 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
634 ASSERT_EQ(0U, credit_cards.size());
635}
636
[email protected]3aa8bec22010-11-23 07:51:28637TEST_F(PersonalDataManagerTest, ImportPhoneNumberSplitAcrossMultipleFields) {
638 FormData form;
639 webkit_glue::FormField field;
640 autofill_test::CreateTestFormField(
641 "First name:", "first_name", "George", "text", &field);
642 form.fields.push_back(field);
643 autofill_test::CreateTestFormField(
644 "Last name:", "last_name", "Washington", "text", &field);
645 form.fields.push_back(field);
646 autofill_test::CreateTestFormField(
647 "Phone #:", "home_phone_area_code", "650", "text", &field);
648 field.set_max_length(3);
649 form.fields.push_back(field);
650 autofill_test::CreateTestFormField(
651 "Phone #:", "home_phone_prefix", "555", "text", &field);
652 field.set_max_length(3);
653 form.fields.push_back(field);
654 autofill_test::CreateTestFormField(
655 "Phone #:", "home_phone_suffix", "0000", "text", &field);
656 field.set_max_length(4);
657 form.fields.push_back(field);
[email protected]37ff612e2011-02-03 15:54:52658 autofill_test::CreateTestFormField(
659 "Address:", "address1", "21 Laussat St", "text", &field);
660 form.fields.push_back(field);
661 autofill_test::CreateTestFormField(
662 "City:", "city", "San Francisco", "text", &field);
663 form.fields.push_back(field);
664 autofill_test::CreateTestFormField(
665 "State:", "state", "California", "text", &field);
666 form.fields.push_back(field);
667 autofill_test::CreateTestFormField(
668 "Zip:", "zip", "94102", "text", &field);
669 form.fields.push_back(field);
[email protected]3aa8bec22010-11-23 07:51:28670 FormStructure form_structure(form);
[email protected]09205efb2011-01-20 21:20:03671 std::vector<const FormStructure*> forms;
[email protected]3aa8bec22010-11-23 07:51:28672 forms.push_back(&form_structure);
[email protected]abcdf1a2011-02-04 23:12:24673 const CreditCard* imported_credit_card;
674 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
675 ASSERT_FALSE(imported_credit_card);
[email protected]3aa8bec22010-11-23 07:51:28676
677 // Wait for the refresh.
678 EXPECT_CALL(personal_data_observer_,
679 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
680
681 MessageLoop::current()->Run();
682
683 AutoFillProfile expected;
[email protected]37ff612e2011-02-03 15:54:52684 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
685 "Washington", NULL, NULL, "21 Laussat St", NULL,
686 "San Francisco", "California", "94102", NULL, "6505550000", NULL);
[email protected]3aa8bec22010-11-23 07:51:28687 const std::vector<AutoFillProfile*>& results = personal_data_->profiles();
688 ASSERT_EQ(1U, results.size());
689 EXPECT_EQ(0, expected.Compare(*results[0]));
690}
691
[email protected]47e119f02010-04-15 00:46:55692TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
[email protected]e43783402010-11-04 19:45:04693 CreditCard credit_card0;
694 credit_card0.set_label(ASCIIToUTF16("Home"));
[email protected]98a94ee2010-07-12 16:03:22695 credit_card0.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("John"));
[email protected]e43783402010-11-04 19:45:04696 CreditCard credit_card1;
697 credit_card1.set_label(ASCIIToUTF16("Home"));
[email protected]98a94ee2010-07-12 16:03:22698 credit_card1.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Paul"));
[email protected]e43783402010-11-04 19:45:04699 CreditCard credit_card2;
700 credit_card2.set_label(ASCIIToUTF16("Home"));
[email protected]98a94ee2010-07-12 16:03:22701 credit_card2.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Ringo"));
[email protected]e43783402010-11-04 19:45:04702 CreditCard credit_card3;
703 credit_card3.set_label(ASCIIToUTF16("NotHome"));
[email protected]98a94ee2010-07-12 16:03:22704 credit_card3.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Other"));
[email protected]e43783402010-11-04 19:45:04705 CreditCard credit_card4;
706 credit_card4.set_label(ASCIIToUTF16("Work"));
[email protected]98a94ee2010-07-12 16:03:22707 credit_card4.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Ozzy"));
[email protected]e43783402010-11-04 19:45:04708 CreditCard credit_card5;
709 credit_card5.set_label(ASCIIToUTF16("Work"));
[email protected]98a94ee2010-07-12 16:03:22710 credit_card5.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Dio"));
[email protected]47e119f02010-04-15 00:46:55711
712 // This will verify that the web database has been loaded and the notification
713 // sent out.
714 EXPECT_CALL(personal_data_observer_,
715 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
716
717 // The message loop will exit when the mock observer is notified.
718 MessageLoop::current()->Run();
719
[email protected]98a94ee2010-07-12 16:03:22720 // Add the test credit cards to the database.
[email protected]47e119f02010-04-15 00:46:55721 std::vector<CreditCard> update;
[email protected]98a94ee2010-07-12 16:03:22722 update.push_back(credit_card0);
723 update.push_back(credit_card1);
724 update.push_back(credit_card2);
725 update.push_back(credit_card3);
726 update.push_back(credit_card4);
727 update.push_back(credit_card5);
[email protected]47e119f02010-04-15 00:46:55728 personal_data_->SetCreditCards(&update);
729
[email protected]18f92d4f2010-10-19 00:14:58730 // Reset the PersonalDataManager. This tests that the personal data was saved
731 // to the web database, and that we can load the credit cards from the web
732 // database.
733 ResetPersonalDataManager();
734
735 // This will verify that the web database has been loaded and the notification
736 // sent out.
737 EXPECT_CALL(personal_data_observer_,
738 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
739
740 // The message loop will exit when the mock observer is notified.
741 MessageLoop::current()->Run();
742
[email protected]47e119f02010-04-15 00:46:55743 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
744 ASSERT_EQ(6U, results.size());
745 EXPECT_EQ(ASCIIToUTF16("Home"), results[0]->Label());
746 EXPECT_EQ(ASCIIToUTF16("Home2"), results[1]->Label());
747 EXPECT_EQ(ASCIIToUTF16("Home3"), results[2]->Label());
748 EXPECT_EQ(ASCIIToUTF16("NotHome"), results[3]->Label());
749 EXPECT_EQ(ASCIIToUTF16("Work"), results[4]->Label());
750 EXPECT_EQ(ASCIIToUTF16("Work2"), results[5]->Label());
751}
[email protected]cea1d112010-07-01 00:57:33752
[email protected]f1ed7272010-11-11 21:57:07753TEST_F(PersonalDataManagerTest, AggregateTwoDifferentProfiles) {
754 FormData form1;
[email protected]cea1d112010-07-01 00:57:33755 webkit_glue::FormField field;
[email protected]468327f2010-10-04 20:02:29756 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33757 "First name:", "first_name", "George", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07758 form1.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29759 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33760 "Last name:", "last_name", "Washington", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07761 form1.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29762 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33763 "Email:", "email", "[email protected]", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07764 form1.fields.push_back(field);
[email protected]37ff612e2011-02-03 15:54:52765 autofill_test::CreateTestFormField(
766 "Address:", "address1", "21 Laussat St", "text", &field);
767 form1.fields.push_back(field);
768 autofill_test::CreateTestFormField(
769 "City:", "city", "San Francisco", "text", &field);
770 form1.fields.push_back(field);
771 autofill_test::CreateTestFormField(
772 "State:", "state", "California", "text", &field);
773 form1.fields.push_back(field);
774 autofill_test::CreateTestFormField(
775 "Zip:", "zip", "94102", "text", &field);
776 form1.fields.push_back(field);
[email protected]cea1d112010-07-01 00:57:33777
[email protected]f1ed7272010-11-11 21:57:07778 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:03779 std::vector<const FormStructure*> forms;
[email protected]f1ed7272010-11-11 21:57:07780 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:24781 const CreditCard* imported_credit_card;
782 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
783 ASSERT_FALSE(imported_credit_card);
[email protected]cea1d112010-07-01 00:57:33784
[email protected]d7da65d2010-11-11 19:26:05785 // Wait for the refresh.
[email protected]cea1d112010-07-01 00:57:33786 EXPECT_CALL(personal_data_observer_,
787 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
788
789 MessageLoop::current()->Run();
790
[email protected]f1ed7272010-11-11 21:57:07791 AutoFillProfile expected;
792 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
[email protected]37ff612e2011-02-03 15:54:52793 "Washington", "[email protected]", NULL, "21 Laussat St", NULL,
794 "San Francisco", "California", "94102", NULL, NULL, NULL);
[email protected]f1ed7272010-11-11 21:57:07795 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
796 ASSERT_EQ(1U, results1.size());
797 EXPECT_EQ(0, expected.Compare(*results1[0]));
[email protected]cea1d112010-07-01 00:57:33798
799 // Now create a completely different profile.
[email protected]f1ed7272010-11-11 21:57:07800 FormData form2;
[email protected]468327f2010-10-04 20:02:29801 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33802 "First name:", "first_name", "John", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07803 form2.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29804 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33805 "Last name:", "last_name", "Adams", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07806 form2.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29807 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33808 "Email:", "email", "[email protected]", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07809 form2.fields.push_back(field);
[email protected]37ff612e2011-02-03 15:54:52810 autofill_test::CreateTestFormField(
811 "Address:", "address1", "21 Laussat St", "text", &field);
812 form2.fields.push_back(field);
813 autofill_test::CreateTestFormField(
814 "City:", "city", "San Francisco", "text", &field);
815 form2.fields.push_back(field);
816 autofill_test::CreateTestFormField(
817 "State:", "state", "California", "text", &field);
818 form2.fields.push_back(field);
819 autofill_test::CreateTestFormField(
820 "Zip:", "zip", "94102", "text", &field);
821 form2.fields.push_back(field);
[email protected]cea1d112010-07-01 00:57:33822
[email protected]f1ed7272010-11-11 21:57:07823 FormStructure form_structure2(form2);
824 forms.clear();
825 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:24826 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
827 ASSERT_FALSE(imported_credit_card);
[email protected]cea1d112010-07-01 00:57:33828
[email protected]d7da65d2010-11-11 19:26:05829 // Wait for the refresh.
[email protected]cea1d112010-07-01 00:57:33830 EXPECT_CALL(personal_data_observer_,
831 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
832
833 MessageLoop::current()->Run();
834
835 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
[email protected]cea1d112010-07-01 00:57:33836
[email protected]f1ed7272010-11-11 21:57:07837 AutoFillProfile expected2;
838 autofill_test::SetProfileInfo(&expected2, NULL, "John", NULL,
[email protected]37ff612e2011-02-03 15:54:52839 "Adams", "[email protected]", NULL, "21 Laussat St", NULL,
840 "San Francisco", "California", "94102", NULL, NULL, NULL);
[email protected]f1ed7272010-11-11 21:57:07841 ASSERT_EQ(2U, results2.size());
842 EXPECT_EQ(0, expected.Compare(*results2[0]));
843 EXPECT_EQ(0, expected2.Compare(*results2[1]));
844}
[email protected]cea1d112010-07-01 00:57:33845
[email protected]f1ed7272010-11-11 21:57:07846TEST_F(PersonalDataManagerTest, AggregateSameProfileWithConflict) {
847 FormData form1;
848 webkit_glue::FormField field;
[email protected]468327f2010-10-04 20:02:29849 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33850 "First name:", "first_name", "George", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07851 form1.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29852 autofill_test::CreateTestFormField(
[email protected]cea1d112010-07-01 00:57:33853 "Last name:", "last_name", "Washington", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07854 form1.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29855 autofill_test::CreateTestFormField(
[email protected]f1ed7272010-11-11 21:57:07856 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
857 form1.fields.push_back(field);
[email protected]468327f2010-10-04 20:02:29858 autofill_test::CreateTestFormField(
[email protected]465d07f2010-11-30 04:52:34859 "Address Line 2:", "address2", "Suite A", "text", &field);
860 form1.fields.push_back(field);
861 autofill_test::CreateTestFormField(
[email protected]37ff612e2011-02-03 15:54:52862 "City:", "city", "San Francisco", "text", &field);
863 form1.fields.push_back(field);
864 autofill_test::CreateTestFormField(
865 "State:", "state", "California", "text", &field);
866 form1.fields.push_back(field);
867 autofill_test::CreateTestFormField(
868 "Zip:", "zip", "94102", "text", &field);
869 form1.fields.push_back(field);
870 autofill_test::CreateTestFormField(
[email protected]f1ed7272010-11-11 21:57:07871 "Email:", "email", "[email protected]", "text", &field);
872 form1.fields.push_back(field);
[email protected]465d07f2010-11-30 04:52:34873 // Phone gets updated.
874 autofill_test::CreateTestFormField(
875 "Phone:", "phone", "4445556666", "text", &field);
876 form1.fields.push_back(field);
[email protected]cea1d112010-07-01 00:57:33877
[email protected]f1ed7272010-11-11 21:57:07878 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:03879 std::vector<const FormStructure*> forms;
[email protected]f1ed7272010-11-11 21:57:07880 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:24881 const CreditCard* imported_credit_card;
882 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
883 ASSERT_FALSE(imported_credit_card);
[email protected]cea1d112010-07-01 00:57:33884
[email protected]d7da65d2010-11-11 19:26:05885 // Wait for the refresh.
[email protected]cea1d112010-07-01 00:57:33886 EXPECT_CALL(personal_data_observer_,
887 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
888
889 MessageLoop::current()->Run();
890
[email protected]f1ed7272010-11-11 21:57:07891 AutoFillProfile expected;
892 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
[email protected]465d07f2010-11-30 04:52:34893 "Washington", "[email protected]", NULL, "1600 Pennsylvania Avenue",
[email protected]37ff612e2011-02-03 15:54:52894 "Suite A", "San Francisco", "California", "94102", NULL, "4445556666",
895 NULL);
[email protected]f1ed7272010-11-11 21:57:07896 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
897 ASSERT_EQ(1U, results1.size());
898 EXPECT_EQ(0, expected.Compare(*results1[0]));
[email protected]cea1d112010-07-01 00:57:33899
[email protected]f1ed7272010-11-11 21:57:07900 // Now create an updated profile.
901 FormData form2;
902 autofill_test::CreateTestFormField(
903 "First name:", "first_name", "George", "text", &field);
904 form2.fields.push_back(field);
905 autofill_test::CreateTestFormField(
906 "Last name:", "last_name", "Washington", "text", &field);
907 form2.fields.push_back(field);
908 autofill_test::CreateTestFormField(
909 "Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
910 form2.fields.push_back(field);
[email protected]465d07f2010-11-30 04:52:34911 autofill_test::CreateTestFormField(
912 "Address Line 2:", "address2", "Suite A", "text", &field);
913 form2.fields.push_back(field);
914 autofill_test::CreateTestFormField(
[email protected]37ff612e2011-02-03 15:54:52915 "City:", "city", "San Francisco", "text", &field);
916 form2.fields.push_back(field);
917 autofill_test::CreateTestFormField(
918 "State:", "state", "California", "text", &field);
919 form2.fields.push_back(field);
920 autofill_test::CreateTestFormField(
921 "Zip:", "zip", "94102", "text", &field);
922 form2.fields.push_back(field);
923 autofill_test::CreateTestFormField(
[email protected]465d07f2010-11-30 04:52:34924 "Email:", "email", "[email protected]", "text", &field);
925 form2.fields.push_back(field);
[email protected]f1ed7272010-11-11 21:57:07926 // Country gets added.
927 autofill_test::CreateTestFormField(
928 "Country:", "country", "USA", "text", &field);
929 form2.fields.push_back(field);
[email protected]465d07f2010-11-30 04:52:34930 // Phone gets updated.
[email protected]f1ed7272010-11-11 21:57:07931 autofill_test::CreateTestFormField(
[email protected]465d07f2010-11-30 04:52:34932 "Phone:", "phone", "1231231234", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07933 form2.fields.push_back(field);
934
935 FormStructure form_structure2(form2);
936 forms.clear();
937 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:24938 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
939 ASSERT_FALSE(imported_credit_card);
[email protected]f1ed7272010-11-11 21:57:07940
941 // Wait for the refresh.
942 EXPECT_CALL(personal_data_observer_,
943 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
944
945 MessageLoop::current()->Run();
946
947 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
948
949 AutoFillProfile expected2;
950 autofill_test::SetProfileInfo(&expected2, NULL, "George", NULL,
[email protected]465d07f2010-11-30 04:52:34951 "Washington", "[email protected]", NULL, "1600 Pennsylvania Avenue",
[email protected]37ff612e2011-02-03 15:54:52952 "Suite A", "San Francisco", "California", "94102", "USA", "1231231234",
953 NULL);
[email protected]f1ed7272010-11-11 21:57:07954 ASSERT_EQ(1U, results2.size());
955 EXPECT_EQ(0, expected2.Compare(*results2[0]));
956}
957
958TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInOld) {
959 FormData form1;
960 webkit_glue::FormField field;
961 autofill_test::CreateTestFormField(
962 "First name:", "first_name", "George", "text", &field);
963 form1.fields.push_back(field);
964 autofill_test::CreateTestFormField(
965 "Last name:", "last_name", "Washington", "text", &field);
966 form1.fields.push_back(field);
967 autofill_test::CreateTestFormField(
[email protected]37ff612e2011-02-03 15:54:52968 "Address Line 1:", "address", "190 High Street", "text", &field);
969 form1.fields.push_back(field);
970 autofill_test::CreateTestFormField(
971 "City:", "city", "Philadelphia", "text", &field);
972 form1.fields.push_back(field);
973 autofill_test::CreateTestFormField(
974 "State:", "state", "Pennsylvania", "text", &field);
975 form1.fields.push_back(field);
976 autofill_test::CreateTestFormField(
977 "Zip:", "zipcode", "19106", "text", &field);
[email protected]f1ed7272010-11-11 21:57:07978 form1.fields.push_back(field);
979
980 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:03981 std::vector<const FormStructure*> forms;
[email protected]f1ed7272010-11-11 21:57:07982 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:24983 const CreditCard* imported_credit_card;
984 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
985 ASSERT_FALSE(imported_credit_card);
[email protected]f1ed7272010-11-11 21:57:07986
987 // Wait for the refresh.
988 EXPECT_CALL(personal_data_observer_,
989 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
990
991 MessageLoop::current()->Run();
992
993 AutoFillProfile expected;
994 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
[email protected]37ff612e2011-02-03 15:54:52995 "Washington", NULL, NULL, "190 High Street", NULL,
996 "Philadelphia", "Pennsylvania", "19106", NULL, NULL, NULL);
[email protected]f1ed7272010-11-11 21:57:07997 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
998 ASSERT_EQ(1U, results1.size());
999 EXPECT_EQ(0, expected.Compare(*results1[0]));
1000
1001 // Submit a form with new data for the first profile.
1002 FormData form2;
1003 autofill_test::CreateTestFormField(
1004 "First name:", "first_name", "George", "text", &field);
1005 form2.fields.push_back(field);
1006 autofill_test::CreateTestFormField(
1007 "Last name:", "last_name", "Washington", "text", &field);
1008 form2.fields.push_back(field);
1009 autofill_test::CreateTestFormField(
[email protected]37ff612e2011-02-03 15:54:521010 "Email:", "email", "[email protected]", "text", &field);
1011 form2.fields.push_back(field);
1012 autofill_test::CreateTestFormField(
[email protected]f1ed7272010-11-11 21:57:071013 "Address Line 1:", "address", "190 High Street", "text", &field);
1014 form2.fields.push_back(field);
1015 autofill_test::CreateTestFormField(
1016 "City:", "city", "Philadelphia", "text", &field);
1017 form2.fields.push_back(field);
1018 autofill_test::CreateTestFormField(
1019 "State:", "state", "Pennsylvania", "text", &field);
1020 form2.fields.push_back(field);
1021 autofill_test::CreateTestFormField(
1022 "Zip:", "zipcode", "19106", "text", &field);
1023 form2.fields.push_back(field);
1024
1025 FormStructure form_structure2(form2);
1026 forms.clear();
1027 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241028 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1029 ASSERT_FALSE(imported_credit_card);
[email protected]f1ed7272010-11-11 21:57:071030
1031 // Wait for the refresh.
1032 EXPECT_CALL(personal_data_observer_,
1033 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1034
1035 MessageLoop::current()->Run();
1036
1037 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
1038
1039 AutoFillProfile expected2;
1040 autofill_test::SetProfileInfo(&expected2, NULL, "George", NULL,
[email protected]cea1d112010-07-01 00:57:331041 "Washington", "[email protected]", NULL, "190 High Street", NULL,
1042 "Philadelphia", "Pennsylvania", "19106", NULL, NULL, NULL);
[email protected]f1ed7272010-11-11 21:57:071043 ASSERT_EQ(1U, results2.size());
1044 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1045}
[email protected]cea1d112010-07-01 00:57:331046
[email protected]f1ed7272010-11-11 21:57:071047TEST_F(PersonalDataManagerTest, AggregateProfileWithMissingInfoInNew) {
1048 FormData form1;
1049 webkit_glue::FormField field;
1050 autofill_test::CreateTestFormField(
1051 "First name:", "first_name", "George", "text", &field);
1052 form1.fields.push_back(field);
1053 autofill_test::CreateTestFormField(
1054 "Last name:", "last_name", "Washington", "text", &field);
1055 form1.fields.push_back(field);
1056 autofill_test::CreateTestFormField(
1057 "Company:", "company", "Government", "text", &field);
1058 form1.fields.push_back(field);
1059 autofill_test::CreateTestFormField(
1060 "Email:", "email", "[email protected]", "text", &field);
1061 form1.fields.push_back(field);
[email protected]37ff612e2011-02-03 15:54:521062 autofill_test::CreateTestFormField(
1063 "Address Line 1:", "address", "190 High Street", "text", &field);
1064 form1.fields.push_back(field);
1065 autofill_test::CreateTestFormField(
1066 "City:", "city", "Philadelphia", "text", &field);
1067 form1.fields.push_back(field);
1068 autofill_test::CreateTestFormField(
1069 "State:", "state", "Pennsylvania", "text", &field);
1070 form1.fields.push_back(field);
1071 autofill_test::CreateTestFormField(
1072 "Zip:", "zipcode", "19106", "text", &field);
1073 form1.fields.push_back(field);
[email protected]f1ed7272010-11-11 21:57:071074
1075 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031076 std::vector<const FormStructure*> forms;
[email protected]f1ed7272010-11-11 21:57:071077 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241078 const CreditCard* imported_credit_card;
1079 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1080 ASSERT_FALSE(imported_credit_card);
[email protected]f1ed7272010-11-11 21:57:071081
1082 // Wait for the refresh.
1083 EXPECT_CALL(personal_data_observer_,
1084 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1085
1086 MessageLoop::current()->Run();
1087
1088 AutoFillProfile expected;
1089 autofill_test::SetProfileInfo(&expected, NULL, "George", NULL,
[email protected]37ff612e2011-02-03 15:54:521090 "Washington", "[email protected]", "Government", "190 High Street", NULL,
1091 "Philadelphia", "Pennsylvania", "19106", NULL, NULL, NULL);
[email protected]f1ed7272010-11-11 21:57:071092 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles();
1093 ASSERT_EQ(1U, results1.size());
1094 EXPECT_EQ(0, expected.Compare(*results1[0]));
1095
1096 // Submit a form with new data for the first profile.
1097 FormData form2;
1098 autofill_test::CreateTestFormField(
1099 "First name:", "first_name", "George", "text", &field);
1100 form2.fields.push_back(field);
1101 autofill_test::CreateTestFormField(
1102 "Last name:", "last_name", "Washington", "text", &field);
1103 form2.fields.push_back(field);
1104 // Note missing Company field.
1105 autofill_test::CreateTestFormField(
1106 "Email:", "email", "[email protected]", "text", &field);
1107 form2.fields.push_back(field);
[email protected]37ff612e2011-02-03 15:54:521108 autofill_test::CreateTestFormField(
1109 "Address Line 1:", "address", "190 High Street", "text", &field);
1110 form2.fields.push_back(field);
1111 autofill_test::CreateTestFormField(
1112 "City:", "city", "Philadelphia", "text", &field);
1113 form2.fields.push_back(field);
1114 autofill_test::CreateTestFormField(
1115 "State:", "state", "Pennsylvania", "text", &field);
1116 form2.fields.push_back(field);
1117 autofill_test::CreateTestFormField(
1118 "Zip:", "zipcode", "19106", "text", &field);
1119 form2.fields.push_back(field);
[email protected]f1ed7272010-11-11 21:57:071120
1121 FormStructure form_structure2(form2);
1122 forms.clear();
1123 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241124 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1125 ASSERT_FALSE(imported_credit_card);
[email protected]f1ed7272010-11-11 21:57:071126
1127 // Wait for the refresh.
1128 EXPECT_CALL(personal_data_observer_,
1129 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1130
1131 MessageLoop::current()->Run();
1132
1133 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles();
1134
1135 // Expect no change.
1136 ASSERT_EQ(1U, results2.size());
1137 EXPECT_EQ(0, expected.Compare(*results2[0]));
[email protected]cea1d112010-07-01 00:57:331138}
[email protected]d7da65d2010-11-11 19:26:051139
[email protected]37ff612e2011-02-03 15:54:521140TEST_F(PersonalDataManagerTest, AggregateProfileWithInsufficientAddress) {
1141 FormData form1;
1142 webkit_glue::FormField field;
1143 autofill_test::CreateTestFormField(
1144 "First name:", "first_name", "George", "text", &field);
1145 form1.fields.push_back(field);
1146 autofill_test::CreateTestFormField(
1147 "Last name:", "last_name", "Washington", "text", &field);
1148 form1.fields.push_back(field);
1149 autofill_test::CreateTestFormField(
1150 "Company:", "company", "Government", "text", &field);
1151 form1.fields.push_back(field);
1152 autofill_test::CreateTestFormField(
1153 "Email:", "email", "[email protected]", "text", &field);
1154 form1.fields.push_back(field);
1155 autofill_test::CreateTestFormField(
1156 "Address Line 1:", "address", "190 High Street", "text", &field);
1157 form1.fields.push_back(field);
1158 autofill_test::CreateTestFormField(
1159 "City:", "city", "Philadelphia", "text", &field);
1160 form1.fields.push_back(field);
1161
1162 FormStructure form_structure1(form1);
1163 std::vector<const FormStructure*> forms;
1164 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241165 const CreditCard* imported_credit_card;
1166 EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1167 ASSERT_FALSE(imported_credit_card);
[email protected]37ff612e2011-02-03 15:54:521168}
1169
1170
[email protected]d7da65d2010-11-11 19:26:051171TEST_F(PersonalDataManagerTest, AggregateTwoDifferentCreditCards) {
1172 FormData form1;
1173
1174 // Start with a single valid credit card form.
1175 webkit_glue::FormField field;
1176 autofill_test::CreateTestFormField(
1177 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1178 form1.fields.push_back(field);
1179 autofill_test::CreateTestFormField(
1180 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1181 form1.fields.push_back(field);
1182 autofill_test::CreateTestFormField(
1183 "Exp Month:", "exp_month", "01", "text", &field);
1184 form1.fields.push_back(field);
1185 autofill_test::CreateTestFormField(
1186 "Exp Year:", "exp_year", "2011", "text", &field);
1187 form1.fields.push_back(field);
1188
1189 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031190 std::vector<const FormStructure*> forms;
[email protected]d7da65d2010-11-11 19:26:051191 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241192 const CreditCard* imported_credit_card;
1193 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1194 ASSERT_TRUE(imported_credit_card);
1195 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1196 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051197
1198 // Wait for the refresh.
1199 EXPECT_CALL(personal_data_observer_,
1200 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1201
1202 MessageLoop::current()->Run();
1203
1204 CreditCard expected;
1205 autofill_test::SetCreditCardInfo(&expected,
1206 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1207 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1208 ASSERT_EQ(1U, results.size());
1209 EXPECT_EQ(0, expected.Compare(*results[0]));
1210
1211 // Add a second different valid credit card.
1212 FormData form2;
1213 autofill_test::CreateTestFormField(
1214 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1215 form2.fields.push_back(field);
1216 autofill_test::CreateTestFormField(
1217 "Card Number:", "card_number", "5500 0000 0000 0004", "text", &field);
1218 form2.fields.push_back(field);
1219 autofill_test::CreateTestFormField(
1220 "Exp Month:", "exp_month", "02", "text", &field);
1221 form2.fields.push_back(field);
1222 autofill_test::CreateTestFormField(
1223 "Exp Year:", "exp_year", "2012", "text", &field);
1224 form2.fields.push_back(field);
1225
1226 FormStructure form_structure2(form2);
1227 forms.clear();
1228 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241229 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1230 ASSERT_TRUE(imported_credit_card);
1231 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1232 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051233
1234 // Wait for the refresh.
1235 EXPECT_CALL(personal_data_observer_,
1236 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1237
1238 MessageLoop::current()->Run();
1239
1240 CreditCard expected2;
1241 autofill_test::SetCreditCardInfo(&expected2,
1242 "L2", "Jim Johansen", "5500000000000004", "02", "2012");
1243 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1244 ASSERT_EQ(2U, results2.size());
1245 EXPECT_EQ(0, expected.Compare(*results2[0]));
1246 EXPECT_EQ(0, expected2.Compare(*results2[1]));
1247}
1248
1249TEST_F(PersonalDataManagerTest, AggregateInvalidCreditCard) {
1250 FormData form1;
1251
1252 // Start with a single valid credit card form.
1253 webkit_glue::FormField field;
1254 autofill_test::CreateTestFormField(
1255 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1256 form1.fields.push_back(field);
1257 autofill_test::CreateTestFormField(
1258 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1259 form1.fields.push_back(field);
1260 autofill_test::CreateTestFormField(
1261 "Exp Month:", "exp_month", "01", "text", &field);
1262 form1.fields.push_back(field);
1263 autofill_test::CreateTestFormField(
1264 "Exp Year:", "exp_year", "2011", "text", &field);
1265 form1.fields.push_back(field);
1266
1267 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031268 std::vector<const FormStructure*> forms;
[email protected]d7da65d2010-11-11 19:26:051269 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241270 const CreditCard* imported_credit_card;
1271 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1272 ASSERT_TRUE(imported_credit_card);
1273 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1274 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051275
1276 // Wait for the refresh.
1277 EXPECT_CALL(personal_data_observer_,
1278 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1279
1280 MessageLoop::current()->Run();
1281
1282 CreditCard expected;
1283 autofill_test::SetCreditCardInfo(&expected,
1284 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1285 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1286 ASSERT_EQ(1U, results.size());
1287 EXPECT_EQ(0, expected.Compare(*results[0]));
1288
1289 // Add a second different invalid credit card.
1290 FormData form2;
1291 autofill_test::CreateTestFormField(
1292 "Name on card:", "name_on_card", "Jim Johansen", "text", &field);
1293 form2.fields.push_back(field);
1294 autofill_test::CreateTestFormField(
1295 "Card Number:", "card_number", "1000000000000000", "text", &field);
1296 form2.fields.push_back(field);
1297 autofill_test::CreateTestFormField(
1298 "Exp Month:", "exp_month", "02", "text", &field);
1299 form2.fields.push_back(field);
1300 autofill_test::CreateTestFormField(
1301 "Exp Year:", "exp_year", "2012", "text", &field);
1302 form2.fields.push_back(field);
1303
1304 FormStructure form_structure2(form2);
1305 forms.clear();
1306 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241307 EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1308 ASSERT_FALSE(imported_credit_card);
[email protected]d7da65d2010-11-11 19:26:051309
1310 // Note: no refresh here.
1311
1312 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1313 ASSERT_EQ(1U, results2.size());
1314 EXPECT_EQ(0, expected.Compare(*results2[0]));
1315}
1316
1317TEST_F(PersonalDataManagerTest, AggregateSameCreditCardWithConflict) {
1318 FormData form1;
1319
1320 // Start with a single valid credit card form.
1321 webkit_glue::FormField field;
1322 autofill_test::CreateTestFormField(
1323 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1324 form1.fields.push_back(field);
1325 autofill_test::CreateTestFormField(
1326 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1327 form1.fields.push_back(field);
1328 autofill_test::CreateTestFormField(
1329 "Exp Month:", "exp_month", "01", "text", &field);
1330 form1.fields.push_back(field);
1331 autofill_test::CreateTestFormField(
1332 "Exp Year:", "exp_year", "2011", "text", &field);
1333 form1.fields.push_back(field);
1334
1335 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031336 std::vector<const FormStructure*> forms;
[email protected]d7da65d2010-11-11 19:26:051337 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241338 const CreditCard* imported_credit_card;
1339 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1340 ASSERT_TRUE(imported_credit_card);
1341 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1342 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051343
1344 // Wait for the refresh.
1345 EXPECT_CALL(personal_data_observer_,
1346 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1347
1348 MessageLoop::current()->Run();
1349
1350 CreditCard expected;
1351 autofill_test::SetCreditCardInfo(&expected,
1352 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1353 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1354 ASSERT_EQ(1U, results.size());
1355 EXPECT_EQ(0, expected.Compare(*results[0]));
1356
1357 // Add a second different valid credit card where the year is different but
1358 // the credit card number matches.
1359 FormData form2;
1360 autofill_test::CreateTestFormField(
1361 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1362 form2.fields.push_back(field);
1363 autofill_test::CreateTestFormField(
1364 "Card Number:", "card_number", "4111 1111 1111 1111", "text", &field);
1365 form2.fields.push_back(field);
1366 autofill_test::CreateTestFormField(
1367 "Exp Month:", "exp_month", "01", "text", &field);
1368 form2.fields.push_back(field);
1369 autofill_test::CreateTestFormField(
1370 "Exp Year:", "exp_year", "2012", "text", &field);
1371 form2.fields.push_back(field);
1372
1373 FormStructure form_structure2(form2);
1374 forms.clear();
1375 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241376 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1377 ASSERT_TRUE(imported_credit_card);
1378 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1379 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051380
1381 // Wait for the refresh.
1382 EXPECT_CALL(personal_data_observer_,
1383 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1384
1385 MessageLoop::current()->Run();
1386
1387 // Expect that the newer information is saved. In this case the year is
1388 // updated to "2012".
1389 CreditCard expected2;
1390 autofill_test::SetCreditCardInfo(&expected2,
1391 "L1", "Biggie Smalls", "4111111111111111", "01", "2012");
1392 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1393 ASSERT_EQ(1U, results2.size());
1394 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1395}
1396
1397TEST_F(PersonalDataManagerTest, AggregateEmptyCreditCardWithConflict) {
1398 FormData form1;
1399
1400 // Start with a single valid credit card form.
1401 webkit_glue::FormField field;
1402 autofill_test::CreateTestFormField(
1403 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1404 form1.fields.push_back(field);
1405 autofill_test::CreateTestFormField(
1406 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1407 form1.fields.push_back(field);
1408 autofill_test::CreateTestFormField(
1409 "Exp Month:", "exp_month", "01", "text", &field);
1410 form1.fields.push_back(field);
1411 autofill_test::CreateTestFormField(
1412 "Exp Year:", "exp_year", "2011", "text", &field);
1413 form1.fields.push_back(field);
1414
1415 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031416 std::vector<const FormStructure*> forms;
[email protected]d7da65d2010-11-11 19:26:051417 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241418 const CreditCard* imported_credit_card;
1419 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1420 ASSERT_TRUE(imported_credit_card);
1421 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1422 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051423
1424 // Wait for the refresh.
1425 EXPECT_CALL(personal_data_observer_,
1426 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1427
1428 MessageLoop::current()->Run();
1429
1430 CreditCard expected;
1431 autofill_test::SetCreditCardInfo(&expected,
1432 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1433 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1434 ASSERT_EQ(1U, results.size());
1435 EXPECT_EQ(0, expected.Compare(*results[0]));
1436
1437 // Add a second credit card with no number.
1438 FormData form2;
1439 autofill_test::CreateTestFormField(
1440 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1441 form2.fields.push_back(field);
1442 autofill_test::CreateTestFormField(
1443 "Exp Month:", "exp_month", "01", "text", &field);
1444 form2.fields.push_back(field);
1445 autofill_test::CreateTestFormField(
1446 "Exp Year:", "exp_year", "2012", "text", &field);
1447 form2.fields.push_back(field);
1448
1449 FormStructure form_structure2(form2);
1450 forms.clear();
1451 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241452 EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1453 ASSERT_FALSE(imported_credit_card);
[email protected]d7da65d2010-11-11 19:26:051454
1455 // Note: no refresh here.
1456
1457 // No change is expected.
1458 CreditCard expected2;
1459 autofill_test::SetCreditCardInfo(&expected2,
1460 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1461 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1462 ASSERT_EQ(1U, results2.size());
1463 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1464}
1465
1466TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInNew) {
1467 FormData form1;
1468
1469 // Start with a single valid credit card form.
1470 webkit_glue::FormField field;
1471 autofill_test::CreateTestFormField(
1472 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1473 form1.fields.push_back(field);
1474 autofill_test::CreateTestFormField(
1475 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1476 form1.fields.push_back(field);
1477 autofill_test::CreateTestFormField(
1478 "Exp Month:", "exp_month", "01", "text", &field);
1479 form1.fields.push_back(field);
1480 autofill_test::CreateTestFormField(
1481 "Exp Year:", "exp_year", "2011", "text", &field);
1482 form1.fields.push_back(field);
1483
1484 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031485 std::vector<const FormStructure*> forms;
[email protected]d7da65d2010-11-11 19:26:051486 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241487 const CreditCard* imported_credit_card;
1488 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1489 ASSERT_TRUE(imported_credit_card);
1490 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1491 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051492
1493 // Wait for the refresh.
1494 EXPECT_CALL(personal_data_observer_,
1495 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1496
1497 MessageLoop::current()->Run();
1498
1499 CreditCard expected;
1500 autofill_test::SetCreditCardInfo(&expected,
1501 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1502 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1503 ASSERT_EQ(1U, results.size());
1504 EXPECT_EQ(0, expected.Compare(*results[0]));
1505
1506 // Add a second different valid credit card where the name is missing but
1507 // the credit card number matches.
1508 FormData form2;
1509 // Note missing name.
1510 autofill_test::CreateTestFormField(
1511 "Card Number:", "card_number", "4111111111111111", "text", &field);
1512 form2.fields.push_back(field);
1513 autofill_test::CreateTestFormField(
1514 "Exp Month:", "exp_month", "01", "text", &field);
1515 form2.fields.push_back(field);
1516 autofill_test::CreateTestFormField(
1517 "Exp Year:", "exp_year", "2011", "text", &field);
1518 form2.fields.push_back(field);
1519
1520 FormStructure form_structure2(form2);
1521 forms.clear();
1522 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241523 EXPECT_FALSE(personal_data_->ImportFormData(forms, &imported_credit_card));
1524 ASSERT_FALSE(imported_credit_card);
[email protected]d7da65d2010-11-11 19:26:051525
[email protected]7ed9caa2010-11-18 01:54:011526 // Note: no refresh here.
[email protected]d7da65d2010-11-11 19:26:051527
1528 // No change is expected.
1529 CreditCard expected2;
1530 autofill_test::SetCreditCardInfo(&expected2,
1531 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1532 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1533 ASSERT_EQ(1U, results2.size());
1534 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1535}
1536
1537TEST_F(PersonalDataManagerTest, AggregateCreditCardWithMissingInfoInOld) {
1538 FormData form1;
1539
1540 // Start with a single valid credit card form.
1541 webkit_glue::FormField field;
1542 // Note missing name.
1543 autofill_test::CreateTestFormField(
1544 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1545 form1.fields.push_back(field);
1546 autofill_test::CreateTestFormField(
1547 "Exp Month:", "exp_month", "01", "text", &field);
1548 form1.fields.push_back(field);
1549 autofill_test::CreateTestFormField(
1550 "Exp Year:", "exp_year", "2011", "text", &field);
1551 form1.fields.push_back(field);
1552
1553 FormStructure form_structure1(form1);
[email protected]09205efb2011-01-20 21:20:031554 std::vector<const FormStructure*> forms;
[email protected]d7da65d2010-11-11 19:26:051555 forms.push_back(&form_structure1);
[email protected]abcdf1a2011-02-04 23:12:241556 const CreditCard* imported_credit_card;
1557 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1558 ASSERT_TRUE(imported_credit_card);
1559 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1560 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051561
1562 // Wait for the refresh.
1563 EXPECT_CALL(personal_data_observer_,
1564 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1565
1566 MessageLoop::current()->Run();
1567
1568 CreditCard expected;
1569 autofill_test::SetCreditCardInfo(&expected,
1570 "L1", NULL, "4111111111111111", "01", "2011");
1571 const std::vector<CreditCard*>& results = personal_data_->credit_cards();
1572 ASSERT_EQ(1U, results.size());
1573 EXPECT_EQ(0, expected.Compare(*results[0]));
1574
1575 // Add a second different valid credit card where the year is different but
1576 // the credit card number matches.
1577 FormData form2;
1578 autofill_test::CreateTestFormField(
1579 "Name on card:", "name_on_card", "Biggie Smalls", "text", &field);
1580 form2.fields.push_back(field);
1581 autofill_test::CreateTestFormField(
1582 "Card Number:", "card_number", "4111-1111-1111-1111", "text", &field);
1583 form2.fields.push_back(field);
1584 autofill_test::CreateTestFormField(
1585 "Exp Month:", "exp_month", "01", "text", &field);
1586 form2.fields.push_back(field);
1587 autofill_test::CreateTestFormField(
1588 "Exp Year:", "exp_year", "2011", "text", &field);
1589 form2.fields.push_back(field);
1590
1591 FormStructure form_structure2(form2);
1592 forms.clear();
1593 forms.push_back(&form_structure2);
[email protected]abcdf1a2011-02-04 23:12:241594 EXPECT_TRUE(personal_data_->ImportFormData(forms, &imported_credit_card));
1595 ASSERT_TRUE(imported_credit_card);
1596 personal_data_->SaveImportedCreditCard(*imported_credit_card);
1597 delete imported_credit_card;
[email protected]d7da65d2010-11-11 19:26:051598
1599 // Wait for the refresh.
1600 EXPECT_CALL(personal_data_observer_,
1601 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop());
1602
1603 MessageLoop::current()->Run();
1604
1605 // Expect that the newer information is saved. In this case the year is
1606 // added to the existing credit card.
1607 CreditCard expected2;
1608 autofill_test::SetCreditCardInfo(&expected2,
1609 "L1", "Biggie Smalls", "4111111111111111", "01", "2011");
1610 const std::vector<CreditCard*>& results2 = personal_data_->credit_cards();
1611 ASSERT_EQ(1U, results2.size());
1612 EXPECT_EQ(0, expected2.Compare(*results2[0]));
1613}