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.
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"
21 namespace browser_sync
{
25 void ConvertDeviceInfoSpecifics(
26 const DeviceInfo
& device_info
,
27 sync_pb::DeviceInfoSpecifics
* specifics
) {
28 specifics
->set_cache_guid(device_info
.guid());
29 specifics
->set_client_name(device_info
.client_name());
30 specifics
->set_chrome_version(device_info
.chrome_version());
31 specifics
->set_sync_user_agent(device_info
.sync_user_agent());
32 specifics
->set_device_type(device_info
.device_type());
37 class SyncedDeviceTrackerTest
: public ::testing::Test
{
39 SyncedDeviceTrackerTest() : transaction_count_baseline_(0) { }
40 virtual ~SyncedDeviceTrackerTest() { }
42 virtual void SetUp() {
43 test_user_share_
.SetUp();
44 syncer::TestUserShare::CreateRoot(syncer::DEVICE_INFO
, user_share());
46 synced_device_tracker_
.reset(
47 new SyncedDeviceTracker(user_share(),
48 user_share()->directory
->cache_guid()));
50 // We don't actually touch the Profile, so we can get away with passing in a
51 // NULL here. Constructing a TestingProfile can take over a 100ms, so this
52 // optimization can be the difference between 'tests run with a noticeable
53 // delay' and 'tests run instantaneously'.
54 synced_device_tracker_
->Start(NULL
, user_share());
57 virtual void TearDown() {
58 synced_device_tracker_
.reset();
59 test_user_share_
.TearDown();
62 syncer::UserShare
* user_share() {
63 return test_user_share_
.user_share();
66 // Expose the private method to our tests.
67 void WriteLocalDeviceInfo(const DeviceInfo
& info
) {
68 synced_device_tracker_
->WriteLocalDeviceInfo(info
);
71 void WriteDeviceInfo(const DeviceInfo
& device_info
) {
72 sync_pb::DeviceInfoSpecifics specifics
;
73 ConvertDeviceInfoSpecifics(device_info
, &specifics
);
74 synced_device_tracker_
->WriteDeviceInfo(specifics
, device_info
.guid());
77 void ResetObservedChangesCounter() {
78 transaction_count_baseline_
= GetTotalTransactionsCount();
81 int GetObservedChangesCounter() {
82 return GetTotalTransactionsCount() - transaction_count_baseline_
;
85 scoped_ptr
<SyncedDeviceTracker
> synced_device_tracker_
;
88 // Count of how many closed WriteTransactions notified of meaningful changes.
89 int GetTotalTransactionsCount() {
90 base::RunLoop run_loop
;
91 run_loop
.RunUntilIdle();
92 return test_user_share_
.transaction_observer()->transactions_observed();
95 base::MessageLoop message_loop_
;
96 syncer::TestUserShare test_user_share_
;
97 int transaction_count_baseline_
;
102 // New client scenario: set device info when no previous info existed.
103 TEST_F(SyncedDeviceTrackerTest
, CreateNewDeviceInfo
) {
104 ASSERT_FALSE(synced_device_tracker_
->ReadLocalDeviceInfo());
106 ResetObservedChangesCounter();
108 // Include the non-ASCII character "’" (typographic apostrophe) in the client
109 // name to ensure that SyncedDeviceTracker can properly handle non-ASCII
110 // characters, which client names can include on some platforms (e.g., Mac
112 DeviceInfo
write_device_info(
113 user_share()->directory
->cache_guid(),
114 "John’s Device", "Chromium 3000", "ChromeSyncAgent 3000",
115 sync_pb::SyncEnums_DeviceType_TYPE_LINUX
);
116 WriteLocalDeviceInfo(write_device_info
);
118 scoped_ptr
<DeviceInfo
> read_device_info(
119 synced_device_tracker_
->ReadLocalDeviceInfo());
120 ASSERT_TRUE(read_device_info
);
121 EXPECT_TRUE(write_device_info
.Equals(*read_device_info
.get()));
123 EXPECT_EQ(1, GetObservedChangesCounter());
126 // Restart scenario: update existing device info with identical data.
127 TEST_F(SyncedDeviceTrackerTest
, DontModifyExistingDeviceInfo
) {
129 DeviceInfo
device_info(
130 user_share()->directory
->cache_guid(),
131 "John’s Device", "XYZ v1", "XYZ SyncAgent v1",
132 sync_pb::SyncEnums_DeviceType_TYPE_LINUX
);
133 WriteLocalDeviceInfo(device_info
);
136 scoped_ptr
<DeviceInfo
> old_device_info(
137 synced_device_tracker_
->ReadLocalDeviceInfo());
138 ASSERT_TRUE(old_device_info
);
140 ResetObservedChangesCounter();
142 // Overwrite the device info with the same data as before.
143 WriteLocalDeviceInfo(device_info
);
145 // Ensure that this didn't count as a change worth syncing.
146 EXPECT_EQ(0, GetObservedChangesCounter());
149 scoped_ptr
<DeviceInfo
> new_device_info(
150 synced_device_tracker_
->ReadLocalDeviceInfo());
151 ASSERT_TRUE(new_device_info
);
152 EXPECT_TRUE(old_device_info
->Equals(*new_device_info
.get()));
155 // Upgrade scenario: update existing device info with new version.
156 TEST_F(SyncedDeviceTrackerTest
, UpdateExistingDeviceInfo
) {
157 // Write v1 device info.
158 DeviceInfo
device_info_v1(
159 user_share()->directory
->cache_guid(),
160 "John’s Device", "XYZ v1", "XYZ SyncAgent v1",
161 sync_pb::SyncEnums_DeviceType_TYPE_LINUX
);
162 WriteLocalDeviceInfo(device_info_v1
);
164 ResetObservedChangesCounter();
166 // Write upgraded device info.
167 DeviceInfo
device_info_v2(
168 user_share()->directory
->cache_guid(),
169 "John’s Device", "XYZ v2", "XYZ SyncAgent v2",
170 sync_pb::SyncEnums_DeviceType_TYPE_LINUX
);
171 WriteLocalDeviceInfo(device_info_v2
);
174 scoped_ptr
<DeviceInfo
> result_device_info(
175 synced_device_tracker_
->ReadLocalDeviceInfo());
176 ASSERT_TRUE(result_device_info
);
178 EXPECT_TRUE(result_device_info
->Equals(device_info_v2
));
180 // The update write should have sent a nudge.
181 EXPECT_EQ(1, GetObservedChangesCounter());
184 // Test retrieving DeviceInfos for all the syncing devices.
185 TEST_F(SyncedDeviceTrackerTest
, GetAllDeviceInfo
) {
186 DeviceInfo
device_info1(
187 base::GenerateGUID(),
188 "abc Device", "XYZ v1", "XYZ SyncAgent v1",
189 sync_pb::SyncEnums_DeviceType_TYPE_LINUX
);
191 std::string guid1
= base::GenerateGUID();
193 DeviceInfo
device_info2(
194 base::GenerateGUID(),
195 "def Device", "XYZ v2", "XYZ SyncAgent v2",
196 sync_pb::SyncEnums_DeviceType_TYPE_LINUX
);
198 std::string guid2
= base::GenerateGUID();
200 WriteDeviceInfo(device_info1
);
201 WriteDeviceInfo(device_info2
);
203 ScopedVector
<DeviceInfo
> device_info
;
204 synced_device_tracker_
->GetAllSyncedDeviceInfo(&device_info
);
206 EXPECT_EQ(device_info
.size(), 2U);
207 EXPECT_TRUE(device_info
[0]->Equals(device_info1
));
208 EXPECT_TRUE(device_info
[1]->Equals(device_info2
));
213 } // namespace browser_sync