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