blob: bae85383eb897cb193a11182961cf3eb783d8ec3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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 Kjellander15583c12016-02-10 10:53:12 +010030#ifndef WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
31#define WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
33#include <string>
34
Henrik Kjellander15583c12016-02-10 10:53:12 +010035#include "webrtc/api/datachannelinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37namespace webrtc {
38
39class 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.orgd4e598d2014-07-29 17:36:52 +000064 rtc::scoped_ptr<SessionDescriptionInterface> desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065};
66
67class 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
90class MockDataChannelObserver : public webrtc::DataChannelObserver {
91 public:
92 explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel)
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000093 : channel_(channel), received_message_count_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 channel_->RegisterObserver(this);
95 state_ = channel_->state();
96 }
97 virtual ~MockDataChannelObserver() {
98 channel_->UnregisterObserver();
99 }
100
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 void OnBufferedAmountChange(uint64_t previous_amount) override {}
bemasc0edd50c2015-07-01 13:34:33 -0700102
103 void OnStateChange() override { state_ = channel_->state(); }
104 void OnMessage(const DataBuffer& buffer) override {
Karl Wiberg94784372015-04-20 14:03:07 +0200105 last_message_.assign(buffer.data.data<char>(), buffer.data.size());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000106 ++received_message_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 }
108
109 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; }
110 const std::string& last_message() const { return last_message_; }
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000111 size_t received_message_count() const { return received_message_count_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
113 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000114 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 DataChannelInterface::DataState state_;
116 std::string last_message_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000117 size_t received_message_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118};
119
120class MockStatsObserver : public webrtc::StatsObserver {
121 public:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000122 MockStatsObserver() : called_(false), stats_() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 virtual ~MockStatsObserver() {}
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000124
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000125 virtual void OnComplete(const StatsReports& reports) {
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000126 ASSERT(!called_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 called_ = true;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000128 stats_.Clear();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000129 stats_.number_of_reports = reports.size();
130 for (const auto* r : reports) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000131 if (r->type() == StatsReport::kStatsReportTypeSsrc) {
jbauchbe24c942015-06-22 15:06:43 -0700132 stats_.timestamp = r->timestamp();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000133 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.org4fb7e252015-01-21 11:36:18 +0000141 } else if (r->type() == StatsReport::kStatsReportTypeBwe) {
jbauchbe24c942015-06-22 15:06:43 -0700142 stats_.timestamp = r->timestamp();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000143 GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth,
144 &stats_.available_receive_bandwidth);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000145 } else if (r->type() == StatsReport::kStatsReportTypeComponent) {
jbauchbe24c942015-06-22 15:06:43 -0700146 stats_.timestamp = r->timestamp();
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000147 GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher,
148 &stats_.dtls_cipher);
149 GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher,
150 &stats_.srtp_cipher);
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000151 }
152 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 }
154
155 bool called() const { return called_; }
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000156 size_t number_of_reports() const { return stats_.number_of_reports; }
jbauchbe24c942015-06-22 15:06:43 -0700157 double timestamp() const { return stats_.timestamp; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000159 int AudioOutputLevel() const {
160 ASSERT(called_);
161 return stats_.audio_output_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 }
163
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000164 int AudioInputLevel() const {
165 ASSERT(called_);
166 return stats_.audio_input_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
168
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000169 int BytesReceived() const {
170 ASSERT(called_);
171 return stats_.bytes_received;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 }
173
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000174 int BytesSent() const {
175 ASSERT(called_);
176 return stats_.bytes_sent;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000177 }
178
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000179 int AvailableReceiveBandwidth() const {
180 ASSERT(called_);
181 return stats_.available_receive_bandwidth;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 }
183
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000184 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.org28e20752013-07-10 00:45:36 +0000194 private:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000195 bool GetIntValue(const StatsReport* report,
196 StatsReport::StatsValueName name,
197 int* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000198 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.org28e20752013-07-10 00:45:36 +0000202 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203 return v != nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000205
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000206 bool GetStringValue(const StatsReport* report,
207 StatsReport::StatsValueName name,
208 std::string* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000209 const StatsReport::Value* v = report->FindValue(name);
210 if (v)
211 *value = v->ToString();
212 return v != nullptr;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
215 bool called_;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000216 struct {
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000217 void Clear() {
218 number_of_reports = 0;
jbauchbe24c942015-06-22 15:06:43 -0700219 timestamp = 0;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000220 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.org209df9b2014-12-17 14:09:05 +0000229 size_t number_of_reports;
jbauchbe24c942015-06-22 15:06:43 -0700230 double timestamp;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000231 int audio_output_level;
232 int audio_input_level;
233 int bytes_received;
234 int bytes_sent;
235 int available_receive_bandwidth;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000236 std::string dtls_cipher;
237 std::string srtp_cipher;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000238 } stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239};
240
241} // namespace webrtc
242
Henrik Kjellander15583c12016-02-10 10:53:12 +0100243#endif // WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_