OLD | NEW |
---|---|
(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. | |
akalin
2012/07/21 01:09:47
is this in the tango headers anywhere?
dcheng
2012/07/21 14:06:53
Done.
| |
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) { | |
akalin
2012/07/21 01:09:47
add test-level comments for each test in this file
dcheng
2012/07/21 14:06:53
Done.
| |
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 REMOTE_NOTIFICATION)); | |
50 helper.DispatchInvalidationsToHandlers(dispatched_payloads, | |
51 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 REMOTE_NOTIFICATION)); | |
63 helper.DispatchInvalidationsToHandlers(dispatched_payloads, | |
64 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 REMOTE_NOTIFICATION)); | |
86 ObjectIdPayloadMap expected_payload2; | |
87 expected_payload2[kObjectId3] = "3"; | |
88 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2, | |
89 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 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(TRANSIENT_NOTIFICATION_ERROR)); | |
103 EXPECT_CALL(observer2, | |
104 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
105 helper.EmitOnNotificationsEnabled(); | |
106 helper.EmitOnNotificationsDisabled(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 EXPECT_DEATH({helper.UpdateRegisteredIds(&observer2, ids); }, | |
akalin
2012/07/21 01:09:47
space after {
dcheng
2012/07/21 14:06:53
Done.
| |
119 "Duplicate registration for .*"); | |
120 } | |
121 | |
122 TEST_F(SyncNotifierHelperTest, EmptySetUnregisters) { | |
123 SyncNotifierHelper helper; | |
124 StrictMock<MockSyncNotifierObserver> observer; | |
125 ObjectIdSet ids; | |
126 ids.insert(kObjectId1); | |
127 ids.insert(kObjectId2); | |
128 helper.UpdateRegisteredIds(&observer, ids); | |
129 // Control observer. | |
130 StrictMock<MockSyncNotifierObserver> observer2; | |
131 ObjectIdSet ids2; | |
132 ids2.insert(kObjectId3); | |
133 helper.UpdateRegisteredIds(&observer2, ids2); | |
134 // Unregister the first observer. It should not receive any further callbacks. | |
135 helper.UpdateRegisteredIds(&observer, ObjectIdSet()); | |
136 | |
137 ObjectIdPayloadMap expected_payload2; | |
138 expected_payload2[kObjectId3] = "3"; | |
139 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2, | |
140 REMOTE_NOTIFICATION)); | |
141 EXPECT_CALL(observer2, OnNotificationsEnabled()); | |
142 EXPECT_CALL(observer2, | |
143 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
144 | |
145 ObjectIdPayloadMap dispatched_payloads; | |
146 dispatched_payloads[kObjectId1] = "1"; | |
147 dispatched_payloads[kObjectId2] = "2"; | |
148 dispatched_payloads[kObjectId3] = "3"; | |
149 helper.DispatchInvalidationsToHandlers(dispatched_payloads, | |
150 REMOTE_NOTIFICATION); | |
151 helper.EmitOnNotificationsEnabled(); | |
152 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); | |
153 } | |
154 | |
155 } // namespace syncer | |
OLD | NEW |