| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/threading/thread_task_runner_handle.h" | 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "chrome/browser/sync/chrome_sync_client.h" | 8 #include "chrome/browser/sync/chrome_sync_client.h" |
| 9 #include "chrome/browser/sync/profile_sync_service_factory.h" | 9 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 10 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" | 10 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 // A FakeModelTypeService that supports observing ApplySyncChanges. | 49 // A FakeModelTypeService that supports observing ApplySyncChanges. |
| 50 class TestModelTypeService : public FakeModelTypeService { | 50 class TestModelTypeService : public FakeModelTypeService { |
| 51 public: | 51 public: |
| 52 class Observer { | 52 class Observer { |
| 53 public: | 53 public: |
| 54 virtual void OnApplySyncChanges() = 0; | 54 virtual void OnApplySyncChanges() = 0; |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 TestModelTypeService() | 57 TestModelTypeService() |
| 58 : FakeModelTypeService( | 58 : FakeModelTypeService( |
| 59 base::Bind(&SharedModelTypeProcessor::CreateAsChangeProcessor)) {} | 59 base::Bind(&SharedModelTypeProcessor::CreateAsChangeProcessor)) { |
| 60 change_processor()->OnMetadataLoaded(syncer::SyncError(), |
| 61 db()->CreateMetadataBatch()); |
| 62 } |
| 60 | 63 |
| 61 syncer::SyncError ApplySyncChanges( | 64 syncer::SyncError ApplySyncChanges( |
| 62 std::unique_ptr<syncer::MetadataChangeList> metadata_changes, | 65 std::unique_ptr<syncer::MetadataChangeList> metadata_changes, |
| 63 syncer::EntityChangeList entity_changes) override { | 66 syncer::EntityChangeList entity_changes) override { |
| 64 syncer::SyncError error = FakeModelTypeService::ApplySyncChanges( | 67 syncer::SyncError error = FakeModelTypeService::ApplySyncChanges( |
| 65 std::move(metadata_changes), entity_changes); | 68 std::move(metadata_changes), entity_changes); |
| 66 NotifyObservers(); | 69 NotifyObservers(); |
| 67 return error; | 70 return error; |
| 68 } | 71 } |
| 69 | 72 |
| 70 void OnChangeProcessorSet() override { | |
| 71 change_processor()->OnMetadataLoaded(syncer::SyncError(), | |
| 72 db().CreateMetadataBatch()); | |
| 73 } | |
| 74 | |
| 75 void AddObserver(Observer* observer) { observers_.insert(observer); } | 73 void AddObserver(Observer* observer) { observers_.insert(observer); } |
| 76 void RemoveObserver(Observer* observer) { observers_.erase(observer); } | 74 void RemoveObserver(Observer* observer) { observers_.erase(observer); } |
| 77 | 75 |
| 78 private: | 76 private: |
| 79 void NotifyObservers() { | 77 void NotifyObservers() { |
| 80 for (Observer* observer : observers_) { | 78 for (Observer* observer : observers_) { |
| 81 observer->OnApplySyncChanges(); | 79 observer->OnApplySyncChanges(); |
| 82 } | 80 } |
| 83 } | 81 } |
| 84 | 82 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 106 | 104 |
| 107 // Wait for data for a key to have a certain value. | 105 // Wait for data for a key to have a certain value. |
| 108 class DataChecker : public KeyChecker { | 106 class DataChecker : public KeyChecker { |
| 109 public: | 107 public: |
| 110 DataChecker(TestModelTypeService* service, | 108 DataChecker(TestModelTypeService* service, |
| 111 const std::string& key, | 109 const std::string& key, |
| 112 const std::string& value) | 110 const std::string& value) |
| 113 : KeyChecker(service, key), value_(value) {} | 111 : KeyChecker(service, key), value_(value) {} |
| 114 | 112 |
| 115 bool IsExitConditionSatisfied() override { | 113 bool IsExitConditionSatisfied() override { |
| 116 const auto& db = service_->db(); | 114 const auto& db = *service_->db(); |
| 117 return db.HasData(key_) && db.GetValue(key_) == value_; | 115 return db.HasData(key_) && db.GetValue(key_) == value_; |
| 118 } | 116 } |
| 119 | 117 |
| 120 std::string GetDebugMessage() const override { | 118 std::string GetDebugMessage() const override { |
| 121 return "Waiting for data for key '" + key_ + "' to be '" + value_ + "'."; | 119 return "Waiting for data for key '" + key_ + "' to be '" + value_ + "'."; |
| 122 } | 120 } |
| 123 | 121 |
| 124 private: | 122 private: |
| 125 const std::string value_; | 123 const std::string value_; |
| 126 }; | 124 }; |
| 127 | 125 |
| 128 // Wait for data for a key to be absent. | 126 // Wait for data for a key to be absent. |
| 129 class DataAbsentChecker : public KeyChecker { | 127 class DataAbsentChecker : public KeyChecker { |
| 130 public: | 128 public: |
| 131 DataAbsentChecker(TestModelTypeService* service, const std::string& key) | 129 DataAbsentChecker(TestModelTypeService* service, const std::string& key) |
| 132 : KeyChecker(service, key) {} | 130 : KeyChecker(service, key) {} |
| 133 | 131 |
| 134 bool IsExitConditionSatisfied() override { | 132 bool IsExitConditionSatisfied() override { |
| 135 return !service_->db().HasData(key_); | 133 return !service_->db()->HasData(key_); |
| 136 } | 134 } |
| 137 | 135 |
| 138 std::string GetDebugMessage() const override { | 136 std::string GetDebugMessage() const override { |
| 139 return "Waiting for data for key '" + key_ + "' to be absent."; | 137 return "Waiting for data for key '" + key_ + "' to be absent."; |
| 140 } | 138 } |
| 141 }; | 139 }; |
| 142 | 140 |
| 143 // Wait for metadata for a key to be present. | 141 // Wait for metadata for a key to be present. |
| 144 class MetadataPresentChecker : public KeyChecker { | 142 class MetadataPresentChecker : public KeyChecker { |
| 145 public: | 143 public: |
| 146 MetadataPresentChecker(TestModelTypeService* service, const std::string& key) | 144 MetadataPresentChecker(TestModelTypeService* service, const std::string& key) |
| 147 : KeyChecker(service, key) {} | 145 : KeyChecker(service, key) {} |
| 148 | 146 |
| 149 bool IsExitConditionSatisfied() override { | 147 bool IsExitConditionSatisfied() override { |
| 150 return service_->db().HasMetadata(key_); | 148 return service_->db()->HasMetadata(key_); |
| 151 } | 149 } |
| 152 | 150 |
| 153 std::string GetDebugMessage() const override { | 151 std::string GetDebugMessage() const override { |
| 154 return "Waiting for metadata for key '" + key_ + "' to be present."; | 152 return "Waiting for metadata for key '" + key_ + "' to be present."; |
| 155 } | 153 } |
| 156 }; | 154 }; |
| 157 | 155 |
| 158 // Wait for metadata for a key to be absent. | 156 // Wait for metadata for a key to be absent. |
| 159 class MetadataAbsentChecker : public KeyChecker { | 157 class MetadataAbsentChecker : public KeyChecker { |
| 160 public: | 158 public: |
| 161 MetadataAbsentChecker(TestModelTypeService* service, const std::string& key) | 159 MetadataAbsentChecker(TestModelTypeService* service, const std::string& key) |
| 162 : KeyChecker(service, key) {} | 160 : KeyChecker(service, key) {} |
| 163 | 161 |
| 164 bool IsExitConditionSatisfied() override { | 162 bool IsExitConditionSatisfied() override { |
| 165 return !service_->db().HasMetadata(key_); | 163 return !service_->db()->HasMetadata(key_); |
| 166 } | 164 } |
| 167 | 165 |
| 168 std::string GetDebugMessage() const override { | 166 std::string GetDebugMessage() const override { |
| 169 return "Waiting for metadata for key '" + key_ + "' to be absent."; | 167 return "Waiting for metadata for key '" + key_ + "' to be absent."; |
| 170 } | 168 } |
| 171 }; | 169 }; |
| 172 | 170 |
| 173 // Wait for PREFERENCES to no longer be running. | 171 // Wait for PREFERENCES to no longer be running. |
| 174 class PrefsNotRunningChecker : public SingleClientStatusChangeChecker { | 172 class PrefsNotRunningChecker : public SingleClientStatusChangeChecker { |
| 175 public: | 173 public: |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 249 } |
| 252 | 250 |
| 253 IN_PROC_BROWSER_TEST_F(TwoClientUssSyncTest, DisableEnable) { | 251 IN_PROC_BROWSER_TEST_F(TwoClientUssSyncTest, DisableEnable) { |
| 254 ASSERT_TRUE(SetupSync()); | 252 ASSERT_TRUE(SetupSync()); |
| 255 TestModelTypeService* model1 = GetModelTypeService(0); | 253 TestModelTypeService* model1 = GetModelTypeService(0); |
| 256 TestModelTypeService* model2 = GetModelTypeService(1); | 254 TestModelTypeService* model2 = GetModelTypeService(1); |
| 257 | 255 |
| 258 // Add an entity to test with. | 256 // Add an entity to test with. |
| 259 model1->WriteItem(kKey1, kValue1); | 257 model1->WriteItem(kKey1, kValue1); |
| 260 ASSERT_TRUE(DataChecker(model2, kKey1, kValue1).Wait()); | 258 ASSERT_TRUE(DataChecker(model2, kKey1, kValue1).Wait()); |
| 261 ASSERT_EQ(1U, model1->db().data_count()); | 259 ASSERT_EQ(1U, model1->db()->data_count()); |
| 262 ASSERT_EQ(1U, model1->db().metadata_count()); | 260 ASSERT_EQ(1U, model1->db()->metadata_count()); |
| 263 ASSERT_EQ(1U, model2->db().data_count()); | 261 ASSERT_EQ(1U, model2->db()->data_count()); |
| 264 ASSERT_EQ(1U, model2->db().metadata_count()); | 262 ASSERT_EQ(1U, model2->db()->metadata_count()); |
| 265 | 263 |
| 266 // Disable PREFERENCES. | 264 // Disable PREFERENCES. |
| 267 syncer::ModelTypeSet types = syncer::UserSelectableTypes(); | 265 syncer::ModelTypeSet types = syncer::UserSelectableTypes(); |
| 268 types.Remove(syncer::PREFERENCES); | 266 types.Remove(syncer::PREFERENCES); |
| 269 GetSyncService(0)->OnUserChoseDatatypes(false, types); | 267 GetSyncService(0)->OnUserChoseDatatypes(false, types); |
| 270 | 268 |
| 271 // Wait for it to take effect and remove the metadata. | 269 // Wait for it to take effect and remove the metadata. |
| 272 ASSERT_TRUE(MetadataAbsentChecker(model1, kKey1).Wait()); | 270 ASSERT_TRUE(MetadataAbsentChecker(model1, kKey1).Wait()); |
| 273 ASSERT_EQ(1U, model1->db().data_count()); | 271 ASSERT_EQ(1U, model1->db()->data_count()); |
| 274 ASSERT_EQ(0U, model1->db().metadata_count()); | 272 ASSERT_EQ(0U, model1->db()->metadata_count()); |
| 275 // Model 2 should not be affected. | 273 // Model 2 should not be affected. |
| 276 ASSERT_EQ(1U, model2->db().data_count()); | 274 ASSERT_EQ(1U, model2->db()->data_count()); |
| 277 ASSERT_EQ(1U, model2->db().metadata_count()); | 275 ASSERT_EQ(1U, model2->db()->metadata_count()); |
| 278 | 276 |
| 279 // Re-enable PREFERENCES. | 277 // Re-enable PREFERENCES. |
| 280 GetSyncService(0)->OnUserChoseDatatypes(true, syncer::UserSelectableTypes()); | 278 GetSyncService(0)->OnUserChoseDatatypes(true, syncer::UserSelectableTypes()); |
| 281 | 279 |
| 282 // Wait for metadata to be re-added. | 280 // Wait for metadata to be re-added. |
| 283 ASSERT_TRUE(MetadataPresentChecker(model1, kKey1).Wait()); | 281 ASSERT_TRUE(MetadataPresentChecker(model1, kKey1).Wait()); |
| 284 ASSERT_EQ(1U, model1->db().data_count()); | 282 ASSERT_EQ(1U, model1->db()->data_count()); |
| 285 ASSERT_EQ(1U, model1->db().metadata_count()); | 283 ASSERT_EQ(1U, model1->db()->metadata_count()); |
| 286 ASSERT_EQ(1U, model2->db().data_count()); | 284 ASSERT_EQ(1U, model2->db()->data_count()); |
| 287 ASSERT_EQ(1U, model2->db().metadata_count()); | 285 ASSERT_EQ(1U, model2->db()->metadata_count()); |
| 288 } | 286 } |
| 289 | 287 |
| 290 IN_PROC_BROWSER_TEST_F(TwoClientUssSyncTest, ConflictResolution) { | 288 IN_PROC_BROWSER_TEST_F(TwoClientUssSyncTest, ConflictResolution) { |
| 291 ASSERT_TRUE(SetupSync()); | 289 ASSERT_TRUE(SetupSync()); |
| 292 TestModelTypeService* model1 = GetModelTypeService(0); | 290 TestModelTypeService* model1 = GetModelTypeService(0); |
| 293 TestModelTypeService* model2 = GetModelTypeService(1); | 291 TestModelTypeService* model2 = GetModelTypeService(1); |
| 294 model1->SetConflictResolution(ConflictResolution::UseNew( | 292 model1->SetConflictResolution(ConflictResolution::UseNew( |
| 295 FakeModelTypeService::GenerateEntityData(kKey1, kValue3))); | 293 FakeModelTypeService::GenerateEntityData(kKey1, kValue3))); |
| 296 model2->SetConflictResolution(ConflictResolution::UseNew( | 294 model2->SetConflictResolution(ConflictResolution::UseNew( |
| 297 FakeModelTypeService::GenerateEntityData(kKey1, kValue3))); | 295 FakeModelTypeService::GenerateEntityData(kKey1, kValue3))); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 315 model1->WriteItem(kKey1, kValue1); | 313 model1->WriteItem(kKey1, kValue1); |
| 316 ASSERT_TRUE(DataChecker(model2, kKey1, kValue1).Wait()); | 314 ASSERT_TRUE(DataChecker(model2, kKey1, kValue1).Wait()); |
| 317 | 315 |
| 318 // Set an error in model 2 to trigger in the next GetUpdates. | 316 // Set an error in model 2 to trigger in the next GetUpdates. |
| 319 model2->SetServiceError(syncer::SyncError::DATATYPE_ERROR); | 317 model2->SetServiceError(syncer::SyncError::DATATYPE_ERROR); |
| 320 // Write an item on model 1 to trigger a GetUpdates in model 2. | 318 // Write an item on model 1 to trigger a GetUpdates in model 2. |
| 321 model1->WriteItem(kKey1, kValue2); | 319 model1->WriteItem(kKey1, kValue2); |
| 322 | 320 |
| 323 // The type should stop syncing but keep tracking metadata. | 321 // The type should stop syncing but keep tracking metadata. |
| 324 ASSERT_TRUE(PrefsNotRunningChecker(GetSyncService(1)).Wait()); | 322 ASSERT_TRUE(PrefsNotRunningChecker(GetSyncService(1)).Wait()); |
| 325 ASSERT_EQ(1U, model2->db().metadata_count()); | 323 ASSERT_EQ(1U, model2->db()->metadata_count()); |
| 326 model2->WriteItem(kKey2, kValue2); | 324 model2->WriteItem(kKey2, kValue2); |
| 327 ASSERT_EQ(2U, model2->db().metadata_count()); | 325 ASSERT_EQ(2U, model2->db()->metadata_count()); |
| 328 } | 326 } |
| OLD | NEW |