Roll ANGLE cc54ab3..c5b2ba5
[chromium-blink-merge.git] / components / gcm_driver / gcm_stats_recorder_impl_unittest.cc
blobbb8ea313c62ab0fe8d28db61365b2f793aab9186
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
3 // found in the LICENSE file.
5 #include "components/gcm_driver/gcm_stats_recorder_impl.h"
7 #include <deque>
8 #include <string>
9 #include <vector>
11 #include "google_apis/gcm/engine/mcs_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace gcm {
16 namespace {
18 static uint64 kAndroidId = 4U;
19 static const char kCheckinStatus[] = "URL_FETCHING_FAILED";
20 static const char kHost[] = "www.example.com";
21 static const char kAppId[] = "app id 1";
22 static const char kFrom[] = "from";
23 static const char kSenderIds[] = "s1,s2";
24 static const char kReceiverId[] = "receiver 1";
25 static const char kMessageId[] = "message id 1";
26 static const int kQueuedSec = 5;
27 static const gcm::MCSClient::MessageSendStatus kMessageSendStatus =
28 gcm::MCSClient::QUEUED;
29 static const int kByteSize = 99;
30 static const int kTTL = 7;
31 static const int kRetries = 3;
32 static const int64 kDelay = 15000;
33 static const ConnectionFactory::ConnectionResetReason kReason =
34 ConnectionFactory::NETWORK_CHANGE;
35 static const int kNetworkError = 1;
37 static const RegistrationRequest::Status kRegistrationStatus =
38 RegistrationRequest::SUCCESS;
39 static const UnregistrationRequest::Status kUnregistrationStatus =
40 UnregistrationRequest::SUCCESS;
42 static const char kCheckinInitiatedEvent[] = "Checkin initiated";
43 static const char kCheckinInitiatedDetails[] = "Android Id: 4";
44 static const char kCheckinDelayedDueToBackoffEvent[] = "Checkin backoff";
45 static const char kCheckinDelayedDueToBackoffDetails[] =
46 "Delayed for 15000 msec";
47 static const char kCheckinSuccessEvent[] = "Checkin succeeded";
48 static const char kCheckinSuccessDetails[] = "";
49 static const char kCheckinFailureEvent[] = "Checkin failed";
50 static const char kCheckinFailureDetails[] = "URL_FETCHING_FAILED. Will retry.";
52 static const char kConnectionInitiatedEvent[] = "Connection initiated";
53 static const char kConnectionInitiatedDetails[] = "www.example.com";
54 static const char kConnectionDelayedDueToBackoffEvent[] = "Connection backoff";
55 static const char kConnectionDelayedDueToBackoffDetails[] =
56 "Delayed for 15000 msec";
57 static const char kConnectionSuccessEvent[] = "Connection succeeded";
58 static const char kConnectionSuccessDetails[] = "";
59 static const char kConnectionFailureEvent[] = "Connection failed";
60 static const char kConnectionFailureDetails[] = "With network error 1";
61 static const char kConnectionResetSignaledEvent[] = "Connection reset";
62 static const char kConnectionResetSignaledDetails[] = "NETWORK_CHANGE";
64 static const char kRegistrationSentEvent[] = "Registration request sent";
65 static const char kRegistrationSentDetails[] = "";
66 static const char kRegistrationResponseEvent[] =
67 "Registration response received";
68 static const char kRegistrationResponseDetails[] = "SUCCESS";
69 static const char kRegistrationRetryDelayedEvent[] =
70 "Registration retry delayed";
71 static const char kRegistrationRetryDelayedDetails[] =
72 "Delayed for 15000 msec, retries left: 3";
73 static const char kUnregistrationSentEvent[] = "Unregistration request sent";
74 static const char kUnregistrationSentDetails[] = "";
75 static const char kUnregistrationResponseEvent[] =
76 "Unregistration response received";
77 static const char kUnregistrationResponseDetails[] = "SUCCESS";
78 static const char kUnregistrationRetryDelayedEvent[] =
79 "Unregistration retry delayed";
80 static const char kUnregistrationRetryDelayedDetails[] =
81 "Delayed for 15000 msec, retries left: 3";
83 static const char kDataReceivedEvent[] = "Data msg received";
84 static const char kDataReceivedDetails[] = "";
85 static const char kDataReceivedNotRegisteredEvent[] = "Data msg received";
86 static const char kDataReceivedNotRegisteredDetails[] =
87 "No such registered app found";
88 static const char kDataDeletedMessageEvent[] = "Data msg received";
89 static const char kDataDeletedMessageDetails[] =
90 "Message has been deleted on server";
92 static const char kDataSentToWireEvent[] = "Data msg sent to wire";
93 static const char kSentToWireDetails[] = "Msg queued for 5 seconds";
94 static const char kNotifySendStatusEvent[] = "SEND status: QUEUED";
95 static const char kNotifySendStatusDetails[] = "Msg size: 99 bytes, TTL: 7";
96 static const char kIncomingSendErrorEvent[] = "Received 'send error' msg";
97 static const char kIncomingSendErrorDetails[] = "";
99 } // namespace
101 class GCMStatsRecorderImplTest : public testing::Test {
102 public:
103 GCMStatsRecorderImplTest();
104 ~GCMStatsRecorderImplTest() override;
105 void SetUp() override;
107 void VerifyRecordedCheckinCount(int expected_count) {
108 EXPECT_EQ(expected_count,
109 static_cast<int>(recorder_.checkin_activities().size()));
111 void VerifyRecordedConnectionCount(int expected_count) {
112 EXPECT_EQ(expected_count,
113 static_cast<int>(recorder_.connection_activities().size()));
115 void VerifyRecordedRegistrationCount(int expected_count) {
116 EXPECT_EQ(expected_count,
117 static_cast<int>(recorder_.registration_activities().size()));
119 void VerifyRecordedReceivingCount(int expected_count) {
120 EXPECT_EQ(expected_count,
121 static_cast<int>(recorder_.receiving_activities().size()));
123 void VerifyRecordedSendingCount(int expected_count) {
124 EXPECT_EQ(expected_count,
125 static_cast<int>(recorder_.sending_activities().size()));
127 void VerifyAllActivityQueueEmpty(const std::string& remark) {
128 EXPECT_TRUE(recorder_.checkin_activities().empty()) << remark;
129 EXPECT_TRUE(recorder_.connection_activities().empty()) << remark;
130 EXPECT_TRUE(recorder_.registration_activities().empty()) << remark;
131 EXPECT_TRUE(recorder_.receiving_activities().empty()) << remark;
132 EXPECT_TRUE(recorder_.sending_activities().empty()) << remark;
135 void VerifyCheckinInitiated(const std::string& remark) {
136 VerifyCheckin(recorder_.checkin_activities(),
137 kCheckinInitiatedEvent,
138 kCheckinInitiatedDetails,
139 remark);
142 void VerifyCheckinDelayedDueToBackoff(const std::string& remark) {
143 VerifyCheckin(recorder_.checkin_activities(),
144 kCheckinDelayedDueToBackoffEvent,
145 kCheckinDelayedDueToBackoffDetails,
146 remark);
149 void VerifyCheckinSuccess(const std::string& remark) {
150 VerifyCheckin(recorder_.checkin_activities(),
151 kCheckinSuccessEvent,
152 kCheckinSuccessDetails,
153 remark);
156 void VerifyCheckinFailure(const std::string& remark) {
157 VerifyCheckin(recorder_.checkin_activities(),
158 kCheckinFailureEvent,
159 kCheckinFailureDetails,
160 remark);
163 void VerifyConnectionInitiated(const std::string& remark) {
164 VerifyConnection(recorder_.connection_activities(),
165 kConnectionInitiatedEvent,
166 kConnectionInitiatedDetails,
167 remark);
170 void VerifyConnectionDelayedDueToBackoff(const std::string& remark) {
171 VerifyConnection(recorder_.connection_activities(),
172 kConnectionDelayedDueToBackoffEvent,
173 kConnectionDelayedDueToBackoffDetails,
174 remark);
177 void VerifyConnectionSuccess(const std::string& remark) {
178 VerifyConnection(recorder_.connection_activities(),
179 kConnectionSuccessEvent,
180 kConnectionSuccessDetails,
181 remark);
184 void VerifyConnectionFailure(const std::string& remark) {
185 VerifyConnection(recorder_.connection_activities(),
186 kConnectionFailureEvent,
187 kConnectionFailureDetails,
188 remark);
191 void VerifyConnectionResetSignaled(const std::string& remark) {
192 VerifyConnection(recorder_.connection_activities(),
193 kConnectionResetSignaledEvent,
194 kConnectionResetSignaledDetails,
195 remark);
198 void VerifyRegistrationSent(const std::string& remark) {
199 VerifyRegistration(recorder_.registration_activities(),
200 kSenderIds,
201 kRegistrationSentEvent,
202 kRegistrationSentDetails,
203 remark);
206 void VerifyRegistrationResponse(const std::string& remark) {
207 VerifyRegistration(recorder_.registration_activities(),
208 kSenderIds,
209 kRegistrationResponseEvent,
210 kRegistrationResponseDetails,
211 remark);
214 void VerifyRegistrationRetryRequested(const std::string& remark) {
215 VerifyRegistration(recorder_.registration_activities(),
216 kSenderIds,
217 kRegistrationRetryDelayedEvent,
218 kRegistrationRetryDelayedDetails,
219 remark);
222 void VerifyUnregistrationSent(const std::string& remark) {
223 VerifyRegistration(recorder_.registration_activities(),
224 kSenderIds,
225 kUnregistrationSentEvent,
226 kUnregistrationSentDetails,
227 remark);
230 void VerifyUnregistrationResponse(const std::string& remark) {
231 VerifyRegistration(recorder_.registration_activities(),
232 kSenderIds,
233 kUnregistrationResponseEvent,
234 kUnregistrationResponseDetails,
235 remark);
238 void VerifyUnregistrationRetryDelayed(const std::string& remark) {
239 VerifyRegistration(recorder_.registration_activities(),
240 kSenderIds,
241 kUnregistrationRetryDelayedEvent,
242 kUnregistrationRetryDelayedDetails,
243 remark);
246 void VerifyDataMessageReceived(const std::string& remark) {
247 VerifyReceivingData(recorder_.receiving_activities(),
248 kDataReceivedEvent,
249 kDataReceivedDetails,
250 remark);
253 void VerifyDataDeletedMessage(const std::string& remark) {
254 VerifyReceivingData(recorder_.receiving_activities(),
255 kDataDeletedMessageEvent,
256 kDataDeletedMessageDetails,
257 remark);
260 void VerifyDataMessageReceivedNotRegistered(const std::string& remark) {
261 VerifyReceivingData(recorder_.receiving_activities(),
262 kDataReceivedNotRegisteredEvent,
263 kDataReceivedNotRegisteredDetails,
264 remark);
267 void VerifyDataSentToWire(const std::string& remark) {
268 VerifySendingData(recorder_.sending_activities(),
269 kDataSentToWireEvent,
270 kSentToWireDetails,
271 remark);
274 void VerifyNotifySendStatus(const std::string& remark) {
275 VerifySendingData(recorder_.sending_activities(),
276 kNotifySendStatusEvent,
277 kNotifySendStatusDetails,
278 remark);
281 void VerifyIncomingSendError(const std::string& remark) {
282 VerifySendingData(recorder_.sending_activities(),
283 kIncomingSendErrorEvent,
284 kIncomingSendErrorDetails,
285 remark);
288 protected:
289 void VerifyCheckin(
290 const std::deque<CheckinActivity>& queue,
291 const std::string& event,
292 const std::string& details,
293 const std::string& remark) {
294 EXPECT_EQ(event, queue.front().event) << remark;
295 EXPECT_EQ(details, queue.front().details) << remark;
298 void VerifyConnection(
299 const std::deque<ConnectionActivity>& queue,
300 const std::string& event,
301 const std::string& details,
302 const std::string& remark) {
303 EXPECT_EQ(event, queue.front().event) << remark;
304 EXPECT_EQ(details, queue.front().details) << remark;
307 void VerifyRegistration(
308 const std::deque<RegistrationActivity>& queue,
309 const std::string& source,
310 const std::string& event,
311 const std::string& details,
312 const std::string& remark) {
313 EXPECT_EQ(kAppId, queue.front().app_id) << remark;
314 EXPECT_EQ(source, queue.front().source) << remark;
315 EXPECT_EQ(event, queue.front().event) << remark;
316 EXPECT_EQ(details, queue.front().details) << remark;
319 void VerifyReceivingData(
320 const std::deque<ReceivingActivity>& queue,
321 const std::string& event,
322 const std::string& details,
323 const std::string& remark) {
324 EXPECT_EQ(kAppId, queue.front().app_id) << remark;
325 EXPECT_EQ(kFrom, queue.front().from) << remark;
326 EXPECT_EQ(kByteSize, queue.front().message_byte_size) << remark;
327 EXPECT_EQ(event, queue.front().event) << remark;
328 EXPECT_EQ(details, queue.front().details) << remark;
331 void VerifySendingData(
332 const std::deque<SendingActivity>& queue,
333 const std::string& event, const std::string& details,
334 const std::string& remark) {
335 EXPECT_EQ(kAppId, queue.front().app_id) << remark;
336 EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark;
337 EXPECT_EQ(kMessageId, queue.front().message_id) << remark;
338 EXPECT_EQ(event, queue.front().event) << remark;
339 EXPECT_EQ(details, queue.front().details) << remark;
342 std::string source_;
343 GCMStatsRecorderImpl recorder_;
346 GCMStatsRecorderImplTest::GCMStatsRecorderImplTest(){
349 GCMStatsRecorderImplTest::~GCMStatsRecorderImplTest() {}
351 void GCMStatsRecorderImplTest::SetUp(){
352 source_ = "s1,s2";
353 recorder_.SetRecording(true);
356 TEST_F(GCMStatsRecorderImplTest, StartStopRecordingTest) {
357 EXPECT_TRUE(recorder_.is_recording());
358 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
359 VerifyRecordedSendingCount(1);
360 VerifyDataSentToWire("1st call");
362 recorder_.SetRecording(false);
363 EXPECT_FALSE(recorder_.is_recording());
364 recorder_.Clear();
365 VerifyAllActivityQueueEmpty("all cleared");
367 // Exercise every recording method below and verify that nothing is recorded.
368 recorder_.RecordCheckinInitiated(kAndroidId);
369 recorder_.RecordCheckinDelayedDueToBackoff(kDelay);
370 recorder_.RecordCheckinSuccess();
371 recorder_.RecordCheckinFailure(kCheckinStatus, true);
372 VerifyAllActivityQueueEmpty("no checkin");
374 recorder_.RecordConnectionInitiated(kHost);
375 recorder_.RecordConnectionDelayedDueToBackoff(kDelay);
376 recorder_.RecordConnectionSuccess();
377 recorder_.RecordConnectionFailure(kNetworkError);
378 recorder_.RecordConnectionResetSignaled(kReason);
379 VerifyAllActivityQueueEmpty("no registration");
381 recorder_.RecordRegistrationSent(kAppId, kSenderIds);
382 recorder_.RecordRegistrationResponse(kAppId, source_,
383 kRegistrationStatus);
384 recorder_.RecordRegistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
385 recorder_.RecordUnregistrationSent(kAppId, source_);
386 recorder_.RecordUnregistrationResponse(
387 kAppId, source_, kUnregistrationStatus);
388 recorder_.RecordUnregistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
389 VerifyAllActivityQueueEmpty("no unregistration");
391 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
392 GCMStatsRecorder::DATA_MESSAGE);
393 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
394 GCMStatsRecorder::DELETED_MESSAGES);
395 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false,
396 GCMStatsRecorder::DATA_MESSAGE);
397 VerifyAllActivityQueueEmpty("no receiving");
399 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
400 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
401 kMessageSendStatus, kByteSize, kTTL);
402 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId);
403 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
404 VerifyAllActivityQueueEmpty("no sending");
407 TEST_F(GCMStatsRecorderImplTest, ClearLogTest) {
408 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
409 VerifyRecordedSendingCount(1);
410 VerifyDataSentToWire("1st call");
412 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
413 kMessageSendStatus, kByteSize, kTTL);
414 VerifyRecordedSendingCount(2);
415 VerifyNotifySendStatus("2nd call");
417 recorder_.Clear();
418 VerifyRecordedSendingCount(0);
421 TEST_F(GCMStatsRecorderImplTest, CheckinTest) {
422 recorder_.RecordCheckinInitiated(kAndroidId);
423 VerifyRecordedCheckinCount(1);
424 VerifyCheckinInitiated("1st call");
426 recorder_.RecordCheckinDelayedDueToBackoff(kDelay);
427 VerifyRecordedCheckinCount(2);
428 VerifyCheckinDelayedDueToBackoff("2nd call");
430 recorder_.RecordCheckinSuccess();
431 VerifyRecordedCheckinCount(3);
432 VerifyCheckinSuccess("3rd call");
434 recorder_.RecordCheckinFailure(kCheckinStatus, true);
435 VerifyRecordedCheckinCount(4);
436 VerifyCheckinFailure("4th call");
439 TEST_F(GCMStatsRecorderImplTest, ConnectionTest) {
440 recorder_.RecordConnectionInitiated(kHost);
441 VerifyRecordedConnectionCount(1);
442 VerifyConnectionInitiated("1st call");
444 recorder_.RecordConnectionDelayedDueToBackoff(kDelay);
445 VerifyRecordedConnectionCount(2);
446 VerifyConnectionDelayedDueToBackoff("2nd call");
448 recorder_.RecordConnectionSuccess();
449 VerifyRecordedConnectionCount(3);
450 VerifyConnectionSuccess("3rd call");
452 recorder_.RecordConnectionFailure(kNetworkError);
453 VerifyRecordedConnectionCount(4);
454 VerifyConnectionFailure("4th call");
456 recorder_.RecordConnectionResetSignaled(kReason);
457 VerifyRecordedConnectionCount(5);
458 VerifyConnectionResetSignaled("5th call");
461 TEST_F(GCMStatsRecorderImplTest, RegistrationTest) {
462 recorder_.RecordRegistrationSent(kAppId, kSenderIds);
463 VerifyRecordedRegistrationCount(1);
464 VerifyRegistrationSent("1st call");
466 recorder_.RecordRegistrationResponse(kAppId, source_,
467 kRegistrationStatus);
468 VerifyRecordedRegistrationCount(2);
469 VerifyRegistrationResponse("2nd call");
471 recorder_.RecordRegistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
472 VerifyRecordedRegistrationCount(3);
473 VerifyRegistrationRetryRequested("3rd call");
475 recorder_.RecordUnregistrationSent(kAppId, source_);
476 VerifyRecordedRegistrationCount(4);
477 VerifyUnregistrationSent("4th call");
479 recorder_.RecordUnregistrationResponse(
480 kAppId, source_, kUnregistrationStatus);
481 VerifyRecordedRegistrationCount(5);
482 VerifyUnregistrationResponse("5th call");
484 recorder_.RecordUnregistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
485 VerifyRecordedRegistrationCount(6);
486 VerifyUnregistrationRetryDelayed("6th call");
489 TEST_F(GCMStatsRecorderImplTest, RecordReceivingTest) {
490 recorder_.RecordConnectionInitiated(std::string());
491 recorder_.RecordConnectionSuccess();
492 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
493 GCMStatsRecorder::DATA_MESSAGE);
494 VerifyRecordedReceivingCount(1);
495 VerifyDataMessageReceived("1st call");
497 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
498 GCMStatsRecorder::DELETED_MESSAGES);
499 VerifyRecordedReceivingCount(2);
500 VerifyDataDeletedMessage("2nd call");
502 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false,
503 GCMStatsRecorder::DATA_MESSAGE);
504 VerifyRecordedReceivingCount(3);
505 VerifyDataMessageReceivedNotRegistered("3rd call");
508 TEST_F(GCMStatsRecorderImplTest, RecordSendingTest) {
509 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
510 VerifyRecordedSendingCount(1);
511 VerifyDataSentToWire("1st call");
513 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
514 kMessageSendStatus, kByteSize, kTTL);
515 VerifyRecordedSendingCount(2);
516 VerifyNotifySendStatus("2nd call");
518 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId);
519 VerifyRecordedSendingCount(3);
520 VerifyIncomingSendError("3rd call");
522 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
523 VerifyRecordedSendingCount(4);
524 VerifyDataSentToWire("4th call");
527 } // namespace gcm