henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 64 | rtc::scoped_ptr<SessionDescriptionInterface> desc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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) |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 93 | : channel_(channel), received_message_count_(0) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 103 | last_message_.assign(buffer.data.data<char>(), buffer.data.size()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 104 | ++received_message_count_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool IsOpen() const { return state_ == DataChannelInterface::kOpen; } |
| 108 | const std::string& last_message() const { return last_message_; } |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 109 | size_t received_message_count() const { return received_message_count_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 112 | rtc::scoped_refptr<webrtc::DataChannelInterface> channel_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | DataChannelInterface::DataState state_; |
| 114 | std::string last_message_; |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 115 | size_t received_message_count_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | class MockStatsObserver : public webrtc::StatsObserver { |
| 119 | public: |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 120 | MockStatsObserver() : called_(false), stats_() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | virtual ~MockStatsObserver() {} |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 122 | |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 123 | virtual void OnComplete(const StatsReports& reports) { |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 124 | ASSERT(!called_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | called_ = true; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 126 | stats_.Clear(); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 127 | stats_.number_of_reports = reports.size(); |
| 128 | for (const auto* r : reports) { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 129 | if (r->type() == StatsReport::kStatsReportTypeSsrc) { |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 130 | stats_.timestamp = r->timestamp(); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 131 | GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel, |
| 132 | &stats_.audio_output_level); |
| 133 | GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel, |
| 134 | &stats_.audio_input_level); |
| 135 | GetIntValue(r, StatsReport::kStatsValueNameBytesReceived, |
| 136 | &stats_.bytes_received); |
| 137 | GetIntValue(r, StatsReport::kStatsValueNameBytesSent, |
| 138 | &stats_.bytes_sent); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 139 | } else if (r->type() == StatsReport::kStatsReportTypeBwe) { |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 140 | stats_.timestamp = r->timestamp(); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 141 | GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth, |
| 142 | &stats_.available_receive_bandwidth); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 143 | } else if (r->type() == StatsReport::kStatsReportTypeComponent) { |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 144 | stats_.timestamp = r->timestamp(); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 145 | GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher, |
| 146 | &stats_.dtls_cipher); |
| 147 | GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher, |
| 148 | &stats_.srtp_cipher); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool called() const { return called_; } |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 154 | size_t number_of_reports() const { return stats_.number_of_reports; } |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 155 | double timestamp() const { return stats_.timestamp; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 156 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 157 | int AudioOutputLevel() const { |
| 158 | ASSERT(called_); |
| 159 | return stats_.audio_output_level; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 162 | int AudioInputLevel() const { |
| 163 | ASSERT(called_); |
| 164 | return stats_.audio_input_level; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 167 | int BytesReceived() const { |
| 168 | ASSERT(called_); |
| 169 | return stats_.bytes_received; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | } |
| 171 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 172 | int BytesSent() const { |
| 173 | ASSERT(called_); |
| 174 | return stats_.bytes_sent; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 175 | } |
| 176 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 177 | int AvailableReceiveBandwidth() const { |
| 178 | ASSERT(called_); |
| 179 | return stats_.available_receive_bandwidth; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | } |
| 181 | |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 182 | std::string DtlsCipher() const { |
| 183 | ASSERT(called_); |
| 184 | return stats_.dtls_cipher; |
| 185 | } |
| 186 | |
| 187 | std::string SrtpCipher() const { |
| 188 | ASSERT(called_); |
| 189 | return stats_.srtp_cipher; |
| 190 | } |
| 191 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | private: |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 193 | bool GetIntValue(const StatsReport* report, |
| 194 | StatsReport::StatsValueName name, |
| 195 | int* value) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 196 | const StatsReport::Value* v = report->FindValue(name); |
| 197 | if (v) { |
| 198 | // TODO(tommi): We should really just be using an int here :-/ |
| 199 | *value = rtc::FromString<int>(v->ToString()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | } |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 201 | return v != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | } |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 203 | |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 204 | bool GetStringValue(const StatsReport* report, |
| 205 | StatsReport::StatsValueName name, |
| 206 | std::string* value) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 207 | const StatsReport::Value* v = report->FindValue(name); |
| 208 | if (v) |
| 209 | *value = v->ToString(); |
| 210 | return v != nullptr; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 211 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | |
| 213 | bool called_; |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 214 | struct { |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 215 | void Clear() { |
| 216 | number_of_reports = 0; |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 217 | timestamp = 0; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 218 | audio_output_level = 0; |
| 219 | audio_input_level = 0; |
| 220 | bytes_received = 0; |
| 221 | bytes_sent = 0; |
| 222 | available_receive_bandwidth = 0; |
| 223 | dtls_cipher.clear(); |
| 224 | srtp_cipher.clear(); |
| 225 | } |
| 226 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 227 | size_t number_of_reports; |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 228 | double timestamp; |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 229 | int audio_output_level; |
| 230 | int audio_input_level; |
| 231 | int bytes_received; |
| 232 | int bytes_sent; |
| 233 | int available_receive_bandwidth; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 234 | std::string dtls_cipher; |
| 235 | std::string srtp_cipher; |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 236 | } stats_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } // namespace webrtc |
| 240 | |
| 241 | #endif // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ |