blob: f31b16c744303d40160884e944d2399aa05c92b8 [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
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
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
101 virtual void OnStateChange() { state_ = channel_->state(); }
102 virtual void OnMessage(const DataBuffer& buffer) {
Karl Wiberg94784372015-04-20 14:03:07 +0200103 last_message_.assign(buffer.data.data<char>(), buffer.data.size());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000104 ++received_message_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 }
106
107 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; }
108 const std::string& last_message() const { return last_message_; }
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000109 size_t received_message_count() const { return received_message_count_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 DataChannelInterface::DataState state_;
114 std::string last_message_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000115 size_t received_message_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116};
117
118class MockStatsObserver : public webrtc::StatsObserver {
119 public:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000120 MockStatsObserver() : called_(false), stats_() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 virtual ~MockStatsObserver() {}
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000122
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000123 virtual void OnComplete(const StatsReports& reports) {
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000124 ASSERT(!called_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 called_ = true;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000126 stats_.Clear();
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000127 stats_.number_of_reports = reports.size();
128 for (const auto* r : reports) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000129 if (r->type() == StatsReport::kStatsReportTypeSsrc) {
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000130 GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel,
131 &stats_.audio_output_level);
132 GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel,
133 &stats_.audio_input_level);
134 GetIntValue(r, StatsReport::kStatsValueNameBytesReceived,
135 &stats_.bytes_received);
136 GetIntValue(r, StatsReport::kStatsValueNameBytesSent,
137 &stats_.bytes_sent);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000138 } else if (r->type() == StatsReport::kStatsReportTypeBwe) {
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000139 GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth,
140 &stats_.available_receive_bandwidth);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000141 } else if (r->type() == StatsReport::kStatsReportTypeComponent) {
142 GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher,
143 &stats_.dtls_cipher);
144 GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher,
145 &stats_.srtp_cipher);
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000146 }
147 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 }
149
150 bool called() const { return called_; }
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000151 size_t number_of_reports() const { return stats_.number_of_reports; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000153 int AudioOutputLevel() const {
154 ASSERT(called_);
155 return stats_.audio_output_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 }
157
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000158 int AudioInputLevel() const {
159 ASSERT(called_);
160 return stats_.audio_input_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
162
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000163 int BytesReceived() const {
164 ASSERT(called_);
165 return stats_.bytes_received;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 }
167
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000168 int BytesSent() const {
169 ASSERT(called_);
170 return stats_.bytes_sent;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000171 }
172
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000173 int AvailableReceiveBandwidth() const {
174 ASSERT(called_);
175 return stats_.available_receive_bandwidth;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 }
177
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000178 std::string DtlsCipher() const {
179 ASSERT(called_);
180 return stats_.dtls_cipher;
181 }
182
183 std::string SrtpCipher() const {
184 ASSERT(called_);
185 return stats_.srtp_cipher;
186 }
187
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 private:
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000189 bool GetIntValue(const StatsReport* report,
190 StatsReport::StatsValueName name,
191 int* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000192 const StatsReport::Value* v = report->FindValue(name);
193 if (v) {
194 // TODO(tommi): We should really just be using an int here :-/
195 *value = rtc::FromString<int>(v->ToString());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000197 return v != nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000199
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000200 bool GetStringValue(const StatsReport* report,
201 StatsReport::StatsValueName name,
202 std::string* value) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203 const StatsReport::Value* v = report->FindValue(name);
204 if (v)
205 *value = v->ToString();
206 return v != nullptr;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000207 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 bool called_;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000210 struct {
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000211 void Clear() {
212 number_of_reports = 0;
213 audio_output_level = 0;
214 audio_input_level = 0;
215 bytes_received = 0;
216 bytes_sent = 0;
217 available_receive_bandwidth = 0;
218 dtls_cipher.clear();
219 srtp_cipher.clear();
220 }
221
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000222 size_t number_of_reports;
223 int audio_output_level;
224 int audio_input_level;
225 int bytes_received;
226 int bytes_sent;
227 int available_receive_bandwidth;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000228 std::string dtls_cipher;
229 std::string srtp_cipher;
tommi@webrtc.org209df9b2014-12-17 14:09:05 +0000230 } stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231};
232
233} // namespace webrtc
234
235#endif // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_