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

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

Issue 10824161: [Sync] Avoid unregistering object IDs on shutdown (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove now-unneeded param Created 8 years, 4 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "google/cacheinvalidation/types.pb.h" 5 #include "google/cacheinvalidation/types.pb.h"
6 #include "sync/notifier/mock_sync_notifier_observer.h"
6 #include "sync/notifier/sync_notifier_helper.h" 7 #include "sync/notifier/sync_notifier_helper.h"
7 #include "sync/notifier/mock_sync_notifier_observer.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 using testing::StrictMock;
11
12 namespace syncer { 10 namespace syncer {
13 11
12 namespace {
13
14 using testing::InSequence;
15 using testing::StrictMock;
16
17 const char kHandlerName1[] = "handler_name1";
18 const char kHandlerName2[] = "handler_name2";
19
14 class SyncNotifierHelperTest : public testing::Test { 20 class SyncNotifierHelperTest : public testing::Test {
15 protected: 21 protected:
16 SyncNotifierHelperTest() 22 SyncNotifierHelperTest()
17 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), 23 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"),
18 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), 24 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"),
19 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c") { 25 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c") {
20 } 26 }
21 27
22 invalidation::ObjectId kObjectId1; 28 invalidation::ObjectId kObjectId1;
23 invalidation::ObjectId kObjectId2; 29 invalidation::ObjectId kObjectId2;
24 invalidation::ObjectId kObjectId3; 30 invalidation::ObjectId kObjectId3;
25 }; 31 };
26 32
27 // Basic check that registrations are correctly updated for one handler. 33 // Basic check that registrations are correctly updated for one handler.
28 TEST_F(SyncNotifierHelperTest, Basic) { 34 TEST_F(SyncNotifierHelperTest, Basic) {
35 InSequence dummy;
36
29 SyncNotifierHelper helper; 37 SyncNotifierHelper helper;
38
30 StrictMock<MockSyncNotifierObserver> observer; 39 StrictMock<MockSyncNotifierObserver> observer;
40
41 ObjectIdPayloadMap expected_payload1;
42 expected_payload1[kObjectId1] = "1";
43 expected_payload1[kObjectId2] = "2";
44 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1,
45 REMOTE_NOTIFICATION));
46
47 ObjectIdPayloadMap expected_payload2;
48 expected_payload2[kObjectId2] = "2";
49 expected_payload2[kObjectId3] = "3";
50 EXPECT_CALL(observer, OnIncomingNotification(expected_payload2,
51 REMOTE_NOTIFICATION));
52
53 helper.SetHandler(kHandlerName1, &observer);
54
31 ObjectIdSet ids; 55 ObjectIdSet ids;
32 ids.insert(kObjectId1); 56 ids.insert(kObjectId1);
33 ids.insert(kObjectId2); 57 ids.insert(kObjectId2);
34 helper.UpdateRegisteredIds(&observer, ids); 58 helper.UpdateRegisteredIds(kHandlerName1, ids);
35 59
36 ObjectIdPayloadMap dispatched_payloads; 60 ObjectIdPayloadMap dispatched_payloads;
37 dispatched_payloads[kObjectId1] = "1"; 61 dispatched_payloads[kObjectId1] = "1";
38 dispatched_payloads[kObjectId2] = "2"; 62 dispatched_payloads[kObjectId2] = "2";
39 dispatched_payloads[kObjectId3] = "3"; 63 dispatched_payloads[kObjectId3] = "3";
40 64
41 // A object ID with no registration should be ignored. 65 // A object ID with no registration should be ignored.
42 ObjectIdPayloadMap expected_payload1;
43 expected_payload1[kObjectId1] = "1";
44 expected_payload1[kObjectId2] = "2";
45 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1,
46 REMOTE_NOTIFICATION));
47 helper.DispatchInvalidationsToHandlers(dispatched_payloads, 66 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
48 REMOTE_NOTIFICATION); 67 REMOTE_NOTIFICATION);
49 68
50 // Removed object IDs should not be notified, newly-added ones should. 69 // Removed object IDs should not be notified, newly-added ones should.
51 ids.erase(kObjectId1); 70 ids.erase(kObjectId1);
52 ids.insert(kObjectId3); 71 ids.insert(kObjectId3);
53 helper.UpdateRegisteredIds(&observer, ids); 72 helper.UpdateRegisteredIds(kHandlerName1, ids);
54 73
55 ObjectIdPayloadMap expected_payload2;
56 expected_payload2[kObjectId2] = "2";
57 expected_payload2[kObjectId3] = "3";
58 EXPECT_CALL(observer, OnIncomingNotification(expected_payload2,
59 REMOTE_NOTIFICATION));
60 helper.DispatchInvalidationsToHandlers(dispatched_payloads, 74 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
61 REMOTE_NOTIFICATION); 75 REMOTE_NOTIFICATION);
62 } 76 }
63 77
64 // Tests that we correctly bucket and dispatch invalidations on multiple objects 78 // Register some IDs and fire an invalidation. Since there are no set
65 // to the corresponding handlers. 79 // handlers when the invalidation is fired, nothing should happen.
66 TEST_F(SyncNotifierHelperTest, MultipleHandlers) { 80 TEST_F(SyncNotifierHelperTest, NullHandler) {
67 SyncNotifierHelper helper; 81 SyncNotifierHelper helper;
82
68 StrictMock<MockSyncNotifierObserver> observer; 83 StrictMock<MockSyncNotifierObserver> observer;
84 helper.SetHandler(kHandlerName1, &observer);
85
69 ObjectIdSet ids; 86 ObjectIdSet ids;
70 ids.insert(kObjectId1); 87 ids.insert(kObjectId1);
71 ids.insert(kObjectId2); 88 ids.insert(kObjectId2);
72 helper.UpdateRegisteredIds(&observer, ids); 89 helper.UpdateRegisteredIds(kHandlerName1, ids);
73 StrictMock<MockSyncNotifierObserver> observer2;
74 ObjectIdSet ids2;
75 ids2.insert(kObjectId3);
76 helper.UpdateRegisteredIds(&observer2, ids2);
77 90
91 helper.SetHandler(kHandlerName1, NULL);
92
93 ObjectIdPayloadMap dispatched_payloads;
94 dispatched_payloads[kObjectId1] = "1";
95 dispatched_payloads[kObjectId2] = "2";
96 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
97 REMOTE_NOTIFICATION);
98 }
99
100 // Tests that we correctly bucket and dispatch invalidations on
101 // multiple objects to the corresponding handlers.
102 TEST_F(SyncNotifierHelperTest, MultipleHandlers) {
103 SyncNotifierHelper helper;
104
105 StrictMock<MockSyncNotifierObserver> observer1;
78 ObjectIdPayloadMap expected_payload1; 106 ObjectIdPayloadMap expected_payload1;
79 expected_payload1[kObjectId1] = "1"; 107 expected_payload1[kObjectId1] = "1";
80 expected_payload1[kObjectId2] = "2"; 108 expected_payload1[kObjectId2] = "2";
81 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1, 109 EXPECT_CALL(observer1, OnIncomingNotification(expected_payload1,
82 REMOTE_NOTIFICATION)); 110 REMOTE_NOTIFICATION));
111 EXPECT_CALL(observer1, OnNotificationsEnabled());
112 EXPECT_CALL(observer1,
113 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
114
115 StrictMock<MockSyncNotifierObserver> observer2;
83 ObjectIdPayloadMap expected_payload2; 116 ObjectIdPayloadMap expected_payload2;
84 expected_payload2[kObjectId3] = "3"; 117 expected_payload2[kObjectId3] = "3";
85 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2, 118 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2,
86 REMOTE_NOTIFICATION)); 119 REMOTE_NOTIFICATION));
120 EXPECT_CALL(observer2, OnNotificationsEnabled());
121 EXPECT_CALL(observer2,
122 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
123
124 helper.SetHandler(kHandlerName1, &observer1);
125 helper.SetHandler(kHandlerName2, &observer2);
126
127 ObjectIdSet ids;
128 ids.insert(kObjectId1);
129 ids.insert(kObjectId2);
130 helper.UpdateRegisteredIds(kHandlerName1, ids);
131
132 ObjectIdSet ids2;
133 ids2.insert(kObjectId3);
134 helper.UpdateRegisteredIds(kHandlerName2, ids2);
87 135
88 ObjectIdPayloadMap dispatched_payloads; 136 ObjectIdPayloadMap dispatched_payloads;
89 dispatched_payloads[kObjectId1] = "1"; 137 dispatched_payloads[kObjectId1] = "1";
90 dispatched_payloads[kObjectId2] = "2"; 138 dispatched_payloads[kObjectId2] = "2";
91 dispatched_payloads[kObjectId3] = "3"; 139 dispatched_payloads[kObjectId3] = "3";
92 helper.DispatchInvalidationsToHandlers(dispatched_payloads, 140 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
93 REMOTE_NOTIFICATION); 141 REMOTE_NOTIFICATION);
94 142
95 // Also verify that the callbacks for OnNotificationsEnabled/Disabled work.
96 EXPECT_CALL(observer, OnNotificationsEnabled());
97 EXPECT_CALL(observer2, OnNotificationsEnabled());
98 EXPECT_CALL(observer,
99 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
100 EXPECT_CALL(observer2,
101 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
102 helper.EmitOnNotificationsEnabled(); 143 helper.EmitOnNotificationsEnabled();
103 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); 144 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR);
104 } 145 }
105 146
106 // Multiple registrations by different handlers on the same object ID should 147 // Multiple registrations by different handlers on the same object ID should
107 // cause a CHECK. 148 // cause a CHECK.
108 TEST_F(SyncNotifierHelperTest, MultipleRegistration) { 149 TEST_F(SyncNotifierHelperTest, MultipleRegistration) {
109 SyncNotifierHelper helper; 150 SyncNotifierHelper helper;
151
110 StrictMock<MockSyncNotifierObserver> observer; 152 StrictMock<MockSyncNotifierObserver> observer;
153 helper.SetHandler(kHandlerName1, &observer);
154
111 ObjectIdSet ids; 155 ObjectIdSet ids;
112 ids.insert(kObjectId1); 156 ids.insert(kObjectId1);
113 ids.insert(kObjectId2); 157 ids.insert(kObjectId2);
114 helper.UpdateRegisteredIds(&observer, ids); 158 helper.UpdateRegisteredIds(kHandlerName1, ids);
115 159
116 StrictMock<MockSyncNotifierObserver> observer2; 160 helper.DetachFromThreadForTest();
117 EXPECT_DEATH({ helper.UpdateRegisteredIds(&observer2, ids); }, 161 EXPECT_DEATH({ helper.UpdateRegisteredIds(kHandlerName2, ids); },
118 "Duplicate registration for .*"); 162 "Duplicate registration: .*");
163 }
164
165 // Setting the same handler for different names should cause a CHECK.
166 TEST_F(SyncNotifierHelperTest, SameHandlerDifferentName) {
167 SyncNotifierHelper helper;
168
169 StrictMock<MockSyncNotifierObserver> observer;
170 helper.SetHandler(kHandlerName1, &observer);
171
172 helper.DetachFromThreadForTest();
173 EXPECT_DEATH({ helper.SetHandler(kHandlerName2, &observer); },
174 "Observers can only be added once!");
119 } 175 }
120 176
121 // Make sure that passing an empty set to UpdateRegisteredIds clears the 177 // Make sure that passing an empty set to UpdateRegisteredIds clears the
122 // corresponding entries for the handler. 178 // corresponding entries for the handler.
123 TEST_F(SyncNotifierHelperTest, EmptySetUnregisters) { 179 TEST_F(SyncNotifierHelperTest, EmptySetUnregisters) {
124 SyncNotifierHelper helper; 180 SyncNotifierHelper helper;
125 StrictMock<MockSyncNotifierObserver> observer; 181
182 StrictMock<MockSyncNotifierObserver> observer1;
183 EXPECT_CALL(observer1, OnNotificationsEnabled());
184 EXPECT_CALL(observer1,
185 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
186
187 helper.SetHandler(kHandlerName1, &observer1);
188
126 ObjectIdSet ids; 189 ObjectIdSet ids;
127 ids.insert(kObjectId1); 190 ids.insert(kObjectId1);
128 ids.insert(kObjectId2); 191 ids.insert(kObjectId2);
129 helper.UpdateRegisteredIds(&observer, ids); 192 helper.UpdateRegisteredIds(kHandlerName1, ids);
193
130 // Control observer. 194 // Control observer.
131 StrictMock<MockSyncNotifierObserver> observer2; 195 StrictMock<MockSyncNotifierObserver> observer2;
132 ObjectIdSet ids2;
133 ids2.insert(kObjectId3);
134 helper.UpdateRegisteredIds(&observer2, ids2);
135 // Unregister the first observer. It should not receive any further callbacks.
136 helper.UpdateRegisteredIds(&observer, ObjectIdSet());
137
138 ObjectIdPayloadMap expected_payload2; 196 ObjectIdPayloadMap expected_payload2;
139 expected_payload2[kObjectId3] = "3"; 197 expected_payload2[kObjectId3] = "3";
140 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2, 198 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2,
141 REMOTE_NOTIFICATION)); 199 REMOTE_NOTIFICATION));
142 EXPECT_CALL(observer2, OnNotificationsEnabled()); 200 EXPECT_CALL(observer2, OnNotificationsEnabled());
143 EXPECT_CALL(observer2, 201 EXPECT_CALL(observer2,
144 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); 202 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
145 203
204 helper.SetHandler(kHandlerName2, &observer2);
205
206 ObjectIdSet ids2;
207 ids2.insert(kObjectId3);
208 helper.UpdateRegisteredIds(kHandlerName2, ids2);
209
210 // Unregister the first observer. It should not receive any further
211 // invalidations.
212 helper.UpdateRegisteredIds(kHandlerName1, ObjectIdSet());
213
146 ObjectIdPayloadMap dispatched_payloads; 214 ObjectIdPayloadMap dispatched_payloads;
147 dispatched_payloads[kObjectId1] = "1"; 215 dispatched_payloads[kObjectId1] = "1";
148 dispatched_payloads[kObjectId2] = "2"; 216 dispatched_payloads[kObjectId2] = "2";
149 dispatched_payloads[kObjectId3] = "3"; 217 dispatched_payloads[kObjectId3] = "3";
150 helper.DispatchInvalidationsToHandlers(dispatched_payloads, 218 helper.DispatchInvalidationsToHandlers(dispatched_payloads,
151 REMOTE_NOTIFICATION); 219 REMOTE_NOTIFICATION);
152 helper.EmitOnNotificationsEnabled(); 220 helper.EmitOnNotificationsEnabled();
153 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); 221 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR);
154 } 222 }
155 223
224 } // namespace
225
156 } // namespace syncer 226 } // namespace syncer
OLDNEW
« sync/notifier/sync_notifier_helper.cc ('K') | « sync/notifier/sync_notifier_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698