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 kSentToWireEvent = "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 = "Msg size: 99 bytes, TTL: 7"; |
| 34 |
| 35 |
| 36 class TestGCMStatsRecorder : public GCMStatsRecorder { |
| 37 public: |
| 38 TestGCMStatsRecorder() {}; |
| 39 virtual ~TestGCMStatsRecorder() {}; |
| 40 |
| 41 std::deque<gcm::GCMStatsRecorder::SendingActivity>* sending_activities() { |
| 42 return &sending_activities_; |
| 43 } |
| 44 }; |
| 45 |
| 46 } // namespace |
| 47 |
| 48 class GCMStatsRecorderTest : public testing::Test { |
| 49 public: |
| 50 GCMStatsRecorderTest(); |
| 51 virtual ~GCMStatsRecorderTest(); |
| 52 virtual void SetUp() OVERRIDE; |
| 53 |
| 54 void VerifyRecordedSendingCount(int expected_count) { |
| 55 EXPECT_EQ(expected_count, |
| 56 static_cast<int>(recorder_.sending_activities()->size())); |
| 57 } |
| 58 |
| 59 void VerifySentToWire(){ |
| 60 VerifyData(*recorder_.sending_activities(), |
| 61 kSentToWireEvent, |
| 62 kSentToWireDetails); |
| 63 } |
| 64 |
| 65 void VerifyNotifySendStatus(){ |
| 66 VerifyData(*recorder_.sending_activities(), |
| 67 kNotifySendStatusEvent, |
| 68 kNotifySendStatusDetails); |
| 69 } |
| 70 |
| 71 void VerifySendCalled(){ |
| 72 VerifyData(*recorder_.sending_activities(), |
| 73 kSendCalledEvent, |
| 74 kSendCalledDetails); |
| 75 } |
| 76 |
| 77 void VerifyIncomingSendError(){ |
| 78 VerifyData(*recorder_.sending_activities(), |
| 79 kIncomingSendErrorEvent, |
| 80 kIncomingSendErrorDetails); |
| 81 } |
| 82 |
| 83 protected: |
| 84 template <typename T> |
| 85 void VerifyData(const std::deque<T>& queue, const std::string& event, |
| 86 const std::string& details) { |
| 87 EXPECT_EQ(kAppId, queue.front().app_id); |
| 88 EXPECT_EQ(kReceiverId, queue.front().receiver_id); |
| 89 EXPECT_EQ(kMessageId, queue.front().message_id); |
| 90 EXPECT_EQ(event, queue.front().event); |
| 91 EXPECT_EQ(details, queue.front().details); |
| 92 } |
| 93 |
| 94 TestGCMStatsRecorder recorder_; |
| 95 }; |
| 96 |
| 97 GCMStatsRecorderTest::GCMStatsRecorderTest(){ |
| 98 } |
| 99 |
| 100 GCMStatsRecorderTest::~GCMStatsRecorderTest() {} |
| 101 |
| 102 void GCMStatsRecorderTest::SetUp(){ |
| 103 recorder_.SetRecording(true); |
| 104 } |
| 105 |
| 106 |
| 107 |
| 108 TEST_F(GCMStatsRecorderTest, RecordSendingTest) { |
| 109 EXPECT_TRUE(recorder_.is_recording()); |
| 110 recorder_.RecordSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 111 VerifyRecordedSendingCount(1); |
| 112 VerifySentToWire(); |
| 113 |
| 114 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, |
| 115 kMessageSendStatus, kByteSize, kTTL); |
| 116 VerifyRecordedSendingCount(2); |
| 117 VerifyNotifySendStatus(); |
| 118 |
| 119 recorder_.Clear(); |
| 120 VerifyRecordedSendingCount(0); |
| 121 |
| 122 recorder_.RecordSendCalled(kAppId, kReceiverId, kMessageId, kByteSize, kTTL); |
| 123 VerifyRecordedSendingCount(1); |
| 124 VerifySendCalled(); |
| 125 |
| 126 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId, kByteSize, |
| 127 kTTL); |
| 128 VerifyRecordedSendingCount(2); |
| 129 VerifyIncomingSendError(); |
| 130 |
| 131 recorder_.SetRecording(false); |
| 132 EXPECT_FALSE(recorder_.is_recording()); |
| 133 recorder_.RecordSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); |
| 134 VerifyRecordedSendingCount(2); |
| 135 } |
| 136 |
| 137 } // namespace gcm |
OLD | NEW |