From 209df9bf77b68fd872c4218704f98418f7b28ae6 Mon Sep 17 00:00:00 2001 From: "tommi@webrtc.org" Date: Wed, 17 Dec 2014 14:09:05 +0000 Subject: [PATCH] Change MockStatsObserver to grab values inside of OnComplete. This is done since StatsReportCopyable is going away and the list of supported properties of the mock class is known. StatsReports holds a list of pointers to objects that cannot be cached, so this is a simple way to grab the values when they're available. R=perkj@webrtc.org Review URL: https://webrtc-codereview.appspot.com/32859004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7932 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../webrtc/test/mockpeerconnectionobservers.h | 90 +++++++++++-------- 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/talk/app/webrtc/test/mockpeerconnectionobservers.h b/talk/app/webrtc/test/mockpeerconnectionobservers.h index 0570d407b..56ca397f9 100644 --- a/talk/app/webrtc/test/mockpeerconnectionobservers.h +++ b/talk/app/webrtc/test/mockpeerconnectionobservers.h @@ -117,67 +117,81 @@ class MockDataChannelObserver : public webrtc::DataChannelObserver { class MockStatsObserver : public webrtc::StatsObserver { public: - MockStatsObserver() - : called_(false) {} + MockStatsObserver() : called_(false), stats_() {} virtual ~MockStatsObserver() {} + virtual void OnComplete(const StatsReports& reports) { + ASSERT(!called_); called_ = true; - reports_.clear(); - reports_.reserve(reports.size()); - StatsReports::const_iterator it; - for (it = reports.begin(); it != reports.end(); ++it) - reports_.push_back(StatsReportCopyable(*(*it))); + memset(&stats_, sizeof(stats_), 0); + stats_.number_of_reports = reports.size(); + for (const auto* r : reports) { + if (r->type == StatsReport::kStatsReportTypeSsrc) { + GetIntValue(r, StatsReport::kStatsValueNameAudioOutputLevel, + &stats_.audio_output_level); + GetIntValue(r, StatsReport::kStatsValueNameAudioInputLevel, + &stats_.audio_input_level); + GetIntValue(r, StatsReport::kStatsValueNameBytesReceived, + &stats_.bytes_received); + GetIntValue(r, StatsReport::kStatsValueNameBytesSent, + &stats_.bytes_sent); + } else if (r->type == StatsReport::kStatsReportTypeBwe) { + GetIntValue(r, StatsReport::kStatsValueNameAvailableReceiveBandwidth, + &stats_.available_receive_bandwidth); + } + } } bool called() const { return called_; } - size_t number_of_reports() const { return reports_.size(); } + size_t number_of_reports() const { return stats_.number_of_reports; } - int AudioOutputLevel() { - return GetStatsValue(StatsReport::kStatsReportTypeSsrc, - StatsReport::kStatsValueNameAudioOutputLevel); + int AudioOutputLevel() const { + ASSERT(called_); + return stats_.audio_output_level; } - int AudioInputLevel() { - return GetStatsValue(StatsReport::kStatsReportTypeSsrc, - StatsReport::kStatsValueNameAudioInputLevel); + int AudioInputLevel() const { + ASSERT(called_); + return stats_.audio_input_level; } - int BytesReceived() { - return GetStatsValue(StatsReport::kStatsReportTypeSsrc, - StatsReport::kStatsValueNameBytesReceived); + int BytesReceived() const { + ASSERT(called_); + return stats_.bytes_received; } - int BytesSent() { - return GetStatsValue(StatsReport::kStatsReportTypeSsrc, - StatsReport::kStatsValueNameBytesSent); + int BytesSent() const { + ASSERT(called_); + return stats_.bytes_sent; } - int AvailableReceiveBandwidth() { - return GetStatsValue(StatsReport::kStatsReportTypeBwe, - StatsReport::kStatsValueNameAvailableReceiveBandwidth); + int AvailableReceiveBandwidth() const { + ASSERT(called_); + return stats_.available_receive_bandwidth; } private: - int GetStatsValue(const std::string& type, StatsReport::StatsValueName name) { - if (reports_.empty()) { - return 0; - } - for (size_t i = 0; i < reports_.size(); ++i) { - if (reports_[i].type != type) - continue; - webrtc::StatsReport::Values::const_iterator it = - reports_[i].values.begin(); - for (; it != reports_[i].values.end(); ++it) { - if (it->name == name) { - return rtc::FromString(it->value); - } + bool GetIntValue(const StatsReport* report, + StatsReport::StatsValueName name, + int* value) { + for (const auto& v : report->values) { + if (v.name == name) { + *value = rtc::FromString(v.value); + return true; } } - return 0; + return false; } bool called_; - std::vector reports_; + struct { + size_t number_of_reports; + int audio_output_level; + int audio_input_level; + int bytes_received; + int bytes_sent; + int available_receive_bandwidth; + } stats_; }; } // namespace webrtc