|
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_P3(DataBundleCheck, n_delete, n_update, n_new, "") { | |
57 return (arg.profiles_to_delete.size() == n_delete) && | |
58 (arg.updated_profiles.size() == n_update) && | |
59 (arg.new_profiles.size() == n_new); | |
60 } | |
61 | |
62 class MockSyncChangeProcessor : public SyncChangeProcessor { | |
63 public: | |
64 MockSyncChangeProcessor() {} | |
65 virtual ~MockSyncChangeProcessor() {} | |
66 | |
67 MOCK_METHOD2(ProcessSyncChanges, | |
68 SyncError(const tracked_objects::Location&, | |
69 const SyncChangeList&)); | |
70 }; | |
71 | |
72 class AutofillProfileSyncableServiceTest : public testing::Test { | |
73 public: | |
74 AutofillProfileSyncableServiceTest() | |
75 : db_thread_(BrowserThread::DB, &message_loop_) {} | |
76 | |
77 protected: | |
78 MessageLoop message_loop_; | |
79 BrowserThread db_thread_; | |
80 MockAutofillProfileSyncableService autofill_syncable_service_; | |
81 MockSyncChangeProcessor sync_processor_; | |
82 }; | |
83 | |
84 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { | |
85 std::vector<AutofillProfile *> profiles_from_web_db; | |
86 std::string guid_present = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | |
87 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | |
dhollowa
2011/09/07 21:10:24
It would be great to verify the four possible case
GeorgeY
2011/09/07 23:26:24
1) Sure, added.
2) It is not received here. It is
| |
88 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; | |
89 | |
90 profiles_from_web_db.push_back(new AutofillProfile(guid_present)); | |
91 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | |
92 | |
93 SyncDataList data_list; | |
94 AutofillProfile profile1(guid_synced1); | |
95 profile1.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | |
96 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); | |
97 AutofillProfile profile2(guid_synced2); | |
98 profile2.SetInfo(NAME_FIRST, UTF8ToUTF16("Harry")); | |
99 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); | |
100 | |
101 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | |
102 .Times(1) | |
103 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | |
104 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) | |
105 .Times(1) | |
106 .WillOnce(Return(true)); | |
107 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | |
108 .WillByDefault(Return(SyncError())); | |
109 EXPECT_CALL(sync_processor_, | |
dhollowa
2011/09/07 21:10:24
Is there a way to verify that this is called with
GeorgeY
2011/09/07 23:26:24
added
| |
110 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(1U)))) | |
111 .Times(1) | |
112 .WillOnce(Return(SyncError())); | |
113 | |
114 autofill_syncable_service_.MergeDataAndStartSyncing( | |
115 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); | |
116 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); | |
117 } | |
118 | |
119 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { | |
120 std::vector<AutofillProfile *> profiles_from_web_db; | |
121 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | |
122 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | |
123 | |
124 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | |
125 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | |
126 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); | |
127 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | |
128 | |
129 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | |
130 .Times(1) | |
131 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | |
132 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) | |
133 .Times(1) | |
134 .WillOnce(Return(true)); | |
135 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | |
136 .WillByDefault(Return(SyncError())); | |
137 EXPECT_CALL(sync_processor_, | |
138 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) | |
139 .Times(1) | |
140 .WillOnce(Return(SyncError())); | |
141 | |
142 SyncDataList data_list; | |
143 autofill_syncable_service_.MergeDataAndStartSyncing( | |
144 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); | |
145 | |
146 SyncDataList data = | |
147 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); | |
148 | |
149 EXPECT_EQ(2U, data.size()); | |
150 EXPECT_EQ(guid_present1, data.front().GetSpecifics() | |
151 .GetExtension(sync_pb::autofill_profile).guid()); | |
152 EXPECT_EQ(guid_present2, data.back().GetSpecifics() | |
153 .GetExtension(sync_pb::autofill_profile).guid()); | |
154 } | |
155 | |
156 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { | |
157 std::vector<AutofillProfile *> profiles_from_web_db; | |
158 std::string guid_present = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | |
159 std::string guid_synced = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | |
160 | |
161 SyncChangeList change_list; | |
162 AutofillProfile profile(guid_synced); | |
163 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | |
164 change_list.push_back( | |
165 SyncChange(SyncChange::ACTION_ADD, | |
166 AutofillProfileSyncableService::CreateData(profile))); | |
167 AutofillProfile empty_profile(guid_present); | |
168 change_list.push_back( | |
169 SyncChange(SyncChange::ACTION_DELETE, | |
170 AutofillProfileSyncableService::CreateData(empty_profile))); | |
171 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( | |
172 DataBundleCheck(1U, 0U, 1U))) | |
173 .Times(1) | |
174 .WillOnce(Return(true)); | |
175 | |
176 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | |
177 &autofill_syncable_service_, &sync_processor_); | |
178 SyncError error = autofill_syncable_service_.ProcessSyncChanges( | |
179 FROM_HERE, change_list); | |
180 | |
181 EXPECT_FALSE(error.IsSet()); | |
182 } | |
183 | |
184 } // namespace browser_sync | |
185 | |
OLD | NEW |