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

Side by Side Diff: chrome/browser/sync/glue/synced_device_tracker_unittest.cc

Issue 566623003: Refactor syncable DEVICE_INFO type from ChangeProcessor to SyncableService - part 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More CR feedback addressed in DeviceInfoSyncService. Created 6 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 (c) 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 <string>
6
7 #include "base/guid.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/sync/glue/device_info.h"
13 #include "chrome/browser/sync/glue/synced_device_tracker.h"
14 #include "sync/internal_api/public/base/model_type.h"
15 #include "sync/internal_api/public/test/test_user_share.h"
16 #include "sync/protocol/sync.pb.h"
17 #include "sync/syncable/directory.h"
18 #include "sync/test/test_transaction_observer.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace browser_sync {
22
23 class SyncedDeviceTrackerTest : public ::testing::Test {
24 protected:
25 SyncedDeviceTrackerTest() : transaction_count_baseline_(0) { }
26 virtual ~SyncedDeviceTrackerTest() { }
27
28 virtual void SetUp() {
29 test_user_share_.SetUp();
30 syncer::TestUserShare::CreateRoot(syncer::DEVICE_INFO, user_share());
31
32 synced_device_tracker_.reset(
33 new SyncedDeviceTracker(user_share(),
34 user_share()->directory->cache_guid()));
35
36 // We don't actually touch the Profile, so we can get away with passing in a
37 // NULL here. Constructing a TestingProfile can take over a 100ms, so this
38 // optimization can be the difference between 'tests run with a noticeable
39 // delay' and 'tests run instantaneously'.
40 synced_device_tracker_->Start(user_share());
41 }
42
43 virtual void TearDown() {
44 synced_device_tracker_.reset();
45 test_user_share_.TearDown();
46 }
47
48 syncer::UserShare* user_share() {
49 return test_user_share_.user_share();
50 }
51
52 // Expose the private method to our tests.
53 void WriteLocalDeviceInfo(const DeviceInfo& info) {
54 synced_device_tracker_->WriteLocalDeviceInfo(info);
55 }
56
57 void WriteDeviceInfo(const DeviceInfo& device_info) {
58 synced_device_tracker_->WriteDeviceInfo(device_info, device_info.guid());
59 }
60
61 void ResetObservedChangesCounter() {
62 transaction_count_baseline_ = GetTotalTransactionsCount();
63 }
64
65 int GetObservedChangesCounter() {
66 return GetTotalTransactionsCount() - transaction_count_baseline_;
67 }
68
69 scoped_ptr<SyncedDeviceTracker> synced_device_tracker_;
70
71 private:
72 // Count of how many closed WriteTransactions notified of meaningful changes.
73 int GetTotalTransactionsCount() {
74 base::RunLoop run_loop;
75 run_loop.RunUntilIdle();
76 return test_user_share_.transaction_observer()->transactions_observed();
77 }
78
79 base::MessageLoop message_loop_;
80 syncer::TestUserShare test_user_share_;
81 int transaction_count_baseline_;
82 };
83
84 namespace {
85
86 // New client scenario: set device info when no previous info existed.
87 TEST_F(SyncedDeviceTrackerTest, CreateNewDeviceInfo) {
88 ASSERT_FALSE(synced_device_tracker_->ReadLocalDeviceInfo());
89
90 ResetObservedChangesCounter();
91
92 // Include the non-ASCII character "’" (typographic apostrophe) in the client
93 // name to ensure that SyncedDeviceTracker can properly handle non-ASCII
94 // characters, which client names can include on some platforms (e.g., Mac
95 // and iOS).
96 DeviceInfo write_device_info(user_share()->directory->cache_guid(),
97 "John’s Device",
98 "Chromium 3000",
99 "ChromeSyncAgent 3000",
100 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
101 "device_id");
102 WriteLocalDeviceInfo(write_device_info);
103
104 scoped_ptr<DeviceInfo> read_device_info(
105 synced_device_tracker_->ReadLocalDeviceInfo());
106 ASSERT_TRUE(read_device_info);
107 EXPECT_TRUE(write_device_info.Equals(*read_device_info.get()));
108
109 EXPECT_EQ(1, GetObservedChangesCounter());
110 }
111
112 // Restart scenario: update existing device info with identical data.
113 TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) {
114 // For writing.
115 DeviceInfo device_info(user_share()->directory->cache_guid(),
116 "John’s Device",
117 "XYZ v1",
118 "XYZ SyncAgent v1",
119 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
120 "device_id");
121 WriteLocalDeviceInfo(device_info);
122
123 // First read.
124 scoped_ptr<DeviceInfo> old_device_info(
125 synced_device_tracker_->ReadLocalDeviceInfo());
126 ASSERT_TRUE(old_device_info);
127
128 ResetObservedChangesCounter();
129
130 // Overwrite the device info with the same data as before.
131 WriteLocalDeviceInfo(device_info);
132
133 // Ensure that this didn't count as a change worth syncing.
134 EXPECT_EQ(0, GetObservedChangesCounter());
135
136 // Second read.
137 scoped_ptr<DeviceInfo> new_device_info(
138 synced_device_tracker_->ReadLocalDeviceInfo());
139 ASSERT_TRUE(new_device_info);
140 EXPECT_TRUE(old_device_info->Equals(*new_device_info.get()));
141 }
142
143 // Upgrade scenario: update existing device info with new version.
144 TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) {
145 // Write v1 device info.
146 DeviceInfo device_info_v1(user_share()->directory->cache_guid(),
147 "John’s Device",
148 "XYZ v1",
149 "XYZ SyncAgent v1",
150 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
151 "device_id1");
152 WriteLocalDeviceInfo(device_info_v1);
153
154 ResetObservedChangesCounter();
155
156 // Write upgraded device info.
157 DeviceInfo device_info_v2(user_share()->directory->cache_guid(),
158 "John’s Device",
159 "XYZ v2",
160 "XYZ SyncAgent v2",
161 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
162 "device_id2");
163 WriteLocalDeviceInfo(device_info_v2);
164
165 // Verify result.
166 scoped_ptr<DeviceInfo> result_device_info(
167 synced_device_tracker_->ReadLocalDeviceInfo());
168 ASSERT_TRUE(result_device_info);
169
170 EXPECT_TRUE(result_device_info->Equals(device_info_v2));
171
172 // The update write should have sent a nudge.
173 EXPECT_EQ(1, GetObservedChangesCounter());
174 }
175
176 // Test retrieving DeviceInfos for all the syncing devices.
177 TEST_F(SyncedDeviceTrackerTest, GetAllDeviceInfo) {
178 DeviceInfo device_info1(base::GenerateGUID(),
179 "abc Device",
180 "XYZ v1",
181 "XYZ SyncAgent v1",
182 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
183 "device_id1");
184
185 std::string guid1 = base::GenerateGUID();
186
187 DeviceInfo device_info2(base::GenerateGUID(),
188 "def Device",
189 "XYZ v2",
190 "XYZ SyncAgent v2",
191 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
192 "device_id2");
193
194 std::string guid2 = base::GenerateGUID();
195
196 WriteDeviceInfo(device_info1);
197 WriteDeviceInfo(device_info2);
198
199 ScopedVector<DeviceInfo> device_info;
200 synced_device_tracker_->GetAllSyncedDeviceInfo(&device_info);
201
202 EXPECT_EQ(device_info.size(), 2U);
203 EXPECT_TRUE(device_info[0]->Equals(device_info1));
204 EXPECT_TRUE(device_info[1]->Equals(device_info2));
205 }
206
207 TEST_F(SyncedDeviceTrackerTest, DeviceBackupTime) {
208 DeviceInfo device_info(user_share()->directory->cache_guid(),
209 "John’s Device",
210 "XYZ v1",
211 "XYZ SyncAgent v1",
212 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
213 "device_id");
214 const base::Time test_backup_time =
215 base::Time::UnixEpoch() + base::TimeDelta::FromDays(10000);
216
217 WriteLocalDeviceInfo(device_info);
218 synced_device_tracker_->UpdateLocalDeviceBackupTime(test_backup_time);
219
220 // Verify read of device info and backup time.
221 EXPECT_EQ(test_backup_time,
222 synced_device_tracker_->GetLocalDeviceBackupTime());
223 scoped_ptr<DeviceInfo> device_info_out(
224 synced_device_tracker_->ReadLocalDeviceInfo());
225 ASSERT_TRUE(device_info_out);
226 EXPECT_TRUE(device_info.Equals(*device_info_out.get()));
227
228 // Verify backup time is not lost after updating device info.
229 DeviceInfo device_info2(user_share()->directory->cache_guid(),
230 "def Device",
231 "XYZ v2",
232 "XYZ SyncAgent v2",
233 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
234 "device_id");
235 WriteLocalDeviceInfo(device_info2);
236 EXPECT_EQ(test_backup_time,
237 synced_device_tracker_->GetLocalDeviceBackupTime());
238 }
239
240 } // namespace
241
242 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/synced_device_tracker.cc ('k') | chrome/browser/sync/profile_sync_components_factory_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698