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

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: Fix sync_client.cc 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/test_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(),
32 user_share()->directory->cache_guid()));
33
34 // We don't actually touch the Profile, so we can get away with passing in a
35 // NULL here. Constructing a TestingProfile can take over a 100ms, so this
36 // optimization can be the difference between 'tests run with a noticeable
37 // delay' and 'tests run instantaneously'.
38 synced_device_tracker_->Start(NULL, user_share());
39 }
40
41 void TearDown() {
42 synced_device_tracker_.reset();
43 test_user_share_.TearDown();
44 }
45
46 syncer::UserShare* user_share() {
47 return test_user_share_.user_share();
48 }
49
50 // Expose the private method to our tests.
51 void WriteLocalDeviceInfo(const DeviceInfo& info) {
52 synced_device_tracker_->WriteLocalDeviceInfo(info);
53 }
54
55 void ResetObservedChangesCounter() {
56 transaction_count_baseline_ = GetTotalTransactionsCount();
57 }
58
59 int GetObservedChangesCounter() {
60 return GetTotalTransactionsCount() - transaction_count_baseline_;
61 }
62
63 scoped_ptr<SyncedDeviceTracker> synced_device_tracker_;
64
65 private:
66 // Count of how many closed WriteTransactions notified of meaningful changes.
67 int GetTotalTransactionsCount() {
68 base::RunLoop run_loop;
69 run_loop.RunUntilIdle();
70 return test_user_share_.transaction_observer()->transactions_observed();
71 }
72
73 MessageLoop message_loop_;
74 syncer::TestUserShare test_user_share_;
75 int transaction_count_baseline_;
76 };
77
78 namespace {
79
80 // New client scenario: set device info when no previous info existed.
81 TEST_F(SyncedDeviceTrackerTest, CreateNewDeviceInfo) {
82 ASSERT_FALSE(synced_device_tracker_->ReadLocalDeviceInfo());
83
84 ResetObservedChangesCounter();
85
86 DeviceInfo write_device_info(
87 "Name", "Chromium 3000", "ChromeSyncAgent 3000",
88 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
89 WriteLocalDeviceInfo(write_device_info);
90
91 scoped_ptr<DeviceInfo> read_device_info(
92 synced_device_tracker_->ReadLocalDeviceInfo());
93 ASSERT_TRUE(read_device_info);
94 EXPECT_TRUE(write_device_info.Equals(*read_device_info.get()));
95
96 EXPECT_EQ(1, GetObservedChangesCounter());
97 }
98
99 // Restart scenario: update existing device info with identical data.
100 TEST_F(SyncedDeviceTrackerTest, DontModifyExistingDeviceInfo) {
101 // For writing.
102 DeviceInfo device_info(
103 "Name", "XYZ v1", "XYZ SyncAgent v1",
104 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
105 WriteLocalDeviceInfo(device_info);
106
107 // First read.
108 scoped_ptr<DeviceInfo> old_device_info(
109 synced_device_tracker_->ReadLocalDeviceInfo());
110 ASSERT_TRUE(old_device_info);
111
112 ResetObservedChangesCounter();
113
114 // Overwrite the device info with the same data as before.
115 WriteLocalDeviceInfo(device_info);
116
117 // Ensure that this didn't count as a change worth syncing.
118 EXPECT_EQ(0, GetObservedChangesCounter());
119
120 // Second read.
121 scoped_ptr<DeviceInfo> new_device_info(
122 synced_device_tracker_->ReadLocalDeviceInfo());
123 ASSERT_TRUE(new_device_info);
124 EXPECT_TRUE(old_device_info->Equals(*new_device_info.get()));
125 }
126
127 // Upgrade scenario: update existing device info with new version.
128 TEST_F(SyncedDeviceTrackerTest, UpdateExistingDeviceInfo) {
129 // Write v1 device info.
130 DeviceInfo device_info_v1(
131 "Name", "XYZ v1", "XYZ SyncAgent v1",
132 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
133 WriteLocalDeviceInfo(device_info_v1);
134
135 ResetObservedChangesCounter();
136
137 // Write upgraded device info.
138 DeviceInfo device_info_v2(
139 "Name", "XYZ v2", "XYZ SyncAgent v2",
140 sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
141 WriteLocalDeviceInfo(device_info_v2);
142
143 // Verify result.
144 scoped_ptr<DeviceInfo> result_device_info(
145 synced_device_tracker_->ReadLocalDeviceInfo());
146 ASSERT_TRUE(result_device_info);
147
148 EXPECT_TRUE(result_device_info->Equals(device_info_v2));
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
« no previous file with comments | « chrome/browser/sync/glue/synced_device_tracker.cc ('k') | chrome/browser/sync/profile_sync_service_session_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698