Use the host instead of the title for streamlined hosted apps.
[chromium-blink-merge.git] / components / gcm_driver / gcm_stats_recorder_impl_unittest.cc
blobf32ca95ddfadeeda8b9a831ab822a79024ac0958
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 kRegistrationRetryRequestedEvent[] =
70 "Registration retry requested";
71 static const char kRegistrationRetryRequestedDetails[] = "Retries left: 3";
72 static const char kUnregistrationSentEvent[] = "Unregistration request sent";
73 static const char kUnregistrationSentDetails[] = "";
74 static const char kUnregistrationResponseEvent[] =
75 "Unregistration response received";
76 static const char kUnregistrationResponseDetails[] = "SUCCESS";
77 static const char kUnregistrationRetryDelayedEvent[] =
78 "Unregistration retry delayed";
79 static const char kUnregistrationRetryDelayedDetails[] =
80 "Delayed for 15000 msec";
82 static const char kDataReceivedEvent[] = "Data msg received";
83 static const char kDataReceivedDetails[] = "";
84 static const char kDataReceivedNotRegisteredEvent[] = "Data msg received";
85 static const char kDataReceivedNotRegisteredDetails[] =
86 "No such registered app found";
87 static const char kDataDeletedMessageEvent[] = "Data msg received";
88 static const char kDataDeletedMessageDetails[] =
89 "Message has been deleted on server";
91 static const char kDataSentToWireEvent[] = "Data msg sent to wire";
92 static const char kSentToWireDetails[] = "Msg queued for 5 seconds";
93 static const char kNotifySendStatusEvent[] = "SEND status: QUEUED";
94 static const char kNotifySendStatusDetails[] = "Msg size: 99 bytes, TTL: 7";
95 static const char kIncomingSendErrorEvent[] = "Received 'send error' msg";
96 static const char kIncomingSendErrorDetails[] = "";
98 } // namespace
100 class GCMStatsRecorderImplTest : public testing::Test {
101 public:
102 GCMStatsRecorderImplTest();
103 virtual ~GCMStatsRecorderImplTest();
104 virtual void SetUp() OVERRIDE;
106 void VerifyRecordedCheckinCount(int expected_count) {
107 EXPECT_EQ(expected_count,
108 static_cast<int>(recorder_.checkin_activities().size()));
110 void VerifyRecordedConnectionCount(int expected_count) {
111 EXPECT_EQ(expected_count,
112 static_cast<int>(recorder_.connection_activities().size()));
114 void VerifyRecordedRegistrationCount(int expected_count) {
115 EXPECT_EQ(expected_count,
116 static_cast<int>(recorder_.registration_activities().size()));
118 void VerifyRecordedReceivingCount(int expected_count) {
119 EXPECT_EQ(expected_count,
120 static_cast<int>(recorder_.receiving_activities().size()));
122 void VerifyRecordedSendingCount(int expected_count) {
123 EXPECT_EQ(expected_count,
124 static_cast<int>(recorder_.sending_activities().size()));
126 void VerifyAllActivityQueueEmpty(const std::string& remark) {
127 EXPECT_TRUE(recorder_.checkin_activities().empty()) << remark;
128 EXPECT_TRUE(recorder_.connection_activities().empty()) << remark;
129 EXPECT_TRUE(recorder_.registration_activities().empty()) << remark;
130 EXPECT_TRUE(recorder_.receiving_activities().empty()) << remark;
131 EXPECT_TRUE(recorder_.sending_activities().empty()) << remark;
134 void VerifyCheckinInitiated(const std::string& remark) {
135 VerifyCheckin(recorder_.checkin_activities(),
136 kCheckinInitiatedEvent,
137 kCheckinInitiatedDetails,
138 remark);
141 void VerifyCheckinDelayedDueToBackoff(const std::string& remark) {
142 VerifyCheckin(recorder_.checkin_activities(),
143 kCheckinDelayedDueToBackoffEvent,
144 kCheckinDelayedDueToBackoffDetails,
145 remark);
148 void VerifyCheckinSuccess(const std::string& remark) {
149 VerifyCheckin(recorder_.checkin_activities(),
150 kCheckinSuccessEvent,
151 kCheckinSuccessDetails,
152 remark);
155 void VerifyCheckinFailure(const std::string& remark) {
156 VerifyCheckin(recorder_.checkin_activities(),
157 kCheckinFailureEvent,
158 kCheckinFailureDetails,
159 remark);
162 void VerifyConnectionInitiated(const std::string& remark) {
163 VerifyConnection(recorder_.connection_activities(),
164 kConnectionInitiatedEvent,
165 kConnectionInitiatedDetails,
166 remark);
169 void VerifyConnectionDelayedDueToBackoff(const std::string& remark) {
170 VerifyConnection(recorder_.connection_activities(),
171 kConnectionDelayedDueToBackoffEvent,
172 kConnectionDelayedDueToBackoffDetails,
173 remark);
176 void VerifyConnectionSuccess(const std::string& remark) {
177 VerifyConnection(recorder_.connection_activities(),
178 kConnectionSuccessEvent,
179 kConnectionSuccessDetails,
180 remark);
183 void VerifyConnectionFailure(const std::string& remark) {
184 VerifyConnection(recorder_.connection_activities(),
185 kConnectionFailureEvent,
186 kConnectionFailureDetails,
187 remark);
190 void VerifyConnectionResetSignaled(const std::string& remark) {
191 VerifyConnection(recorder_.connection_activities(),
192 kConnectionResetSignaledEvent,
193 kConnectionResetSignaledDetails,
194 remark);
197 void VerifyRegistrationSent(const std::string& remark) {
198 VerifyRegistration(recorder_.registration_activities(),
199 kSenderIds,
200 kRegistrationSentEvent,
201 kRegistrationSentDetails,
202 remark);
205 void VerifyRegistrationResponse(const std::string& remark) {
206 VerifyRegistration(recorder_.registration_activities(),
207 kSenderIds,
208 kRegistrationResponseEvent,
209 kRegistrationResponseDetails,
210 remark);
213 void VerifyRegistrationRetryRequested(const std::string& remark) {
214 VerifyRegistration(recorder_.registration_activities(),
215 kSenderIds,
216 kRegistrationRetryRequestedEvent,
217 kRegistrationRetryRequestedDetails,
218 remark);
221 void VerifyUnregistrationSent(const std::string& remark) {
222 VerifyRegistration(recorder_.registration_activities(),
223 std::string(),
224 kUnregistrationSentEvent,
225 kUnregistrationSentDetails,
226 remark);
229 void VerifyUnregistrationResponse(const std::string& remark) {
230 VerifyRegistration(recorder_.registration_activities(),
231 std::string(),
232 kUnregistrationResponseEvent,
233 kUnregistrationResponseDetails,
234 remark);
237 void VerifyUnregistrationRetryDelayed(const std::string& remark) {
238 VerifyRegistration(recorder_.registration_activities(),
239 std::string(),
240 kUnregistrationRetryDelayedEvent,
241 kUnregistrationRetryDelayedDetails,
242 remark);
245 void VerifyDataMessageReceived(const std::string& remark) {
246 VerifyReceivingData(recorder_.receiving_activities(),
247 kDataReceivedEvent,
248 kDataReceivedDetails,
249 remark);
252 void VerifyDataDeletedMessage(const std::string& remark) {
253 VerifyReceivingData(recorder_.receiving_activities(),
254 kDataDeletedMessageEvent,
255 kDataDeletedMessageDetails,
256 remark);
259 void VerifyDataMessageReceivedNotRegistered(const std::string& remark) {
260 VerifyReceivingData(recorder_.receiving_activities(),
261 kDataReceivedNotRegisteredEvent,
262 kDataReceivedNotRegisteredDetails,
263 remark);
266 void VerifyDataSentToWire(const std::string& remark) {
267 VerifySendingData(recorder_.sending_activities(),
268 kDataSentToWireEvent,
269 kSentToWireDetails,
270 remark);
273 void VerifyNotifySendStatus(const std::string& remark) {
274 VerifySendingData(recorder_.sending_activities(),
275 kNotifySendStatusEvent,
276 kNotifySendStatusDetails,
277 remark);
280 void VerifyIncomingSendError(const std::string& remark) {
281 VerifySendingData(recorder_.sending_activities(),
282 kIncomingSendErrorEvent,
283 kIncomingSendErrorDetails,
284 remark);
287 protected:
288 void VerifyCheckin(
289 const std::deque<CheckinActivity>& queue,
290 const std::string& event,
291 const std::string& details,
292 const std::string& remark) {
293 EXPECT_EQ(event, queue.front().event) << remark;
294 EXPECT_EQ(details, queue.front().details) << remark;
297 void VerifyConnection(
298 const std::deque<ConnectionActivity>& queue,
299 const std::string& event,
300 const std::string& details,
301 const std::string& remark) {
302 EXPECT_EQ(event, queue.front().event) << remark;
303 EXPECT_EQ(details, queue.front().details) << remark;
306 void VerifyRegistration(
307 const std::deque<RegistrationActivity>& queue,
308 const std::string& sender_ids,
309 const std::string& event,
310 const std::string& details,
311 const std::string& remark) {
312 EXPECT_EQ(kAppId, queue.front().app_id) << remark;
313 EXPECT_EQ(sender_ids, queue.front().sender_ids) << remark;
314 EXPECT_EQ(event, queue.front().event) << remark;
315 EXPECT_EQ(details, queue.front().details) << remark;
318 void VerifyReceivingData(
319 const std::deque<ReceivingActivity>& queue,
320 const std::string& event,
321 const std::string& details,
322 const std::string& remark) {
323 EXPECT_EQ(kAppId, queue.front().app_id) << remark;
324 EXPECT_EQ(kFrom, queue.front().from) << remark;
325 EXPECT_EQ(kByteSize, queue.front().message_byte_size) << remark;
326 EXPECT_EQ(event, queue.front().event) << remark;
327 EXPECT_EQ(details, queue.front().details) << remark;
330 void VerifySendingData(
331 const std::deque<SendingActivity>& queue,
332 const std::string& event, const std::string& details,
333 const std::string& remark) {
334 EXPECT_EQ(kAppId, queue.front().app_id) << remark;
335 EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark;
336 EXPECT_EQ(kMessageId, queue.front().message_id) << remark;
337 EXPECT_EQ(event, queue.front().event) << remark;
338 EXPECT_EQ(details, queue.front().details) << remark;
341 std::vector<std::string> sender_ids_;
342 GCMStatsRecorderImpl recorder_;
345 GCMStatsRecorderImplTest::GCMStatsRecorderImplTest(){
348 GCMStatsRecorderImplTest::~GCMStatsRecorderImplTest() {}
350 void GCMStatsRecorderImplTest::SetUp(){
351 sender_ids_.push_back("s1");
352 sender_ids_.push_back("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, sender_ids_,
383 kRegistrationStatus);
384 recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries);
385 recorder_.RecordUnregistrationSent(kAppId);
386 recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus);
387 recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay);
388 VerifyAllActivityQueueEmpty("no unregistration");
390 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
391 GCMStatsRecorder::DATA_MESSAGE);
392 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
393 GCMStatsRecorder::DELETED_MESSAGES);
394 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false,
395 GCMStatsRecorder::DATA_MESSAGE);
396 VerifyAllActivityQueueEmpty("no receiving");
398 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
399 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
400 kMessageSendStatus, kByteSize, kTTL);
401 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId);
402 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
403 VerifyAllActivityQueueEmpty("no sending");
406 TEST_F(GCMStatsRecorderImplTest, ClearLogTest) {
407 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
408 VerifyRecordedSendingCount(1);
409 VerifyDataSentToWire("1st call");
411 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
412 kMessageSendStatus, kByteSize, kTTL);
413 VerifyRecordedSendingCount(2);
414 VerifyNotifySendStatus("2nd call");
416 recorder_.Clear();
417 VerifyRecordedSendingCount(0);
420 TEST_F(GCMStatsRecorderImplTest, CheckinTest) {
421 recorder_.RecordCheckinInitiated(kAndroidId);
422 VerifyRecordedCheckinCount(1);
423 VerifyCheckinInitiated("1st call");
425 recorder_.RecordCheckinDelayedDueToBackoff(kDelay);
426 VerifyRecordedCheckinCount(2);
427 VerifyCheckinDelayedDueToBackoff("2nd call");
429 recorder_.RecordCheckinSuccess();
430 VerifyRecordedCheckinCount(3);
431 VerifyCheckinSuccess("3rd call");
433 recorder_.RecordCheckinFailure(kCheckinStatus, true);
434 VerifyRecordedCheckinCount(4);
435 VerifyCheckinFailure("4th call");
438 TEST_F(GCMStatsRecorderImplTest, ConnectionTest) {
439 recorder_.RecordConnectionInitiated(kHost);
440 VerifyRecordedConnectionCount(1);
441 VerifyConnectionInitiated("1st call");
443 recorder_.RecordConnectionDelayedDueToBackoff(kDelay);
444 VerifyRecordedConnectionCount(2);
445 VerifyConnectionDelayedDueToBackoff("2nd call");
447 recorder_.RecordConnectionSuccess();
448 VerifyRecordedConnectionCount(3);
449 VerifyConnectionSuccess("3rd call");
451 recorder_.RecordConnectionFailure(kNetworkError);
452 VerifyRecordedConnectionCount(4);
453 VerifyConnectionFailure("4th call");
455 recorder_.RecordConnectionResetSignaled(kReason);
456 VerifyRecordedConnectionCount(5);
457 VerifyConnectionResetSignaled("5th call");
460 TEST_F(GCMStatsRecorderImplTest, RegistrationTest) {
461 recorder_.RecordRegistrationSent(kAppId, kSenderIds);
462 VerifyRecordedRegistrationCount(1);
463 VerifyRegistrationSent("1st call");
465 recorder_.RecordRegistrationResponse(kAppId, sender_ids_,
466 kRegistrationStatus);
467 VerifyRecordedRegistrationCount(2);
468 VerifyRegistrationResponse("2nd call");
470 recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries);
471 VerifyRecordedRegistrationCount(3);
472 VerifyRegistrationRetryRequested("3rd call");
474 recorder_.RecordUnregistrationSent(kAppId);
475 VerifyRecordedRegistrationCount(4);
476 VerifyUnregistrationSent("4th call");
478 recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus);
479 VerifyRecordedRegistrationCount(5);
480 VerifyUnregistrationResponse("5th call");
482 recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay);
483 VerifyRecordedRegistrationCount(6);
484 VerifyUnregistrationRetryDelayed("6th call");
487 TEST_F(GCMStatsRecorderImplTest, RecordReceivingTest) {
488 recorder_.RecordConnectionInitiated(std::string());
489 recorder_.RecordConnectionSuccess();
490 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
491 GCMStatsRecorder::DATA_MESSAGE);
492 VerifyRecordedReceivingCount(1);
493 VerifyDataMessageReceived("1st call");
495 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true,
496 GCMStatsRecorder::DELETED_MESSAGES);
497 VerifyRecordedReceivingCount(2);
498 VerifyDataDeletedMessage("2nd call");
500 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false,
501 GCMStatsRecorder::DATA_MESSAGE);
502 VerifyRecordedReceivingCount(3);
503 VerifyDataMessageReceivedNotRegistered("3rd call");
506 TEST_F(GCMStatsRecorderImplTest, RecordSendingTest) {
507 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
508 VerifyRecordedSendingCount(1);
509 VerifyDataSentToWire("1st call");
511 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
512 kMessageSendStatus, kByteSize, kTTL);
513 VerifyRecordedSendingCount(2);
514 VerifyNotifySendStatus("2nd call");
516 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId);
517 VerifyRecordedSendingCount(3);
518 VerifyIncomingSendError("3rd call");
520 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
521 VerifyRecordedSendingCount(4);
522 VerifyDataSentToWire("4th call");
525 } // namespace gcm