| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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 "components/sync/driver/glue/sync_backend_registrar.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/location.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/threading/thread.h" | |
| 14 #include "components/sync/engine/browser_thread_model_worker.h" | |
| 15 #include "components/sync/engine/passive_model_worker.h" | |
| 16 #include "components/sync/model/change_processor_mock.h" | |
| 17 #include "components/sync/syncable/test_user_share.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace syncer { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 using ::testing::_; | |
| 26 using ::testing::InSequence; | |
| 27 using ::testing::Return; | |
| 28 using ::testing::StrictMock; | |
| 29 | |
| 30 class SyncBackendRegistrarTest : public testing::Test { | |
| 31 public: | |
| 32 SyncBackendRegistrarTest() | |
| 33 : db_thread_("DBThreadForTest"), | |
| 34 file_thread_("FileThreadForTest"), | |
| 35 sync_thread_("SyncThreadForTest") {} | |
| 36 | |
| 37 void SetUp() override { | |
| 38 db_thread_.StartAndWaitForTesting(); | |
| 39 file_thread_.StartAndWaitForTesting(); | |
| 40 sync_thread_.StartAndWaitForTesting(); | |
| 41 test_user_share_.SetUp(); | |
| 42 registrar_ = base::MakeUnique<SyncBackendRegistrar>( | |
| 43 "test", base::Bind(&SyncBackendRegistrarTest::CreateModelWorkerForGroup, | |
| 44 base::Unretained(this))); | |
| 45 } | |
| 46 | |
| 47 void TearDown() override { | |
| 48 registrar_->RequestWorkerStopOnUIThread(); | |
| 49 test_user_share_.TearDown(); | |
| 50 sync_thread_.task_runner()->DeleteSoon(FROM_HERE, registrar_.release()); | |
| 51 sync_thread_.FlushForTesting(); | |
| 52 } | |
| 53 | |
| 54 void TriggerChanges(ModelType type) { | |
| 55 registrar_->OnChangesApplied(type, 0, nullptr, ImmutableChangeRecordList()); | |
| 56 registrar_->OnChangesComplete(type); | |
| 57 } | |
| 58 | |
| 59 void ExpectRoutingInfo(const ModelSafeRoutingInfo& expected_routing_info) { | |
| 60 ModelSafeRoutingInfo actual_routing_info; | |
| 61 registrar_->GetModelSafeRoutingInfo(&actual_routing_info); | |
| 62 EXPECT_EQ(expected_routing_info, actual_routing_info); | |
| 63 } | |
| 64 | |
| 65 void ExpectHasProcessorsForTypes(ModelTypeSet types) { | |
| 66 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | |
| 67 ModelType model_type = ModelTypeFromInt(i); | |
| 68 EXPECT_EQ(types.Has(model_type), | |
| 69 registrar_->IsTypeActivatedForTest(model_type)); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 size_t GetWorkersSize() { | |
| 74 std::vector<scoped_refptr<ModelSafeWorker>> workers; | |
| 75 registrar_->GetWorkers(&workers); | |
| 76 return workers.size(); | |
| 77 } | |
| 78 | |
| 79 // Part of the ActivateDeactivateNonUIDataType test below. | |
| 80 void TestNonUIDataTypeActivationAsync(ChangeProcessor* processor, | |
| 81 base::WaitableEvent* done) { | |
| 82 registrar_->ActivateDataType(AUTOFILL, GROUP_DB, processor, user_share()); | |
| 83 ExpectRoutingInfo({{AUTOFILL, GROUP_DB}}); | |
| 84 ExpectHasProcessorsForTypes(ModelTypeSet(AUTOFILL)); | |
| 85 TriggerChanges(AUTOFILL); | |
| 86 done->Signal(); | |
| 87 } | |
| 88 | |
| 89 SyncBackendRegistrar* registrar() { return registrar_.get(); } | |
| 90 UserShare* user_share() { return test_user_share_.user_share(); } | |
| 91 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner() { | |
| 92 return db_thread_.task_runner(); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 scoped_refptr<ModelSafeWorker> CreateModelWorkerForGroup( | |
| 97 ModelSafeGroup group) { | |
| 98 switch (group) { | |
| 99 case GROUP_UI: | |
| 100 return new BrowserThreadModelWorker(message_loop_.task_runner(), group); | |
| 101 case GROUP_DB: | |
| 102 return new BrowserThreadModelWorker(db_thread_.task_runner(), group); | |
| 103 case GROUP_FILE: | |
| 104 return new BrowserThreadModelWorker(file_thread_.task_runner(), group); | |
| 105 case GROUP_PASSIVE: | |
| 106 return new PassiveModelWorker(); | |
| 107 default: | |
| 108 return nullptr; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 base::MessageLoop message_loop_; | |
| 113 base::Thread db_thread_; | |
| 114 base::Thread file_thread_; | |
| 115 base::Thread sync_thread_; | |
| 116 | |
| 117 TestUserShare test_user_share_; | |
| 118 std::unique_ptr<SyncBackendRegistrar> registrar_; | |
| 119 }; | |
| 120 | |
| 121 TEST_F(SyncBackendRegistrarTest, ConstructorEmpty) { | |
| 122 registrar()->SetInitialTypes(ModelTypeSet()); | |
| 123 EXPECT_FALSE(registrar()->IsNigoriEnabled()); | |
| 124 EXPECT_EQ(4u, GetWorkersSize()); | |
| 125 ExpectRoutingInfo(ModelSafeRoutingInfo()); | |
| 126 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 127 } | |
| 128 | |
| 129 TEST_F(SyncBackendRegistrarTest, ConstructorNonEmpty) { | |
| 130 registrar()->RegisterNonBlockingType(BOOKMARKS); | |
| 131 registrar()->SetInitialTypes(ModelTypeSet(BOOKMARKS, NIGORI, PASSWORDS)); | |
| 132 EXPECT_TRUE(registrar()->IsNigoriEnabled()); | |
| 133 EXPECT_EQ(4u, GetWorkersSize()); | |
| 134 EXPECT_EQ(ModelTypeSet(NIGORI), registrar()->GetLastConfiguredTypes()); | |
| 135 // Bookmarks dropped because it is nonblocking. | |
| 136 // Passwords dropped because of no password store. | |
| 137 ExpectRoutingInfo({{NIGORI, GROUP_PASSIVE}}); | |
| 138 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(SyncBackendRegistrarTest, ConstructorNonEmptyReversedInitialization) { | |
| 142 // The blocking types get to set initial types before NonBlocking types here. | |
| 143 registrar()->SetInitialTypes(ModelTypeSet(BOOKMARKS, NIGORI, PASSWORDS)); | |
| 144 registrar()->RegisterNonBlockingType(BOOKMARKS); | |
| 145 EXPECT_TRUE(registrar()->IsNigoriEnabled()); | |
| 146 EXPECT_EQ(4u, GetWorkersSize()); | |
| 147 EXPECT_EQ(ModelTypeSet(NIGORI), registrar()->GetLastConfiguredTypes()); | |
| 148 // Bookmarks dropped because it is nonblocking. | |
| 149 // Passwords dropped because of no password store. | |
| 150 ExpectRoutingInfo({{NIGORI, GROUP_PASSIVE}}); | |
| 151 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 152 } | |
| 153 | |
| 154 TEST_F(SyncBackendRegistrarTest, ConfigureDataTypes) { | |
| 155 registrar()->RegisterNonBlockingType(BOOKMARKS); | |
| 156 registrar()->SetInitialTypes(ModelTypeSet()); | |
| 157 | |
| 158 // Add. | |
| 159 const ModelTypeSet types1(BOOKMARKS, NIGORI, AUTOFILL); | |
| 160 EXPECT_EQ(types1, registrar()->ConfigureDataTypes(types1, ModelTypeSet())); | |
| 161 ExpectRoutingInfo({{BOOKMARKS, GROUP_NON_BLOCKING}, | |
| 162 {NIGORI, GROUP_PASSIVE}, | |
| 163 {AUTOFILL, GROUP_PASSIVE}}); | |
| 164 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 165 EXPECT_EQ(types1, registrar()->GetLastConfiguredTypes()); | |
| 166 | |
| 167 // Add and remove. | |
| 168 const ModelTypeSet types2(PREFERENCES, THEMES); | |
| 169 EXPECT_EQ(types2, registrar()->ConfigureDataTypes(types2, types1)); | |
| 170 | |
| 171 ExpectRoutingInfo({{PREFERENCES, GROUP_PASSIVE}, {THEMES, GROUP_PASSIVE}}); | |
| 172 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 173 EXPECT_EQ(types2, registrar()->GetLastConfiguredTypes()); | |
| 174 | |
| 175 // Remove. | |
| 176 EXPECT_TRUE(registrar()->ConfigureDataTypes(ModelTypeSet(), types2).Empty()); | |
| 177 ExpectRoutingInfo(ModelSafeRoutingInfo()); | |
| 178 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 179 EXPECT_EQ(ModelTypeSet(), registrar()->GetLastConfiguredTypes()); | |
| 180 } | |
| 181 | |
| 182 TEST_F(SyncBackendRegistrarTest, ActivateDeactivateUIDataType) { | |
| 183 InSequence in_sequence; | |
| 184 registrar()->SetInitialTypes(ModelTypeSet()); | |
| 185 | |
| 186 // Should do nothing. | |
| 187 TriggerChanges(BOOKMARKS); | |
| 188 | |
| 189 StrictMock<ChangeProcessorMock> change_processor_mock; | |
| 190 EXPECT_CALL(change_processor_mock, StartImpl()); | |
| 191 EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true)); | |
| 192 EXPECT_CALL(change_processor_mock, ApplyChangesFromSyncModel(nullptr, _, _)); | |
| 193 EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true)); | |
| 194 EXPECT_CALL(change_processor_mock, CommitChangesFromSyncModel()); | |
| 195 EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(false)); | |
| 196 | |
| 197 const ModelTypeSet types(BOOKMARKS); | |
| 198 EXPECT_EQ(types, registrar()->ConfigureDataTypes(types, ModelTypeSet())); | |
| 199 registrar()->ActivateDataType(BOOKMARKS, GROUP_UI, &change_processor_mock, | |
| 200 user_share()); | |
| 201 ExpectRoutingInfo({{BOOKMARKS, GROUP_UI}}); | |
| 202 ExpectHasProcessorsForTypes(types); | |
| 203 | |
| 204 TriggerChanges(BOOKMARKS); | |
| 205 | |
| 206 registrar()->DeactivateDataType(BOOKMARKS); | |
| 207 ExpectRoutingInfo(ModelSafeRoutingInfo()); | |
| 208 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 209 | |
| 210 // Should do nothing. | |
| 211 TriggerChanges(BOOKMARKS); | |
| 212 } | |
| 213 | |
| 214 TEST_F(SyncBackendRegistrarTest, ActivateDeactivateNonUIDataType) { | |
| 215 InSequence in_sequence; | |
| 216 registrar()->SetInitialTypes(ModelTypeSet()); | |
| 217 | |
| 218 // Should do nothing. | |
| 219 TriggerChanges(AUTOFILL); | |
| 220 | |
| 221 StrictMock<ChangeProcessorMock> change_processor_mock; | |
| 222 EXPECT_CALL(change_processor_mock, StartImpl()); | |
| 223 EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true)); | |
| 224 EXPECT_CALL(change_processor_mock, ApplyChangesFromSyncModel(nullptr, _, _)); | |
| 225 EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(true)); | |
| 226 EXPECT_CALL(change_processor_mock, CommitChangesFromSyncModel()); | |
| 227 EXPECT_CALL(change_processor_mock, IsRunning()).WillRepeatedly(Return(false)); | |
| 228 | |
| 229 const ModelTypeSet types(AUTOFILL); | |
| 230 EXPECT_EQ(types, registrar()->ConfigureDataTypes(types, ModelTypeSet())); | |
| 231 | |
| 232 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 233 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 234 db_task_runner()->PostTask( | |
| 235 FROM_HERE, | |
| 236 base::Bind(&SyncBackendRegistrarTest::TestNonUIDataTypeActivationAsync, | |
| 237 base::Unretained(this), &change_processor_mock, &done)); | |
| 238 done.Wait(); | |
| 239 | |
| 240 registrar()->DeactivateDataType(AUTOFILL); | |
| 241 ExpectRoutingInfo(ModelSafeRoutingInfo()); | |
| 242 ExpectHasProcessorsForTypes(ModelTypeSet()); | |
| 243 | |
| 244 // Should do nothing. | |
| 245 TriggerChanges(AUTOFILL); | |
| 246 } | |
| 247 | |
| 248 // Tests that registration and configuration of non-blocking data types is | |
| 249 // handled correctly in SyncBackendRegistrar. | |
| 250 TEST_F(SyncBackendRegistrarTest, ConfigureNonBlockingDataType) { | |
| 251 registrar()->RegisterNonBlockingType(AUTOFILL); | |
| 252 registrar()->RegisterNonBlockingType(BOOKMARKS); | |
| 253 | |
| 254 ExpectRoutingInfo(ModelSafeRoutingInfo()); | |
| 255 // Simulate that initial sync was already done for AUTOFILL. | |
| 256 registrar()->AddRestoredNonBlockingType(AUTOFILL); | |
| 257 // It should be added to routing info and set of configured types. | |
| 258 EXPECT_EQ(ModelTypeSet(AUTOFILL), registrar()->GetLastConfiguredTypes()); | |
| 259 ExpectRoutingInfo({{AUTOFILL, GROUP_NON_BLOCKING}}); | |
| 260 | |
| 261 // Configure two non-blocking types. Initial sync wasn't done for BOOKMARKS so | |
| 262 // it should be included in types to be downloaded. | |
| 263 ModelTypeSet types_to_add(AUTOFILL, BOOKMARKS); | |
| 264 ModelTypeSet newly_added_types = | |
| 265 registrar()->ConfigureDataTypes(types_to_add, ModelTypeSet()); | |
| 266 EXPECT_EQ(ModelTypeSet(BOOKMARKS), newly_added_types); | |
| 267 EXPECT_EQ(types_to_add, registrar()->GetLastConfiguredTypes()); | |
| 268 ExpectRoutingInfo( | |
| 269 {{AUTOFILL, GROUP_NON_BLOCKING}, {BOOKMARKS, GROUP_NON_BLOCKING}}); | |
| 270 } | |
| 271 | |
| 272 } // namespace | |
| 273 | |
| 274 } // namespace syncer | |
| OLD | NEW |