3 * Copyright 2012, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 // This file contains mock implementations of observers used in PeerConnection.
30 #ifndef TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
31 #define TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_
35 #include "talk/app/webrtc/datachannelinterface.h"
39 class MockCreateSessionDescriptionObserver
40 : public webrtc::CreateSessionDescriptionObserver {
42 MockCreateSessionDescriptionObserver()
45 virtual ~MockCreateSessionDescriptionObserver() {}
46 virtual void OnSuccess(SessionDescriptionInterface* desc) {
51 virtual void OnFailure(const std::string& error) {
55 bool called() const { return called_; }
56 bool result() const { return result_; }
57 SessionDescriptionInterface* release_desc() {
58 return desc_.release();
64 rtc::scoped_ptr<SessionDescriptionInterface> desc_;
67 class MockSetSessionDescriptionObserver
68 : public webrtc::SetSessionDescriptionObserver {
70 MockSetSessionDescriptionObserver()
73 virtual ~MockSetSessionDescriptionObserver() {}
74 virtual void OnSuccess() {
78 virtual void OnFailure(const std::string& error) {
82 bool called() const { return called_; }
83 bool result() const { return result_; }
90 class MockDataChannelObserver : public webrtc::DataChannelObserver {
92 explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel)
93 : channel_(channel), received_message_count_(0) {
94 channel_->RegisterObserver(this);
95 state_ = channel_->state();
97 virtual ~MockDataChannelObserver() {
98 channel_->UnregisterObserver();
101 virtual void OnStateChange() { state_ = channel_->state(); }
102 virtual void OnMessage(const DataBuffer& buffer) {
103 last_message_.assign(buffer.data.data(), buffer.data.length());
104 ++received_message_count_;
107 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; }
108 const std::string& last_message() const { return last_message_; }
109 size_t received_message_count() const { return received_message_count_; }
112 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
113 DataChannelInterface::DataState state_;
114 std::string last_message_;
115 size_t received_message_count_;
118 class MockStatsObserver : public webrtc::StatsObserver {
122 virtual ~MockStatsObserver() {}
123 virtual void OnComplete(const StatsReports& reports) {
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)));
132 bool called() const { return called_; }
133 size_t number_of_reports() const { return reports_.size(); }
135 int AudioOutputLevel() {
136 return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
137 StatsReport::kStatsValueNameAudioOutputLevel);
140 int AudioInputLevel() {
141 return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
142 StatsReport::kStatsValueNameAudioInputLevel);
145 int BytesReceived() {
146 return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
147 StatsReport::kStatsValueNameBytesReceived);
151 return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
152 StatsReport::kStatsValueNameBytesSent);
155 int AvailableReceiveBandwidth() {
156 return GetStatsValue(StatsReport::kStatsReportTypeBwe,
157 StatsReport::kStatsValueNameAvailableReceiveBandwidth);
161 int GetStatsValue(const std::string& type, StatsReport::StatsValueName name) {
162 if (reports_.empty()) {
165 for (size_t i = 0; i < reports_.size(); ++i) {
166 if (reports_[i].type != type)
168 webrtc::StatsReport::Values::const_iterator it =
169 reports_[i].values.begin();
170 for (; it != reports_[i].values.end(); ++it) {
171 if (it->name == name) {
172 return rtc::FromString<int>(it->value);
180 std::vector<StatsReportCopyable> reports_;
183 } // namespace webrtc
185 #endif // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_