OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> |
| 6 |
5 #include <string> | 7 #include <string> |
6 | 8 |
7 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
8 #include "components/invalidation/impl/push_client_channel.h" | 10 #include "components/invalidation/impl/push_client_channel.h" |
9 #include "jingle/notifier/listener/fake_push_client.h" | 11 #include "jingle/notifier/listener/fake_push_client.h" |
10 #include "jingle/notifier/listener/notification_defines.h" | 12 #include "jingle/notifier/listener/notification_defines.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 namespace syncer { | 15 namespace syncer { |
14 namespace { | 16 namespace { |
(...skipping 28 matching lines...) Expand all Loading... |
43 } | 45 } |
44 | 46 |
45 notifier::FakePushClient* fake_push_client_; | 47 notifier::FakePushClient* fake_push_client_; |
46 PushClientChannel push_client_channel_; | 48 PushClientChannel push_client_channel_; |
47 std::string last_message_; | 49 std::string last_message_; |
48 InvalidatorState last_invalidator_state_; | 50 InvalidatorState last_invalidator_state_; |
49 }; | 51 }; |
50 | 52 |
51 const char kMessage[] = "message"; | 53 const char kMessage[] = "message"; |
52 const char kServiceContext[] = "service context"; | 54 const char kServiceContext[] = "service context"; |
53 const int64 kSchedulingHash = 100; | 55 const int64_t kSchedulingHash = 100; |
54 | 56 |
55 // Make sure the channel subscribes to the correct notifications | 57 // Make sure the channel subscribes to the correct notifications |
56 // channel on construction. | 58 // channel on construction. |
57 TEST_F(PushClientChannelTest, Subscriptions) { | 59 TEST_F(PushClientChannelTest, Subscriptions) { |
58 notifier::Subscription expected_subscription; | 60 notifier::Subscription expected_subscription; |
59 expected_subscription.channel = "tango_raw"; | 61 expected_subscription.channel = "tango_raw"; |
60 EXPECT_TRUE(notifier::SubscriptionListsEqual( | 62 EXPECT_TRUE(notifier::SubscriptionListsEqual( |
61 fake_push_client_->subscriptions(), | 63 fake_push_client_->subscriptions(), |
62 notifier::SubscriptionList(1, expected_subscription))); | 64 notifier::SubscriptionList(1, expected_subscription))); |
63 } | 65 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 fake_push_client_->sent_notifications()[0].data); | 105 fake_push_client_->sent_notifications()[0].data); |
104 } | 106 } |
105 | 107 |
106 // Encode a message with some context and then decode it. The decoded info | 108 // Encode a message with some context and then decode it. The decoded info |
107 // should match the original info. | 109 // should match the original info. |
108 TEST_F(PushClientChannelTest, EncodeDecode) { | 110 TEST_F(PushClientChannelTest, EncodeDecode) { |
109 const std::string& data = PushClientChannel::EncodeMessageForTest( | 111 const std::string& data = PushClientChannel::EncodeMessageForTest( |
110 kMessage, kServiceContext, kSchedulingHash); | 112 kMessage, kServiceContext, kSchedulingHash); |
111 std::string message; | 113 std::string message; |
112 std::string service_context; | 114 std::string service_context; |
113 int64 scheduling_hash = 0LL; | 115 int64_t scheduling_hash = 0LL; |
114 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( | 116 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( |
115 data, &message, &service_context, &scheduling_hash)); | 117 data, &message, &service_context, &scheduling_hash)); |
116 EXPECT_EQ(kMessage, message); | 118 EXPECT_EQ(kMessage, message); |
117 EXPECT_EQ(kServiceContext, service_context); | 119 EXPECT_EQ(kServiceContext, service_context); |
118 EXPECT_EQ(kSchedulingHash, scheduling_hash); | 120 EXPECT_EQ(kSchedulingHash, scheduling_hash); |
119 } | 121 } |
120 | 122 |
121 // Encode a message with no context and then decode it. The decoded message | 123 // Encode a message with no context and then decode it. The decoded message |
122 // should match the original message, but the context and hash should be | 124 // should match the original message, but the context and hash should be |
123 // untouched. | 125 // untouched. |
124 TEST_F(PushClientChannelTest, EncodeDecodeNoContext) { | 126 TEST_F(PushClientChannelTest, EncodeDecodeNoContext) { |
125 const std::string& data = PushClientChannel::EncodeMessageForTest( | 127 const std::string& data = PushClientChannel::EncodeMessageForTest( |
126 kMessage, std::string(), kSchedulingHash); | 128 kMessage, std::string(), kSchedulingHash); |
127 std::string message; | 129 std::string message; |
128 std::string service_context = kServiceContext; | 130 std::string service_context = kServiceContext; |
129 int64 scheduling_hash = kSchedulingHash + 1; | 131 int64_t scheduling_hash = kSchedulingHash + 1; |
130 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( | 132 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( |
131 data, &message, &service_context, &scheduling_hash)); | 133 data, &message, &service_context, &scheduling_hash)); |
132 EXPECT_EQ(kMessage, message); | 134 EXPECT_EQ(kMessage, message); |
133 EXPECT_EQ(kServiceContext, service_context); | 135 EXPECT_EQ(kServiceContext, service_context); |
134 EXPECT_EQ(kSchedulingHash + 1, scheduling_hash); | 136 EXPECT_EQ(kSchedulingHash + 1, scheduling_hash); |
135 } | 137 } |
136 | 138 |
137 // Decode an empty notification. It should result in an empty message | 139 // Decode an empty notification. It should result in an empty message |
138 // but should leave the context and hash untouched. | 140 // but should leave the context and hash untouched. |
139 TEST_F(PushClientChannelTest, DecodeEmpty) { | 141 TEST_F(PushClientChannelTest, DecodeEmpty) { |
140 std::string message = kMessage; | 142 std::string message = kMessage; |
141 std::string service_context = kServiceContext; | 143 std::string service_context = kServiceContext; |
142 int64 scheduling_hash = kSchedulingHash; | 144 int64_t scheduling_hash = kSchedulingHash; |
143 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( | 145 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( |
144 std::string(), &message, &service_context, &scheduling_hash)); | 146 std::string(), &message, &service_context, &scheduling_hash)); |
145 EXPECT_TRUE(message.empty()); | 147 EXPECT_TRUE(message.empty()); |
146 EXPECT_EQ(kServiceContext, service_context); | 148 EXPECT_EQ(kServiceContext, service_context); |
147 EXPECT_EQ(kSchedulingHash, scheduling_hash); | 149 EXPECT_EQ(kSchedulingHash, scheduling_hash); |
148 } | 150 } |
149 | 151 |
150 // Try to decode a garbage notification. It should leave all its | 152 // Try to decode a garbage notification. It should leave all its |
151 // arguments untouched and return false. | 153 // arguments untouched and return false. |
152 TEST_F(PushClientChannelTest, DecodeGarbage) { | 154 TEST_F(PushClientChannelTest, DecodeGarbage) { |
153 std::string data = "garbage"; | 155 std::string data = "garbage"; |
154 std::string message = kMessage; | 156 std::string message = kMessage; |
155 std::string service_context = kServiceContext; | 157 std::string service_context = kServiceContext; |
156 int64 scheduling_hash = kSchedulingHash; | 158 int64_t scheduling_hash = kSchedulingHash; |
157 EXPECT_FALSE(PushClientChannel::DecodeMessageForTest( | 159 EXPECT_FALSE(PushClientChannel::DecodeMessageForTest( |
158 data, &message, &service_context, &scheduling_hash)); | 160 data, &message, &service_context, &scheduling_hash)); |
159 EXPECT_EQ(kMessage, message); | 161 EXPECT_EQ(kMessage, message); |
160 EXPECT_EQ(kServiceContext, service_context); | 162 EXPECT_EQ(kServiceContext, service_context); |
161 EXPECT_EQ(kSchedulingHash, scheduling_hash); | 163 EXPECT_EQ(kSchedulingHash, scheduling_hash); |
162 } | 164 } |
163 | 165 |
164 // Simulate an incoming notification. It should be decoded properly | 166 // Simulate an incoming notification. It should be decoded properly |
165 // by the channel. | 167 // by the channel. |
166 TEST_F(PushClientChannelTest, OnIncomingMessage) { | 168 TEST_F(PushClientChannelTest, OnIncomingMessage) { |
(...skipping 14 matching lines...) Expand all Loading... |
181 // the channel. | 183 // the channel. |
182 TEST_F(PushClientChannelTest, OnIncomingMessageNoReceiver) { | 184 TEST_F(PushClientChannelTest, OnIncomingMessageNoReceiver) { |
183 push_client_channel_.SetMessageReceiver(NULL); | 185 push_client_channel_.SetMessageReceiver(NULL); |
184 | 186 |
185 notifier::Notification notification; | 187 notifier::Notification notification; |
186 notification.data = PushClientChannel::EncodeMessageForTest( | 188 notification.data = PushClientChannel::EncodeMessageForTest( |
187 kMessage, kServiceContext, kSchedulingHash); | 189 kMessage, kServiceContext, kSchedulingHash); |
188 fake_push_client_->SimulateIncomingNotification(notification); | 190 fake_push_client_->SimulateIncomingNotification(notification); |
189 | 191 |
190 EXPECT_TRUE(push_client_channel_.GetServiceContextForTest().empty()); | 192 EXPECT_TRUE(push_client_channel_.GetServiceContextForTest().empty()); |
191 EXPECT_EQ(static_cast<int64>(0), | 193 EXPECT_EQ(static_cast<int64_t>(0), |
192 push_client_channel_.GetSchedulingHashForTest()); | 194 push_client_channel_.GetSchedulingHashForTest()); |
193 EXPECT_TRUE(last_message_.empty()); | 195 EXPECT_TRUE(last_message_.empty()); |
194 } | 196 } |
195 | 197 |
196 // Simulate an incoming garbage notification. It should be dropped by | 198 // Simulate an incoming garbage notification. It should be dropped by |
197 // the channel. | 199 // the channel. |
198 TEST_F(PushClientChannelTest, OnIncomingMessageGarbage) { | 200 TEST_F(PushClientChannelTest, OnIncomingMessageGarbage) { |
199 notifier::Notification notification; | 201 notifier::Notification notification; |
200 notification.data = "garbage"; | 202 notification.data = "garbage"; |
201 fake_push_client_->SimulateIncomingNotification(notification); | 203 fake_push_client_->SimulateIncomingNotification(notification); |
202 EXPECT_TRUE(push_client_channel_.GetServiceContextForTest().empty()); | 204 EXPECT_TRUE(push_client_channel_.GetServiceContextForTest().empty()); |
203 EXPECT_EQ(static_cast<int64>(0), | 205 EXPECT_EQ(static_cast<int64_t>(0), |
204 push_client_channel_.GetSchedulingHashForTest()); | 206 push_client_channel_.GetSchedulingHashForTest()); |
205 EXPECT_TRUE(last_message_.empty()); | 207 EXPECT_TRUE(last_message_.empty()); |
206 } | 208 } |
207 | 209 |
208 // Send a message, simulate an incoming message with context, and then | 210 // Send a message, simulate an incoming message with context, and then |
209 // send the same message again. The first sent message should not | 211 // send the same message again. The first sent message should not |
210 // have any context, but the second sent message should have the | 212 // have any context, but the second sent message should have the |
211 // context from the incoming emssage. | 213 // context from the incoming emssage. |
212 TEST_F(PushClientChannelTest, PersistedMessageState) { | 214 TEST_F(PushClientChannelTest, PersistedMessageState) { |
213 push_client_channel_.SendMessage(kMessage); | 215 push_client_channel_.SendMessage(kMessage); |
214 ASSERT_EQ(1u, fake_push_client_->sent_notifications().size()); | 216 ASSERT_EQ(1u, fake_push_client_->sent_notifications().size()); |
215 { | 217 { |
216 std::string message; | 218 std::string message; |
217 std::string service_context; | 219 std::string service_context; |
218 int64 scheduling_hash = 0LL; | 220 int64_t scheduling_hash = 0LL; |
219 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( | 221 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( |
220 fake_push_client_->sent_notifications()[0].data, | 222 fake_push_client_->sent_notifications()[0].data, |
221 &message, | 223 &message, |
222 &service_context, | 224 &service_context, |
223 &scheduling_hash)); | 225 &scheduling_hash)); |
224 EXPECT_EQ(kMessage, message); | 226 EXPECT_EQ(kMessage, message); |
225 EXPECT_TRUE(service_context.empty()); | 227 EXPECT_TRUE(service_context.empty()); |
226 EXPECT_EQ(0LL, scheduling_hash); | 228 EXPECT_EQ(0LL, scheduling_hash); |
227 } | 229 } |
228 | 230 |
229 notifier::Notification notification; | 231 notifier::Notification notification; |
230 notification.data = PushClientChannel::EncodeMessageForTest( | 232 notification.data = PushClientChannel::EncodeMessageForTest( |
231 kMessage, kServiceContext, kSchedulingHash); | 233 kMessage, kServiceContext, kSchedulingHash); |
232 fake_push_client_->SimulateIncomingNotification(notification); | 234 fake_push_client_->SimulateIncomingNotification(notification); |
233 | 235 |
234 push_client_channel_.SendMessage(kMessage); | 236 push_client_channel_.SendMessage(kMessage); |
235 ASSERT_EQ(2u, fake_push_client_->sent_notifications().size()); | 237 ASSERT_EQ(2u, fake_push_client_->sent_notifications().size()); |
236 { | 238 { |
237 std::string message; | 239 std::string message; |
238 std::string service_context; | 240 std::string service_context; |
239 int64 scheduling_hash = 0LL; | 241 int64_t scheduling_hash = 0LL; |
240 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( | 242 EXPECT_TRUE(PushClientChannel::DecodeMessageForTest( |
241 fake_push_client_->sent_notifications()[1].data, | 243 fake_push_client_->sent_notifications()[1].data, |
242 &message, | 244 &message, |
243 &service_context, | 245 &service_context, |
244 &scheduling_hash)); | 246 &scheduling_hash)); |
245 EXPECT_EQ(kMessage, message); | 247 EXPECT_EQ(kMessage, message); |
246 EXPECT_EQ(kServiceContext, service_context); | 248 EXPECT_EQ(kServiceContext, service_context); |
247 EXPECT_EQ(kSchedulingHash, scheduling_hash); | 249 EXPECT_EQ(kSchedulingHash, scheduling_hash); |
248 } | 250 } |
249 } | 251 } |
250 | 252 |
251 } // namespace | 253 } // namespace |
252 } // namespace syncer | 254 } // namespace syncer |
OLD | NEW |