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

Side by Side Diff: sync/notifier/sync_notifier_helper_unittest.cc

Issue 10702074: Refactor sync-specific parts out of SyncNotifier/SyncNotifierObserver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now with tests Created 8 years, 5 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 | 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 "sync/notifier/sync_notifier_helper.h"
6 #include "sync/notifier/mock_sync_notifier_observer.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 using testing::StrictMock;
10
11 namespace syncer {
12
13 namespace {
14
15 const int kTestSourceId = 4; // Specified by Tango for testing.
16
17 } // namespace
18
19 class SyncNotifierHelperTest : public testing::Test {
20 protected:
21 SyncNotifierHelperTest() : kObjectId1(kTestSourceId, "a"),
22 kObjectId2(kTestSourceId, "b"),
23 kObjectId3(kTestSourceId, "c") {
24 }
25
26 invalidation::ObjectId kObjectId1;
27 invalidation::ObjectId kObjectId2;
28 invalidation::ObjectId kObjectId3;
29 };
30
31 TEST_F(SyncNotifierHelperTest, Basic) {
32 SyncNotifierHelper helper;
33 StrictMock<MockSyncNotifierObserver> observer;
34 ObjectIdSet ids;
35 ids.insert(kObjectId1);
36 ids.insert(kObjectId2);
37 helper.UpdateRegisteredIds(&observer, ids);
38
39 ObjectIdPayloadMap dispatched_payloads;
40 dispatched_payloads[kObjectId1] = "1";
41 dispatched_payloads[kObjectId2] = "2";
42 dispatched_payloads[kObjectId3] = "3";
43
44 // A object ID with no registration should be ignored.
45 ObjectIdPayloadMap expected_payload1;
46 expected_payload1[kObjectId1] = "1";
47 expected_payload1[kObjectId2] = "2";
48 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1,
49 syncer::REMOTE_NOTIFICATION));
50 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
51 syncer::REMOTE_NOTIFICATION);
52
53 // Removed object IDs should not be notified, newly-added ones should.
54 ids.erase(kObjectId1);
55 ids.insert(kObjectId3);
56 helper.UpdateRegisteredIds(&observer, ids);
57
58 ObjectIdPayloadMap expected_payload2;
59 expected_payload2[kObjectId2] = "2";
60 expected_payload2[kObjectId3] = "3";
61 EXPECT_CALL(observer, OnIncomingNotification(expected_payload2,
62 syncer::REMOTE_NOTIFICATION));
63 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
64 syncer::REMOTE_NOTIFICATION);
65 }
66
67 // Tests that we correctly bucket and dispatch invalidations on multiple objects
68 // to the corresponding handlers.
69 TEST_F(SyncNotifierHelperTest, MultipleHandlers) {
70 SyncNotifierHelper helper;
71 StrictMock<MockSyncNotifierObserver> observer;
72 ObjectIdSet ids;
73 ids.insert(kObjectId1);
74 ids.insert(kObjectId2);
75 helper.UpdateRegisteredIds(&observer, ids);
76 StrictMock<MockSyncNotifierObserver> observer2;
77 ObjectIdSet ids2;
78 ids2.insert(kObjectId3);
79 helper.UpdateRegisteredIds(&observer2, ids2);
80
81 ObjectIdPayloadMap expected_payload1;
82 expected_payload1[kObjectId1] = "1";
83 expected_payload1[kObjectId2] = "2";
84 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1,
85 syncer::REMOTE_NOTIFICATION));
86 ObjectIdPayloadMap expected_payload2;
87 expected_payload2[kObjectId3] = "3";
88 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2,
89 syncer::REMOTE_NOTIFICATION));
90
91 ObjectIdPayloadMap dispatched_payloads;
92 dispatched_payloads[kObjectId1] = "1";
93 dispatched_payloads[kObjectId2] = "2";
94 dispatched_payloads[kObjectId3] = "3";
95 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
96 syncer::REMOTE_NOTIFICATION);
97
98 // Also verify that the callbacks for OnNotificationsEnabled/Disabled work.
99 EXPECT_CALL(observer, OnNotificationsEnabled());
100 EXPECT_CALL(observer2, OnNotificationsEnabled());
101 EXPECT_CALL(observer,
102 OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR));
103 EXPECT_CALL(observer2,
104 OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR));
105 helper.OnNotificationsEnabled();
106 helper.OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR);
107 }
108
109 TEST_F(SyncNotifierHelperTest, MultipleRegistration) {
110 SyncNotifierHelper helper;
111 StrictMock<MockSyncNotifierObserver> observer;
112 ObjectIdSet ids;
113 ids.insert(kObjectId1);
114 ids.insert(kObjectId2);
115 helper.UpdateRegisteredIds(&observer, ids);
116
117 StrictMock<MockSyncNotifierObserver> observer2;
118 helper.UpdateRegisteredIds(&observer2, ids);
119
120 // Only the first observer should receive notifications.
121 ObjectIdPayloadMap expected_payload;
122 expected_payload[kObjectId1] = "1";
123 expected_payload[kObjectId2] = "2";
124 EXPECT_CALL(observer, OnIncomingNotification(expected_payload,
125 syncer::REMOTE_NOTIFICATION));
126
127 ObjectIdPayloadMap dispatched_payloads;
128 dispatched_payloads[kObjectId1] = "1";
129 dispatched_payloads[kObjectId2] = "2";
130 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
131 syncer::REMOTE_NOTIFICATION);
132 }
133
134 TEST_F(SyncNotifierHelperTest, EmptySetUnregisters) {
135 SyncNotifierHelper helper;
136 StrictMock<MockSyncNotifierObserver> observer;
137 ObjectIdSet ids;
138 ids.insert(kObjectId1);
139 ids.insert(kObjectId2);
140 helper.UpdateRegisteredIds(&observer, ids);
141 // Control observer.
142 StrictMock<MockSyncNotifierObserver> observer2;
143 ObjectIdSet ids2;
144 ids2.insert(kObjectId3);
145 helper.UpdateRegisteredIds(&observer2, ids2);
146 // Unregister the first observer. It should not receive any further callbacks.
147 helper.UpdateRegisteredIds(&observer, ObjectIdSet());
148
149 ObjectIdPayloadMap expected_payload2;
150 expected_payload2[kObjectId3] = "3";
151 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2,
152 syncer::REMOTE_NOTIFICATION));
153 EXPECT_CALL(observer2, OnNotificationsEnabled());
154 EXPECT_CALL(observer2,
155 OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR));
156
157 ObjectIdPayloadMap dispatched_payloads;
158 dispatched_payloads[kObjectId1] = "1";
159 dispatched_payloads[kObjectId2] = "2";
160 dispatched_payloads[kObjectId3] = "3";
161 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
162 syncer::REMOTE_NOTIFICATION);
163 helper.OnNotificationsEnabled();
164 helper.OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR);
165 }
166
167 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698