Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / third_party / libwebrtc / stats / rtc_stats_report_unittest.cc
blobb3ac0a2db49cd95307ba883b7b625c004bf86563
1 /*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
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 */
11 #include "api/stats/rtc_stats_report.h"
13 #include "api/stats/attribute.h"
14 #include "api/stats/rtc_stats.h"
15 #include "rtc_base/checks.h"
16 #include "test/gtest.h"
18 namespace webrtc {
20 class RTCTestStats1 : public RTCStats {
21 public:
22 WEBRTC_RTCSTATS_DECL();
24 RTCTestStats1(const std::string& id, Timestamp timestamp)
25 : RTCStats(id, timestamp) {}
27 RTCStatsMember<int32_t> integer;
30 WEBRTC_RTCSTATS_IMPL(RTCTestStats1,
31 RTCStats,
32 "test-stats-1",
33 AttributeInit("integer", &integer))
35 class RTCTestStats2 : public RTCStats {
36 public:
37 WEBRTC_RTCSTATS_DECL();
39 RTCTestStats2(const std::string& id, Timestamp timestamp)
40 : RTCStats(id, timestamp) {}
42 RTCStatsMember<double> number;
45 WEBRTC_RTCSTATS_IMPL(RTCTestStats2,
46 RTCStats,
47 "test-stats-2",
48 AttributeInit("number", &number))
50 class RTCTestStats3 : public RTCStats {
51 public:
52 WEBRTC_RTCSTATS_DECL();
54 RTCTestStats3(const std::string& id, Timestamp timestamp)
55 : RTCStats(id, timestamp) {}
57 RTCStatsMember<std::string> string;
60 WEBRTC_RTCSTATS_IMPL(RTCTestStats3,
61 RTCStats,
62 "test-stats-3",
63 AttributeInit("string", &string))
65 TEST(RTCStatsReport, AddAndGetStats) {
66 rtc::scoped_refptr<RTCStatsReport> report =
67 RTCStatsReport::Create(Timestamp::Micros(1337));
68 EXPECT_EQ(report->timestamp().us_or(-1), 1337u);
69 EXPECT_EQ(report->size(), static_cast<size_t>(0));
70 report->AddStats(
71 std::unique_ptr<RTCStats>(new RTCTestStats1("a0", Timestamp::Micros(1))));
72 report->AddStats(
73 std::unique_ptr<RTCStats>(new RTCTestStats1("a1", Timestamp::Micros(2))));
74 report->AddStats(
75 std::unique_ptr<RTCStats>(new RTCTestStats2("b0", Timestamp::Micros(4))));
76 report->AddStats(
77 std::unique_ptr<RTCStats>(new RTCTestStats2("b1", Timestamp::Micros(8))));
78 report->AddStats(std::unique_ptr<RTCStats>(
79 new RTCTestStats1("a2", Timestamp::Micros(16))));
80 report->AddStats(std::unique_ptr<RTCStats>(
81 new RTCTestStats2("b2", Timestamp::Micros(32))));
82 EXPECT_EQ(report->size(), static_cast<size_t>(6));
84 EXPECT_EQ(report->Get("missing"), nullptr);
85 EXPECT_EQ(report->Get("a0")->id(), "a0");
86 EXPECT_EQ(report->Get("b2")->id(), "b2");
88 std::vector<const RTCTestStats1*> a = report->GetStatsOfType<RTCTestStats1>();
89 EXPECT_EQ(a.size(), static_cast<size_t>(3));
90 int64_t mask = 0;
91 for (const RTCTestStats1* stats : a)
92 mask |= stats->timestamp().us();
93 EXPECT_EQ(mask, static_cast<int64_t>(1 | 2 | 16));
95 std::vector<const RTCTestStats2*> b = report->GetStatsOfType<RTCTestStats2>();
96 EXPECT_EQ(b.size(), static_cast<size_t>(3));
97 mask = 0;
98 for (const RTCTestStats2* stats : b)
99 mask |= stats->timestamp().us();
100 EXPECT_EQ(mask, static_cast<int64_t>(4 | 8 | 32));
102 EXPECT_EQ(report->GetStatsOfType<RTCTestStats3>().size(),
103 static_cast<size_t>(0));
106 TEST(RTCStatsReport, StatsOrder) {
107 rtc::scoped_refptr<RTCStatsReport> report =
108 RTCStatsReport::Create(Timestamp::Micros(1337));
109 EXPECT_EQ(report->timestamp().us(), 1337u);
110 EXPECT_EQ(report->timestamp().us_or(-1), 1337u);
111 report->AddStats(
112 std::unique_ptr<RTCStats>(new RTCTestStats1("C", Timestamp::Micros(2))));
113 report->AddStats(
114 std::unique_ptr<RTCStats>(new RTCTestStats1("D", Timestamp::Micros(3))));
115 report->AddStats(
116 std::unique_ptr<RTCStats>(new RTCTestStats2("B", Timestamp::Micros(1))));
117 report->AddStats(
118 std::unique_ptr<RTCStats>(new RTCTestStats2("A", Timestamp::Micros(0))));
119 report->AddStats(
120 std::unique_ptr<RTCStats>(new RTCTestStats2("E", Timestamp::Micros(4))));
121 report->AddStats(
122 std::unique_ptr<RTCStats>(new RTCTestStats2("F", Timestamp::Micros(5))));
123 report->AddStats(
124 std::unique_ptr<RTCStats>(new RTCTestStats2("G", Timestamp::Micros(6))));
125 int64_t i = 0;
126 for (const RTCStats& stats : *report) {
127 EXPECT_EQ(stats.timestamp().us(), i);
128 ++i;
130 EXPECT_EQ(i, static_cast<int64_t>(7));
133 TEST(RTCStatsReport, Take) {
134 rtc::scoped_refptr<RTCStatsReport> report =
135 RTCStatsReport::Create(Timestamp::Zero());
136 report->AddStats(
137 std::unique_ptr<RTCStats>(new RTCTestStats1("A", Timestamp::Micros(1))));
138 report->AddStats(
139 std::unique_ptr<RTCStats>(new RTCTestStats1("B", Timestamp::Micros(2))));
140 EXPECT_TRUE(report->Get("A"));
141 EXPECT_EQ(report->size(), 2u);
142 auto a = report->Take("A");
143 EXPECT_TRUE(a);
144 EXPECT_EQ(report->size(), 1u);
145 EXPECT_FALSE(report->Get("A"));
146 EXPECT_FALSE(report->Take("A"));
149 TEST(RTCStatsReport, TakeMembersFrom) {
150 rtc::scoped_refptr<RTCStatsReport> a =
151 RTCStatsReport::Create(Timestamp::Micros(1337));
152 EXPECT_EQ(a->timestamp().us_or(-1), 1337u);
153 a->AddStats(
154 std::unique_ptr<RTCStats>(new RTCTestStats1("B", Timestamp::Micros(1))));
155 a->AddStats(
156 std::unique_ptr<RTCStats>(new RTCTestStats1("C", Timestamp::Micros(2))));
157 a->AddStats(
158 std::unique_ptr<RTCStats>(new RTCTestStats1("E", Timestamp::Micros(4))));
159 rtc::scoped_refptr<RTCStatsReport> b =
160 RTCStatsReport::Create(Timestamp::Micros(1338));
161 EXPECT_EQ(b->timestamp().us_or(-1), 1338u);
162 b->AddStats(
163 std::unique_ptr<RTCStats>(new RTCTestStats1("A", Timestamp::Micros(0))));
164 b->AddStats(
165 std::unique_ptr<RTCStats>(new RTCTestStats1("D", Timestamp::Micros(3))));
166 b->AddStats(
167 std::unique_ptr<RTCStats>(new RTCTestStats1("F", Timestamp::Micros(5))));
169 a->TakeMembersFrom(b);
170 EXPECT_EQ(b->size(), static_cast<size_t>(0));
171 int64_t i = 0;
172 for (const RTCStats& stats : *a) {
173 EXPECT_EQ(stats.timestamp().us(), i);
174 ++i;
176 EXPECT_EQ(i, static_cast<int64_t>(6));
179 } // namespace webrtc