| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "google_apis/gcm/gcm_stats_recorder.h" |
| 6 |
| 7 #include <deque> |
| 8 #include <string> |
| 9 |
| 10 #include "google_apis/gcm/engine/mcs_client.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace gcm { |
| 14 |
| 15 namespace { |
| 16 |
| 17 const std::string kAppId = "app id 1"; |
| 18 const std::string kReceiverId = "receiver 1"; |
| 19 const std::string kMessageId = "message id 1"; |
| 20 const int kQueuedSec = 5; |
| 21 const gcm::MCSClient::MessageSendStatus kMessageSendStatus = |
| 22 gcm::MCSClient::QUEUED; |
| 23 const int kByteSize = 99; |
| 24 const int kTTL = 7; |
| 25 |
| 26 const std::string kDataSentToWireEvent = "Data msg sent to wire"; |
| 27 const std::string kSentToWireDetails = "Msg queued for 5 seconds"; |
| 28 const std::string kNotifySendStatusEvent = "SEND status: QUEUED"; |
| 29 const std::string kNotifySendStatusDetails = "Msg size: 99 bytes, TTL: 7"; |
| 30 const std::string kSendCalledEvent = "SEND is called"; |
| 31 const std::string kSendCalledDetails = "Msg size: 99 bytes, TTL: 7"; |
| 32 const std::string kIncomingSendErrorEvent = "Received 'send error' msg"; |
| 33 const std::string kIncomingSendErrorDetails = ""; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class GCMStatsRecorderTest : public testing::Test { |
| 38 public: |
| 39 GCMStatsRecorderTest(); |
| 40 virtual ~GCMStatsRecorderTest(); |
| 41 virtual void SetUp() OVERRIDE; |
| 42 |
| 43 void VerifyRecordedSendingCount(int expected_count) { |
| 44 EXPECT_EQ(expected_count, |
| 45 static_cast<int>(recorder_.sending_activities().size())); |
| 46 } |
| 47 |
| 48 void VerifyDataSentToWire(const std::string& remark){ |
| 49 VerifyData(recorder_.sending_activities(), |
| 50 kDataSentToWireEvent, |
| 51 kSentToWireDetails, |
| 52 remark); |
| 53 } |
| 54 |
| 55 void VerifyNotifySendStatus(const std::string& remark){ |
| 56 VerifyData(recorder_.sending_activities(), |
| 57 kNotifySendStatusEvent, |
| 58 kNotifySendStatusDetails, |
| 59 remark); |
| 60 } |
| 61 |
| 62 void VerifyIncomingSendError(const std::string& remark){ |
| 63 VerifyData(recorder_.sending_activities(), |
| 64 kIncomingSendErrorEvent, |
| 65 kIncomingSendErrorDetails, |
| 66 remark); |
| 67 } |
| 68 |
| 69 protected: |
| 70 template <typename T> |
| 71 void VerifyData(const std::deque<T>& queue, const std::string& event, |
| 72 const std::string& details, const std::string& remark) { |
| 73 EXPECT_EQ(kAppId, queue.front().app_id) << remark; |
| 74 EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark; |
| 75 EXPECT_EQ(kMessageId, queue.front().message_id) << remark; |
| 76 EXPECT_EQ(event, queue.front().event) << remark; |
| 77 EXPECT_EQ(details, queue.front().details) << remark; |
| 78 } |
| 79 |
| 80 GCMStatsRecorder recorder_; |
| 81 }; |
| 82 |
| 83 GCMStatsRecorderTest::GCMStatsRecorderTest(){ |
| 84 } |
| 85 |
| 86 GCMStatsRecorderTest::~GCMStatsRecorderTest() {} |
| 87 |
| 88 void GCMStatsRecorderTest::SetUp(){ |
| 89 recorder_.SetRecording(true); |
| 90 } |
| 91 |
| 92 TEST_F(GCMStatsRecorderTest, StartStopRecordingTest) { |
| 93 EXPECT_TRUE(recorder_.is_recording()); |
| 94 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 95 VerifyRecordedSendingCount(1); |
| 96 VerifyDataSentToWire("1st call"); |
| 97 |
| 98 recorder_.SetRecording(false); |
| 99 EXPECT_FALSE(recorder_.is_recording()); |
| 100 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 101 VerifyRecordedSendingCount(1); |
| 102 VerifyDataSentToWire("2nd call"); |
| 103 } |
| 104 |
| 105 TEST_F(GCMStatsRecorderTest, ClearLogTest) { |
| 106 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 107 VerifyRecordedSendingCount(1); |
| 108 VerifyDataSentToWire("1st call"); |
| 109 |
| 110 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, |
| 111 kMessageSendStatus, kByteSize, kTTL); |
| 112 VerifyRecordedSendingCount(2); |
| 113 VerifyNotifySendStatus("2nd call"); |
| 114 |
| 115 recorder_.Clear(); |
| 116 VerifyRecordedSendingCount(0); |
| 117 } |
| 118 |
| 119 TEST_F(GCMStatsRecorderTest, RecordSendingTest) { |
| 120 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 121 VerifyRecordedSendingCount(1); |
| 122 VerifyDataSentToWire("1st call"); |
| 123 |
| 124 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, |
| 125 kMessageSendStatus, kByteSize, kTTL); |
| 126 VerifyRecordedSendingCount(2); |
| 127 VerifyNotifySendStatus("2nd call"); |
| 128 |
| 129 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); |
| 130 VerifyRecordedSendingCount(3); |
| 131 VerifyIncomingSendError("3rd call"); |
| 132 |
| 133 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 134 VerifyRecordedSendingCount(4); |
| 135 VerifyDataSentToWire("4th call"); |
| 136 } |
| 137 |
| 138 } // namespace gcm |
| OLD | NEW |