| Index: base/metrics/statistics_recorder_unittest.cc
|
| diff --git a/base/metrics/statistics_recorder_unittest.cc b/base/metrics/statistics_recorder_unittest.cc
|
| index 22504ddfdda7e64c1b7bb21942d5839f8c911397..f9e35a9344ea6cc21b919d8742d6047860d08c39 100644
|
| --- a/base/metrics/statistics_recorder_unittest.cc
|
| +++ b/base/metrics/statistics_recorder_unittest.cc
|
| @@ -4,9 +4,11 @@
|
|
|
| #include <vector>
|
|
|
| +#include "base/json/json_reader.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/metrics/histogram.h"
|
| #include "base/metrics/statistics_recorder.h"
|
| +#include "base/values.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace base {
|
| @@ -263,4 +265,67 @@ TEST_F(StatisticsRecorderTest, BucketRangesSharing) {
|
| EXPECT_EQ(2u, ranges.size());
|
| }
|
|
|
| +TEST_F(StatisticsRecorderTest, WriteJSON) {
|
| + HISTOGRAM_COUNTS("TestHistogram1", 30);
|
| + HISTOGRAM_COUNTS("TestHistogram1", 40);
|
| + HISTOGRAM_COUNTS("TestHistogram2", 30);
|
| + HISTOGRAM_COUNTS("TestHistogram2", 40);
|
| +
|
| + std::string json;
|
| + std::string query;
|
| + StatisticsRecorder::WriteJSON(query, &json);
|
| +
|
| + // Check for valid JSON.
|
| + scoped_ptr<Value> root;
|
| + root.reset(JSONReader::Read(json));
|
| + ASSERT_TRUE(root.get());
|
| +
|
| + DictionaryValue* root_dict = NULL;
|
| + ASSERT_TRUE(root->GetAsDictionary(&root_dict));
|
| +
|
| + // No query should be set.
|
| + ASSERT_FALSE(root_dict->HasKey("query"));
|
| +
|
| + ListValue* histogram_list = NULL;
|
| + ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list));
|
| + ASSERT_EQ(2u, histogram_list->GetSize());
|
| +
|
| + // Examine the first histogram.
|
| + DictionaryValue* histogram_dict = NULL;
|
| + ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict));
|
| +
|
| + int sample_count;
|
| + ASSERT_TRUE(histogram_dict->GetInteger("count", &sample_count));
|
| + EXPECT_EQ(2, sample_count);
|
| +
|
| + // Test the query filter.
|
| + query = "TestHistogram2";
|
| + json.clear();
|
| + StatisticsRecorder::WriteJSON(query, &json);
|
| +
|
| + root.reset(JSONReader::Read(json));
|
| + ASSERT_TRUE(root.get());
|
| + ASSERT_TRUE(root->GetAsDictionary(&root_dict));
|
| +
|
| + std::string query_value;
|
| + ASSERT_TRUE(root_dict->GetString("query", &query_value));
|
| + EXPECT_EQ(query, query_value);
|
| +
|
| + ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list));
|
| + ASSERT_EQ(1u, histogram_list->GetSize());
|
| +
|
| + ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict));
|
| +
|
| + std::string histogram_name;
|
| + ASSERT_TRUE(histogram_dict->GetString("name", &histogram_name));
|
| + EXPECT_EQ("TestHistogram2", histogram_name);
|
| +
|
| + json.clear();
|
| + UninitializeStatisticsRecorder();
|
| +
|
| + // No data should be returned.
|
| + StatisticsRecorder::WriteJSON(query, &json);
|
| + EXPECT_TRUE(json.empty());
|
| +}
|
| +
|
| } // namespace base
|
|
|