OLD | NEW |
(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/api/rtcstatsreport.h" |
| 12 |
| 13 #include "webrtc/api/rtcstats.h" |
| 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/gunit.h" |
| 16 |
| 17 namespace webrtc { |
| 18 |
| 19 class RTCTestStats1 : public RTCStats { |
| 20 public: |
| 21 RTCTestStats1(const std::string& id, double timestamp) |
| 22 : RTCStats(id, timestamp), |
| 23 integer("integer") {} |
| 24 |
| 25 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats1, |
| 26 &integer); |
| 27 |
| 28 RTCStatsMember<int32_t> integer; |
| 29 }; |
| 30 |
| 31 const char RTCTestStats1::kType[] = "test-stats-1"; |
| 32 |
| 33 class RTCTestStats2 : public RTCStats { |
| 34 public: |
| 35 RTCTestStats2(const std::string& id, double timestamp) |
| 36 : RTCStats(id, timestamp), |
| 37 number("number") {} |
| 38 |
| 39 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats2, |
| 40 &number); |
| 41 |
| 42 RTCStatsMember<double> number; |
| 43 }; |
| 44 |
| 45 const char RTCTestStats2::kType[] = "test-stats-2"; |
| 46 |
| 47 class RTCTestStats3 : public RTCStats { |
| 48 public: |
| 49 RTCTestStats3(const std::string& id, double timestamp) |
| 50 : RTCStats(id, timestamp), |
| 51 string("string") {} |
| 52 |
| 53 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats3, |
| 54 &string); |
| 55 |
| 56 RTCStatsMember<std::string> string; |
| 57 }; |
| 58 |
| 59 const char RTCTestStats3::kType[] = "test-stats-3"; |
| 60 |
| 61 TEST(RTCStatsReport, AddAndGetStats) { |
| 62 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| 63 EXPECT_EQ(report->size(), static_cast<size_t>(0)); |
| 64 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a0", 1.0))); |
| 65 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a1", 2.0))); |
| 66 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b0", 4.0))); |
| 67 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b1", 8.0))); |
| 68 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a2", 16.0))); |
| 69 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b2", 32.0))); |
| 70 EXPECT_EQ(report->size(), static_cast<size_t>(6)); |
| 71 |
| 72 EXPECT_EQ(report->Get("missing"), nullptr); |
| 73 EXPECT_EQ(report->Get("a0")->id(), "a0"); |
| 74 EXPECT_EQ(report->Get("b2")->id(), "b2"); |
| 75 |
| 76 std::vector<const RTCTestStats1*> a = report->GetStatsOfType<RTCTestStats1>(); |
| 77 EXPECT_EQ(a.size(), static_cast<size_t>(3)); |
| 78 uint32_t mask = 0; |
| 79 for (const RTCTestStats1* stats : a) |
| 80 mask |= static_cast<uint32_t>(stats->timestamp()); |
| 81 EXPECT_EQ(mask, static_cast<uint32_t>(1 | 2 | 16)); |
| 82 |
| 83 std::vector<const RTCTestStats2*> b = report->GetStatsOfType<RTCTestStats2>(); |
| 84 EXPECT_EQ(b.size(), static_cast<size_t>(3)); |
| 85 mask = 0; |
| 86 for (const RTCTestStats2* stats : b) |
| 87 mask |= static_cast<uint32_t>(stats->timestamp()); |
| 88 EXPECT_EQ(mask, static_cast<uint32_t>(4 | 8 | 32)); |
| 89 |
| 90 EXPECT_EQ(report->GetStatsOfType<RTCTestStats3>().size(), |
| 91 static_cast<size_t>(0)); |
| 92 } |
| 93 |
| 94 TEST(RTCStatsReport, StatsOrder) { |
| 95 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| 96 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2.0))); |
| 97 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3.0))); |
| 98 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("B", 1.0))); |
| 99 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("A", 0.0))); |
| 100 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("E", 4.0))); |
| 101 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("F", 5.0))); |
| 102 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("G", 6.0))); |
| 103 size_t i = 0; |
| 104 for (const RTCStats& stats : *report) { |
| 105 EXPECT_EQ(static_cast<size_t>(stats.timestamp()), i); |
| 106 ++i; |
| 107 } |
| 108 EXPECT_EQ(i, static_cast<size_t>(7)); |
| 109 } |
| 110 |
| 111 TEST(RTCStatsReport, TakeMembersFrom) { |
| 112 rtc::scoped_refptr<RTCStatsReport> a = RTCStatsReport::Create(); |
| 113 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("B", 1.0))); |
| 114 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2.0))); |
| 115 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("E", 4.0))); |
| 116 rtc::scoped_refptr<RTCStatsReport> b = RTCStatsReport::Create(); |
| 117 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("A", 0.0))); |
| 118 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3.0))); |
| 119 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("F", 5.0))); |
| 120 |
| 121 a->TakeMembersFrom(b); |
| 122 EXPECT_EQ(b->size(), static_cast<size_t>(0)); |
| 123 size_t i = 0; |
| 124 for (const RTCStats& stats : *a) { |
| 125 EXPECT_EQ(static_cast<size_t>(stats.timestamp()), i); |
| 126 ++i; |
| 127 } |
| 128 EXPECT_EQ(i, static_cast<size_t>(6)); |
| 129 } |
| 130 |
| 131 } // namespace webrtc |
OLD | NEW |