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

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

Issue 11360259: Sync: Add DeviceInfo's ChangeProcessor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month 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 | Annotate | Revision Log
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/memory/scoped_ptr.h"
8 #include "base/message_loop.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/sync/glue/device_info.h"
11 #include "chrome/browser/sync/glue/synced_device_tracker.h"
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/internal_api/public/test/test_user_share.h"
14 #include "sync/protocol/sync.pb.h"
15 #include "sync/syncable/directory.h"
16 #include "sync/test/mock_transaction_observer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace browser_sync {
20
21 class SyncedDeviceTrackerTest : public ::testing::Test {
22 protected:
23 SyncedDeviceTrackerTest() : transaction_count_baseline_(0) { }
24 ~SyncedDeviceTrackerTest() { }
25
26 void SetUp() {
27 test_user_share_.SetUp();
28 syncer::TestUserShare::CreateRoot(syncer::DEVICE_INFO, user_share());
29
30 synced_device_tracker_.reset(
31 new SyncedDeviceTracker(user_share()->directory->cache_guid()));
32
33 // We don't actually touch the Profile, so we can get away with passing in a
34 // NULL here. Constructing a TestingProfile can take over a 100ms, so this
35 // optimization can be the difference between 'tests run with a noticeable
36 // delay' and 'tests run instantaneously'.
37 synced_device_tracker_->Start(NULL, user_share());
38 }
39
40 void TearDown() {
41 synced_device_tracker_.reset();
42 test_user_share_.TearDown();
43 }
44
45 syncer::UserShare* user_share() {
46 return test_user_share_.user_share();
47 }
48
49 // Expose the private method to our tests.
50 void WriteLocalDeviceInfo(const DeviceInfo& info) {
51 synced_device_tracker_->WriteLocalDeviceInfo(info);
52 }
53
54 void ResetObservedChangesCounter() {
55 transaction_count_baseline_ = GetTotalTransactionsCount();
56 }
57
58 int GetObservedChangesCounter() {
59 return GetTotalTransactionsCount() - transaction_count_baseline_;
60 }
61
62 scoped_ptr<SyncedDeviceTracker> synced_device_tracker_;
63
64 private:
65 // Count of how many closed WriteTransactions notified of meaningful changes.
66 int GetTotalTransactionsCount() {
67 base::RunLoop run_loop;
68 run_loop.RunUntilIdle();
69 return test_user_share_.transaction_observer()->transactions_observed();
70 }
71
72 MessageLoop message_loop_;
73 syncer::TestUserShare test_user_share_;
74 int transaction_count_baseline_;
75 };
76
77 namespace {
78
79 // New client scenario: set device info when no previous info existed.
80 TEST_F(SyncedDeviceTrackerTest, CreateNewDeviceInfo) {
81 ASSERT_FALSE(synced_device_tracker_->ReadLocalDeviceInfo());
82
83 ResetObservedChangesCounter();
84
85 DeviceInfo write_device_info(
86 "Name", "Chromium 3000", "ChromeSyncAgent 3000",
87 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
88 WriteLocalDeviceInfo(write_device_info);
89
90 scoped_ptr<DeviceInfo> read_device_info(
91 synced_device_tracker_->ReadLocalDeviceInfo());
92 ASSERT_TRUE(read_device_info);
93 EXPECT_TRUE(write_device_info.Equals(*read_device_info.get()));
94
95 EXPECT_EQ(1, GetObservedChangesCounter());
96 }
97
98 // Restart scenario: update existing device info with identical data.
99 TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) {
100 // For writing.
101 DeviceInfo device_info(
102 "Name", "XYZ v1", "XYZ SyncAgent v1",
103 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
104 WriteLocalDeviceInfo(device_info);
105
106 // First read.
107 scoped_ptr<DeviceInfo> old_device_info(
108 synced_device_tracker_->ReadLocalDeviceInfo());
109 ASSERT_TRUE(old_device_info);
110
111 ResetObservedChangesCounter();
112
113 // Overwrite the device info with the same data as before.
114 WriteLocalDeviceInfo(device_info);
115
116 // Ensure that this didn't count as a change worth syncing.
117 EXPECT_EQ(0, GetObservedChangesCounter());
118
119 // Second read.
120 scoped_ptr<DeviceInfo> new_device_info(
121 synced_device_tracker_->ReadLocalDeviceInfo());
122 ASSERT_TRUE(new_device_info);
123 EXPECT_TRUE(old_device_info->Equals(*new_device_info.get()));
124 }
125
126 // Upgrade scenario: update existing device info with new version.
127 TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) {
128 // Write v1 device info.
129 DeviceInfo device_info_v1(
130 "Name", "XYZ v1", "XYZ SyncAgent v1",
131 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
132 WriteLocalDeviceInfo(device_info_v1);
133
134 ResetObservedChangesCounter();
135
136 // Write upgraded device info.
137 DeviceInfo device_info_v2(
138 "Name", "XYZ v2", "XYZ SyncAgent v2",
139 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
140 WriteLocalDeviceInfo(device_info_v2);
141
142 // Verify result.
143 scoped_ptr<DeviceInfo> result_device_info(
144 synced_device_tracker_->ReadLocalDeviceInfo());
145 ASSERT_TRUE(result_device_info);
146
147 EXPECT_EQ(device_info_v2.sync_user_agent(),
Nicolas Zea 2012/11/16 19:42:27 device_info_v2.equals(result_device_info)?
rlarocque 2012/11/17 00:07:21 Yes, it could make that assertion. Done.
148 result_device_info->sync_user_agent());
149
150 // The update write should have sent a nudge.
151 EXPECT_EQ(1, GetObservedChangesCounter());
152 }
153
154 } // namespace
155
156 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698