OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * 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. | |
9 */ | |
10 | |
11 // This file contains mock implementations of observers used in PeerConnection. | |
12 | |
13 #ifndef WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ | |
14 #define WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ | |
15 | |
16 #include <memory> | |
17 #include <string> | |
18 | |
19 #include "webrtc/api/datachannelinterface.h" | |
20 #include "webrtc/base/checks.h" | |
21 | |
22 namespace webrtc { | |
23 | |
24 class MockCreateSessionDescriptionObserver | |
25 : public webrtc::CreateSessionDescriptionObserver { | |
26 public: | |
27 MockCreateSessionDescriptionObserver() | |
28 : called_(false), | |
29 result_(false) {} | |
30 virtual ~MockCreateSessionDescriptionObserver() {} | |
31 virtual void OnSuccess(SessionDescriptionInterface* desc) { | |
32 called_ = true; | |
33 result_ = true; | |
34 desc_.reset(desc); | |
35 } | |
36 virtual void OnFailure(const std::string& error) { | |
37 called_ = true; | |
38 result_ = false; | |
39 } | |
40 bool called() const { return called_; } | |
41 bool result() const { return result_; } | |
42 SessionDescriptionInterface* release_desc() { | |
43 return desc_.release(); | |
44 } | |
45 | |
46 private: | |
47 bool called_; | |
48 bool result_; | |
49 std::unique_ptr<SessionDescriptionInterface> desc_; | |
50 }; | |
51 | |
52 class MockSetSessionDescriptionObserver | |
53 : public webrtc::SetSessionDescriptionObserver { | |
54 public: | |
55 MockSetSessionDescriptionObserver() | |
56 : called_(false), | |
57 result_(false) {} | |
58 virtual ~MockSetSessionDescriptionObserver() {} | |
59 virtual void OnSuccess() { | |
60 called_ = true; | |
61 result_ = true; | |
62 } | |
63 virtual void OnFailure(const std::string& error) { | |
64 called_ = true; | |
65 result_ = false; | |
66 } | |
67 bool called() const { return called_; } | |
68 bool result() const { return result_; } | |
69 | |
70 private: | |
71 bool called_; | |
72 bool result_; | |
73 }; | |
74 | |
75 class MockDataChannelObserver : public webrtc::DataChannelObserver { | |
76 public: | |
77 explicit MockDataChannelObserver(webrtc::DataChannelInterface* channel) | |
78 : channel_(channel) { | |
79 channel_->RegisterObserver(this); | |
80 state_ = channel_->state(); | |
81 } | |
82 virtual ~MockDataChannelObserver() { | |
83 channel_->UnregisterObserver(); | |
84 } | |
85 | |
86 void OnBufferedAmountChange(uint64_t previous_amount) override {} | |
87 | |
88 void OnStateChange() override { state_ = channel_->state(); } | |
89 void OnMessage(const DataBuffer& buffer) override { | |
90 messages_.push_back( | |
91 std::string(buffer.data.data<char>(), buffer.data.size())); | |
92 } | |
93 | |
94 bool IsOpen() const { return state_ == DataChannelInterface::kOpen; } | |
95 std::vector<std::string> messages() const { return messages_; } | |
96 std::string last_message() const { | |
97 return messages_.empty() ? std::string() : messages_.back(); | |
98 } | |
99 size_t received_message_count() const { return messages_.size(); } | |
100 | |
101 private: | |
102 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_; | |
103 DataChannelInterface::DataState state_; | |
104 std::vector<std::string> messages_; | |
105 }; | |
106 | |
107 class MockStatsObserver : public webrtc::StatsObserver { | |
108 public: | |
109 MockStatsObserver() : called_(false), stats_() {} | |
110 virtual ~MockStatsObserver() {} | |
111 | |
112 virtual void OnComplete(const StatsReports& reports) { | |
113 RTC_CHECK(!called_); | |
114 called_ = true; | |
115 stats_.Clear(); | |
116 stats_.number_of_reports = reports.size(); | |
117 for (const auto* r : reports) { | |
118 if (r->type() == StatsReport::kStatsReportTypeSsrc) { | |
119 stats_.timestamp = r->timestamp(); | |
120 GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel, | |
121 &stats_.audio_output_level); | |
122 GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel, | |
123 &stats_.audio_input_level); | |
124 GetIntValue(r, StatsReport::kStatsValueNameBytesReceived, | |
125 &stats_.bytes_received); | |
126 GetIntValue(r, StatsReport::kStatsValueNameBytesSent, | |
127 &stats_.bytes_sent); | |
128 } else if (r->type() == StatsReport::kStatsReportTypeBwe) { | |
129 stats_.timestamp = r->timestamp(); | |
130 GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth, | |
131 &stats_.available_receive_bandwidth); | |
132 } else if (r->type() == StatsReport::kStatsReportTypeComponent) { | |
133 stats_.timestamp = r->timestamp(); | |
134 GetStringValue(r, StatsReport::kStatsValueNameDtlsCipher, | |
135 &stats_.dtls_cipher); | |
136 GetStringValue(r, StatsReport::kStatsValueNameSrtpCipher, | |
137 &stats_.srtp_cipher); | |
138 } | |
139 } | |
140 } | |
141 | |
142 bool called() const { return called_; } | |
143 size_t number_of_reports() const { return stats_.number_of_reports; } | |
144 double timestamp() const { return stats_.timestamp; } | |
145 | |
146 int AudioOutputLevel() const { | |
147 RTC_CHECK(called_); | |
148 return stats_.audio_output_level; | |
149 } | |
150 | |
151 int AudioInputLevel() const { | |
152 RTC_CHECK(called_); | |
153 return stats_.audio_input_level; | |
154 } | |
155 | |
156 int BytesReceived() const { | |
157 RTC_CHECK(called_); | |
158 return stats_.bytes_received; | |
159 } | |
160 | |
161 int BytesSent() const { | |
162 RTC_CHECK(called_); | |
163 return stats_.bytes_sent; | |
164 } | |
165 | |
166 int AvailableReceiveBandwidth() const { | |
167 RTC_CHECK(called_); | |
168 return stats_.available_receive_bandwidth; | |
169 } | |
170 | |
171 std::string DtlsCipher() const { | |
172 RTC_CHECK(called_); | |
173 return stats_.dtls_cipher; | |
174 } | |
175 | |
176 std::string SrtpCipher() const { | |
177 RTC_CHECK(called_); | |
178 return stats_.srtp_cipher; | |
179 } | |
180 | |
181 private: | |
182 bool GetIntValue(const StatsReport* report, | |
183 StatsReport::StatsValueName name, | |
184 int* value) { | |
185 const StatsReport::Value* v = report->FindValue(name); | |
186 if (v) { | |
187 // TODO(tommi): We should really just be using an int here :-/ | |
188 *value = rtc::FromString<int>(v->ToString()); | |
189 } | |
190 return v != nullptr; | |
191 } | |
192 | |
193 bool GetStringValue(const StatsReport* report, | |
194 StatsReport::StatsValueName name, | |
195 std::string* value) { | |
196 const StatsReport::Value* v = report->FindValue(name); | |
197 if (v) | |
198 *value = v->ToString(); | |
199 return v != nullptr; | |
200 } | |
201 | |
202 bool called_; | |
203 struct { | |
204 void Clear() { | |
205 number_of_reports = 0; | |
206 timestamp = 0; | |
207 audio_output_level = 0; | |
208 audio_input_level = 0; | |
209 bytes_received = 0; | |
210 bytes_sent = 0; | |
211 available_receive_bandwidth = 0; | |
212 dtls_cipher.clear(); | |
213 srtp_cipher.clear(); | |
214 } | |
215 | |
216 size_t number_of_reports; | |
217 double timestamp; | |
218 int audio_output_level; | |
219 int audio_input_level; | |
220 int bytes_received; | |
221 int bytes_sent; | |
222 int available_receive_bandwidth; | |
223 std::string dtls_cipher; | |
224 std::string srtp_cipher; | |
225 } stats_; | |
226 }; | |
227 | |
228 } // namespace webrtc | |
229 | |
230 #endif // WEBRTC_API_TEST_MOCKPEERCONNECTIONOBSERVERS_H_ | |
OLD | NEW |