OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/rtcstats.h" | 11 #include "webrtc/api/rtcstats.h" |
12 #include "webrtc/api/rtcstatsreport.h" | |
13 | 12 |
14 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
15 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
16 #include "webrtc/base/logging.h" | |
17 | 15 |
18 namespace webrtc { | 16 namespace webrtc { |
19 | 17 |
20 class RTCTestStats : public RTCStats { | 18 class RTCTestStats : public RTCStats { |
21 public: | 19 public: |
22 RTCTestStats(const std::string& id, double timestamp) | 20 RTCTestStats(const std::string& id, double timestamp) |
23 : RTCStats(id, timestamp), | 21 : RTCStats(id, timestamp), |
24 m_int32("mInt32"), | 22 m_int32("mInt32"), |
25 m_uint32("mUint32"), | 23 m_uint32("mUint32"), |
26 m_int64("mInt64"), | 24 m_int64("mInt64"), |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 sum += *member->cast_to<const RTCStatsMember<int32_t>>(); | 156 sum += *member->cast_to<const RTCStatsMember<int32_t>>(); |
159 } | 157 } |
160 EXPECT_EQ(sum, static_cast<int32_t>(3)); | 158 EXPECT_EQ(sum, static_cast<int32_t>(3)); |
161 | 159 |
162 std::unique_ptr<RTCStats> copy_ptr = stats.copy(); | 160 std::unique_ptr<RTCStats> copy_ptr = stats.copy(); |
163 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>(); | 161 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>(); |
164 EXPECT_EQ(*copy.child_int, *stats.child_int); | 162 EXPECT_EQ(*copy.child_int, *stats.child_int); |
165 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int); | 163 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int); |
166 } | 164 } |
167 | 165 |
168 TEST(RTCStatsTest, TestRTCStatsReport) { | |
169 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | |
170 EXPECT_EQ(report->size(), static_cast<size_t>(0)); | |
171 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats("a0", 1.0))); | |
172 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats("a1", 2.0))); | |
173 report->AddStats(std::unique_ptr<RTCStats>(new RTCChildStats("b0", 4.0))); | |
174 report->AddStats(std::unique_ptr<RTCStats>(new RTCChildStats("b1", 8.0))); | |
175 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats("a2", 16.0))); | |
176 report->AddStats(std::unique_ptr<RTCStats>(new RTCChildStats("b2", 32.0))); | |
177 EXPECT_EQ(report->size(), static_cast<size_t>(6)); | |
178 | |
179 EXPECT_EQ((*report)["missing"], nullptr); | |
180 EXPECT_EQ((*report)["a0"]->id(), "a0"); | |
181 EXPECT_EQ((*report)["b2"]->id(), "b2"); | |
182 | |
183 std::vector<const RTCTestStats*> a = report->GetStatsOfType<RTCTestStats>(); | |
184 EXPECT_EQ(a.size(), static_cast<size_t>(3)); | |
185 uint32_t mask = 0; | |
186 for (const RTCTestStats* stats : a) | |
187 mask |= static_cast<uint32_t>(stats->timestamp()); | |
188 EXPECT_EQ(mask, static_cast<uint32_t>(1 | 2 | 16)); | |
189 | |
190 std::vector<const RTCChildStats*> b = report->GetStatsOfType<RTCChildStats>(); | |
191 EXPECT_EQ(b.size(), static_cast<size_t>(3)); | |
192 mask = 0; | |
193 for (const RTCChildStats* stats : b) | |
194 mask |= static_cast<uint32_t>(stats->timestamp()); | |
195 EXPECT_EQ(mask, static_cast<uint32_t>(4 | 8 | 32)); | |
196 | |
197 EXPECT_EQ(report->GetStatsOfType<RTCGrandChildStats>().size(), | |
198 static_cast<size_t>(0)); | |
199 } | |
200 | |
201 // Death tests. | 166 // Death tests. |
202 // Disabled on Android because death tests misbehave on Android, see | 167 // Disabled on Android because death tests misbehave on Android, see |
203 // base/test/gtest_util.h. | 168 // base/test/gtest_util.h. |
204 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) | 169 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
205 | 170 |
206 TEST(RTCStatsDeathTest, ValueOfUndefinedMember) { | 171 TEST(RTCStatsDeathTest, ValueOfUndefinedMember) { |
207 RTCTestStats stats("testId", 0.0); | 172 RTCTestStats stats("testId", 0.0); |
208 EXPECT_FALSE(stats.m_int32.is_defined()); | 173 EXPECT_FALSE(stats.m_int32.is_defined()); |
209 EXPECT_DEATH(*stats.m_int32, ""); | 174 EXPECT_DEATH(*stats.m_int32, ""); |
210 } | 175 } |
211 | 176 |
212 TEST(RTCStatsDeathTest, InvalidCasting) { | 177 TEST(RTCStatsDeathTest, InvalidCasting) { |
213 RTCGrandChildStats stats("grandchild", 0.0); | 178 RTCGrandChildStats stats("grandchild", 0.0); |
214 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), ""); | 179 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), ""); |
215 } | 180 } |
216 | 181 |
217 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) | 182 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
218 | 183 |
219 } // namespace webrtc | 184 } // namespace webrtc |
OLD | NEW |