Chromium Code Reviews| Index: sync/internal_api/sync_rollback_manager_unittest.cc |
| diff --git a/sync/internal_api/sync_rollback_manager_unittest.cc b/sync/internal_api/sync_rollback_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a8b24f46b4690f85ce3bd8760331d32bf4158713 |
| --- /dev/null |
| +++ b/sync/internal_api/sync_rollback_manager_unittest.cc |
| @@ -0,0 +1,185 @@ |
| +// 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_rollback_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/internal_api/sync_backup_manager.h" |
| +#include "sync/syncable/entry.h" |
| +#include "sync/test/engine/fake_model_worker.h" |
| +#include "sync/test/test_directory_backing_store.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using ::testing::_; |
| +using ::testing::Invoke; |
| +using ::testing::WithArgs; |
| + |
| +namespace syncer { |
| + |
| +class SyncRollbackManagerTest |
|
Nicolas Zea
2014/04/14 21:36:31
put this all within an anon namespace?
haitaol1
2014/04/15 00:19:06
Done.
|
| + : public testing::Test, |
| + public SyncManager::ChangeDelegate { |
| + public: |
| + virtual void SetUp() OVERRIDE { |
| + db_.reset(new sql::Connection); |
| + CHECK(db_->OpenInMemory()); |
| + |
| + worker_ = new FakeModelWorker(GROUP_UI); |
| + } |
| + |
| + MOCK_METHOD4(OnChangesApplied, |
| + void(ModelType model_type, |
| + int64 model_version, |
| + const BaseTransaction* trans, |
| + const ImmutableChangeRecordList& changes)); |
| + MOCK_METHOD1(OnChangesComplete, void(ModelType model_type)); |
| + |
| + void OnConfigDone(bool success) { |
| + EXPECT_TRUE(success); |
| + } |
| + |
| + void VerifyChanges(const ImmutableChangeRecordList& changes) { |
| + std::set<int64> deleted; |
| + for (size_t i = 0; i < changes.Get().size(); ++i) { |
| + const ChangeRecord& change = (changes.Get())[i]; |
| + EXPECT_EQ(ChangeRecord::ACTION_DELETE, change.action); |
| + EXPECT_TRUE(deleted.find(change.id) == deleted.end()); |
| + deleted.insert(change.id); |
| + } |
| + EXPECT_TRUE(expected_deletes_ == deleted); |
| + } |
| + |
| + protected: |
|
Nicolas Zea
2014/04/14 21:36:31
I'm not sure there's any point to distinguish betw
haitaol1
2014/04/15 00:19:06
Done.
|
| + int64 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)); |
| + return node.GetEntry()->GetMetahandle(); |
| + } |
| + |
| + void InitManager(SyncManager *manager, ModelTypeSet types) { |
|
Nicolas Zea
2014/04/14 21:36:31
nit: SyncManager *manager -> SyncManager* manager
haitaol1
2014/04/15 00:19:06
Done.
|
| + TestInternalComponentsFactory factory( |
| + scoped_ptr<syncable::DirectoryBackingStore>( |
| + new syncable::TestDirectoryBackingStore("test", db_.get()))); |
| + |
| + manager->Init(base::FilePath("test"), |
|
Nicolas Zea
2014/04/14 21:36:31
is this going to leave the database behind after t
haitaol1
2014/04/15 00:19:06
No, db will be destroyed when test instance is des
|
| + MakeWeakHandle(base::WeakPtr<JsEventHandler>()), |
| + "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), |
| + std::vector<scoped_refptr<ModelSafeWorker> >(1, |
| + worker_.get()), |
| + NULL, this, SyncCredentials(), "", "", "", &factory, |
| + NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), |
| + NULL, NULL); |
| + manager->ConfigureSyncer( |
| + CONFIGURE_REASON_NEW_CLIENT, |
| + types, |
| + ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), ModelSafeRoutingInfo(), |
| + base::Bind(&SyncRollbackManagerTest::OnConfigDone, |
| + base::Unretained(this), true), |
| + base::Bind(&SyncRollbackManagerTest::OnConfigDone, |
| + base::Unretained(this), false)); |
| + } |
| + |
| + void PrepopulateDb(ModelType type, const std::string& client_tag) { |
|
Nicolas Zea
2014/04/14 21:36:31
Comment for the method.
haitaol1
2014/04/15 00:19:06
Done.
|
| + SyncBackupManager backup_manager; |
| + InitManager(&backup_manager, ModelTypeSet(type)); |
| + CreateEntry(backup_manager.GetUserShare(), type, client_tag); |
| + backup_manager.ShutdownOnSyncThread(); |
| + } |
| + |
| + bool VerifyEntry(UserShare* user_share, ModelType type, |
|
Nicolas Zea
2014/04/14 21:36:31
pass UserShare by const ref?
Also comment for the
haitaol1
2014/04/15 00:19:06
ReadTransaction doesn't use const share.
|
| + const std::string& client_tag) { |
| + ReadTransaction trans(FROM_HERE, user_share); |
| + ReadNode node(&trans); |
| + return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); |
| + } |
| + |
| + protected: |
|
Nicolas Zea
2014/04/14 21:36:31
this section is already within a "protected"
haitaol1
2014/04/15 00:19:06
Done.
|
| + scoped_ptr<sql::Connection> db_; |
| + scoped_refptr<ModelSafeWorker> worker_; |
| + std::set<int64> expected_deletes_; |
| + base::MessageLoop loop_; // Needed for WeakHandle |
| +}; |
| + |
| +TEST_F(SyncRollbackManagerTest, RollbackBasic) { |
| + PrepopulateDb(PREFERENCES, "pref1"); |
| + |
| + SyncRollbackManager rollback_manager; |
| + InitManager(&rollback_manager, ModelTypeSet(PREFERENCES)); |
| + |
| + // Simulate a new entry added during type initialization. |
| + int64 new_pref_id = |
| + CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
| + |
| + expected_deletes_.insert(new_pref_id); |
| + EXPECT_CALL(*this, OnChangesApplied(_, _, _, _)) |
|
Nicolas Zea
2014/04/14 21:36:31
In general it's encouraged to avoid mock logic lik
haitaol1
2014/04/15 00:19:06
Done.
|
| + .Times(1) |
| + .WillOnce(WithArgs<3>( |
| + Invoke(this,&SyncRollbackManagerTest::VerifyChanges))); |
| + EXPECT_CALL(*this, OnChangesComplete(_)).Times(1); |
| + |
| + ModelSafeRoutingInfo routing_info; |
| + routing_info[PREFERENCES] = GROUP_UI; |
| + rollback_manager.StartSyncingNormally(routing_info); |
| +} |
| + |
| +TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { |
| + PrepopulateDb(PREFERENCES, "pref1"); |
| + |
| + SyncRollbackManager rollback_manager; |
| + InitManager(&rollback_manager, ModelTypeSet(PREFERENCES, APPS)); |
| + |
| + // Simulate new entry added during type initialization. |
| + int64 new_pref_id = |
| + CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
| + CreateEntry(rollback_manager.GetUserShare(), APPS, "app1"); |
| + |
| + expected_deletes_.insert(new_pref_id); |
| + EXPECT_CALL(*this, OnChangesApplied(_, _, _, _)) |
| + .Times(1) |
| + .WillOnce(WithArgs<3>( |
| + Invoke(this,&SyncRollbackManagerTest::VerifyChanges))); |
| + EXPECT_CALL(*this, OnChangesComplete(_)).Times(1); |
| + |
| + ModelSafeRoutingInfo routing_info; |
| + routing_info[PREFERENCES] = GROUP_UI; |
| + rollback_manager.StartSyncingNormally(routing_info); |
| + |
| + // APP entry is still valid. |
| + EXPECT_TRUE(VerifyEntry(rollback_manager.GetUserShare(), APPS, "app1")); |
| +} |
| + |
| +TEST_F(SyncRollbackManagerTest, BackupDbNotChangedOnAbort) { |
| + PrepopulateDb(PREFERENCES, "pref1"); |
| + |
| + scoped_ptr<SyncRollbackManager> rollback_manager( |
| + new SyncRollbackManager); |
| + InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES)); |
| + |
| + // Simulate a new entry added during type initialization. |
| + CreateEntry(rollback_manager->GetUserShare(), PREFERENCES, "pref2"); |
| + |
| + // Manager was shut down before sync starts. |
| + rollback_manager->ShutdownOnSyncThread(); |
| + |
| + // Verify new entry was not persisted. |
| + rollback_manager.reset(new SyncRollbackManager); |
| + InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES)); |
| + EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, |
| + "pref2")); |
| +} |
| + |
| +} // namespace syncer |