Chromium Code Reviews| Index: sync/internal_api/sync_backup_manager_unittest.cc |
| diff --git a/sync/internal_api/sync_backup_manager_unittest.cc b/sync/internal_api/sync_backup_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2f8b62d394fd9b49f7de4743a6f07b768d6316ea |
| --- /dev/null |
| +++ b/sync/internal_api/sync_backup_manager_unittest.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2014 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 "sync/internal_api/sync_backup_manager.h" |
| + |
| +#include "sql/connection.h" |
| +#include "sync/internal_api/public/read_node.h" |
| +#include "sync/internal_api/public/read_transaction.h" |
| +#include "sync/internal_api/public/test/test_internal_components_factory.h" |
| +#include "sync/internal_api/public/write_node.h" |
| +#include "sync/internal_api/public/write_transaction.h" |
| +#include "sync/syncable/entry.h" |
| +#include "sync/test/test_directory_backing_store.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace syncer { |
| + |
| +namespace { |
| + |
| +void OnConfigDone(bool success) { |
| + EXPECT_TRUE(success); |
| +} |
| + |
| +class SyncBackupManagerTest : public testing::Test { |
| + protected: |
| + virtual void SetUp() OVERRIDE { |
| + db_.reset(new sql::Connection); |
| + CHECK(db_->OpenInMemory()); |
| + } |
| + |
| + void InitManager(SyncManager* manager) { |
| + TestInternalComponentsFactory factory( |
| + scoped_ptr<syncable::DirectoryBackingStore>( |
|
Nicolas Zea
2014/04/15 23:05:51
do you have to create this here? Could you instead
haitaol1
2014/04/16 21:52:36
That's not sufficient because I want to verify dat
|
| + new syncable::TestDirectoryBackingStore("test", db_.get()))); |
| + |
| + manager->Init(base::FilePath("test"), |
| + MakeWeakHandle(base::WeakPtr<JsEventHandler>()), |
| + "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), |
| + std::vector<scoped_refptr<ModelSafeWorker> >(), |
| + NULL, NULL, SyncCredentials(), "", "", "", &factory, |
| + NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), |
| + NULL, NULL); |
| + manager->ConfigureSyncer( |
| + CONFIGURE_REASON_NEW_CLIENT, |
| + ModelTypeSet(PREFERENCES), |
| + ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), |
| + ModelSafeRoutingInfo(), |
| + base::Bind(&OnConfigDone, true), |
| + base::Bind(&OnConfigDone, false)); |
| + } |
| + |
| + void CreateEntry(UserShare* user_share, ModelType type, |
| + const std::string& client_tag) { |
| + WriteTransaction trans(FROM_HERE, user_share); |
| + ReadNode type_root(&trans); |
| + EXPECT_EQ(BaseNode::INIT_OK, |
| + type_root.InitByTagLookup(ModelTypeToRootTag(type))); |
| + |
| + WriteNode node(&trans); |
| + EXPECT_EQ(WriteNode::INIT_SUCCESS, |
| + node.InitUniqueByCreation(type, type_root, client_tag)); |
| + } |
| + |
| + scoped_ptr<sql::Connection> db_; |
| + base::MessageLoop loop_; // Needed for WeakHandle |
| +}; |
| + |
| +TEST_F(SyncBackupManagerTest, NormalizeAndPersist) { |
| + scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| + InitManager(manager.get()); |
| + |
| + CreateEntry(manager->GetUserShare(), PREFERENCES, "test"); |
| + |
| + { |
| + // New entry is local and unsynced at first. |
| + ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| + ReadNode pref(&trans); |
| + EXPECT_EQ(BaseNode::INIT_OK, |
| + pref.InitByClientTagLookup(PREFERENCES, "test")); |
| + EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); |
| + EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); |
| + } |
| + |
| + manager->SaveChanges(); |
| + |
| + { |
| + // New entry has server ID and unsynced bit is cleared after saving. |
| + ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| + ReadNode pref(&trans); |
| + EXPECT_EQ(BaseNode::INIT_OK, |
| + pref.InitByClientTagLookup(PREFERENCES, "test")); |
| + EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| + EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| + } |
| + manager->ShutdownOnSyncThread(); |
| + |
| + // Reopen db to verify entry is persisted. |
| + manager.reset(new SyncBackupManager); |
| + InitManager(manager.get()); |
| + { |
| + ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| + ReadNode pref(&trans); |
| + EXPECT_EQ(BaseNode::INIT_OK, |
| + pref.InitByClientTagLookup(PREFERENCES, "test")); |
| + EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| + EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| + } |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +} // namespace syncer |
| + |