Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: chrome/browser/sync/test/integration/two_client_uss_sync_test.cc

Issue 2328393002: [Sync] Add a sanity integration test for USS. (Closed)
Patch Set: Ensure static changes don't persist between tests. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
skym 2016/09/12 19:57:49 Could probably get away with slapping const on lik
maxbogue 2016/09/13 03:44:54 Umm I've made the member vars in TestSyncClient an
26 : ChromeSyncClient(profile), service_(service) {}
27
28 base::WeakPtr<ModelTypeService> GetModelTypeServiceForType(
29 syncer::ModelType type) override {
30 if (type != syncer::PREFERENCES) {
skym 2016/09/12 19:57:49 Might be more elegant as a ternary.
maxbogue 2016/09/13 03:44:54 Done.
31 return ChromeSyncClient::GetModelTypeServiceForType(type);
32 }
33 return service_->AsWeakPtr();
34 }
35
36 private:
37 ModelTypeService* service_;
38 };
39
40 // A FakeModelTypeService that supports observing ApplySyncChanges.
41 class TestModelTypeService : public FakeModelTypeService {
42 public:
43 class Observer {
44 public:
45 virtual void OnApplySyncChanges() = 0;
46 };
47
48 TestModelTypeService()
49 : FakeModelTypeService(
50 base::Bind(&SharedModelTypeProcessor::CreateAsChangeProcessor)) {}
51
52 syncer::SyncError ApplySyncChanges(
53 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_changes,
54 syncer_v2::EntityChangeList entity_changes) override {
55 syncer::SyncError error = FakeModelTypeService::ApplySyncChanges(
56 std::move(metadata_changes), entity_changes);
57 NotifyObservers();
58 return error;
59 }
60
61 void OnChangeProcessorSet() override {
62 change_processor()->OnMetadataLoaded(syncer::SyncError(),
63 db().CreateMetadataBatch());
64 }
65
66 void AddObserver(Observer* observer) { observers_.insert(observer); }
67 void RemoveObserver(Observer* observer) { observers_.erase(observer); }
68
69 private:
70 void NotifyObservers() {
71 for (Observer* observer : observers_) {
72 observer->OnApplySyncChanges();
73 }
74 }
75
76 std::set<Observer*> observers_;
77 };
78
79 // A StatusChangeChecker for checking the status of keys in a
80 // TestModelTypeService::Store.
81 class KeyChecker : public StatusChangeChecker,
82 public TestModelTypeService::Observer {
83 public:
84 KeyChecker(TestModelTypeService* service, const std::string& key)
85 : service_(service), key_(key) {}
86 ~KeyChecker() override {}
87
88 void OnApplySyncChanges() override { CheckExitCondition(); }
89
90 void Wait() {
91 if (IsExitConditionSatisfied()) {
92 DVLOG(1) << "Wait() -> Exit before waiting: " << GetDebugMessage();
93 return;
94 }
95
96 service_->AddObserver(this);
97 StartBlockingWait();
98 service_->RemoveObserver(this);
99 }
100
101 protected:
102 TestModelTypeService* 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);
skym 2016/09/12 19:57:49 All these test cases run in serial, right? Even wh
maxbogue 2016/09/13 03:44:54 My understanding is that they run in serial within
151 ProfileSyncComponentsFactoryImpl::OverridePrefsForUssTest(false);
skym 2016/09/12 19:57:50 Does this get called if you crash mid test? Could
maxbogue 2016/09/13 03:44:54 The flag is just in memory. If we crash mid-test i
152 }
153
154 bool TestUsesSelfNotifications() override { return false; }
155
156 TestModelTypeService* GetModelTypeService(int i) {
157 return services_.at(i).get();
158 }
159
160 static bool WaitForKeyPresent(TestModelTypeService* model,
skym 2016/09/12 19:57:49 Why not push these methods into the checker classe
maxbogue 2016/09/13 03:44:54 I think this way reads a little better. Otherwise
skym 2016/09/13 15:54:32 Remember that you have your arguments in there giv
maxbogue 2016/09/13 19:15:26 I've gone with KeyPresentChecker(model, "key").Wai
161 const std::string& key) {
162 KeyPresentChecker checker(model, key);
163 checker.Wait();
164 return !checker.TimedOut();
165 }
166
167 static bool WaitForKeyAbsent(TestModelTypeService* model,
168 const std::string& key) {
169 KeyAbsentChecker checker(model, key);
170 checker.Wait();
171 return !checker.TimedOut();
172 }
173
174 protected:
175 std::unique_ptr<sync_driver::SyncClient> CreateSyncClient(Profile* profile) {
176 auto service = base::MakeUnique<TestModelTypeService>();
177 auto client = base::MakeUnique<TestSyncClient>(profile, service.get());
178 clients_.push_back(client.get());
179 services_.push_back(std::move(service));
180 return std::move(client);
181 }
182
183 ProfileSyncServiceFactory::SyncClientFactory sync_client_factory_;
184 std::vector<std::unique_ptr<TestModelTypeService>> services_;
185 std::vector<TestSyncClient*> clients_;
186
187 DISALLOW_COPY_AND_ASSIGN(TwoClientUssSyncTest);
188 };
189
190 IN_PROC_BROWSER_TEST_F(TwoClientUssSyncTest, Sanity) {
191 ASSERT_TRUE(SetupSync());
192 // There are three because the test infra creates a profile before the two
skym 2016/09/12 19:57:50 This seems awkward, like something we may want to
maxbogue 2016/09/13 03:44:54 Yes, but I have no way of overwriting the thing on
193 // made for our tests. Ignore index 0 and use indexes 1 and 2.
skym 2016/09/12 19:57:50 So, technically 3 client?
maxbogue 2016/09/13 03:44:54 Nothing ever happens with sync in the original pro
194 ASSERT_EQ(3U, clients_.size());
195 ASSERT_EQ(3U, services_.size());
196 TestModelTypeService* model1 = GetModelTypeService(1);
197 TestModelTypeService* model2 = GetModelTypeService(2);
198
199 model1->WriteItem("foo", "bar");
200 ASSERT_TRUE(WaitForKeyPresent(model2, "foo"));
201 EXPECT_EQ("bar", model2->db().GetValue("foo"));
202
203 model1->DeleteItem("foo");
204 ASSERT_TRUE(WaitForKeyAbsent(model2, "foo"));
205 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698