blob: 23647f6de3b5cbf3d5c8e6fda845103c77a04441 [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
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <string>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/datachannelinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21namespace webrtc {
22
23class MockCreateSessionDescriptionObserver
24 : public webrtc::CreateSessionDescriptionObserver {
25 public:
26 MockCreateSessionDescriptionObserver()
27 : called_(false),
28 result_(false) {}
29 virtual ~MockCreateSessionDescriptionObserver() {}
30 virtual void OnSuccess(SessionDescriptionInterface* desc) {
31 called_ = true;
32 result_ = true;
33 desc_.reset(desc);
34 }
35 virtual void OnFailure(const std::string& error) {
36 called_ = true;
37 result_ = false;
38 }
39 bool called() const { return called_; }
40 bool result() const { return result_; }
41 SessionDescriptionInterface* release_desc() {
42 return desc_.release();
43 }
44
45 private:
46 bool called_;
47 bool result_;
kwibergd1fe2812016-04-27 06:47:29 -070048 std::unique_ptr<SessionDescriptionInterface> desc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049};
50
51class MockSetSessionDescriptionObserver
52 : public webrtc::SetSessionDescriptionObserver {
53 public:
54 MockSetSessionDescriptionObserver()
55 : called_(false),
56 result_(false) {}
57 virtual ~MockSetSessionDescriptionObserver() {}
58 virtual void OnSuccess() {
59 called_ = true;
60 result_ = true;
61 }
62 virtual void OnFailure(const std::string& error) {
63 called_ = true;
64 result_ = false;
65 }
66 bool called() const { return called_; }
67 bool result() const { return result_; }
68
69 private:
70 bool called_;
71 bool result_;
72};
73
74class MockDataChannelObserver : public webrtc::DataChannelObserver {
75 public:
76 explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel)
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -070077 : channel_(channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 channel_->RegisterObserver(this);
79 state_ = channel_->state();
80 }
81 virtual ~MockDataChannelObserver() {
82 channel_->UnregisterObserver();
83 }
84
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 void OnBufferedAmountChange(uint64_t previous_amount) override {}
bemasc0edd50c2015-07-01 13:34:33 -070086
87 void OnStateChange() override { state_ = channel_->state(); }
88 void OnMessage(const DataBuffer& buffer) override {
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -070089 messages_.push_back(
90 std::string(buffer.data.data<char>(), buffer.data.size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 }
92
93 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; }
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -070094 std::vector<std::string> messages() const { return messages_; }
95 std::string last_message() const {
96 return messages_.empty() ? std::string() : messages_.back();
97 }
98 size_t received_message_count() const { return messages_.size(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 DataChannelInterface::DataState state_;
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -0700103 std::vector<std::string> messages_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104};
105
106class MockStatsObserver : public webrtc::StatsObserver {
107 public:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000108 MockStatsObserver() : called_(false), stats_() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 virtual ~MockStatsObserver() {}
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000110
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000111 virtual void OnComplete(const StatsReports& reports) {
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000112 ASSERT(!called_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 called_ = true;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000114 stats_.Clear();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000115 stats_.number_of_reports = reports.size();
116 for (const auto* r : reports) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000117 if (r->type() == StatsReport::kStatsReportTypeSsrc) {
jbauchbe24c942015-06-22 15:06:43 -0700118 stats_.timestamp = r->timestamp();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000119 GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel,
120 &stats_.audio_output_level);
121 GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel,
122 &stats_.audio_input_level);
123 GetIntValue(r, StatsReport::kStatsValueNameBytesReceived,
124 &stats_.bytes_received);
125 GetIntValue(r, StatsReport::kStatsValueNameBytesSent,
126 &stats_.bytes_sent);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000127 } else if (r->type() == StatsReport::kStatsReportTypeBwe) {
jbauchbe24c942015-06-22 15:06:43 -0700128 stats_.timestamp = r->timestamp();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000129 GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth,
130 &stats_.available_receive_bandwidth);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000131 } else if (r->type() == StatsReport::kStatsReportTypeComponent) {
jbauchbe24c942015-06-22 15:06:43 -0700132 stats_.timestamp = r->timestamp();
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000133 GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher,
134 &stats_.dtls_cipher);
135 GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher,
136 &stats_.srtp_cipher);
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000137 }
138 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 }
140
141 bool called() const { return called_; }
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000142 size_t number_of_reports() const { return stats_.number_of_reports; }
jbauchbe24c942015-06-22 15:06:43 -0700143 double timestamp() const { return stats_.timestamp; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000145 int AudioOutputLevel() const {
146 ASSERT(called_);
147 return stats_.audio_output_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 }
149
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000150 int AudioInputLevel() const {
151 ASSERT(called_);
152 return stats_.audio_input_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 }
154
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000155 int BytesReceived() const {
156 ASSERT(called_);
157 return stats_.bytes_received;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 }
159
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000160 int BytesSent() const {
161 ASSERT(called_);
162 return stats_.bytes_sent;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000163 }
164
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000165 int AvailableReceiveBandwidth() const {
166 ASSERT(called_);
167 return stats_.available_receive_bandwidth;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 }
169
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000170 std::string DtlsCipher() const {
171 ASSERT(called_);
172 return stats_.dtls_cipher;
173 }
174
175 std::string SrtpCipher() const {
176 ASSERT(called_);
177 return stats_.srtp_cipher;
178 }
179
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 private:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000181 bool GetIntValue(const StatsReport* report,
182 StatsReport::StatsValueName name,
183 int* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184 const StatsReport::Value* v = report->FindValue(name);
185 if (v) {
186 // TODO(tommi): We should really just be using an int here :-/
187 *value = rtc::FromString<int>(v->ToString());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000189 return v != nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000191
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000192 bool GetStringValue(const StatsReport* report,
193 StatsReport::StatsValueName name,
194 std::string* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000195 const StatsReport::Value* v = report->FindValue(name);
196 if (v)
197 *value = v->ToString();
198 return v != nullptr;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000199 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
201 bool called_;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000202 struct {
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000203 void Clear() {
204 number_of_reports = 0;
jbauchbe24c942015-06-22 15:06:43 -0700205 timestamp = 0;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000206 audio_output_level = 0;
207 audio_input_level = 0;
208 bytes_received = 0;
209 bytes_sent = 0;
210 available_receive_bandwidth = 0;
211 dtls_cipher.clear();
212 srtp_cipher.clear();
213 }
214
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000215 size_t number_of_reports;
jbauchbe24c942015-06-22 15:06:43 -0700216 double timestamp;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000217 int audio_output_level;
218 int audio_input_level;
219 int bytes_received;
220 int bytes_sent;
221 int available_receive_bandwidth;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000222 std::string dtls_cipher;
223 std::string srtp_cipher;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000224 } stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225};
226
227} // namespace webrtc
228
Henrik Kjellander15583c12016-02-10 10:53:12 +0100229#endif // WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_