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 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 30 | #ifndef WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ |
| 31 | #define WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | |
| 33 | #include <string> |
| 34 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 35 | #include "webrtc/api/datachannelinterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 101 | void OnBufferedAmountChange(uint64_t previous_amount) override {} |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 102 | |
| 103 | void OnStateChange() override { state_ = channel_->state(); } |
| 104 | void OnMessage(const DataBuffer& buffer) override { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 105 | last_message_.assign(buffer.data.data<char>(), buffer.data.size()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 106 | ++received_message_count_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool IsOpen() const { return state_ == DataChannelInterface::kOpen; } |
| 110 | const std::string& last_message() const { return last_message_; } |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 111 | size_t received_message_count() const { return received_message_count_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 114 | rtc::scoped_refptr<webrtc::DataChannelInterface> channel_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | DataChannelInterface::DataState state_; |
| 116 | std::string last_message_; |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 117 | size_t received_message_count_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | class MockStatsObserver : public webrtc::StatsObserver { |
| 121 | public: |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 122 | MockStatsObserver() : called_(false), stats_() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | virtual ~MockStatsObserver() {} |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 124 | |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 125 | virtual void OnComplete(const StatsReports& reports) { |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 126 | ASSERT(!called_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | called_ = true; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 128 | stats_.Clear(); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 129 | stats_.number_of_reports = reports.size(); |
| 130 | for (const auto* r : reports) { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 131 | if (r->type() == StatsReport::kStatsReportTypeSsrc) { |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 132 | stats_.timestamp = r->timestamp(); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 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); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 141 | } else if (r->type() == StatsReport::kStatsReportTypeBwe) { |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 142 | stats_.timestamp = r->timestamp(); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 143 | GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth, |
| 144 | &stats_.available_receive_bandwidth); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 145 | } else if (r->type() == StatsReport::kStatsReportTypeComponent) { |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 146 | stats_.timestamp = r->timestamp(); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 147 | GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher, |
| 148 | &stats_.dtls_cipher); |
| 149 | GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher, |
| 150 | &stats_.srtp_cipher); |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool called() const { return called_; } |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 156 | size_t number_of_reports() const { return stats_.number_of_reports; } |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 157 | double timestamp() const { return stats_.timestamp; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 159 | int AudioOutputLevel() const { |
| 160 | ASSERT(called_); |
| 161 | return stats_.audio_output_level; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | } |
| 163 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 164 | int AudioInputLevel() const { |
| 165 | ASSERT(called_); |
| 166 | return stats_.audio_input_level; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | } |
| 168 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 169 | int BytesReceived() const { |
| 170 | ASSERT(called_); |
| 171 | return stats_.bytes_received; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | } |
| 173 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 174 | int BytesSent() const { |
| 175 | ASSERT(called_); |
| 176 | return stats_.bytes_sent; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 177 | } |
| 178 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 179 | int AvailableReceiveBandwidth() const { |
| 180 | ASSERT(called_); |
| 181 | return stats_.available_receive_bandwidth; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | } |
| 183 | |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | private: |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 195 | bool GetIntValue(const StatsReport* report, |
| 196 | StatsReport::StatsValueName name, |
| 197 | int* value) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 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()); |
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 | return v != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | } |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 205 | |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 206 | bool GetStringValue(const StatsReport* report, |
| 207 | StatsReport::StatsValueName name, |
| 208 | std::string* value) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 209 | const StatsReport::Value* v = report->FindValue(name); |
| 210 | if (v) |
| 211 | *value = v->ToString(); |
| 212 | return v != nullptr; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 213 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | |
| 215 | bool called_; |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 216 | struct { |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 217 | void Clear() { |
| 218 | number_of_reports = 0; |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 219 | timestamp = 0; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 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 | |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 229 | size_t number_of_reports; |
jbauch | be24c94 | 2015-06-22 15:06:43 -0700 | [diff] [blame] | 230 | double timestamp; |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 231 | int audio_output_level; |
| 232 | int audio_input_level; |
| 233 | int bytes_received; |
| 234 | int bytes_sent; |
| 235 | int available_receive_bandwidth; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 236 | std::string dtls_cipher; |
| 237 | std::string srtp_cipher; |
tommi@webrtc.org | 209df9b | 2014-12-17 14:09:05 +0000 | [diff] [blame] | 238 | } stats_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | } // namespace webrtc |
| 242 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 243 | #endif // WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ |