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/internal_api/read_node_mock.h" | |
8 #include "chrome/browser/sync/internal_api/syncapi_mock.h" | |
9 #include "chrome/browser/sync/glue/autofill_profile_model_associator.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::Return; | |
17 using ::testing::DoDefault; | |
18 using ::testing::ReturnRef; | |
19 using ::testing::Pointee; | |
20 using ::testing::Ref; | |
21 using ::testing::Invoke; | |
22 class AutofillProfile; | |
23 | |
24 using browser_sync::AutofillProfileModelAssociator; | |
25 | |
26 // Note this is not a generic mock. This is a mock used to | |
27 // test AutofillProfileModelAssociator class itself by mocking | |
28 // other functions that are called by the functions we need to test. | |
29 class MockAutofillProfileModelAssociator | |
30 : public AutofillProfileModelAssociator { | |
31 public: | |
32 MockAutofillProfileModelAssociator() { | |
33 } | |
34 virtual ~MockAutofillProfileModelAssociator() {} | |
35 bool TraverseAndAssociateChromeAutofillProfilesWrapper( | |
36 sync_api::WriteTransaction* write_trans, | |
37 const sync_api::ReadNode& autofill_root, | |
38 const std::vector<AutofillProfile*>& all_profiles_from_db, | |
39 std::set<std::string>* current_profiles, | |
40 std::vector<AutofillProfile*>* updated_profiles, | |
41 std::vector<AutofillProfile*>* new_profiles, | |
42 std::vector<std::string>* profiles_to_delete) { | |
43 return TraverseAndAssociateChromeAutofillProfiles(write_trans, | |
44 autofill_root, | |
45 all_profiles_from_db, | |
46 current_profiles, | |
47 updated_profiles, | |
48 new_profiles, | |
49 profiles_to_delete); | |
50 } | |
51 MOCK_METHOD3(AddNativeProfileIfNeeded, | |
52 void(const sync_pb::AutofillProfileSpecifics&, | |
53 DataBundle*, | |
54 const sync_api::ReadNode&)); | |
55 MOCK_METHOD2(OverwriteProfileWithServerData, | |
56 bool(AutofillProfile*, | |
57 const sync_pb::AutofillProfileSpecifics&)); | |
58 MOCK_METHOD6(MakeNewAutofillProfileSyncNodeIfNeeded, | |
59 bool(sync_api::WriteTransaction*, | |
60 const sync_api::BaseNode&, | |
61 const AutofillProfile&, | |
62 std::vector<AutofillProfile*>*, | |
63 std::set<std::string>*, | |
64 std::vector<std::string>*)); | |
65 MOCK_METHOD2(Associate, void(const std::string*, int64)); | |
66 | |
67 bool TraverseAndAssociateAllSyncNodesWrapper( | |
68 sync_api::WriteTransaction *trans, | |
69 const sync_api::ReadNode &autofill_root, | |
70 browser_sync::AutofillProfileModelAssociator::DataBundle *bundle) { | |
71 return TraverseAndAssociateAllSyncNodes(trans, autofill_root, bundle); | |
72 } | |
73 | |
74 void AddNativeProfileIfNeededWrapper( | |
75 const sync_pb::AutofillProfileSpecifics& profile, | |
76 DataBundle* bundle, | |
77 const sync_api::ReadNode& node) { | |
78 AutofillProfileModelAssociator::AddNativeProfileIfNeeded( | |
79 profile, | |
80 bundle, | |
81 node); | |
82 } | |
83 }; | |
84 | |
85 class AutofillProfileModelAssociatorTest : public testing::Test { | |
86 public: | |
87 AutofillProfileModelAssociatorTest() | |
88 : db_thread_(BrowserThread::DB, &message_loop_) {} | |
89 | |
90 protected: | |
91 MessageLoop message_loop_; | |
92 BrowserThread db_thread_; | |
93 MockAutofillProfileModelAssociator associator_; | |
94 }; | |
95 | |
96 TEST_F(AutofillProfileModelAssociatorTest, | |
97 TestAssociateProfileInWebDBWithSyncDB) { | |
98 ScopedVector<AutofillProfile> profiles_from_web_db; | |
99 std::string guid = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | |
100 | |
101 sync_pb::EntitySpecifics specifics; | |
102 MockDirectory mock_directory; | |
103 sync_pb::AutofillProfileSpecifics *profile_specifics = | |
104 specifics.MutableExtension(sync_pb::autofill_profile); | |
105 | |
106 profile_specifics->set_guid(guid); | |
107 | |
108 std::set<std::string> current_profiles; | |
109 | |
110 // This will be released inside the function | |
111 // TraverseAndAssociateChromeAutofillProfiles | |
112 AutofillProfile *profile = new AutofillProfile(guid); | |
113 | |
114 // Set up the entry kernel with what we want. | |
115 EntryKernel kernel; | |
116 kernel.put(syncable::SPECIFICS, specifics); | |
117 kernel.put(syncable::META_HANDLE, 1); | |
118 | |
119 MockWriteTransaction write_trans(FROM_HERE, &mock_directory); | |
120 EXPECT_CALL(mock_directory, GetEntryByClientTag(_)) | |
121 .WillOnce(Return(&kernel)); | |
122 | |
123 sync_api::ReadNode read_node(&write_trans); | |
124 | |
125 EXPECT_CALL(associator_, Associate(Pointee(guid), 1)); | |
126 | |
127 profiles_from_web_db.push_back(profile); | |
128 | |
129 associator_.TraverseAndAssociateChromeAutofillProfilesWrapper(&write_trans, | |
130 read_node, | |
131 profiles_from_web_db.get(), | |
132 ¤t_profiles, | |
133 NULL, | |
134 NULL, | |
135 NULL); | |
136 | |
137 EXPECT_EQ((unsigned int)1, current_profiles.size()); | |
138 } | |
139 | |
140 TEST_F(AutofillProfileModelAssociatorTest, TestAssociatingMissingWebDBProfile) { | |
141 ScopedVector<AutofillProfile> profiles_from_web_db; | |
142 MockDirectory mock_directory; | |
143 | |
144 MockWriteTransaction write_trans(FROM_HERE, &mock_directory); | |
145 EXPECT_CALL(mock_directory, | |
146 GetEntryByClientTag(_)) | |
147 .WillOnce(Return(reinterpret_cast<EntryKernel*>(NULL))); | |
148 | |
149 sync_api::ReadNode autofill_root(&write_trans); | |
150 | |
151 std::string guid = "EDC609ED-7EEE-4F27-B00C-423242A9C44A"; | |
152 std::set<std::string> current_profiles; | |
153 AutofillProfile *profile = new AutofillProfile(guid); | |
154 | |
155 EXPECT_CALL(associator_, | |
156 MakeNewAutofillProfileSyncNodeIfNeeded(&write_trans, | |
157 Ref(autofill_root), | |
158 Ref(*profile), | |
159 _, | |
160 ¤t_profiles, | |
161 NULL)) | |
162 .WillOnce(Return(true)); | |
163 | |
164 profiles_from_web_db.push_back(profile); | |
165 | |
166 associator_.TraverseAndAssociateChromeAutofillProfilesWrapper(&write_trans, | |
167 autofill_root, | |
168 profiles_from_web_db.get(), | |
169 ¤t_profiles, | |
170 NULL, | |
171 NULL, | |
172 NULL); | |
173 } | |
174 | |
175 TEST_F(AutofillProfileModelAssociatorTest, | |
176 TestAssociateProfileInSyncDBWithWebDB) { | |
177 ReadNodeMock autofill_root; | |
178 | |
179 // The constrcutor itself will initialize the id to root. | |
180 syncable::Id root_id; | |
181 | |
182 sync_pb::EntitySpecifics specifics; | |
183 MockDirectory mock_directory; | |
184 sync_pb::AutofillProfileSpecifics *profile_specifics = | |
185 specifics.MutableExtension(sync_pb::autofill_profile); | |
186 | |
187 profile_specifics->set_guid("abc"); | |
188 | |
189 // Set up the entry kernel with what we want. | |
190 EntryKernel kernel; | |
191 kernel.put(syncable::SPECIFICS, specifics); | |
192 kernel.put(syncable::META_HANDLE, 1); | |
193 kernel.put(syncable::ID, root_id); | |
194 | |
195 MockWriteTransaction write_trans(FROM_HERE, &mock_directory); | |
196 | |
197 browser_sync::AutofillProfileModelAssociator::DataBundle bundle; | |
198 | |
199 EXPECT_CALL(autofill_root, GetFirstChildId()) | |
200 .WillOnce(Return(1)); | |
201 | |
202 EXPECT_CALL(mock_directory, | |
203 GetEntryByHandle(_)) | |
204 .WillOnce(Return(&kernel)); | |
205 | |
206 EXPECT_CALL(associator_, | |
207 AddNativeProfileIfNeeded(_, | |
208 &bundle, | |
209 _)); | |
210 | |
211 associator_.TraverseAndAssociateAllSyncNodesWrapper(&write_trans, | |
212 autofill_root, | |
213 &bundle); | |
214 } | |
215 | |
216 TEST_F(AutofillProfileModelAssociatorTest, TestDontNeedToAddNativeProfile) { | |
217 ::testing::StrictMock<MockAutofillProfileModelAssociator> associator; | |
218 sync_pb::AutofillProfileSpecifics profile_specifics; | |
219 ReadNodeMock read_node; | |
220 std::string guid = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | |
221 std::set<std::string> current_profiles; | |
222 browser_sync::AutofillProfileModelAssociator::DataBundle bundle; | |
223 | |
224 profile_specifics.set_guid(guid); | |
225 | |
226 bundle.current_profiles.insert(guid); | |
227 | |
228 // We have no expectations to set. We have used a strict mock. | |
229 // If the profile is already present no other function | |
230 // should be called. | |
231 associator.AddNativeProfileIfNeededWrapper(profile_specifics, &bundle, | |
232 read_node); | |
233 } | |
234 | |
235 TEST_F(AutofillProfileModelAssociatorTest, TestNeedToAddNativeProfile) { | |
236 sync_pb::AutofillProfileSpecifics profile_specifics; | |
237 ReadNodeMock read_node; | |
238 std::string guid = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; | |
239 std::set<std::string> current_profiles; | |
240 browser_sync::AutofillProfileModelAssociator::DataBundle bundle; | |
241 std::string first_name = "lingesh"; | |
242 | |
243 profile_specifics.set_guid(guid); | |
244 profile_specifics.set_name_first(first_name); | |
245 | |
246 EXPECT_CALL(read_node, GetId()) | |
247 .WillOnce(Return(1)); | |
248 | |
249 EXPECT_CALL(associator_, | |
250 Associate(Pointee(guid), 1)); | |
251 | |
252 associator_.AddNativeProfileIfNeededWrapper( | |
253 profile_specifics, | |
254 &bundle, | |
255 read_node); | |
256 | |
257 EXPECT_EQ(bundle.new_profiles.size(), (unsigned int)1); | |
258 EXPECT_EQ(bundle.new_profiles.front()->GetInfo(NAME_FIRST), | |
259 UTF8ToUTF16(first_name)); | |
260 } | |
261 | |
OLD | NEW |