blob: bd593c2ab5286fea30d40390f5b1ac40ae756b08 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// This file contains mock implementations of observers used in PeerConnection.
12
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#ifndef WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
14#define WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
16#include <string>
17
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/datachannelinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
20namespace webrtc {
21
22class MockCreateSessionDescriptionObserver
23 : public webrtc::CreateSessionDescriptionObserver {
24 public:
25 MockCreateSessionDescriptionObserver()
26 : called_(false),
27 result_(false) {}
28 virtual ~MockCreateSessionDescriptionObserver() {}
29 virtual void OnSuccess(SessionDescriptionInterface* desc) {
30 called_ = true;
31 result_ = true;
32 desc_.reset(desc);
33 }
34 virtual void OnFailure(const std::string& error) {
35 called_ = true;
36 result_ = false;
37 }
38 bool called() const { return called_; }
39 bool result() const { return result_; }
40 SessionDescriptionInterface* release_desc() {
41 return desc_.release();
42 }
43
44 private:
45 bool called_;
46 bool result_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047 rtc::scoped_ptr<SessionDescriptionInterface> desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048};
49
50class MockSetSessionDescriptionObserver
51 : public webrtc::SetSessionDescriptionObserver {
52 public:
53 MockSetSessionDescriptionObserver()
54 : called_(false),
55 result_(false) {}
56 virtual ~MockSetSessionDescriptionObserver() {}
57 virtual void OnSuccess() {
58 called_ = true;
59 result_ = true;
60 }
61 virtual void OnFailure(const std::string& error) {
62 called_ = true;
63 result_ = false;
64 }
65 bool called() const { return called_; }
66 bool result() const { return result_; }
67
68 private:
69 bool called_;
70 bool result_;
71};
72
73class MockDataChannelObserver : public webrtc::DataChannelObserver {
74 public:
75 explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel)
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000076 : channel_(channel), received_message_count_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 channel_->RegisterObserver(this);
78 state_ = channel_->state();
79 }
80 virtual ~MockDataChannelObserver() {
81 channel_->UnregisterObserver();
82 }
83
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 void OnBufferedAmountChange(uint64_t previous_amount) override {}
bemasc0edd50c2015-07-01 13:34:33 -070085
86 void OnStateChange() override { state_ = channel_->state(); }
87 void OnMessage(const DataBuffer& buffer) override {
Karl Wiberg94784372015-04-20 14:03:07 +020088 last_message_.assign(buffer.data.data<char>(), buffer.data.size());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000089 ++received_message_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 }
91
92 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; }
93 const std::string& last_message() const { return last_message_; }
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000094 size_t received_message_count() const { return received_message_count_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
96 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 DataChannelInterface::DataState state_;
99 std::string last_message_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000100 size_t received_message_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101};
102
103class MockStatsObserver : public webrtc::StatsObserver {
104 public:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000105 MockStatsObserver() : called_(false), stats_() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 virtual ~MockStatsObserver() {}
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000107
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000108 virtual void OnComplete(const StatsReports& reports) {
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000109 ASSERT(!called_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 called_ = true;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000111 stats_.Clear();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000112 stats_.number_of_reports = reports.size();
113 for (const auto* r : reports) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000114 if (r->type() == StatsReport::kStatsReportTypeSsrc) {
jbauchbe24c942015-06-22 15:06:43 -0700115 stats_.timestamp = r->timestamp();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000116 GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel,
117 &stats_.audio_output_level);
118 GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel,
119 &stats_.audio_input_level);
120 GetIntValue(r, StatsReport::kStatsValueNameBytesReceived,
121 &stats_.bytes_received);
122 GetIntValue(r, StatsReport::kStatsValueNameBytesSent,
123 &stats_.bytes_sent);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000124 } else if (r->type() == StatsReport::kStatsReportTypeBwe) {
jbauchbe24c942015-06-22 15:06:43 -0700125 stats_.timestamp = r->timestamp();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000126 GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth,
127 &stats_.available_receive_bandwidth);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000128 } else if (r->type() == StatsReport::kStatsReportTypeComponent) {
jbauchbe24c942015-06-22 15:06:43 -0700129 stats_.timestamp = r->timestamp();
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000130 GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher,
131 &stats_.dtls_cipher);
132 GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher,
133 &stats_.srtp_cipher);
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000134 }
135 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 }
137
138 bool called() const { return called_; }
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000139 size_t number_of_reports() const { return stats_.number_of_reports; }
jbauchbe24c942015-06-22 15:06:43 -0700140 double timestamp() const { return stats_.timestamp; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000142 int AudioOutputLevel() const {
143 ASSERT(called_);
144 return stats_.audio_output_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 }
146
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000147 int AudioInputLevel() const {
148 ASSERT(called_);
149 return stats_.audio_input_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 }
151
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000152 int BytesReceived() const {
153 ASSERT(called_);
154 return stats_.bytes_received;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 }
156
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000157 int BytesSent() const {
158 ASSERT(called_);
159 return stats_.bytes_sent;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000160 }
161
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000162 int AvailableReceiveBandwidth() const {
163 ASSERT(called_);
164 return stats_.available_receive_bandwidth;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
166
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000167 std::string DtlsCipher() const {
168 ASSERT(called_);
169 return stats_.dtls_cipher;
170 }
171
172 std::string SrtpCipher() const {
173 ASSERT(called_);
174 return stats_.srtp_cipher;
175 }
176
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 private:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000178 bool GetIntValue(const StatsReport* report,
179 StatsReport::StatsValueName name,
180 int* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000181 const StatsReport::Value* v = report->FindValue(name);
182 if (v) {
183 // TODO(tommi): We should really just be using an int here :-/
184 *value = rtc::FromString<int>(v->ToString());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000186 return v != nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000188
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000189 bool GetStringValue(const StatsReport* report,
190 StatsReport::StatsValueName name,
191 std::string* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000192 const StatsReport::Value* v = report->FindValue(name);
193 if (v)
194 *value = v->ToString();
195 return v != nullptr;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
198 bool called_;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000199 struct {
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000200 void Clear() {
201 number_of_reports = 0;
jbauchbe24c942015-06-22 15:06:43 -0700202 timestamp = 0;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000203 audio_output_level = 0;
204 audio_input_level = 0;
205 bytes_received = 0;
206 bytes_sent = 0;
207 available_receive_bandwidth = 0;
208 dtls_cipher.clear();
209 srtp_cipher.clear();
210 }
211
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000212 size_t number_of_reports;
jbauchbe24c942015-06-22 15:06:43 -0700213 double timestamp;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000214 int audio_output_level;
215 int audio_input_level;
216 int bytes_received;
217 int bytes_sent;
218 int available_receive_bandwidth;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000219 std::string dtls_cipher;
220 std::string srtp_cipher;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000221 } stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222};
223
224} // namespace webrtc
225
Henrik Kjellander15583c12016-02-10 10:53:12 +0100226#endif // WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_