Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "sync/internal_api/sync_rollback_manager.h" | |
| 6 | |
| 7 #include "sql/connection.h" | |
| 8 #include "sync/internal_api/public/read_node.h" | |
| 9 #include "sync/internal_api/public/read_transaction.h" | |
| 10 #include "sync/internal_api/public/test/test_internal_components_factory.h" | |
| 11 #include "sync/internal_api/public/write_node.h" | |
| 12 #include "sync/internal_api/public/write_transaction.h" | |
| 13 #include "sync/internal_api/sync_backup_manager.h" | |
| 14 #include "sync/syncable/entry.h" | |
| 15 #include "sync/test/engine/fake_model_worker.h" | |
| 16 #include "sync/test/test_directory_backing_store.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using ::testing::_; | |
| 21 using ::testing::Invoke; | |
| 22 using ::testing::WithArgs; | |
| 23 | |
| 24 namespace syncer { | |
| 25 | |
| 26 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.
| |
| 27 : public testing::Test, | |
| 28 public SyncManager::ChangeDelegate { | |
| 29 public: | |
| 30 virtual void SetUp() OVERRIDE { | |
| 31 db_.reset(new sql::Connection); | |
| 32 CHECK(db_->OpenInMemory()); | |
| 33 | |
| 34 worker_ = new FakeModelWorker(GROUP_UI); | |
| 35 } | |
| 36 | |
| 37 MOCK_METHOD4(OnChangesApplied, | |
| 38 void(ModelType model_type, | |
| 39 int64 model_version, | |
| 40 const BaseTransaction* trans, | |
| 41 const ImmutableChangeRecordList& changes)); | |
| 42 MOCK_METHOD1(OnChangesComplete, void(ModelType model_type)); | |
| 43 | |
| 44 void OnConfigDone(bool success) { | |
| 45 EXPECT_TRUE(success); | |
| 46 } | |
| 47 | |
| 48 void VerifyChanges(const ImmutableChangeRecordList& changes) { | |
| 49 std::set<int64> deleted; | |
| 50 for (size_t i = 0; i < changes.Get().size(); ++i) { | |
| 51 const ChangeRecord& change = (changes.Get())[i]; | |
| 52 EXPECT_EQ(ChangeRecord::ACTION_DELETE, change.action); | |
| 53 EXPECT_TRUE(deleted.find(change.id) == deleted.end()); | |
| 54 deleted.insert(change.id); | |
| 55 } | |
| 56 EXPECT_TRUE(expected_deletes_ == deleted); | |
| 57 } | |
| 58 | |
| 59 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.
| |
| 60 int64 CreateEntry(UserShare* user_share, ModelType type, | |
| 61 const std::string& client_tag) { | |
| 62 WriteTransaction trans(FROM_HERE, user_share); | |
| 63 ReadNode type_root(&trans); | |
| 64 EXPECT_EQ(BaseNode::INIT_OK, | |
| 65 type_root.InitByTagLookup(ModelTypeToRootTag(type))); | |
| 66 | |
| 67 WriteNode node(&trans); | |
| 68 EXPECT_EQ(WriteNode::INIT_SUCCESS, | |
| 69 node.InitUniqueByCreation(type, type_root, client_tag)); | |
| 70 return node.GetEntry()->GetMetahandle(); | |
| 71 } | |
| 72 | |
| 73 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.
| |
| 74 TestInternalComponentsFactory factory( | |
| 75 scoped_ptr<syncable::DirectoryBackingStore>( | |
| 76 new syncable::TestDirectoryBackingStore("test", db_.get()))); | |
| 77 | |
| 78 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
| |
| 79 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), | |
| 80 "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), | |
| 81 std::vector<scoped_refptr<ModelSafeWorker> >(1, | |
| 82 worker_.get()), | |
| 83 NULL, this, SyncCredentials(), "", "", "", &factory, | |
| 84 NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), | |
| 85 NULL, NULL); | |
| 86 manager->ConfigureSyncer( | |
| 87 CONFIGURE_REASON_NEW_CLIENT, | |
| 88 types, | |
| 89 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), ModelSafeRoutingInfo(), | |
| 90 base::Bind(&SyncRollbackManagerTest::OnConfigDone, | |
| 91 base::Unretained(this), true), | |
| 92 base::Bind(&SyncRollbackManagerTest::OnConfigDone, | |
| 93 base::Unretained(this), false)); | |
| 94 } | |
| 95 | |
| 96 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.
| |
| 97 SyncBackupManager backup_manager; | |
| 98 InitManager(&backup_manager, ModelTypeSet(type)); | |
| 99 CreateEntry(backup_manager.GetUserShare(), type, client_tag); | |
| 100 backup_manager.ShutdownOnSyncThread(); | |
| 101 } | |
| 102 | |
| 103 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.
| |
| 104 const std::string& client_tag) { | |
| 105 ReadTransaction trans(FROM_HERE, user_share); | |
| 106 ReadNode node(&trans); | |
| 107 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); | |
| 108 } | |
| 109 | |
| 110 protected: | |
|
Nicolas Zea
2014/04/14 21:36:31
this section is already within a "protected"
haitaol1
2014/04/15 00:19:06
Done.
| |
| 111 scoped_ptr<sql::Connection> db_; | |
| 112 scoped_refptr<ModelSafeWorker> worker_; | |
| 113 std::set<int64> expected_deletes_; | |
| 114 base::MessageLoop loop_; // Needed for WeakHandle | |
| 115 }; | |
| 116 | |
| 117 TEST_F(SyncRollbackManagerTest, RollbackBasic) { | |
| 118 PrepopulateDb(PREFERENCES, "pref1"); | |
| 119 | |
| 120 SyncRollbackManager rollback_manager; | |
| 121 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES)); | |
| 122 | |
| 123 // Simulate a new entry added during type initialization. | |
| 124 int64 new_pref_id = | |
| 125 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | |
| 126 | |
| 127 expected_deletes_.insert(new_pref_id); | |
| 128 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.
| |
| 129 .Times(1) | |
| 130 .WillOnce(WithArgs<3>( | |
| 131 Invoke(this,&SyncRollbackManagerTest::VerifyChanges))); | |
| 132 EXPECT_CALL(*this, OnChangesComplete(_)).Times(1); | |
| 133 | |
| 134 ModelSafeRoutingInfo routing_info; | |
| 135 routing_info[PREFERENCES] = GROUP_UI; | |
| 136 rollback_manager.StartSyncingNormally(routing_info); | |
| 137 } | |
| 138 | |
| 139 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { | |
| 140 PrepopulateDb(PREFERENCES, "pref1"); | |
| 141 | |
| 142 SyncRollbackManager rollback_manager; | |
| 143 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES, APPS)); | |
| 144 | |
| 145 // Simulate new entry added during type initialization. | |
| 146 int64 new_pref_id = | |
| 147 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | |
| 148 CreateEntry(rollback_manager.GetUserShare(), APPS, "app1"); | |
| 149 | |
| 150 expected_deletes_.insert(new_pref_id); | |
| 151 EXPECT_CALL(*this, OnChangesApplied(_, _, _, _)) | |
| 152 .Times(1) | |
| 153 .WillOnce(WithArgs<3>( | |
| 154 Invoke(this,&SyncRollbackManagerTest::VerifyChanges))); | |
| 155 EXPECT_CALL(*this, OnChangesComplete(_)).Times(1); | |
| 156 | |
| 157 ModelSafeRoutingInfo routing_info; | |
| 158 routing_info[PREFERENCES] = GROUP_UI; | |
| 159 rollback_manager.StartSyncingNormally(routing_info); | |
| 160 | |
| 161 // APP entry is still valid. | |
| 162 EXPECT_TRUE(VerifyEntry(rollback_manager.GetUserShare(), APPS, "app1")); | |
| 163 } | |
| 164 | |
| 165 TEST_F(SyncRollbackManagerTest, BackupDbNotChangedOnAbort) { | |
| 166 PrepopulateDb(PREFERENCES, "pref1"); | |
| 167 | |
| 168 scoped_ptr<SyncRollbackManager> rollback_manager( | |
| 169 new SyncRollbackManager); | |
| 170 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES)); | |
| 171 | |
| 172 // Simulate a new entry added during type initialization. | |
| 173 CreateEntry(rollback_manager->GetUserShare(), PREFERENCES, "pref2"); | |
| 174 | |
| 175 // Manager was shut down before sync starts. | |
| 176 rollback_manager->ShutdownOnSyncThread(); | |
| 177 | |
| 178 // Verify new entry was not persisted. | |
| 179 rollback_manager.reset(new SyncRollbackManager); | |
| 180 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES)); | |
| 181 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, | |
| 182 "pref2")); | |
| 183 } | |
| 184 | |
| 185 } // namespace syncer | |
| OLD | NEW |