Chromium Code Reviews
|
| 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 { | |
|
dhollowa
2011/09/01 23:20:29
nit: indent 4 spaces
| |
| 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 throught the | |
|
dhollowa
2011/09/01 23:20:29
s/throught/through/
| |
| 37 // MergeDataAndStartSyncing(). | |
| 38 class AutoSetSyncProcessor { | |
| 39 public: | |
| 40 AutoSetSyncProcessor(MockAutofillProfileSyncableService *service, | |
|
dhollowa
2011/09/01 20:17:49
nit: s/ */* /
GeorgeY
2011/09/02 04:34:12
Done.
| |
| 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_; | |
|
dhollowa
2011/09/01 20:17:49
nit: s/ */* /
GeorgeY
2011/09/02 04:34:12
Done.
| |
| 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))); | |
|
lipalani1
2011/09/01 05:06:02
Dont you want to verify the data that is being sen
GeorgeY
2011/09/02 04:34:12
CopyData(&profiles_from_web_db) puts data into the
| |
| 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 | |
| 134 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | |
| 135 &autofill_syncable_service_, &sync_processor_); | |
| 136 | |
| 137 SyncDataList data = | |
| 138 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); | |
| 139 | |
| 140 EXPECT_EQ(2U, data.size()); | |
| 141 EXPECT_EQ(guid_present1, data.front().GetSpecifics() | |
| 142 .GetExtension(sync_pb::autofill_profile).guid()); | |
| 143 EXPECT_EQ(guid_present2, data.back().GetSpecifics() | |
| 144 .GetExtension(sync_pb::autofill_profile).guid()); | |
| 145 } | |
| 146 | |
| 147 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { | |
| 148 std::vector<AutofillProfile *> profiles_from_web_db; | |
| 149 std::string guid_present = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | |
| 150 std::string guid_synced = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | |
| 151 | |
| 152 profiles_from_web_db.push_back(new AutofillProfile(guid_present)); | |
| 153 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | |
| 154 | |
| 155 SyncChangeList change_list; | |
| 156 AutofillProfile profile(guid_synced); | |
| 157 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | |
| 158 change_list.push_back( | |
| 159 autofill_syncable_service_.CreateChange(SyncChange::ACTION_ADD, profile)); | |
| 160 change_list.push_back( | |
| 161 autofill_syncable_service_.CreateDeleteChange(guid_present)); | |
| 162 | |
| 163 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | |
| 164 .Times(1) | |
| 165 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | |
| 166 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( | |
| 167 DataBundleCheck(1U, 0U, 1U))) | |
| 168 .Times(1) | |
| 169 .WillOnce(Return(true)); | |
| 170 | |
| 171 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | |
| 172 &autofill_syncable_service_, &sync_processor_); | |
| 173 SyncError error = autofill_syncable_service_.ProcessSyncChanges( | |
| 174 FROM_HERE, change_list); | |
| 175 | |
| 176 EXPECT_FALSE(error.IsSet()); | |
| 177 } | |
| 178 | |
| 179 } // namespace browser_sync | |
| 180 | |
|
dhollowa
2011/09/01 20:17:49
nit: remove extra blank line.
GeorgeY
2011/09/02 04:34:12
Done. Either this, or Lint complains :)
| |
| 181 | |
| OLD | NEW |