Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / app / webrtc / test / mockpeerconnectionobservers.h
1 /*
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
37 namespace webrtc {
38
39 class 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_;
64   rtc::scoped_ptr<SessionDescriptionInterface> desc_;
65 };
66
67 class 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
90 class MockDataChannelObserver : public webrtc::DataChannelObserver {
91  public:
92   explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel)
93      : channel_(channel), received_message_count_(0) {
94     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());
104     ++received_message_count_;
105   }
106
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_; }
110
111  private:
112   rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
113   DataChannelInterface::DataState state_;
114   std::string last_message_;
115   size_t received_message_count_;
116 };
117
118 class MockStatsObserver : public webrtc::StatsObserver {
119  public:
120   MockStatsObserver()
121       : called_(false) {}
122   virtual ~MockStatsObserver() {}
123   virtual void OnComplete(const StatsReports& reports) {
124     called_ = true;
125     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)));
130   }
131
132   bool called() const { return called_; }
133   size_t number_of_reports() const { return reports_.size(); }
134
135   int AudioOutputLevel() {
136     return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
137                          StatsReport::kStatsValueNameAudioOutputLevel);
138   }
139
140   int AudioInputLevel() {
141     return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
142                          StatsReport::kStatsValueNameAudioInputLevel);
143   }
144
145   int BytesReceived() {
146     return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
147                          StatsReport::kStatsValueNameBytesReceived);
148   }
149
150   int BytesSent() {
151     return GetStatsValue(StatsReport::kStatsReportTypeSsrc,
152                          StatsReport::kStatsValueNameBytesSent);
153   }
154
155   int AvailableReceiveBandwidth() {
156     return GetStatsValue(StatsReport::kStatsReportTypeBwe,
157                          StatsReport::kStatsValueNameAvailableReceiveBandwidth);
158   }
159
160  private:
161   int GetStatsValue(const std::string& type, StatsReport::StatsValueName name) {
162     if (reports_.empty()) {
163       return 0;
164     }
165     for (size_t i = 0; i < reports_.size(); ++i) {
166       if (reports_[i].type != type)
167         continue;
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);
173         }
174       }
175     }
176     return 0;
177   }
178
179   bool called_;
180   std::vector<StatsReportCopyable> reports_;
181 };
182
183 }  // namespace webrtc
184
185 #endif  // TALK_APP_WEBRTC_TEST_MOCKPEERCONNECTIONOBSERVERS_H_