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

Unified Diff: webrtc/stats/rtcstats_unittest.cc

Issue 2983243002: Make RTCStatsReport::ToString() return JSON-parseable string. (Closed)
Patch Set: Add test. Created 3 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/stats/rtcstats_unittest.cc
diff --git a/webrtc/stats/rtcstats_unittest.cc b/webrtc/stats/rtcstats_unittest.cc
index 925a6239b789475c647954c50c0bd5503da89db6..34c0e32a1181e72452d01c7acbbf330173f17d7f 100644
--- a/webrtc/stats/rtcstats_unittest.cc
+++ b/webrtc/stats/rtcstats_unittest.cc
@@ -14,6 +14,7 @@
#include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/gunit.h"
+#include "webrtc/rtc_base/json.h"
#include "webrtc/stats/test/rtcteststats.h"
namespace webrtc {
@@ -196,6 +197,77 @@ TEST(RTCStatsTest, RTCStatsGrandChild) {
EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
}
+TEST(RTCStatsTest, RTCStatsPrintsValidJson) {
+ std::string id = "statsId";
+ int timestamp = 42;
+ bool m_bool = true;
+ int m_int32 = 123;
+ double m_double = 123.0;
+ std::string m_string = "123";
+
+ std::vector<bool> sequence_bool;
+ std::vector<int32_t> sequence_int32;
+ sequence_int32.push_back(static_cast<int32_t>(1));
+ std::vector<double> sequence_double;
+ sequence_double.push_back(2.0);
+ sequence_double.push_back(3.0);
+ std::vector<std::string> sequence_string;
+ sequence_string.push_back(std::string("four"));
+
+ RTCTestStats stats(id, timestamp);
+ stats.m_bool = m_bool;
+ stats.m_int32 = m_int32;
+ stats.m_double = m_double;
+ stats.m_string = m_string;
+ stats.m_sequence_bool = sequence_bool;
+ stats.m_sequence_int32 = sequence_int32;
+ stats.m_sequence_double = sequence_double;
+ stats.m_sequence_string = sequence_string;
hbos 2017/07/25 12:29:21 Hmm, will this be a problem for you? https://stack
+
+ Json::Value json_output;
+ EXPECT_TRUE(Json::Reader().parse(stats.ToString(), json_output));
+
+ EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id));
+ EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", &timestamp));
+ EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool));
+ EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32));
+ EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string));
hbos 2017/07/25 12:29:21 nit: you skipped mDouble
+
+ Json::Value json_array;
+
+ EXPECT_TRUE(
+ rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array));
+ EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool));
+
+ EXPECT_TRUE(
+ rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array));
+ EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32));
+
+ EXPECT_TRUE(
+ rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array));
+ EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double));
+
+ EXPECT_TRUE(
+ rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array));
+ EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string));
+
+ EXPECT_EQ(id, stats.id());
+ EXPECT_EQ(timestamp, stats.timestamp_us());
+ EXPECT_EQ(m_bool, *stats.m_bool);
+ EXPECT_EQ(m_int32, *stats.m_int32);
+ EXPECT_EQ(m_string, *stats.m_string);
+ EXPECT_EQ(sequence_bool, *stats.m_sequence_bool);
+ EXPECT_EQ(sequence_int32, *stats.m_sequence_int32);
+ EXPECT_EQ(sequence_double, *stats.m_sequence_double);
+ EXPECT_EQ(sequence_string, *stats.m_sequence_string);
+
+ // stats.m_int64 is not defined, so "mInt64" is not part of the generated json
+ // object.
+ int m_int64;
+ EXPECT_FALSE(stats.m_int64.is_defined());
+ 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
+}
+
// Death tests.
// Disabled on Android because death tests misbehave on Android, see
// base/test/gtest_util.h.

Powered by Google App Engine
This is Rietveld 408576698