OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/location.h" | 5 #include "base/location.h" |
6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
7 #include "chrome/browser/autofill/autofill_profile.h" | 7 #include "chrome/browser/autofill/autofill_profile.h" |
8 #include "chrome/browser/sync/glue/autofill_profile_syncable_service.h" | 8 #include "chrome/browser/sync/glue/autofill_profile_syncable_service.h" |
9 #include "chrome/browser/sync/internal_api/read_node_mock.h" | 9 #include "chrome/browser/sync/internal_api/read_node_mock.h" |
10 #include "chrome/browser/sync/internal_api/syncapi_mock.h" | 10 #include "chrome/browser/sync/internal_api/syncapi_mock.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 class MockAutofillProfileSyncableService | 27 class MockAutofillProfileSyncableService |
28 : public browser_sync::AutofillProfileSyncableService { | 28 : public browser_sync::AutofillProfileSyncableService { |
29 public: | 29 public: |
30 MockAutofillProfileSyncableService() { | 30 MockAutofillProfileSyncableService() { |
31 } | 31 } |
32 virtual ~MockAutofillProfileSyncableService() {} | 32 virtual ~MockAutofillProfileSyncableService() {} |
33 | 33 |
34 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); | 34 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); |
35 MOCK_METHOD1(SaveChangesToWebData, | 35 MOCK_METHOD1(SaveChangesToWebData, |
36 bool(const AutofillProfileSyncableService::DataBundle&)); | 36 bool(const AutofillProfileSyncableService::DataBundle&)); |
37 | |
38 // Helper for tests that do not need to setup service completely through the | |
39 // MergeDataAndStartSyncing(). | |
40 class AutoSetSyncProcessor { | |
41 public: | |
42 AutoSetSyncProcessor(MockAutofillProfileSyncableService* service, | |
43 SyncChangeProcessor* sync_processor) | |
44 : service_(service) { | |
45 service->set_sync_processor(sync_processor); | |
46 } | |
47 ~AutoSetSyncProcessor() { | |
48 service_->set_sync_processor(NULL); | |
49 } | |
50 MockAutofillProfileSyncableService* service_; | |
51 }; | |
52 }; | 37 }; |
53 | 38 |
54 ACTION_P(CopyData, data) { | 39 ACTION_P(CopyData, data) { |
55 arg0->resize(data->size()); | 40 arg0->resize(data->size()); |
56 std::copy(data->begin(), data->end(), arg0->begin()); | 41 std::copy(data->begin(), data->end(), arg0->begin()); |
57 } | 42 } |
58 | 43 |
59 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { | 44 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { |
60 if (arg.size() != n_sync_changes_list.size()) | 45 if (arg.size() != n_sync_changes_list.size()) |
61 return false; | 46 return false; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 MOCK_METHOD2(ProcessSyncChanges, | 92 MOCK_METHOD2(ProcessSyncChanges, |
108 SyncError(const tracked_objects::Location&, | 93 SyncError(const tracked_objects::Location&, |
109 const SyncChangeList&)); | 94 const SyncChangeList&)); |
110 }; | 95 }; |
111 | 96 |
112 class AutofillProfileSyncableServiceTest : public testing::Test { | 97 class AutofillProfileSyncableServiceTest : public testing::Test { |
113 public: | 98 public: |
114 AutofillProfileSyncableServiceTest() | 99 AutofillProfileSyncableServiceTest() |
115 : db_thread_(BrowserThread::DB, &message_loop_) {} | 100 : db_thread_(BrowserThread::DB, &message_loop_) {} |
116 | 101 |
| 102 virtual void SetUp() OVERRIDE { |
| 103 sync_processor_.reset(new MockSyncChangeProcessor); |
| 104 } |
| 105 |
| 106 virtual void TearDown() OVERRIDE { |
| 107 // Each test passes ownership of the sync processor to the SyncableService. |
| 108 // We don't release it immediately so we can verify the mock calls, so |
| 109 // release it at teardown. Any test that doesn't call |
| 110 // MergeDataAndStartSyncing or set_sync_processor must ensure the |
| 111 // sync_processor_ gets properly reset. |
| 112 ignore_result(sync_processor_.release()); |
| 113 } |
117 protected: | 114 protected: |
118 MessageLoop message_loop_; | 115 MessageLoop message_loop_; |
119 BrowserThread db_thread_; | 116 BrowserThread db_thread_; |
120 MockAutofillProfileSyncableService autofill_syncable_service_; | 117 MockAutofillProfileSyncableService autofill_syncable_service_; |
121 MockSyncChangeProcessor sync_processor_; | 118 scoped_ptr<MockSyncChangeProcessor> sync_processor_; |
122 }; | 119 }; |
123 | 120 |
124 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { | 121 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { |
125 std::vector<AutofillProfile *> profiles_from_web_db; | 122 std::vector<AutofillProfile *> profiles_from_web_db; |
126 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | 123 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
127 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 124 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
128 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; | 125 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; |
129 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; | 126 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; |
130 | 127 |
131 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | 128 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
(...skipping 24 matching lines...) Expand all Loading... |
156 expected_bundle.profiles_to_add.push_back(&profile2); | 153 expected_bundle.profiles_to_add.push_back(&profile2); |
157 expected_bundle.profiles_to_update.push_back(&profile3); | 154 expected_bundle.profiles_to_update.push_back(&profile3); |
158 | 155 |
159 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 156 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
160 .Times(1) | 157 .Times(1) |
161 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 158 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
162 EXPECT_CALL(autofill_syncable_service_, | 159 EXPECT_CALL(autofill_syncable_service_, |
163 SaveChangesToWebData(DataBundleCheck(expected_bundle))) | 160 SaveChangesToWebData(DataBundleCheck(expected_bundle))) |
164 .Times(1) | 161 .Times(1) |
165 .WillOnce(Return(true)); | 162 .WillOnce(Return(true)); |
166 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | 163 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
167 .WillByDefault(Return(SyncError())); | 164 .WillByDefault(Return(SyncError())); |
168 EXPECT_CALL(sync_processor_, | 165 EXPECT_CALL(*sync_processor_, |
169 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) | 166 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) |
170 .Times(1) | 167 .Times(1) |
171 .WillOnce(Return(SyncError())); | 168 .WillOnce(Return(SyncError())); |
172 | 169 |
| 170 // Takes ownership of sync_processor_. |
173 autofill_syncable_service_.MergeDataAndStartSyncing( | 171 autofill_syncable_service_.MergeDataAndStartSyncing( |
174 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); | 172 syncable::AUTOFILL_PROFILE, data_list, sync_processor_.get()); |
175 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); | 173 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); |
176 } | 174 } |
177 | 175 |
178 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { | 176 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { |
179 std::vector<AutofillProfile *> profiles_from_web_db; | 177 std::vector<AutofillProfile *> profiles_from_web_db; |
180 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | 178 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
181 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 179 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
182 | 180 |
183 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | 181 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
184 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | 182 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
185 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); | 183 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
186 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 184 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
187 | 185 |
188 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 186 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
189 .Times(1) | 187 .Times(1) |
190 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 188 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
191 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) | 189 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) |
192 .Times(1) | 190 .Times(1) |
193 .WillOnce(Return(true)); | 191 .WillOnce(Return(true)); |
194 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | 192 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
195 .WillByDefault(Return(SyncError())); | 193 .WillByDefault(Return(SyncError())); |
196 EXPECT_CALL(sync_processor_, | 194 EXPECT_CALL(*sync_processor_, |
197 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) | 195 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) |
198 .Times(1) | 196 .Times(1) |
199 .WillOnce(Return(SyncError())); | 197 .WillOnce(Return(SyncError())); |
200 | 198 |
201 SyncDataList data_list; | 199 SyncDataList data_list; |
| 200 // Takes ownership of sync_processor_. |
202 autofill_syncable_service_.MergeDataAndStartSyncing( | 201 autofill_syncable_service_.MergeDataAndStartSyncing( |
203 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); | 202 syncable::AUTOFILL_PROFILE, data_list, sync_processor_.get()); |
204 | 203 |
205 SyncDataList data = | 204 SyncDataList data = |
206 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); | 205 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); |
207 | 206 |
208 EXPECT_EQ(2U, data.size()); | 207 EXPECT_EQ(2U, data.size()); |
209 EXPECT_EQ(guid_present1, data.front().GetSpecifics() | 208 EXPECT_EQ(guid_present1, data.front().GetSpecifics() |
210 .GetExtension(sync_pb::autofill_profile).guid()); | 209 .GetExtension(sync_pb::autofill_profile).guid()); |
211 EXPECT_EQ(guid_present2, data.back().GetSpecifics() | 210 EXPECT_EQ(guid_present2, data.back().GetSpecifics() |
212 .GetExtension(sync_pb::autofill_profile).guid()); | 211 .GetExtension(sync_pb::autofill_profile).guid()); |
213 } | 212 } |
(...skipping 16 matching lines...) Expand all Loading... |
230 | 229 |
231 AutofillProfileSyncableService::DataBundle expected_bundle; | 230 AutofillProfileSyncableService::DataBundle expected_bundle; |
232 expected_bundle.profiles_to_delete.push_back(guid_present); | 231 expected_bundle.profiles_to_delete.push_back(guid_present); |
233 expected_bundle.profiles_to_add.push_back(&profile); | 232 expected_bundle.profiles_to_add.push_back(&profile); |
234 | 233 |
235 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( | 234 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( |
236 DataBundleCheck(expected_bundle))) | 235 DataBundleCheck(expected_bundle))) |
237 .Times(1) | 236 .Times(1) |
238 .WillOnce(Return(true)); | 237 .WillOnce(Return(true)); |
239 | 238 |
240 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | 239 autofill_syncable_service_.set_sync_processor(sync_processor_.get()); |
241 &autofill_syncable_service_, &sync_processor_); | |
242 SyncError error = autofill_syncable_service_.ProcessSyncChanges( | 240 SyncError error = autofill_syncable_service_.ProcessSyncChanges( |
243 FROM_HERE, change_list); | 241 FROM_HERE, change_list); |
244 | 242 |
245 EXPECT_FALSE(error.IsSet()); | 243 EXPECT_FALSE(error.IsSet()); |
246 } | 244 } |
247 | 245 |
248 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { | 246 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { |
249 std::string guid1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | 247 std::string guid1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
250 std::string guid2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 248 std::string guid2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
251 | 249 |
252 AutofillProfile profile(guid1); | 250 AutofillProfile profile(guid1); |
253 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 251 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
254 AutofillProfileChange change1(AutofillProfileChange::ADD, guid1, &profile); | 252 AutofillProfileChange change1(AutofillProfileChange::ADD, guid1, &profile); |
255 AutofillProfileChange change2(AutofillProfileChange::REMOVE, guid2, NULL); | 253 AutofillProfileChange change2(AutofillProfileChange::REMOVE, guid2, NULL); |
256 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | 254 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
257 .WillByDefault(Return(SyncError(FROM_HERE, std::string("an error"), | 255 .WillByDefault(Return(SyncError(FROM_HERE, std::string("an error"), |
258 syncable::AUTOFILL_PROFILE))); | 256 syncable::AUTOFILL_PROFILE))); |
259 | 257 |
260 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | 258 autofill_syncable_service_.set_sync_processor(sync_processor_.get()); |
261 &autofill_syncable_service_, &sync_processor_); | |
262 autofill_syncable_service_.ActOnChange(change1); | 259 autofill_syncable_service_.ActOnChange(change1); |
263 autofill_syncable_service_.ActOnChange(change2); | 260 autofill_syncable_service_.ActOnChange(change2); |
264 } | 261 } |
265 | 262 |
266 } // namespace browser_sync | 263 } // namespace browser_sync |
OLD | NEW |