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_CONST_METHOD1(LoadAutofillData, |
| 32 bool(std::vector<AutofillProfile*>*)); |
| 33 MOCK_METHOD1(SaveChangesToWebData, |
| 34 bool(const AutofillProfileSyncableService::DataBundle&)); |
| 35 |
| 36 // Helper for tests that do not need to setup service completely through the |
| 37 // MergeDataAndStartSyncing(). |
| 38 class AutoSetSyncProcessor { |
| 39 public: |
| 40 AutoSetSyncProcessor(MockAutofillProfileSyncableService* service, |
| 41 SyncChangeProcessor* sync_processor) |
| 42 : service_(service) { |
| 43 service->set_sync_processor(sync_processor); |
| 44 } |
| 45 ~AutoSetSyncProcessor() { |
| 46 service_->set_sync_processor(NULL); |
| 47 } |
| 48 MockAutofillProfileSyncableService* service_; |
| 49 }; |
| 50 }; |
| 51 |
| 52 ACTION_P(CopyData, data) { |
| 53 arg0->resize(data->size()); |
| 54 std::copy(data->begin(), data->end(), arg0->begin()); |
| 55 } |
| 56 |
| 57 MATCHER_P3(DataBundleCheck, n_delete, n_update, n_new, "") { |
| 58 return (arg.profiles_to_delete.size() == n_delete) && |
| 59 (arg.updated_profiles.size() == n_update) && |
| 60 (arg.new_profiles.size() == n_new); |
| 61 } |
| 62 |
| 63 class MockSyncChangeProcessor : public SyncChangeProcessor { |
| 64 public: |
| 65 MockSyncChangeProcessor() {} |
| 66 virtual ~MockSyncChangeProcessor() {} |
| 67 |
| 68 MOCK_METHOD2(ProcessSyncChanges, |
| 69 SyncError(const tracked_objects::Location&, |
| 70 const SyncChangeList&)); |
| 71 }; |
| 72 |
| 73 class AutofillProfileSyncableServiceTest : public testing::Test { |
| 74 public: |
| 75 AutofillProfileSyncableServiceTest() |
| 76 : db_thread_(BrowserThread::DB, &message_loop_) {} |
| 77 |
| 78 protected: |
| 79 MessageLoop message_loop_; |
| 80 BrowserThread db_thread_; |
| 81 MockAutofillProfileSyncableService autofill_syncable_service_; |
| 82 MockSyncChangeProcessor sync_processor_; |
| 83 }; |
| 84 |
| 85 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { |
| 86 std::vector<AutofillProfile *> profiles_from_web_db; |
| 87 std::string guid_present = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 88 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 89 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; |
| 90 |
| 91 profiles_from_web_db.push_back(new AutofillProfile(guid_present)); |
| 92 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 93 |
| 94 SyncDataList data_list; |
| 95 AutofillProfile profile1(guid_synced1); |
| 96 profile1.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 97 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); |
| 98 AutofillProfile profile2(guid_synced2); |
| 99 profile2.SetInfo(NAME_FIRST, UTF8ToUTF16("Harry")); |
| 100 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); |
| 101 |
| 102 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 103 .Times(1) |
| 104 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 105 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) |
| 106 .Times(1) |
| 107 .WillOnce(Return(true)); |
| 108 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) |
| 109 .WillByDefault(Return(SyncError())); |
| 110 EXPECT_CALL(sync_processor_, |
| 111 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(1U)))) |
| 112 .Times(1) |
| 113 .WillOnce(Return(SyncError())); |
| 114 |
| 115 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 116 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); |
| 117 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); |
| 118 } |
| 119 |
| 120 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { |
| 121 std::vector<AutofillProfile *> profiles_from_web_db; |
| 122 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 123 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 124 |
| 125 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| 126 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 127 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
| 128 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 129 |
| 130 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 131 .Times(1) |
| 132 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 133 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) |
| 134 .Times(1) |
| 135 .WillOnce(Return(true)); |
| 136 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) |
| 137 .WillByDefault(Return(SyncError())); |
| 138 EXPECT_CALL(sync_processor_, |
| 139 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) |
| 140 .Times(1) |
| 141 .WillOnce(Return(SyncError())); |
| 142 |
| 143 SyncDataList data_list; |
| 144 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 145 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); |
| 146 |
| 147 SyncDataList data = |
| 148 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); |
| 149 |
| 150 EXPECT_EQ(2U, data.size()); |
| 151 EXPECT_EQ(guid_present1, data.front().GetSpecifics() |
| 152 .GetExtension(sync_pb::autofill_profile).guid()); |
| 153 EXPECT_EQ(guid_present2, data.back().GetSpecifics() |
| 154 .GetExtension(sync_pb::autofill_profile).guid()); |
| 155 } |
| 156 |
| 157 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { |
| 158 std::vector<AutofillProfile *> profiles_from_web_db; |
| 159 std::string guid_present = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 160 std::string guid_synced = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 161 |
| 162 SyncChangeList change_list; |
| 163 AutofillProfile profile(guid_synced); |
| 164 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 165 change_list.push_back( |
| 166 SyncChange(SyncChange::ACTION_ADD, |
| 167 AutofillProfileSyncableService::CreateData(profile))); |
| 168 AutofillProfile empty_profile(guid_present); |
| 169 change_list.push_back( |
| 170 SyncChange(SyncChange::ACTION_DELETE, |
| 171 AutofillProfileSyncableService::CreateData(empty_profile))); |
| 172 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( |
| 173 DataBundleCheck(1U, 0U, 1U))) |
| 174 .Times(1) |
| 175 .WillOnce(Return(true)); |
| 176 |
| 177 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( |
| 178 &autofill_syncable_service_, &sync_processor_); |
| 179 SyncError error = autofill_syncable_service_.ProcessSyncChanges( |
| 180 FROM_HERE, change_list); |
| 181 |
| 182 EXPECT_FALSE(error.IsSet()); |
| 183 } |
| 184 |
| 185 } // namespace browser_sync |
| 186 |
OLD | NEW |