OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/sync_rollback_manager.h" | 5 #include "sync/internal_api/sync_rollback_manager.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "sync/internal_api/public/read_node.h" | 8 #include "sync/internal_api/public/read_node.h" |
9 #include "sync/internal_api/public/read_transaction.h" | 9 #include "sync/internal_api/public/read_transaction.h" |
| 10 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
10 #include "sync/internal_api/public/test/test_internal_components_factory.h" | 11 #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_node.h" |
12 #include "sync/internal_api/public/write_transaction.h" | 13 #include "sync/internal_api/public/write_transaction.h" |
13 #include "sync/internal_api/sync_backup_manager.h" | 14 #include "sync/internal_api/sync_backup_manager.h" |
14 #include "sync/syncable/entry.h" | 15 #include "sync/syncable/entry.h" |
15 #include "sync/test/engine/fake_model_worker.h" | 16 #include "sync/test/engine/fake_model_worker.h" |
16 #include "sync/test/test_directory_backing_store.h" | 17 #include "sync/test/test_directory_backing_store.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 using ::testing::_; | 21 using ::testing::_; |
21 using ::testing::DoDefault; | 22 using ::testing::DoDefault; |
22 using ::testing::Invoke; | 23 using ::testing::Invoke; |
| 24 using ::testing::Truly; |
23 using ::testing::WithArgs; | 25 using ::testing::WithArgs; |
24 | 26 |
25 namespace syncer { | 27 namespace syncer { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 class TestChangeDelegate : public SyncManager::ChangeDelegate { | 31 class TestChangeDelegate : public SyncManager::ChangeDelegate { |
30 public: | 32 public: |
31 TestChangeDelegate() { | 33 TestChangeDelegate() { |
32 ON_CALL(*this, OnChangesApplied(_, _, _, _)) | 34 ON_CALL(*this, OnChangesApplied(_, _, _, _)) |
(...skipping 21 matching lines...) Expand all Loading... |
54 EXPECT_EQ(ChangeRecord::ACTION_DELETE, change.action); | 56 EXPECT_EQ(ChangeRecord::ACTION_DELETE, change.action); |
55 EXPECT_TRUE(deleted.find(change.id) == deleted.end()); | 57 EXPECT_TRUE(deleted.find(change.id) == deleted.end()); |
56 deleted.insert(change.id); | 58 deleted.insert(change.id); |
57 } | 59 } |
58 EXPECT_TRUE(expected_deletes_ == deleted); | 60 EXPECT_TRUE(expected_deletes_ == deleted); |
59 } | 61 } |
60 | 62 |
61 std::set<int64> expected_deletes_; | 63 std::set<int64> expected_deletes_; |
62 }; | 64 }; |
63 | 65 |
64 class SyncRollbackManagerTest : public testing::Test { | 66 class SyncRollbackManagerTest : public testing::Test, |
| 67 public SyncManager::Observer { |
65 protected: | 68 protected: |
66 virtual void SetUp() OVERRIDE { | 69 virtual void SetUp() OVERRIDE { |
67 CHECK(temp_dir_.CreateUniqueTempDir()); | 70 CHECK(temp_dir_.CreateUniqueTempDir()); |
68 | 71 |
69 worker_ = new FakeModelWorker(GROUP_UI); | 72 worker_ = new FakeModelWorker(GROUP_UI); |
70 } | 73 } |
71 | 74 |
| 75 MOCK_METHOD1(OnSyncCycleCompleted, |
| 76 void(const sessions::SyncSessionSnapshot&)); |
| 77 MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus)); |
| 78 MOCK_METHOD4(OnInitializationComplete, |
| 79 void(const WeakHandle<JsBackend>&, |
| 80 const WeakHandle<DataTypeDebugInfoListener>&, |
| 81 bool, ModelTypeSet)); |
| 82 MOCK_METHOD1(OnActionableError, void(const SyncProtocolError&)); |
| 83 MOCK_METHOD1(OnMigrationRequested, void(ModelTypeSet));; |
| 84 MOCK_METHOD1(OnProtocolEvent, void(const ProtocolEvent&)); |
| 85 |
72 void OnConfigDone(bool success) { | 86 void OnConfigDone(bool success) { |
73 EXPECT_TRUE(success); | 87 EXPECT_TRUE(success); |
74 } | 88 } |
75 | 89 |
76 int64 CreateEntry(UserShare* user_share, ModelType type, | 90 int64 CreateEntry(UserShare* user_share, ModelType type, |
77 const std::string& client_tag) { | 91 const std::string& client_tag) { |
78 WriteTransaction trans(FROM_HERE, user_share); | 92 WriteTransaction trans(FROM_HERE, user_share); |
79 ReadNode type_root(&trans); | 93 ReadNode type_root(&trans); |
80 EXPECT_EQ(BaseNode::INIT_OK, | 94 EXPECT_EQ(BaseNode::INIT_OK, |
81 type_root.InitByTagLookup(ModelTypeToRootTag(type))); | 95 type_root.InitByTagLookup(ModelTypeToRootTag(type))); |
82 | 96 |
83 WriteNode node(&trans); | 97 WriteNode node(&trans); |
84 EXPECT_EQ(WriteNode::INIT_SUCCESS, | 98 EXPECT_EQ(WriteNode::INIT_SUCCESS, |
85 node.InitUniqueByCreation(type, type_root, client_tag)); | 99 node.InitUniqueByCreation(type, type_root, client_tag)); |
86 return node.GetEntry()->GetMetahandle(); | 100 return node.GetEntry()->GetMetahandle(); |
87 } | 101 } |
88 | 102 |
89 void InitManager(SyncManager* manager, ModelTypeSet types, | 103 void InitManager(SyncManager* manager, ModelTypeSet types, |
90 TestChangeDelegate* delegate) { | 104 TestChangeDelegate* delegate) { |
| 105 manager->AddObserver(this); |
91 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), | 106 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), |
92 STORAGE_ON_DISK); | 107 STORAGE_ON_DISK); |
93 | 108 |
94 manager->Init(temp_dir_.path(), | 109 manager->Init(temp_dir_.path(), |
95 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), | 110 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), |
96 "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), | 111 "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), |
97 std::vector<scoped_refptr<ModelSafeWorker> >(1, | 112 std::vector<scoped_refptr<ModelSafeWorker> >(1, |
98 worker_.get()), | 113 worker_.get()), |
99 NULL, delegate, SyncCredentials(), "", "", "", &factory, | 114 NULL, delegate, SyncCredentials(), "", "", "", &factory, |
100 NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), | 115 NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), |
(...skipping 23 matching lines...) Expand all Loading... |
124 ReadTransaction trans(FROM_HERE, user_share); | 139 ReadTransaction trans(FROM_HERE, user_share); |
125 ReadNode node(&trans); | 140 ReadNode node(&trans); |
126 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); | 141 return BaseNode::INIT_OK == node.InitByClientTagLookup(type, client_tag); |
127 } | 142 } |
128 | 143 |
129 base::ScopedTempDir temp_dir_; | 144 base::ScopedTempDir temp_dir_; |
130 scoped_refptr<ModelSafeWorker> worker_; | 145 scoped_refptr<ModelSafeWorker> worker_; |
131 base::MessageLoop loop_; // Needed for WeakHandle | 146 base::MessageLoop loop_; // Needed for WeakHandle |
132 }; | 147 }; |
133 | 148 |
| 149 bool IsRollbackDoneAction(SyncProtocolError e) { |
| 150 return e.action == syncer::ROLLBACK_DONE; |
| 151 } |
| 152 |
134 TEST_F(SyncRollbackManagerTest, RollbackBasic) { | 153 TEST_F(SyncRollbackManagerTest, RollbackBasic) { |
135 PrepopulateDb(PREFERENCES, "pref1"); | 154 PrepopulateDb(PREFERENCES, "pref1"); |
136 | 155 |
137 TestChangeDelegate delegate; | 156 TestChangeDelegate delegate; |
138 SyncRollbackManager rollback_manager; | 157 SyncRollbackManager rollback_manager; |
139 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES), &delegate); | 158 InitManager(&rollback_manager, ModelTypeSet(PREFERENCES), &delegate); |
140 | 159 |
141 // Simulate a new entry added during type initialization. | 160 // Simulate a new entry added during type initialization. |
142 int64 new_pref_id = | 161 int64 new_pref_id = |
143 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); | 162 CreateEntry(rollback_manager.GetUserShare(), PREFERENCES, "pref2"); |
144 | 163 |
145 delegate.add_expected_delete(new_pref_id); | 164 delegate.add_expected_delete(new_pref_id); |
146 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) | 165 EXPECT_CALL(delegate, OnChangesApplied(_, _, _, _)) |
147 .Times(1) | 166 .Times(1) |
148 .WillOnce(DoDefault()); | 167 .WillOnce(DoDefault()); |
149 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); | 168 EXPECT_CALL(delegate, OnChangesComplete(_)).Times(1); |
| 169 EXPECT_CALL(*this, OnActionableError(Truly(IsRollbackDoneAction))).Times(1); |
150 | 170 |
151 ModelSafeRoutingInfo routing_info; | 171 ModelSafeRoutingInfo routing_info; |
152 routing_info[PREFERENCES] = GROUP_UI; | 172 routing_info[PREFERENCES] = GROUP_UI; |
153 rollback_manager.StartSyncingNormally(routing_info); | 173 rollback_manager.StartSyncingNormally(routing_info); |
154 } | 174 } |
155 | 175 |
156 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { | 176 TEST_F(SyncRollbackManagerTest, NoRollbackOfTypesNotBackedUp) { |
157 PrepopulateDb(PREFERENCES, "pref1"); | 177 PrepopulateDb(PREFERENCES, "pref1"); |
158 | 178 |
159 TestChangeDelegate delegate; | 179 TestChangeDelegate delegate; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 // Verify new entry was not persisted. | 216 // Verify new entry was not persisted. |
197 rollback_manager.reset(new SyncRollbackManager); | 217 rollback_manager.reset(new SyncRollbackManager); |
198 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate); | 218 InitManager(rollback_manager.get(), ModelTypeSet(PREFERENCES), &delegate); |
199 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, | 219 EXPECT_FALSE(VerifyEntry(rollback_manager->GetUserShare(), PREFERENCES, |
200 "pref2")); | 220 "pref2")); |
201 } | 221 } |
202 | 222 |
203 } // anonymous namespace | 223 } // anonymous namespace |
204 | 224 |
205 } // namespace syncer | 225 } // namespace syncer |
OLD | NEW |