Index: chrome/browser/sync/glue/autofill_profile_model_associator_unittest.cc |
diff --git a/chrome/browser/sync/glue/autofill_profile_model_associator_unittest.cc b/chrome/browser/sync/glue/autofill_profile_model_associator_unittest.cc |
new file mode 100755 |
index 0000000000000000000000000000000000000000..c5089f12e30c72cdf14b9acc760c738ac2cb1a25 |
--- /dev/null |
+++ b/chrome/browser/sync/glue/autofill_profile_model_associator_unittest.cc |
@@ -0,0 +1,187 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/sync/engine/ReadNodeMock.h" |
+#include "chrome/browser/sync/glue/autofill_profile_model_associator.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::_; |
+using ::testing::Return; |
+using ::testing::DoDefault; |
+using ::testing::ReturnRef; |
+using ::testing::Pointee; |
+using ::testing::Ref; |
+using ::testing::Invoke; |
+class AutoFillProfile; |
+class MockAutofillProfileModelAssociator : public browser_sync::AutofillProfileModelAssociator |
+{ |
+public: |
+ MockAutofillProfileModelAssociator() { |
+ ON_CALL(*this, EnsureOnThread(_)) |
+ .WillByDefault(Return(true)); |
+ |
+ dontCheckThreadOnExit_ = true; |
+ } |
+ virtual ~MockAutofillProfileModelAssociator() {} |
+ bool TraverseAndAssociateChromeAutoFillProfilesWrapper( |
+ sync_api::WriteTransaction* write_trans, |
+ const sync_api::ReadNode& autofill_root, |
+ const std::vector<AutoFillProfile*>& all_profiles_from_db, |
+ std::set<std::string>* current_profiles, |
+ std::vector<AutoFillProfile*>* updated_profiles) |
+ { |
+ return TraverseAndAssociateChromeAutoFillProfiles( |
+ write_trans, |
+ autofill_root, |
+ all_profiles_from_db, |
+ current_profiles, |
+ updated_profiles); |
+ } |
+ MOCK_METHOD3(AddNativeProfileIfNeeded, |
+ void(const sync_pb::AutofillProfile2Specifics& profile, |
+ DataBundle* bundle, |
+ const sync_api::ReadNode& node)); |
+ MOCK_METHOD2(OverwriteProfileWithServerData, |
+ bool(AutoFillProfile* merge_into, |
+ const sync_pb::AutofillProfile2Specifics& specifics)); |
+ MOCK_METHOD4(MakeNewAutofillProfileSyncNode, |
+ bool( sync_api::WriteTransaction* trans, |
+ const sync_api::BaseNode& autofill_root, |
+ const AutoFillProfile& profile, |
+ int64* sync_id)); |
+ MOCK_METHOD1(GetReadNode, sync_api::ReadNode* (sync_api::WriteTransaction *trans)); |
+ MOCK_METHOD2(Associate, void(const std::string* autofill, int64 id)); |
+ MOCK_METHOD1(EnsureOnThread, bool(BrowserThread::ID)); |
+ |
+ bool TraverseAndAssociateAllSyncNodesWrapper(sync_api::WriteTransaction *trans, const sync_api::ReadNode &autofill_root, browser_sync::AutofillProfileModelAssociator::DataBundle *bundle) |
+ { |
+ return TraverseAndAssociateAllSyncNodes(trans, autofill_root, bundle); |
+ } |
+ |
+ void AddNativeProfileIfNeededWrapper(const sync_pb::AutofillProfile2Specifics& profile, |
+ DataBundle* bundle, |
+ const sync_api::ReadNode& node) |
+ { |
+ AutofillProfileModelAssociator::AddNativeProfileIfNeeded(profile, bundle, node); |
+ } |
+}; |
+ |
+bool SetSyncId( sync_api::WriteTransaction* trans, |
+ const sync_api::BaseNode& autofill_root, |
+ const AutoFillProfile& profile, |
+ int64* sync_id) |
+{ |
+ *sync_id = (int64)1; |
+ return true; |
+} |
+ |
+TEST(AutofillProfile2Test, TestAssociateProfileInWebDBWithSyncDB) |
+{ |
+ MockAutofillProfileModelAssociator associator; |
+ ScopedVector<AutoFillProfile> profilesFromWebDB; |
+ sync_pb::AutofillProfile2Specifics profileSpecifics; |
+ ReadNodeMock *readNode = new ReadNodeMock(); // Will be released inside the function TraverseAndAssociateChromeAutoFillProfilesWrapper |
+ std::string guid = "abc"; |
+ std::set<std::string> currentProfiles; |
+ AutoFillProfile *profile = new AutoFillProfile(guid); |
+ |
+ profileSpecifics.set_guid(guid); |
+ |
+ EXPECT_CALL(associator, GetReadNode(_)) |
+ .WillOnce(Return(readNode)); |
+ |
+ EXPECT_CALL(*readNode, InitByClientTagLookup(syncable::AUTOFILL_PROFILE, guid)) |
+ .WillOnce(Return(true)); |
+ |
+ EXPECT_CALL(*readNode, GetAutofillProfileSpecifics()) |
+ .WillOnce(ReturnRef(profileSpecifics)); |
+ EXPECT_CALL(*readNode, GetId()) |
+ .WillOnce(Return((int64)1)); |
+ |
+ EXPECT_CALL(associator, Associate(Pointee(guid), (int64)1)); |
+ |
+ profilesFromWebDB.push_back(profile); |
+ |
+ associator.TraverseAndAssociateChromeAutoFillProfilesWrapper(NULL, *readNode, profilesFromWebDB.get(), ¤tProfiles, NULL); |
+ |
+ EXPECT_EQ(1, currentProfiles.size()); |
+} |
+ |
+TEST(AutofillProfile2Test, TestAssociatingMissingWebDBProfile) |
+{ |
+ MockAutofillProfileModelAssociator associator; |
+ ScopedVector<AutoFillProfile> profilesFromWebDB; |
+ sync_pb::AutofillProfile2Specifics profileSpecifics; |
+ ReadNodeMock *readNode = new ReadNodeMock(); // Will be released inside the function TraverseAndAssociateChromeAutoFillProfilesWrapper |
+ std::string guid = "abc"; |
+ std::set<std::string> currentProfiles; |
+ AutoFillProfile *profile = new AutoFillProfile(guid); |
+ |
+ profileSpecifics.set_guid(guid); |
+ |
+ EXPECT_CALL(associator, GetReadNode(_)) |
+ .WillOnce(Return(readNode)); |
+ |
+ EXPECT_CALL(*readNode, InitByClientTagLookup(syncable::AUTOFILL_PROFILE, guid)) |
+ .WillOnce(Return(false)); |
+ |
+ EXPECT_CALL(associator, MakeNewAutofillProfileSyncNode(NULL, Ref(*readNode), Ref(*profile), _)) |
+ .WillOnce(Invoke(SetSyncId)); |
+ |
+ EXPECT_CALL(associator, Associate(Pointee(guid), (int64)1)); |
+ |
+ profilesFromWebDB.push_back(profile); |
+ |
+ associator.TraverseAndAssociateChromeAutoFillProfilesWrapper(NULL, *readNode, profilesFromWebDB.get(), ¤tProfiles, NULL); |
+ |
+ EXPECT_EQ(1, currentProfiles.size()); |
+} |
+ |
+TEST(AutofillProfile2Test, TestAssociateProfileInSyncDBWithWebDB) |
+{ |
+ MockAutofillProfileModelAssociator associator; |
+ ReadNodeMock *readNode = new ReadNodeMock(); |
+ ReadNodeMock autofill_root; |
+ sync_pb::AutofillProfile2Specifics profileSpecifics; |
+ |
+ browser_sync::AutofillProfileModelAssociator::DataBundle bundle; |
+ |
+ EXPECT_CALL(autofill_root, GetFirstChildId()) |
+ .WillOnce(Return((int64)1)); |
+ |
+ EXPECT_CALL(associator, GetReadNode(_)) |
+ .WillOnce(Return(readNode)); |
+ |
+ EXPECT_CALL(*readNode, InitByIdLookup((int64)1)) |
+ .WillOnce(Return(true)); |
+ |
+ EXPECT_CALL(*readNode, GetAutofillProfileSpecifics()) |
+ .WillOnce(ReturnRef(profileSpecifics)); |
+ |
+ EXPECT_CALL(associator, AddNativeProfileIfNeeded(Ref(profileSpecifics), &bundle, Ref(*readNode))); |
+ |
+ EXPECT_CALL(*readNode, GetSuccessorId()) |
+ .WillOnce(Return(sync_api::kInvalidId)); |
+ |
+ associator.TraverseAndAssociateAllSyncNodesWrapper(NULL, |
+ autofill_root, |
+ &bundle); |
+} |
+ |
+TEST(AutofillProfile2Test, TestDontNeedToAddNativeProfile) |
+{ |
+ MockAutofillProfileModelAssociator associator; |
+ sync_pb::AutofillProfile2Specifics profileSpecifics; |
+ ReadNodeMock readNode; |
+ std::string guid = "abc"; |
+ std::set<std::string> currentProfiles; |
+ AutoFillProfile *profile = new AutoFillProfile(guid); |
+ browser_sync::AutofillProfileModelAssociator::DataBundle bundle; |
+ |
+ bundle.current_profiles.insert(guid); |
+ |
+ associator.AddNativeProfileIfNeededWrapper(profileSpecifics, &bundle, readNode); |
+ |
+} |