OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/tracked.h" |
| 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/sync/glue/autofill_profile_syncable_service.h" |
| 8 #include "chrome/browser/sync/internal_api/read_node_mock.h" |
| 9 #include "chrome/browser/sync/internal_api/syncapi_mock.h" |
| 10 #include "chrome/browser/sync/syncable/syncable.h" |
| 11 #include "chrome/browser/sync/syncable/syncable_mock.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 using ::testing::_; |
| 16 using ::testing::DoAll; |
| 17 using ::testing::Eq; |
| 18 using ::testing::Return; |
| 19 using ::testing::Property; |
| 20 class AutofillProfile; |
| 21 |
| 22 namespace browser_sync { |
| 23 |
| 24 class MockAutofillProfileSyncableService |
| 25 : public browser_sync::AutofillProfileSyncableService { |
| 26 public: |
| 27 MockAutofillProfileSyncableService() { |
| 28 } |
| 29 virtual ~MockAutofillProfileSyncableService() {} |
| 30 |
| 31 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); |
| 32 MOCK_METHOD1(SaveChangesToWebData, |
| 33 bool(const AutofillProfileSyncableService::DataBundle&)); |
| 34 |
| 35 // Helper for tests that do not need to setup service completely through the |
| 36 // MergeDataAndStartSyncing(). |
| 37 class AutoSetSyncProcessor { |
| 38 public: |
| 39 AutoSetSyncProcessor(MockAutofillProfileSyncableService* service, |
| 40 SyncChangeProcessor* sync_processor) |
| 41 : service_(service) { |
| 42 service->set_sync_processor(sync_processor); |
| 43 } |
| 44 ~AutoSetSyncProcessor() { |
| 45 service_->set_sync_processor(NULL); |
| 46 } |
| 47 MockAutofillProfileSyncableService* service_; |
| 48 }; |
| 49 }; |
| 50 |
| 51 ACTION_P(CopyData, data) { |
| 52 arg0->resize(data->size()); |
| 53 std::copy(data->begin(), data->end(), arg0->begin()); |
| 54 } |
| 55 |
| 56 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { |
| 57 if (arg.size() != n_sync_changes_list.size()) |
| 58 return false; |
| 59 SyncChangeList::const_iterator passed, expected; |
| 60 for (passed = arg.begin(), expected = n_sync_changes_list.begin(); |
| 61 passed != arg.end() && expected != n_sync_changes_list.end(); |
| 62 ++passed, ++expected) { |
| 63 DCHECK(passed->IsValid()); |
| 64 if (passed->change_type() != expected->change_type()) |
| 65 return false; |
| 66 if (passed->sync_data().GetSpecifics().GetExtension( |
| 67 sync_pb::autofill_profile).guid() != |
| 68 expected->sync_data().GetSpecifics().GetExtension( |
| 69 sync_pb::autofill_profile).guid()) { |
| 70 return false; |
| 71 } |
| 72 } |
| 73 return true; |
| 74 } |
| 75 |
| 76 MATCHER_P(DataBundleCheck, n_bundle, "") { |
| 77 if ((arg.profiles_to_delete.size() != n_bundle.profiles_to_delete.size()) || |
| 78 (arg.profiles_to_update.size() != n_bundle.profiles_to_update.size()) || |
| 79 (arg.profiles_to_add.size() != n_bundle.profiles_to_add.size())) { |
| 80 return false; |
| 81 } |
| 82 for (size_t i = 0; i < arg.profiles_to_delete.size(); ++i) { |
| 83 if (arg.profiles_to_delete[i] != n_bundle.profiles_to_delete[i]) |
| 84 return false; |
| 85 } |
| 86 for (size_t i = 0; i < arg.profiles_to_update.size(); ++i) { |
| 87 if (arg.profiles_to_update[i]->guid() != |
| 88 n_bundle.profiles_to_update[i]->guid()) { |
| 89 return false; |
| 90 } |
| 91 } |
| 92 for (size_t i = 0; i < arg.profiles_to_add.size(); ++i) { |
| 93 if (arg.profiles_to_add[i]->Compare(*n_bundle.profiles_to_add[i])) |
| 94 return false; |
| 95 } |
| 96 return true; |
| 97 } |
| 98 |
| 99 class MockSyncChangeProcessor : public SyncChangeProcessor { |
| 100 public: |
| 101 MockSyncChangeProcessor() {} |
| 102 virtual ~MockSyncChangeProcessor() {} |
| 103 |
| 104 MOCK_METHOD2(ProcessSyncChanges, |
| 105 SyncError(const tracked_objects::Location&, |
| 106 const SyncChangeList&)); |
| 107 }; |
| 108 |
| 109 class AutofillProfileSyncableServiceTest : public testing::Test { |
| 110 public: |
| 111 AutofillProfileSyncableServiceTest() |
| 112 : db_thread_(BrowserThread::DB, &message_loop_) {} |
| 113 |
| 114 protected: |
| 115 MessageLoop message_loop_; |
| 116 BrowserThread db_thread_; |
| 117 MockAutofillProfileSyncableService autofill_syncable_service_; |
| 118 MockSyncChangeProcessor sync_processor_; |
| 119 }; |
| 120 |
| 121 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { |
| 122 std::vector<AutofillProfile *> profiles_from_web_db; |
| 123 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 124 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 125 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; |
| 126 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; |
| 127 |
| 128 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| 129 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 130 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
| 131 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Tom")); |
| 132 |
| 133 SyncDataList data_list; |
| 134 AutofillProfile profile1(guid_synced1); |
| 135 profile1.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 136 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); |
| 137 AutofillProfile profile2(guid_synced2); |
| 138 profile2.SetInfo(NAME_FIRST, UTF8ToUTF16("Harry")); |
| 139 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); |
| 140 // This one will have the name updated. |
| 141 AutofillProfile profile3(guid_present2); |
| 142 profile3.SetInfo(NAME_FIRST, UTF8ToUTF16("Tom Doe")); |
| 143 data_list.push_back(autofill_syncable_service_.CreateData(profile3)); |
| 144 |
| 145 SyncChangeList expected_change_list; |
| 146 expected_change_list.push_back( |
| 147 SyncChange(SyncChange::ACTION_ADD, |
| 148 AutofillProfileSyncableService::CreateData( |
| 149 (*profiles_from_web_db.front())))); |
| 150 |
| 151 AutofillProfileSyncableService::DataBundle expected_bundle; |
| 152 expected_bundle.profiles_to_add.push_back(&profile1); |
| 153 expected_bundle.profiles_to_add.push_back(&profile2); |
| 154 expected_bundle.profiles_to_update.push_back(&profile3); |
| 155 |
| 156 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 157 .Times(1) |
| 158 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 159 EXPECT_CALL(autofill_syncable_service_, |
| 160 SaveChangesToWebData(DataBundleCheck(expected_bundle))) |
| 161 .Times(1) |
| 162 .WillOnce(Return(true)); |
| 163 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) |
| 164 .WillByDefault(Return(SyncError())); |
| 165 EXPECT_CALL(sync_processor_, |
| 166 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) |
| 167 .Times(1) |
| 168 .WillOnce(Return(SyncError())); |
| 169 |
| 170 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 171 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); |
| 172 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); |
| 173 } |
| 174 |
| 175 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { |
| 176 std::vector<AutofillProfile *> profiles_from_web_db; |
| 177 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 178 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 179 |
| 180 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| 181 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 182 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
| 183 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 184 |
| 185 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 186 .Times(1) |
| 187 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 188 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) |
| 189 .Times(1) |
| 190 .WillOnce(Return(true)); |
| 191 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) |
| 192 .WillByDefault(Return(SyncError())); |
| 193 EXPECT_CALL(sync_processor_, |
| 194 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) |
| 195 .Times(1) |
| 196 .WillOnce(Return(SyncError())); |
| 197 |
| 198 SyncDataList data_list; |
| 199 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 200 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); |
| 201 |
| 202 SyncDataList data = |
| 203 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); |
| 204 |
| 205 EXPECT_EQ(2U, data.size()); |
| 206 EXPECT_EQ(guid_present1, data.front().GetSpecifics() |
| 207 .GetExtension(sync_pb::autofill_profile).guid()); |
| 208 EXPECT_EQ(guid_present2, data.back().GetSpecifics() |
| 209 .GetExtension(sync_pb::autofill_profile).guid()); |
| 210 } |
| 211 |
| 212 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { |
| 213 std::vector<AutofillProfile *> profiles_from_web_db; |
| 214 std::string guid_present = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 215 std::string guid_synced = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 216 |
| 217 SyncChangeList change_list; |
| 218 AutofillProfile profile(guid_synced); |
| 219 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 220 change_list.push_back( |
| 221 SyncChange(SyncChange::ACTION_ADD, |
| 222 AutofillProfileSyncableService::CreateData(profile))); |
| 223 AutofillProfile empty_profile(guid_present); |
| 224 change_list.push_back( |
| 225 SyncChange(SyncChange::ACTION_DELETE, |
| 226 AutofillProfileSyncableService::CreateData(empty_profile))); |
| 227 |
| 228 AutofillProfileSyncableService::DataBundle expected_bundle; |
| 229 expected_bundle.profiles_to_delete.push_back(guid_present); |
| 230 expected_bundle.profiles_to_add.push_back(&profile); |
| 231 |
| 232 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( |
| 233 DataBundleCheck(expected_bundle))) |
| 234 .Times(1) |
| 235 .WillOnce(Return(true)); |
| 236 |
| 237 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( |
| 238 &autofill_syncable_service_, &sync_processor_); |
| 239 SyncError error = autofill_syncable_service_.ProcessSyncChanges( |
| 240 FROM_HERE, change_list); |
| 241 |
| 242 EXPECT_FALSE(error.IsSet()); |
| 243 } |
| 244 |
| 245 } // namespace browser_sync |
| 246 |
OLD | NEW |