OLD | NEW |
(Empty) | |
| 1 // 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 |
| 5 #include "base/basictypes.h" |
| 6 #include "base/message_loop.h" |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "chrome/browser/autofill/autofill_profile.h" |
| 9 #include "chrome/browser/autofill/personal_data_manager.h" |
| 10 #include "chrome/browser/chrome_thread.h" |
| 11 #include "chrome/common/notification_details.h" |
| 12 #include "chrome/common/notification_observer_mock.h" |
| 13 #include "chrome/common/notification_registrar.h" |
| 14 #include "chrome/common/notification_service.h" |
| 15 #include "chrome/common/notification_type.h" |
| 16 #include "chrome/test/testing_profile.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 ACTION(QuitUIMessageLoop) { |
| 21 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 22 MessageLoop::current()->Quit(); |
| 23 } |
| 24 |
| 25 class PersonalDataLoadedObserverMock : public PersonalDataManager::Observer { |
| 26 public: |
| 27 PersonalDataLoadedObserverMock() {} |
| 28 virtual ~PersonalDataLoadedObserverMock() {} |
| 29 |
| 30 MOCK_METHOD0(OnPersonalDataLoaded, void()); |
| 31 }; |
| 32 |
| 33 class PersonalDataManagerTest : public testing::Test { |
| 34 protected: |
| 35 PersonalDataManagerTest() |
| 36 : ui_thread_(ChromeThread::UI, &message_loop_), |
| 37 db_thread_(ChromeThread::DB) { |
| 38 } |
| 39 |
| 40 virtual void SetUp() { |
| 41 db_thread_.Start(); |
| 42 |
| 43 profile_.reset(new TestingProfile); |
| 44 profile_->CreateWebDataService(false); |
| 45 |
| 46 ResetPersonalDataManager(); |
| 47 } |
| 48 |
| 49 virtual void TearDown() { |
| 50 if (profile_.get()) |
| 51 profile_.reset(NULL); |
| 52 |
| 53 db_thread_.Stop(); |
| 54 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 55 MessageLoop::current()->Run(); |
| 56 } |
| 57 |
| 58 void ResetPersonalDataManager() { |
| 59 personal_data_.reset(new PersonalDataManager(profile_.get())); |
| 60 personal_data_->SetObserver(&personal_data_observer_); |
| 61 } |
| 62 |
| 63 static void SetProfileInfo(AutoFillProfile* profile, |
| 64 const char* label, const char* first_name, const char* middle_name, |
| 65 const char* last_name, const char* email, const char* company, |
| 66 const char* address1, const char* address2, const char* city, |
| 67 const char* state, const char* zipcode, const char* country, |
| 68 const char* phone, const char* fax) { |
| 69 profile->set_label(ASCIIToUTF16(label)); |
| 70 profile->SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16(first_name)); |
| 71 profile->SetInfo(AutoFillType(NAME_MIDDLE), ASCIIToUTF16(middle_name)); |
| 72 profile->SetInfo(AutoFillType(NAME_LAST), ASCIIToUTF16(last_name)); |
| 73 profile->SetInfo(AutoFillType(EMAIL_ADDRESS), ASCIIToUTF16(email)); |
| 74 profile->SetInfo(AutoFillType(COMPANY_NAME), ASCIIToUTF16(company)); |
| 75 profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE1), ASCIIToUTF16(address1)); |
| 76 profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE2), ASCIIToUTF16(address2)); |
| 77 profile->SetInfo(AutoFillType(ADDRESS_HOME_CITY), ASCIIToUTF16(city)); |
| 78 profile->SetInfo(AutoFillType(ADDRESS_HOME_STATE), ASCIIToUTF16(state)); |
| 79 profile->SetInfo(AutoFillType(ADDRESS_HOME_ZIP), ASCIIToUTF16(zipcode)); |
| 80 profile->SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16(country)); |
| 81 profile->SetInfo(AutoFillType(PHONE_HOME_NUMBER), ASCIIToUTF16(phone)); |
| 82 profile->SetInfo(AutoFillType(PHONE_FAX_NUMBER), ASCIIToUTF16(fax)); |
| 83 } |
| 84 |
| 85 MessageLoopForUI message_loop_; |
| 86 ChromeThread ui_thread_; |
| 87 ChromeThread db_thread_; |
| 88 scoped_ptr<TestingProfile> profile_; |
| 89 scoped_ptr<PersonalDataManager> personal_data_; |
| 90 NotificationRegistrar registrar_; |
| 91 NotificationObserverMock observer_; |
| 92 PersonalDataLoadedObserverMock personal_data_observer_; |
| 93 }; |
| 94 |
| 95 // TODO(jhawkins): Test SaveProfiles w/out a WebDataService in the profile. |
| 96 TEST_F(PersonalDataManagerTest, SetProfiles) { |
| 97 AutoFillProfile profile0(string16(), 0); |
| 98 SetProfileInfo(&profile0, "Billing", "Marion", "Mitchell", "Morrison", |
| 99 "johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA", |
| 100 "91601", "US", "12345678910", "01987654321"); |
| 101 |
| 102 AutoFillProfile profile1(string16(), 0); |
| 103 SetProfileInfo(&profile1, "Home", "Josephine", "Alicia", "Saenz", |
| 104 "joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801", |
| 105 "US", "19482937549", "13502849239"); |
| 106 |
| 107 AutoFillProfile profile2(string16(), 0); |
| 108 SetProfileInfo(&profile2, "Work", "Josephine", "Alicia", "Saenz", |
| 109 "joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL", |
| 110 "32801", "US", "19482937549", "13502849239"); |
| 111 |
| 112 // This will verify that the web database has been loaded and the notification |
| 113 // sent out. |
| 114 EXPECT_CALL(personal_data_observer_, |
| 115 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop()); |
| 116 |
| 117 // The message loop will exit when the mock observer is notified. |
| 118 MessageLoop::current()->Run(); |
| 119 |
| 120 // Add the three test profiles to the database. |
| 121 std::vector<AutoFillProfile> update; |
| 122 update.push_back(profile0); |
| 123 update.push_back(profile1); |
| 124 personal_data_->SetProfiles(&update); |
| 125 |
| 126 // The PersonalDataManager will update the unique IDs when saving the |
| 127 // profiles, so we have to update the expectations. |
| 128 profile0.set_unique_id(update[0].unique_id()); |
| 129 profile1.set_unique_id(update[1].unique_id()); |
| 130 |
| 131 const std::vector<AutoFillProfile*>& results1 = personal_data_->profiles(); |
| 132 ASSERT_EQ(2U, results1.size()); |
| 133 EXPECT_EQ(profile0, *results1.at(0)); |
| 134 EXPECT_EQ(profile1, *results1.at(1)); |
| 135 |
| 136 // Three operations in one: |
| 137 // - Update profile0 |
| 138 // - Remove profile1 |
| 139 // - Add profile2 |
| 140 profile0.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("John")); |
| 141 update.clear(); |
| 142 update.push_back(profile0); |
| 143 update.push_back(profile2); |
| 144 personal_data_->SetProfiles(&update); |
| 145 |
| 146 // Set the expected unique ID for profile2. |
| 147 profile2.set_unique_id(update[1].unique_id()); |
| 148 |
| 149 // AutoFillProfile IDs are re-used, so the third profile to be added will have |
| 150 // a unique ID that matches the old unique ID of the removed profile1, even |
| 151 // though that ID has already been used. |
| 152 const std::vector<AutoFillProfile*>& results2 = personal_data_->profiles(); |
| 153 ASSERT_EQ(2U, results2.size()); |
| 154 EXPECT_EQ(profile0, *results2.at(0)); |
| 155 EXPECT_EQ(profile2, *results2.at(1)); |
| 156 EXPECT_EQ(profile2.unique_id(), profile1.unique_id()); |
| 157 |
| 158 // Reset the PersonalDataManager. This tests that the personal data was saved |
| 159 // to the web database, and that we can load the profiles from the web |
| 160 // database. |
| 161 ResetPersonalDataManager(); |
| 162 |
| 163 //PersonalDataLoadedObserver observer; |
| 164 // This will verify that the web database has been loaded and the notification |
| 165 // sent out. |
| 166 EXPECT_CALL(personal_data_observer_, |
| 167 OnPersonalDataLoaded()).WillOnce(QuitUIMessageLoop()); |
| 168 |
| 169 // The message loop will exit when the PersonalDataLoadedObserver is notified. |
| 170 MessageLoop::current()->Run(); |
| 171 |
| 172 // Verify that we've loaded the profiles from the web database. |
| 173 const std::vector<AutoFillProfile*>& results3 = personal_data_->profiles(); |
| 174 ASSERT_EQ(2U, results3.size()); |
| 175 EXPECT_EQ(profile0, *results3.at(0)); |
| 176 EXPECT_EQ(profile2, *results3.at(1)); |
| 177 } |
OLD | NEW |