OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/utf_string_conversions.h" |
| 6 #include "chrome/browser/sync/engine/read_node_mock.h" |
| 7 #include "chrome/browser/sync/glue/autofill_profile_model_associator.h" |
| 8 #include "chrome/browser/sync/syncable/syncable.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using ::testing::_; |
| 13 using ::testing::Return; |
| 14 using ::testing::DoDefault; |
| 15 using ::testing::ReturnRef; |
| 16 using ::testing::Pointee; |
| 17 using ::testing::Ref; |
| 18 using ::testing::Invoke; |
| 19 class AutoFillProfile; |
| 20 |
| 21 using browser_sync::AutofillProfileModelAssociator; |
| 22 |
| 23 using sync_api::WriteTransaction; |
| 24 using syncable::Directory; |
| 25 using syncable::EntryKernel; |
| 26 |
| 27 class MockDirectory : public Directory { |
| 28 public: |
| 29 MockDirectory() { |
| 30 init_kernel("myk"); |
| 31 } |
| 32 MOCK_METHOD1(GetEntryById, syncable::EntryKernel* (int64 id)); |
| 33 MOCK_METHOD1(GetEntryByClientTag, |
| 34 syncable::EntryKernel* (const std::string&)); |
| 35 }; |
| 36 |
| 37 class MockSyncableWriteTransaction : public syncable::WriteTransaction { |
| 38 public: |
| 39 MockSyncableWriteTransaction(Directory *directory) : |
| 40 WriteTransaction(directory, syncable::SYNCER, "dontcare.cpp", 25) { |
| 41 } |
| 42 }; |
| 43 |
| 44 class MockWriteTransaction : public sync_api::WriteTransaction { |
| 45 public: |
| 46 MockWriteTransaction(Directory* directory) : sync_api::WriteTransaction() { |
| 47 this->transaction_ = new MockSyncableWriteTransaction(directory); |
| 48 } |
| 49 }; |
| 50 |
| 51 |
| 52 // Note this is not a generic mock. This is a mock used to |
| 53 // test AutofillProfileModelAssociator class itself by mocking |
| 54 // other functions that are called by the functions we need to test. |
| 55 class MockAutofillProfileModelAssociator : |
| 56 public AutofillProfileModelAssociator |
| 57 { |
| 58 public: |
| 59 MockAutofillProfileModelAssociator() { |
| 60 ON_CALL(*this, EnsureOnThread(_)) |
| 61 .WillByDefault(Return(true)); |
| 62 |
| 63 dontCheckThreadOnExit_ = true; |
| 64 } |
| 65 virtual ~MockAutofillProfileModelAssociator() {} |
| 66 bool TraverseAndAssociateChromeAutoFillProfilesWrapper( |
| 67 sync_api::WriteTransaction* write_trans, |
| 68 const sync_api::ReadNode& autofill_root, |
| 69 const std::vector<AutoFillProfile*>& all_profiles_from_db, |
| 70 std::set<std::string>* current_profiles, |
| 71 std::vector<AutoFillProfile*>* updated_profiles) |
| 72 { |
| 73 return TraverseAndAssociateChromeAutoFillProfiles( |
| 74 write_trans, |
| 75 autofill_root, |
| 76 all_profiles_from_db, |
| 77 current_profiles, |
| 78 updated_profiles); |
| 79 } |
| 80 MOCK_METHOD3(AddNativeProfileIfNeeded, |
| 81 void(const sync_pb::AutofillProfile2Specifics& profile, |
| 82 DataBundle* bundle, |
| 83 const sync_api::ReadNode& node)); |
| 84 MOCK_METHOD2(OverwriteProfileWithServerData, |
| 85 bool(AutoFillProfile* merge_into, |
| 86 const sync_pb::AutofillProfile2Specifics& specifics)); |
| 87 MOCK_METHOD4(MakeNewAutofillProfileSyncNode, |
| 88 bool( sync_api::WriteTransaction* trans, |
| 89 const sync_api::BaseNode& autofill_root, |
| 90 const AutoFillProfile& profile, |
| 91 int64* sync_id)); |
| 92 MOCK_METHOD2(Associate, void(const std::string* autofill, int64 id)); |
| 93 MOCK_METHOD1(EnsureOnThread, bool(BrowserThread::ID)); |
| 94 |
| 95 bool TraverseAndAssociateAllSyncNodesWrapper( |
| 96 sync_api::WriteTransaction *trans, |
| 97 const sync_api::ReadNode &autofill_root, |
| 98 browser_sync::AutofillProfileModelAssociator::DataBundle *bundle) |
| 99 { |
| 100 return TraverseAndAssociateAllSyncNodes(trans, autofill_root, bundle); |
| 101 } |
| 102 |
| 103 void AddNativeProfileIfNeededWrapper( |
| 104 const sync_pb::AutofillProfile2Specifics& profile, |
| 105 DataBundle* bundle, |
| 106 const sync_api::ReadNode& node) |
| 107 { |
| 108 AutofillProfileModelAssociator::AddNativeProfileIfNeeded( |
| 109 profile, |
| 110 bundle, |
| 111 node); |
| 112 } |
| 113 }; |
| 114 |
| 115 bool SetSyncId(sync_api::WriteTransaction* trans, |
| 116 const sync_api::BaseNode& autofill_root, |
| 117 const AutoFillProfile& profile, |
| 118 int64* sync_id) |
| 119 { |
| 120 *sync_id = (int64)1; |
| 121 return true; |
| 122 } |
| 123 |
| 124 TEST(AutofillProfile2Test, TestAssociateProfileInWebDBWithSyncDB) |
| 125 { |
| 126 MockAutofillProfileModelAssociator associator; |
| 127 ScopedVector<AutoFillProfile> profiles_from_web_db; |
| 128 std::string guid = "abc"; |
| 129 |
| 130 sync_pb::EntitySpecifics specifics; |
| 131 MockDirectory mock_directory; |
| 132 sync_pb::AutofillProfile2Specifics *profile_specifics = |
| 133 specifics.MutableExtension(sync_pb::autofill_profile); |
| 134 |
| 135 profile_specifics->set_guid(guid); |
| 136 |
| 137 std::set<std::string> current_profiles; |
| 138 AutoFillProfile *profile = new AutoFillProfile(guid); |
| 139 |
| 140 // Set up the entry kernel with what we want. |
| 141 EntryKernel kernel; |
| 142 kernel.put(syncable::SPECIFICS, specifics); |
| 143 kernel.put(syncable::META_HANDLE, (int64)1); |
| 144 |
| 145 MockWriteTransaction write_trans(&mock_directory); |
| 146 EXPECT_CALL(mock_directory, |
| 147 GetEntryByClientTag(_)) |
| 148 .WillOnce(Return(&kernel)); |
| 149 |
| 150 sync_api::ReadNode read_node(&write_trans); |
| 151 |
| 152 EXPECT_CALL(associator, Associate(Pointee(guid), (int64)1)); |
| 153 |
| 154 profiles_from_web_db.push_back(profile); |
| 155 |
| 156 associator.TraverseAndAssociateChromeAutoFillProfilesWrapper(&write_trans, |
| 157 read_node, |
| 158 profiles_from_web_db.get(), |
| 159 ¤t_profiles, |
| 160 NULL); |
| 161 |
| 162 EXPECT_EQ(1, current_profiles.size()); |
| 163 } |
| 164 |
| 165 TEST(AutofillProfile2Test, TestAssociatingMissingWebDBProfile) |
| 166 { |
| 167 MockAutofillProfileModelAssociator associator; |
| 168 ScopedVector<AutoFillProfile> profiles_from_web_db; |
| 169 sync_pb::AutofillProfile2Specifics profile_specifics; |
| 170 |
| 171 // Will be released inside the function |
| 172 // TraverseAndAssociateChromeAutoFillProfilesWrapper |
| 173 ReadNodeMock *read_node = new ReadNodeMock(); |
| 174 std::string guid = "abc"; |
| 175 std::set<std::string> current_profiles; |
| 176 AutoFillProfile *profile = new AutoFillProfile(guid); |
| 177 |
| 178 profile_specifics.set_guid(guid); |
| 179 |
| 180 // EXPECT_CALL(associator, GetReadNode(_)) |
| 181 // .WillOnce(Return(read_node)); |
| 182 |
| 183 EXPECT_CALL(*read_node, |
| 184 InitByClientTagLookup(syncable::AUTOFILL_PROFILE, guid)) |
| 185 .WillOnce(Return(false)); |
| 186 |
| 187 EXPECT_CALL(associator, |
| 188 MakeNewAutofillProfileSyncNode(NULL, |
| 189 Ref(*read_node), |
| 190 Ref(*profile), _)) |
| 191 .WillOnce(Invoke(SetSyncId)); |
| 192 |
| 193 EXPECT_CALL(associator, Associate(Pointee(guid), (int64)1)); |
| 194 |
| 195 profiles_from_web_db.push_back(profile); |
| 196 |
| 197 associator.TraverseAndAssociateChromeAutoFillProfilesWrapper(NULL, |
| 198 *read_node, |
| 199 profiles_from_web_db.get(), |
| 200 ¤t_profiles, |
| 201 NULL); |
| 202 |
| 203 EXPECT_EQ(1, current_profiles.size()); |
| 204 } |
| 205 |
| 206 TEST(AutofillProfile2Test, TestAssociateProfileInSyncDBWithWebDB) |
| 207 { |
| 208 MockAutofillProfileModelAssociator associator; |
| 209 ReadNodeMock *read_node = new ReadNodeMock(); |
| 210 ReadNodeMock autofill_root; |
| 211 sync_pb::AutofillProfile2Specifics profile_specifics; |
| 212 |
| 213 browser_sync::AutofillProfileModelAssociator::DataBundle bundle; |
| 214 |
| 215 EXPECT_CALL(autofill_root, GetFirstChildId()) |
| 216 .WillOnce(Return((int64)1)); |
| 217 |
| 218 // EXPECT_CALL(associator, GetReadNode(_)) |
| 219 // .WillOnce(Return(read_node)); |
| 220 |
| 221 EXPECT_CALL(*read_node, InitByIdLookup((int64)1)) |
| 222 .WillOnce(Return(true)); |
| 223 |
| 224 EXPECT_CALL(*read_node, GetAutofillProfileSpecifics()) |
| 225 .WillOnce(ReturnRef(profile_specifics)); |
| 226 |
| 227 EXPECT_CALL(associator, |
| 228 AddNativeProfileIfNeeded(Ref(profile_specifics), |
| 229 &bundle, |
| 230 Ref(*read_node))); |
| 231 |
| 232 EXPECT_CALL(*read_node, GetSuccessorId()) |
| 233 .WillOnce(Return(sync_api::kInvalidId)); |
| 234 |
| 235 associator.TraverseAndAssociateAllSyncNodesWrapper(NULL, |
| 236 autofill_root, |
| 237 &bundle); |
| 238 } |
| 239 |
| 240 TEST(AutofillProfile2Test, TestDontNeedToAddNativeProfile) |
| 241 { |
| 242 MockAutofillProfileModelAssociator associator; |
| 243 sync_pb::AutofillProfile2Specifics profile_specifics; |
| 244 ReadNodeMock read_node; |
| 245 std::string guid = "abc"; |
| 246 std::set<std::string> current_profiles; |
| 247 AutoFillProfile *profile = new AutoFillProfile(guid); |
| 248 browser_sync::AutofillProfileModelAssociator::DataBundle bundle; |
| 249 |
| 250 bundle.current_profiles.insert(guid); |
| 251 |
| 252 // We have no expectations to set. |
| 253 // If the profile is already present no other function |
| 254 // should be called. |
| 255 associator.AddNativeProfileIfNeededWrapper(profile_specifics, &bundle, read_no
de); |
| 256 } |
| 257 |
| 258 TEST(AutofillProfile2Test, TestNeedToAddNativeProfile) |
| 259 { |
| 260 MockAutofillProfileModelAssociator associator; |
| 261 sync_pb::AutofillProfile2Specifics profile_specifics; |
| 262 ReadNodeMock read_node; |
| 263 std::string guid = "abc"; |
| 264 std::set<std::string> current_profiles; |
| 265 AutoFillProfile *profile = new AutoFillProfile(guid); |
| 266 browser_sync::AutofillProfileModelAssociator::DataBundle bundle; |
| 267 std::string first_name = "lingesh"; |
| 268 |
| 269 profile_specifics.set_guid(guid); |
| 270 profile_specifics.set_name_first(first_name); |
| 271 |
| 272 EXPECT_CALL(read_node, GetId()) |
| 273 .WillOnce(Return((int64)1)); |
| 274 |
| 275 EXPECT_CALL(associator, Associate(Pointee(guid), (int64)1)); |
| 276 |
| 277 associator.AddNativeProfileIfNeededWrapper(profile_specifics, &bundle, read_no
de); |
| 278 |
| 279 EXPECT_EQ(bundle.new_profiles.size(), 1); |
| 280 EXPECT_EQ( |
| 281 bundle.new_profiles.front()->GetFieldText(AutoFillType(NAME_FIRST)), |
| 282 UTF8ToUTF16(first_name)); |
| 283 } |
OLD | NEW |