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 rtc::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 void OnBufferedAmountChange(uint64_t previous_amount) override {} 102 103 void OnStateChange() override { state_ = channel_->state(); } 104 void OnMessage(const DataBuffer& buffer) override { 105 last_message_.assign(buffer.data.data<char>(), buffer.data.size()); 106 ++received_message_count_; 107 } 108 109 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; } 110 const std::string& last_message() const { return last_message_; } 111 size_t received_message_count() const { return received_message_count_; } 112 113 private: 114 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_; 115 DataChannelInterface::DataState state_; 116 std::string last_message_; 117 size_t received_message_count_; 118 }; 119 120 class MockStatsObserver : public webrtc::StatsObserver { 121 public: 122 MockStatsObserver() : called_(false), stats_() {} 123 virtual ~MockStatsObserver() {} 124 125 virtual void OnComplete(const StatsReports& reports) { 126 ASSERT(!called_); 127 called_ = true; 128 stats_.Clear(); 129 stats_.number_of_reports = reports.size(); 130 for (const auto* r : reports) { 131 if (r->type() == StatsReport::kStatsReportTypeSsrc) { 132 stats_.timestamp = r->timestamp(); 133 GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel, 134 &stats_.audio_output_level); 135 GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel, 136 &stats_.audio_input_level); 137 GetIntValue(r, StatsReport::kStatsValueNameBytesReceived, 138 &stats_.bytes_received); 139 GetIntValue(r, StatsReport::kStatsValueNameBytesSent, 140 &stats_.bytes_sent); 141 } else if (r->type() == StatsReport::kStatsReportTypeBwe) { 142 stats_.timestamp = r->timestamp(); 143 GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth, 144 &stats_.available_receive_bandwidth); 145 } else if (r->type() == StatsReport::kStatsReportTypeComponent) { 146 stats_.timestamp = r->timestamp(); 147 GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher, 148 &stats_.dtls_cipher); 149 GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher, 150 &stats_.srtp_cipher); 151 } 152 } 153 } 154 155 bool called() const { return called_; } 156 size_t number_of_reports() const { return stats_.number_of_reports; } 157 double timestamp() const { return stats_.timestamp; } 158 159 int AudioOutputLevel() const { 160 ASSERT(called_); 161 return stats_.audio_output_level; 162 } 163 164 int AudioInputLevel() const { 165 ASSERT(called_); 166 return stats_.audio_input_level; 167 } 168 169 int BytesReceived() const { 170 ASSERT(called_); 171 return stats_.bytes_received; 172 } 173 174 int BytesSent() const { 175 ASSERT(called_); 176 return stats_.bytes_sent; 177 } 178 179 int AvailableReceiveBandwidth() const { 180 ASSERT(called_); 181 return stats_.available_receive_bandwidth; 182 } 183 184 std::string DtlsCipher() const { 185 ASSERT(called_); 186 return stats_.dtls_cipher; 187 } 188 189 std::string SrtpCipher() const { 190 ASSERT(called_); 191 return stats_.srtp_cipher; 192 } 193 194 private: 195 bool GetIntValue(const StatsReport* report, 196 StatsReport::StatsValueName name, 197 int* value) { 198 const StatsReport::Value* v = report->FindValue(name); 199 if (v) { 200 // TODO(tommi): We should really just be using an int here :-/ 201 *value = rtc::FromString<int>(v->ToString()); 202 } 203 return v != nullptr; 204 } 205 206 bool GetStringValue(const StatsReport* report, 207 StatsReport::StatsValueName name, 208 std::string* value) { 209 const StatsReport::Value* v = report->FindValue(name); 210 if (v) 211 *value = v->ToString(); 212 return v != nullptr; 213 } 214 215 bool called_; 216 struct { 217 void Clear() { 218 number_of_reports = 0; 219 timestamp = 0; 220 audio_output_level = 0; 221 audio_input_level = 0; 222 bytes_received = 0; 223 bytes_sent = 0; 224 available_receive_bandwidth = 0; 225 dtls_cipher.clear(); 226 srtp_cipher.clear(); 227 } 228 229 size_t number_of_reports; 230 double timestamp; 231 int audio_output_level; 232 int audio_input_level; 233 int bytes_received; 234 int bytes_sent; 235 int available_receive_bandwidth; 236 std::string dtls_cipher; 237 std::string srtp_cipher; 238 } stats_; 239 }; 240 241 } // namespace webrtc 242 243 #endif // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ 244