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

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

Issue 2983243002: Make RTCStatsReport::ToString() return JSON-parseable string. (Closed)
Patch Set: Add test. Created 3 years, 4 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
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/stats/rtcstats.h" 11 #include "webrtc/api/stats/rtcstats.h"
12 12
13 #include <cstring> 13 #include <cstring>
14 14
15 #include "webrtc/rtc_base/checks.h" 15 #include "webrtc/rtc_base/checks.h"
16 #include "webrtc/rtc_base/gunit.h" 16 #include "webrtc/rtc_base/gunit.h"
17 #include "webrtc/rtc_base/json.h"
17 #include "webrtc/stats/test/rtcteststats.h" 18 #include "webrtc/stats/test/rtcteststats.h"
18 19
19 namespace webrtc { 20 namespace webrtc {
20 21
21 class RTCChildStats : public RTCStats { 22 class RTCChildStats : public RTCStats {
22 public: 23 public:
23 WEBRTC_RTCSTATS_DECL(); 24 WEBRTC_RTCSTATS_DECL();
24 25
25 RTCChildStats(const std::string& id, int64_t timestamp_us) 26 RTCChildStats(const std::string& id, int64_t timestamp_us)
26 : RTCStats(id, timestamp_us), 27 : RTCStats(id, timestamp_us),
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 sum += *member->cast_to<const RTCStatsMember<int32_t>>(); 190 sum += *member->cast_to<const RTCStatsMember<int32_t>>();
190 } 191 }
191 EXPECT_EQ(sum, static_cast<int32_t>(3)); 192 EXPECT_EQ(sum, static_cast<int32_t>(3));
192 193
193 std::unique_ptr<RTCStats> copy_ptr = stats.copy(); 194 std::unique_ptr<RTCStats> copy_ptr = stats.copy();
194 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>(); 195 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>();
195 EXPECT_EQ(*copy.child_int, *stats.child_int); 196 EXPECT_EQ(*copy.child_int, *stats.child_int);
196 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int); 197 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
197 } 198 }
198 199
200 TEST(RTCStatsTest, RTCStatsPrintsValidJson) {
201 std::string id = "statsId";
202 int timestamp = 42;
203 bool m_bool = true;
204 int m_int32 = 123;
205 double m_double = 123.0;
206 std::string m_string = "123";
207
208 std::vector<bool> sequence_bool;
209 std::vector<int32_t> sequence_int32;
210 sequence_int32.push_back(static_cast<int32_t>(1));
211 std::vector<double> sequence_double;
212 sequence_double.push_back(2.0);
213 sequence_double.push_back(3.0);
214 std::vector<std::string> sequence_string;
215 sequence_string.push_back(std::string("four"));
216
217 RTCTestStats stats(id, timestamp);
218 stats.m_bool = m_bool;
219 stats.m_int32 = m_int32;
220 stats.m_double = m_double;
221 stats.m_string = m_string;
222 stats.m_sequence_bool = sequence_bool;
223 stats.m_sequence_int32 = sequence_int32;
224 stats.m_sequence_double = sequence_double;
225 stats.m_sequence_string = sequence_string;
hbos 2017/07/25 12:29:21 Hmm, will this be a problem for you? https://stack
226
227 Json::Value json_output;
228 EXPECT_TRUE(Json::Reader().parse(stats.ToString(), json_output));
229
230 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id));
231 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", &timestamp));
232 EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool));
233 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32));
234 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string));
hbos 2017/07/25 12:29:21 nit: you skipped mDouble
235
236 Json::Value json_array;
237
238 EXPECT_TRUE(
239 rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array));
240 EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool));
241
242 EXPECT_TRUE(
243 rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array));
244 EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32));
245
246 EXPECT_TRUE(
247 rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array));
248 EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double));
249
250 EXPECT_TRUE(
251 rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array));
252 EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string));
253
254 EXPECT_EQ(id, stats.id());
255 EXPECT_EQ(timestamp, stats.timestamp_us());
256 EXPECT_EQ(m_bool, *stats.m_bool);
257 EXPECT_EQ(m_int32, *stats.m_int32);
258 EXPECT_EQ(m_string, *stats.m_string);
259 EXPECT_EQ(sequence_bool, *stats.m_sequence_bool);
260 EXPECT_EQ(sequence_int32, *stats.m_sequence_int32);
261 EXPECT_EQ(sequence_double, *stats.m_sequence_double);
262 EXPECT_EQ(sequence_string, *stats.m_sequence_string);
263
264 // stats.m_int64 is not defined, so "mInt64" is not part of the generated json
265 // object.
266 int m_int64;
267 EXPECT_FALSE(stats.m_int64.is_defined());
268 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mInt64", &m_int64));
hbos 2017/07/25 12:29:21 This is good. Keep at least one member undefined t
269 }
270
199 // Death tests. 271 // Death tests.
200 // Disabled on Android because death tests misbehave on Android, see 272 // Disabled on Android because death tests misbehave on Android, see
201 // base/test/gtest_util.h. 273 // base/test/gtest_util.h.
202 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) 274 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
203 275
204 TEST(RTCStatsDeathTest, ValueOfUndefinedMember) { 276 TEST(RTCStatsDeathTest, ValueOfUndefinedMember) {
205 RTCTestStats stats("testId", 0.0); 277 RTCTestStats stats("testId", 0.0);
206 EXPECT_FALSE(stats.m_int32.is_defined()); 278 EXPECT_FALSE(stats.m_int32.is_defined());
207 EXPECT_DEATH(*stats.m_int32, ""); 279 EXPECT_DEATH(*stats.m_int32, "");
208 } 280 }
209 281
210 TEST(RTCStatsDeathTest, InvalidCasting) { 282 TEST(RTCStatsDeathTest, InvalidCasting) {
211 RTCGrandChildStats stats("grandchild", 0.0); 283 RTCGrandChildStats stats("grandchild", 0.0);
212 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), ""); 284 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), "");
213 } 285 }
214 286
215 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) 287 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
216 288
217 } // namespace webrtc 289 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698