1 /* 2 * libjingle 3 * Copyright 2012, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 // This file contains mock implementations of observers used in PeerConnection. 29 30 #ifndef TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ 31 #define TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ 32 33 #include <string> 34 35 #include "talk/app/webrtc/datachannelinterface.h" 36 37 namespace webrtc { 38 39 class MockCreateSessionDescriptionObserver 40 : public webrtc::CreateSessionDescriptionObserver { 41 public: 42 MockCreateSessionDescriptionObserver() 43 : called_(false), 44 result_(false) {} 45 virtual ~MockCreateSessionDescriptionObserver() {} 46 virtual void OnSuccess(SessionDescriptionInterface* desc) { 47 called_ = true; 48 result_ = true; 49 desc_.reset(desc); 50 } 51 virtual void OnFailure(const std::string& error) { 52 called_ = true; 53 result_ = false; 54 } 55 bool called() const { return called_; } 56 bool result() const { return result_; } 57 SessionDescriptionInterface* release_desc() { 58 return desc_.release(); 59 } 60 61 private: 62 bool called_; 63 bool result_; 64 talk_base::scoped_ptr<SessionDescriptionInterface> desc_; 65 }; 66 67 class MockSetSessionDescriptionObserver 68 : public webrtc::SetSessionDescriptionObserver { 69 public: 70 MockSetSessionDescriptionObserver() 71 : called_(false), 72 result_(false) {} 73 virtual ~MockSetSessionDescriptionObserver() {} 74 virtual void OnSuccess() { 75 called_ = true; 76 result_ = true; 77 } 78 virtual void OnFailure(const std::string& error) { 79 called_ = true; 80 result_ = false; 81 } 82 bool called() const { return called_; } 83 bool result() const { return result_; } 84 85 private: 86 bool called_; 87 bool result_; 88 }; 89 90 class MockDataChannelObserver : public webrtc::DataChannelObserver { 91 public: 92 explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel) 93 : channel_(channel), received_message_count_(0) { 94 channel_->RegisterObserver(this); 95 state_ = channel_->state(); 96 } 97 virtual ~MockDataChannelObserver() { 98 channel_->UnregisterObserver(); 99 } 100 101 virtual void OnStateChange() { state_ = channel_->state(); } 102 virtual void OnMessage(const DataBuffer& buffer) { 103 last_message_.assign(buffer.data.data(), buffer.data.length()); 104 ++received_message_count_; 105 } 106 107 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; } 108 const std::string& last_message() const { return last_message_; } 109 size_t received_message_count() const { return received_message_count_; } 110 111 private: 112 talk_base::scoped_refptr<webrtc::DataChannelInterface> channel_; 113 DataChannelInterface::DataState state_; 114 std::string last_message_; 115 size_t received_message_count_; 116 }; 117 118 class MockStatsObserver : public webrtc::StatsObserver { 119 public: 120 MockStatsObserver() 121 : called_(false) {} 122 virtual ~MockStatsObserver() {} 123 virtual void OnComplete(const std::vector<webrtc::StatsReport>& reports) { 124 called_ = true; 125 reports_ = reports; 126 } 127 128 bool called() const { return called_; } 129 size_t number_of_reports() const { return reports_.size(); } 130 131 int AudioOutputLevel() { 132 return GetSsrcStatsValue( 133 webrtc::StatsReport::kStatsValueNameAudioOutputLevel); 134 } 135 136 int AudioInputLevel() { 137 return GetSsrcStatsValue( 138 webrtc::StatsReport::kStatsValueNameAudioInputLevel); 139 } 140 141 int BytesReceived() { 142 return GetSsrcStatsValue( 143 webrtc::StatsReport::kStatsValueNameBytesReceived); 144 } 145 146 int BytesSent() { 147 return GetSsrcStatsValue(webrtc::StatsReport::kStatsValueNameBytesSent); 148 } 149 150 private: 151 int GetSsrcStatsValue(const std::string name) { 152 if (reports_.empty()) { 153 return 0; 154 } 155 for (size_t i = 0; i < reports_.size(); ++i) { 156 if (reports_[i].type != StatsReport::kStatsReportTypeSsrc) 157 continue; 158 webrtc::StatsReport::Values::const_iterator it = 159 reports_[i].values.begin(); 160 for (; it != reports_[i].values.end(); ++it) { 161 if (it->name == name) { 162 return talk_base::FromString<int>(it->value); 163 } 164 } 165 } 166 return 0; 167 } 168 169 bool called_; 170 std::vector<webrtc::StatsReport> reports_; 171 }; 172 173 } // namespace webrtc 174 175 #endif // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ 176