blob: 78543557442823f7d50fde50203417d3577b5a36 [file] [log] [blame]
hbos615d3012016-08-24 08:33:131/*
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
hbos74e1a4f2016-09-16 06:33:0111#include "webrtc/api/stats/rtcstats.h"
hbos615d3012016-08-24 08:33:1312
hbosfdafab82016-09-14 13:02:1313#include <cstring>
14
hbos615d3012016-08-24 08:33:1315#include "webrtc/base/checks.h"
16#include "webrtc/base/gunit.h"
hbos27e177c2016-09-19 13:05:5617#include "webrtc/stats/test/rtcteststats.h"
hbos615d3012016-08-24 08:33:1318
19namespace webrtc {
20
hbos615d3012016-08-24 08:33:1321class RTCChildStats : public RTCStats {
22 public:
hbosfc5e0502016-10-06 09:06:1023 WEBRTC_RTCSTATS_DECL();
24
hbos0e6758d2016-08-31 14:57:3625 RTCChildStats(const std::string& id, int64_t timestamp_us)
26 : RTCStats(id, timestamp_us),
hbos615d3012016-08-24 08:33:1327 child_int("childInt") {}
28
hbos615d3012016-08-24 08:33:1329 RTCStatsMember<int32_t> child_int;
30};
31
hbosfc5e0502016-10-06 09:06:1032WEBRTC_RTCSTATS_IMPL(RTCChildStats, RTCStats, "child-stats",
33 &child_int);
hbos615d3012016-08-24 08:33:1334
35class RTCGrandChildStats : public RTCChildStats {
36 public:
hbosfc5e0502016-10-06 09:06:1037 WEBRTC_RTCSTATS_DECL();
38
hbos0e6758d2016-08-31 14:57:3639 RTCGrandChildStats(const std::string& id, int64_t timestamp_us)
40 : RTCChildStats(id, timestamp_us),
hbos615d3012016-08-24 08:33:1341 grandchild_int("grandchildInt") {}
42
hbos615d3012016-08-24 08:33:1343 RTCStatsMember<int32_t> grandchild_int;
44};
45
hbosfc5e0502016-10-06 09:06:1046WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats, RTCChildStats, "grandchild-stats",
47 &grandchild_int);
hbos615d3012016-08-24 08:33:1348
49TEST(RTCStatsTest, RTCStatsAndMembers) {
hbos0e6758d2016-08-31 14:57:3650 RTCTestStats stats("testId", 42);
hbos615d3012016-08-24 08:33:1351 EXPECT_EQ(stats.id(), "testId");
hbos0e6758d2016-08-31 14:57:3652 EXPECT_EQ(stats.timestamp_us(), static_cast<int64_t>(42));
hbos615d3012016-08-24 08:33:1353 std::vector<const RTCStatsMemberInterface*> members = stats.Members();
hbosb20f3872016-10-04 21:37:1154 EXPECT_EQ(members.size(), static_cast<size_t>(14));
hbos615d3012016-08-24 08:33:1355 for (const RTCStatsMemberInterface* member : members) {
56 EXPECT_FALSE(member->is_defined());
57 }
hbosb20f3872016-10-04 21:37:1158 stats.m_bool = true;
hbos615d3012016-08-24 08:33:1359 stats.m_int32 = 123;
60 stats.m_uint32 = 123;
61 stats.m_int64 = 123;
62 stats.m_uint64 = 123;
63 stats.m_double = 123.0;
hbos615d3012016-08-24 08:33:1364 stats.m_string = std::string("123");
hbosfdafab82016-09-14 13:02:1365
hbosb20f3872016-10-04 21:37:1166 std::vector<bool> sequence_bool;
67 sequence_bool.push_back(true);
hbosfdafab82016-09-14 13:02:1368 std::vector<int32_t> sequence_int32;
69 sequence_int32.push_back(static_cast<int32_t>(1));
70 std::vector<uint32_t> sequence_uint32;
71 sequence_uint32.push_back(static_cast<uint32_t>(2));
72 std::vector<int64_t> sequence_int64;
73 sequence_int64.push_back(static_cast<int64_t>(3));
74 std::vector<uint64_t> sequence_uint64;
75 sequence_uint64.push_back(static_cast<uint64_t>(4));
76 std::vector<double> sequence_double;
77 sequence_double.push_back(5.0);
hbosfdafab82016-09-14 13:02:1378 std::vector<std::string> sequence_string;
hbos8faf9e02016-09-15 13:52:4379 sequence_string.push_back(std::string("six"));
hbosfdafab82016-09-14 13:02:1380
hbosb20f3872016-10-04 21:37:1181 stats.m_sequence_bool = sequence_bool;
hbosfdafab82016-09-14 13:02:1382 stats.m_sequence_int32 = sequence_int32;
83 stats.m_sequence_uint32 = sequence_uint32;
hbos615d3012016-08-24 08:33:1384 EXPECT_FALSE(stats.m_sequence_int64.is_defined());
hbosfdafab82016-09-14 13:02:1385 stats.m_sequence_int64 = sequence_int64;
86 stats.m_sequence_uint64 = sequence_uint64;
87 stats.m_sequence_double = sequence_double;
hbosfdafab82016-09-14 13:02:1388 stats.m_sequence_string = sequence_string;
hbos615d3012016-08-24 08:33:1389 for (const RTCStatsMemberInterface* member : members) {
90 EXPECT_TRUE(member->is_defined());
91 }
hbosb20f3872016-10-04 21:37:1192 EXPECT_EQ(*stats.m_bool, true);
hbos615d3012016-08-24 08:33:1393 EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123));
94 EXPECT_EQ(*stats.m_uint32, static_cast<uint32_t>(123));
95 EXPECT_EQ(*stats.m_int64, static_cast<int64_t>(123));
96 EXPECT_EQ(*stats.m_uint64, static_cast<uint64_t>(123));
97 EXPECT_EQ(*stats.m_double, 123.0);
hbos615d3012016-08-24 08:33:1398 EXPECT_EQ(*stats.m_string, std::string("123"));
hbosb20f3872016-10-04 21:37:1199 EXPECT_EQ(*stats.m_sequence_bool, sequence_bool);
hbosfdafab82016-09-14 13:02:13100 EXPECT_EQ(*stats.m_sequence_int32, sequence_int32);
101 EXPECT_EQ(*stats.m_sequence_uint32, sequence_uint32);
102 EXPECT_EQ(*stats.m_sequence_int64, sequence_int64);
103 EXPECT_EQ(*stats.m_sequence_uint64, sequence_uint64);
104 EXPECT_EQ(*stats.m_sequence_double, sequence_double);
hbosfdafab82016-09-14 13:02:13105 EXPECT_EQ(*stats.m_sequence_string, sequence_string);
106
hbos615d3012016-08-24 08:33:13107 int32_t numbers[] = { 4, 8, 15, 16, 23, 42 };
hbosfdafab82016-09-14 13:02:13108 std::vector<int32_t> numbers_sequence(&numbers[0], &numbers[6]);
109 stats.m_sequence_int32->clear();
hbos615d3012016-08-24 08:33:13110 stats.m_sequence_int32->insert(stats.m_sequence_int32->end(),
111 numbers_sequence.begin(),
112 numbers_sequence.end());
113 EXPECT_EQ(*stats.m_sequence_int32, numbers_sequence);
114}
115
hbos67c8bc42016-10-25 11:31:23116TEST(RTCStatsTest, EqualityOperator) {
117 RTCTestStats empty_stats("testId", 123);
118 EXPECT_EQ(empty_stats, empty_stats);
119
120 RTCTestStats stats_with_all_values = empty_stats;
121 stats_with_all_values.m_bool = true;
122 stats_with_all_values.m_int32 = 123;
123 stats_with_all_values.m_uint32 = 123;
124 stats_with_all_values.m_int64 = 123;
125 stats_with_all_values.m_uint64 = 123;
126 stats_with_all_values.m_double = 123.0;
127 stats_with_all_values.m_string = "123";
128 stats_with_all_values.m_sequence_bool = std::vector<bool>();
129 stats_with_all_values.m_sequence_int32 = std::vector<int32_t>();
130 stats_with_all_values.m_sequence_uint32 = std::vector<uint32_t>();
131 stats_with_all_values.m_sequence_int64 = std::vector<int64_t>();
132 stats_with_all_values.m_sequence_uint64 = std::vector<uint64_t>();
133 stats_with_all_values.m_sequence_double = std::vector<double>();
134 stats_with_all_values.m_sequence_string = std::vector<std::string>();
135 EXPECT_NE(stats_with_all_values, empty_stats);
136 EXPECT_EQ(stats_with_all_values, stats_with_all_values);
137 EXPECT_NE(stats_with_all_values.m_int32, stats_with_all_values.m_uint32);
138
139 RTCTestStats one_member_different[] = {
140 stats_with_all_values, stats_with_all_values, stats_with_all_values,
141 stats_with_all_values, stats_with_all_values, stats_with_all_values,
142 stats_with_all_values, stats_with_all_values, stats_with_all_values,
143 stats_with_all_values, stats_with_all_values, stats_with_all_values,
144 stats_with_all_values, stats_with_all_values,
145 };
146 for (size_t i = 0; i < 14; ++i) {
147 EXPECT_EQ(stats_with_all_values, one_member_different[i]);
148 }
149 one_member_different[0].m_bool = false;
150 one_member_different[1].m_int32 = 321;
151 one_member_different[2].m_uint32 = 321;
152 one_member_different[3].m_int64 = 321;
153 one_member_different[4].m_uint64 = 321;
154 one_member_different[5].m_double = 321.0;
155 one_member_different[6].m_string = "321";
156 one_member_different[7].m_sequence_bool->push_back(false);
157 one_member_different[8].m_sequence_int32->push_back(321);
158 one_member_different[9].m_sequence_uint32->push_back(321);
159 one_member_different[10].m_sequence_int64->push_back(321);
160 one_member_different[11].m_sequence_uint64->push_back(321);
161 one_member_different[12].m_sequence_double->push_back(321.0);
162 one_member_different[13].m_sequence_string->push_back("321");
163 for (size_t i = 0; i < 14; ++i) {
164 EXPECT_NE(stats_with_all_values, one_member_different[i]);
165 }
166
167 RTCTestStats empty_stats_different_id("testId2", 123);
168 EXPECT_NE(empty_stats, empty_stats_different_id);
169 RTCTestStats empty_stats_different_timestamp("testId", 321);
170 EXPECT_NE(empty_stats, empty_stats_different_timestamp);
171
172 RTCChildStats child("childId", 42);
173 RTCGrandChildStats grandchild("grandchildId", 42);
174 EXPECT_NE(child, grandchild);
hbos28747962016-11-21 17:17:41175
176 RTCChildStats stats_with_defined_member("leId", 0);
177 stats_with_defined_member.child_int = 0;
178 RTCChildStats stats_with_undefined_member("leId", 0);
179 EXPECT_NE(stats_with_defined_member, stats_with_undefined_member);
180 EXPECT_NE(stats_with_undefined_member, stats_with_defined_member);
hbos67c8bc42016-10-25 11:31:23181}
182
hbos615d3012016-08-24 08:33:13183TEST(RTCStatsTest, RTCStatsGrandChild) {
184 RTCGrandChildStats stats("grandchild", 0.0);
185 stats.child_int = 1;
186 stats.grandchild_int = 2;
187 int32_t sum = 0;
188 for (const RTCStatsMemberInterface* member : stats.Members()) {
189 sum += *member->cast_to<const RTCStatsMember<int32_t>>();
190 }
191 EXPECT_EQ(sum, static_cast<int32_t>(3));
192
193 std::unique_ptr<RTCStats> copy_ptr = stats.copy();
194 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>();
195 EXPECT_EQ(*copy.child_int, *stats.child_int);
196 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
197}
198
hbos615d3012016-08-24 08:33:13199// Death tests.
200// Disabled on Android because death tests misbehave on Android, see
201// base/test/gtest_util.h.
202#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
203
204TEST(RTCStatsDeathTest, ValueOfUndefinedMember) {
205 RTCTestStats stats("testId", 0.0);
206 EXPECT_FALSE(stats.m_int32.is_defined());
207 EXPECT_DEATH(*stats.m_int32, "");
208}
209
210TEST(RTCStatsDeathTest, InvalidCasting) {
211 RTCGrandChildStats stats("grandchild", 0.0);
212 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), "");
213}
214
215#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
216
217} // namespace webrtc