blob: 174b80bb53927ca125bfa4339319fdbecf08e83f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
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
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) {
103 last_message_.assign(buffer.data.data(), buffer.data.length());
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:
120 MockStatsObserver()
121 : called_(false) {}
122 virtual ~MockStatsObserver() {}
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000123 virtual void OnComplete(const StatsReports& reports) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 called_ = true;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000125 reports_.clear();
126 reports_.reserve(reports.size());
127 StatsReports::const_iterator it;
128 for (it = reports.begin(); it != reports.end(); ++it)
129 reports_.push_back(StatsReportCopyable(*(*it)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 }
131
132 bool called() const { return called_; }
133 size_t number_of_reports() const { return reports_.size(); }
134
135 int AudioOutputLevel() {
136 return GetSsrcStatsValue(
137 webrtc::StatsReport::kStatsValueNameAudioOutputLevel);
138 }
139
140 int AudioInputLevel() {
141 return GetSsrcStatsValue(
142 webrtc::StatsReport::kStatsValueNameAudioInputLevel);
143 }
144
145 int BytesReceived() {
146 return GetSsrcStatsValue(
147 webrtc::StatsReport::kStatsValueNameBytesReceived);
148 }
149
150 int BytesSent() {
151 return GetSsrcStatsValue(webrtc::StatsReport::kStatsValueNameBytesSent);
152 }
153
154 private:
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000155 int GetSsrcStatsValue(StatsReport::StatsValueName name) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 if (reports_.empty()) {
157 return 0;
158 }
159 for (size_t i = 0; i < reports_.size(); ++i) {
160 if (reports_[i].type != StatsReport::kStatsReportTypeSsrc)
161 continue;
162 webrtc::StatsReport::Values::const_iterator it =
163 reports_[i].values.begin();
164 for (; it != reports_[i].values.end(); ++it) {
165 if (it->name == name) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000166 return rtc::FromString<int>(it->value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
168 }
169 }
170 return 0;
171 }
172
173 bool called_;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000174 std::vector<StatsReportCopyable> reports_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175};
176
177} // namespace webrtc
178
179#endif // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_