| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/macros.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "chrome/browser/sync/chrome_sync_client.h" |
| 9 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 10 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" |
| 11 #include "chrome/browser/sync/test/integration/status_change_checker.h" |
| 12 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h" |
| 13 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 14 #include "components/browser_sync/browser/profile_sync_components_factory_impl.h
" |
| 15 #include "components/sync/api/fake_model_type_service.h" |
| 16 |
| 17 using browser_sync::ChromeSyncClient; |
| 18 using syncer_v2::FakeModelTypeService; |
| 19 using syncer_v2::ModelTypeService; |
| 20 using syncer_v2::SharedModelTypeProcessor; |
| 21 |
| 22 // A ChromeSyncClient that provides a ModelTypeService for PREFERENCES. |
| 23 class TestSyncClient : public ChromeSyncClient { |
| 24 public: |
| 25 TestSyncClient(Profile* profile, ModelTypeService* service) |
| 26 : ChromeSyncClient(profile), service_(service) {} |
| 27 |
| 28 base::WeakPtr<ModelTypeService> GetModelTypeServiceForType( |
| 29 syncer::ModelType type) override { |
| 30 return type == syncer::PREFERENCES |
| 31 ? service_->AsWeakPtr() |
| 32 : ChromeSyncClient::GetModelTypeServiceForType(type); |
| 33 } |
| 34 |
| 35 private: |
| 36 ModelTypeService* const service_; |
| 37 }; |
| 38 |
| 39 // A FakeModelTypeService that supports observing ApplySyncChanges. |
| 40 class TestModelTypeService : public FakeModelTypeService { |
| 41 public: |
| 42 class Observer { |
| 43 public: |
| 44 virtual void OnApplySyncChanges() = 0; |
| 45 }; |
| 46 |
| 47 TestModelTypeService() |
| 48 : FakeModelTypeService( |
| 49 base::Bind(&SharedModelTypeProcessor::CreateAsChangeProcessor)) {} |
| 50 |
| 51 syncer::SyncError ApplySyncChanges( |
| 52 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_changes, |
| 53 syncer_v2::EntityChangeList entity_changes) override { |
| 54 syncer::SyncError error = FakeModelTypeService::ApplySyncChanges( |
| 55 std::move(metadata_changes), entity_changes); |
| 56 NotifyObservers(); |
| 57 return error; |
| 58 } |
| 59 |
| 60 void OnChangeProcessorSet() override { |
| 61 change_processor()->OnMetadataLoaded(syncer::SyncError(), |
| 62 db().CreateMetadataBatch()); |
| 63 } |
| 64 |
| 65 void AddObserver(Observer* observer) { observers_.insert(observer); } |
| 66 void RemoveObserver(Observer* observer) { observers_.erase(observer); } |
| 67 |
| 68 private: |
| 69 void NotifyObservers() { |
| 70 for (Observer* observer : observers_) { |
| 71 observer->OnApplySyncChanges(); |
| 72 } |
| 73 } |
| 74 |
| 75 std::set<Observer*> observers_; |
| 76 }; |
| 77 |
| 78 // A StatusChangeChecker for checking the status of keys in a |
| 79 // TestModelTypeService::Store. |
| 80 class KeyChecker : public StatusChangeChecker, |
| 81 public TestModelTypeService::Observer { |
| 82 public: |
| 83 KeyChecker(TestModelTypeService* service, const std::string& key) |
| 84 : service_(service), key_(key) {} |
| 85 ~KeyChecker() override {} |
| 86 |
| 87 void OnApplySyncChanges() override { CheckExitCondition(); } |
| 88 |
| 89 bool Wait() { |
| 90 if (IsExitConditionSatisfied()) { |
| 91 DVLOG(1) << "Wait() -> Exit before waiting: " << GetDebugMessage(); |
| 92 return true; |
| 93 } |
| 94 |
| 95 service_->AddObserver(this); |
| 96 StartBlockingWait(); |
| 97 service_->RemoveObserver(this); |
| 98 return !TimedOut(); |
| 99 } |
| 100 |
| 101 protected: |
| 102 TestModelTypeService* const service_; |
| 103 const std::string key_; |
| 104 }; |
| 105 |
| 106 // Wait for a key to be present. |
| 107 class KeyPresentChecker : public KeyChecker { |
| 108 public: |
| 109 KeyPresentChecker(TestModelTypeService* service, const std::string& key) |
| 110 : KeyChecker(service, key) {} |
| 111 ~KeyPresentChecker() override {} |
| 112 |
| 113 bool IsExitConditionSatisfied() override { |
| 114 return service_->db().HasData(key_); |
| 115 } |
| 116 |
| 117 std::string GetDebugMessage() const override { |
| 118 return "Waiting for key '" + key_ + "' to be present."; |
| 119 } |
| 120 }; |
| 121 |
| 122 // Wait for a key to be absent. |
| 123 class KeyAbsentChecker : public KeyChecker { |
| 124 public: |
| 125 KeyAbsentChecker(TestModelTypeService* service, const std::string& key) |
| 126 : KeyChecker(service, key) {} |
| 127 ~KeyAbsentChecker() override {} |
| 128 |
| 129 bool IsExitConditionSatisfied() override { |
| 130 return !service_->db().HasData(key_); |
| 131 } |
| 132 |
| 133 std::string GetDebugMessage() const override { |
| 134 return "Waiting for key '" + key_ + "' to be absent."; |
| 135 } |
| 136 }; |
| 137 |
| 138 class TwoClientUssSyncTest : public SyncTest { |
| 139 public: |
| 140 TwoClientUssSyncTest() : SyncTest(TWO_CLIENT) { |
| 141 DisableVerifier(); |
| 142 sync_client_factory_ = base::Bind(&TwoClientUssSyncTest::CreateSyncClient, |
| 143 base::Unretained(this)); |
| 144 ProfileSyncServiceFactory::SetSyncClientFactoryForTest( |
| 145 &sync_client_factory_); |
| 146 ProfileSyncComponentsFactoryImpl::OverridePrefsForUssTest(true); |
| 147 } |
| 148 |
| 149 ~TwoClientUssSyncTest() override { |
| 150 ProfileSyncServiceFactory::SetSyncClientFactoryForTest(nullptr); |
| 151 ProfileSyncComponentsFactoryImpl::OverridePrefsForUssTest(false); |
| 152 } |
| 153 |
| 154 bool TestUsesSelfNotifications() override { return false; } |
| 155 |
| 156 TestModelTypeService* GetModelTypeService(int i) { |
| 157 return services_.at(i).get(); |
| 158 } |
| 159 |
| 160 protected: |
| 161 std::unique_ptr<sync_driver::SyncClient> CreateSyncClient(Profile* profile) { |
| 162 if (!first_client_ignored_) { |
| 163 // The test infra creates a profile before the two made for sync tests. |
| 164 first_client_ignored_ = true; |
| 165 return base::MakeUnique<ChromeSyncClient>(profile); |
| 166 } |
| 167 auto service = base::MakeUnique<TestModelTypeService>(); |
| 168 auto client = base::MakeUnique<TestSyncClient>(profile, service.get()); |
| 169 clients_.push_back(client.get()); |
| 170 services_.push_back(std::move(service)); |
| 171 return std::move(client); |
| 172 } |
| 173 |
| 174 ProfileSyncServiceFactory::SyncClientFactory sync_client_factory_; |
| 175 std::vector<std::unique_ptr<TestModelTypeService>> services_; |
| 176 std::vector<TestSyncClient*> clients_; |
| 177 bool first_client_ignored_ = false; |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(TwoClientUssSyncTest); |
| 180 }; |
| 181 |
| 182 IN_PROC_BROWSER_TEST_F(TwoClientUssSyncTest, Sanity) { |
| 183 ASSERT_TRUE(SetupSync()); |
| 184 ASSERT_EQ(2U, clients_.size()); |
| 185 ASSERT_EQ(2U, services_.size()); |
| 186 TestModelTypeService* model1 = GetModelTypeService(0); |
| 187 TestModelTypeService* model2 = GetModelTypeService(1); |
| 188 |
| 189 model1->WriteItem("foo", "bar"); |
| 190 ASSERT_TRUE(KeyPresentChecker(model2, "foo").Wait()); |
| 191 EXPECT_EQ("bar", model2->db().GetValue("foo")); |
| 192 |
| 193 model1->DeleteItem("foo"); |
| 194 ASSERT_TRUE(KeyAbsentChecker(model2, "foo").Wait()); |
| 195 } |
| OLD | NEW |