Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1041)

Side by Side Diff: webrtc/stats/rtcstatscollector_unittest.cc

Issue 2242043002: RTCStatsCollector and RTCPeerConnectionStats added (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Making sure the RTCPeerConnectionStats has the correct timestamp Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 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 #include "webrtc/stats/rtcstatscollector.h"
12
13 #include <memory>
14 #include <string>
15 #include <utility>
magjed_webrtc 2016/08/25 09:07:52 This looks unused as well?
hbos 2016/08/29 13:10:59 Done.
16 #include <vector>
17
18 #include "webrtc/api/jsepsessiondescription.h"
19 #include "webrtc/api/rtcstats_objects.h"
20 #include "webrtc/api/rtcstatsreport.h"
21 #include "webrtc/api/test/mock_datachannel.h"
22 #include "webrtc/api/test/mock_peerconnection.h"
23 #include "webrtc/api/test/mock_webrtcsession.h"
24 #include "webrtc/base/checks.h"
25 #include "webrtc/base/gunit.h"
26 #include "webrtc/base/logging.h"
27 #include "webrtc/base/test/faketiming.h"
28 #include "webrtc/media/base/fakemediaengine.h"
29
30 using testing::Return;
31 using testing::ReturnRef;
32
33 namespace webrtc {
34
35 class RTCStatsCollectorTester : public SetSessionDescriptionObserver {
36 public:
37 RTCStatsCollectorTester()
38 : worker_thread_(rtc::Thread::Current()),
39 network_thread_(rtc::Thread::Current()),
40 media_engine_(new cricket::FakeMediaEngine()),
41 channel_manager_(new cricket::ChannelManager(media_engine_,
42 worker_thread_,
43 network_thread_)),
44 media_controller_(
45 MediaControllerInterface::Create(cricket::MediaConfig(),
46 worker_thread_,
47 channel_manager_.get())),
48 session_(media_controller_.get()),
49 pc_() {
50 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_));
51 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly(
52 ReturnRef(data_channels_));
53 }
54
55 MockWebRtcSession& session() { return session_; }
56 MockPeerConnection& pc() { return pc_; }
57 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() {
58 return data_channels_;
59 }
60
61 // SetSessionDescriptionObserver overrides.
62 void OnSuccess() override {}
63 void OnFailure(const std::string& error) override {
64 RTC_NOTREACHED() << error;
65 }
66
67 private:
68 rtc::Thread* const worker_thread_;
69 rtc::Thread* const network_thread_;
70 cricket::FakeMediaEngine* media_engine_;
magjed_webrtc 2016/08/25 09:07:52 This member variable is unused.
hbos 2016/08/29 13:10:59 Removed it.
71 std::unique_ptr<cricket::ChannelManager> channel_manager_;
72 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
73 MockWebRtcSession session_;
74 MockPeerConnection pc_;
75
76 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_;
77 };
78
79 class RTCStatsCollectorTest : public testing::Test {
80 public:
81 RTCStatsCollectorTest()
82 : test_(new rtc::RefCountedObject<RTCStatsCollectorTester>()),
83 timing_(new rtc::FakeTiming()),
84 collector_(&test_->pc(), 0.05, std::unique_ptr<rtc::Timing>(timing_)) {
85 }
86
87 protected:
88 rtc::scoped_refptr<RTCStatsCollectorTester> test_;
89 rtc::FakeTiming* timing_; // Owned by |collector_|.
90 RTCStatsCollector collector_;
91 };
92
93 TEST_F(RTCStatsCollectorTest, CachedStatsReport) {
94 // Caching should ensure |a| and |b| are the same report.
95 rtc::scoped_refptr<const RTCStatsReport> a = collector_.GetStatsReport();
96 rtc::scoped_refptr<const RTCStatsReport> b = collector_.GetStatsReport();
97 EXPECT_TRUE(a);
98 EXPECT_EQ(a.get(), b.get());
99 // Invalidate cache by clearing it.
100 collector_.ClearCachedStatsReport();
101 rtc::scoped_refptr<const RTCStatsReport> c = collector_.GetStatsReport();
102 EXPECT_TRUE(c);
103 EXPECT_NE(b.get(), c.get());
104 // Invalidate cache by advancing time.
105 timing_->AdvanceTimeMillisecs(51.0);
106 rtc::scoped_refptr<const RTCStatsReport> d = collector_.GetStatsReport();
107 EXPECT_TRUE(d);
108 EXPECT_NE(c.get(), d.get());
109 }
110
111 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) {
112 rtc::scoped_refptr<const RTCStatsReport> report = collector_.GetStatsReport();
113 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(),
114 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats.";
115 const RTCStats* stats = (*report)["RTCPeerConnection"];
116 EXPECT_TRUE(stats);
117 EXPECT_EQ(stats->timestamp(), timing_->TimerNow());
118 {
119 // Expected stats with no data channels
120 const RTCPeerConnectionStats& pcstats =
121 stats->cast_to<RTCPeerConnectionStats>();
122 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0));
123 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0));
124 }
125
126 test_->data_channels().push_back(
127 new MockDataChannel(DataChannelInterface::kConnecting));
128 test_->data_channels().push_back(
129 new MockDataChannel(DataChannelInterface::kOpen));
130 test_->data_channels().push_back(
131 new MockDataChannel(DataChannelInterface::kClosing));
132 test_->data_channels().push_back(
133 new MockDataChannel(DataChannelInterface::kClosed));
134
135 collector_.ClearCachedStatsReport();
136 report = collector_.GetStatsReport();
137 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(),
138 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats.";
139 stats = (*report)["RTCPeerConnection"];
140 EXPECT_TRUE(stats);
141 {
142 // Expected stats with the above four data channels
143 // TODO(hbos): When the |RTCPeerConnectionStats| is the number of data
144 // channels that have been opened and closed, not the numbers currently
145 // open/closed, we would expect opened >= closed and (opened - closed) to be
146 // the number currently open. crbug.com/636818.
147 const RTCPeerConnectionStats& pcstats =
148 stats->cast_to<RTCPeerConnectionStats>();
149 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1));
150 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3));
151 }
152 }
153
154 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698