Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/values.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 namespace base { | 14 namespace base { |
| 13 | 15 |
| 14 class StatisticsRecorderTest : public testing::Test { | 16 class StatisticsRecorderTest : public testing::Test { |
| 15 protected: | 17 protected: |
| 16 virtual void SetUp() { | 18 virtual void SetUp() { |
| 17 // Each test will have a clean state (no Histogram / BucketRanges | 19 // Each test will have a clean state (no Histogram / BucketRanges |
| 18 // registered). | 20 // registered). |
| 19 InitializeStatisticsRecorder(); | 21 InitializeStatisticsRecorder(); |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 StatisticsRecorder::GetBucketRanges(&ranges); | 258 StatisticsRecorder::GetBucketRanges(&ranges); |
| 257 EXPECT_EQ(1u, ranges.size()); | 259 EXPECT_EQ(1u, ranges.size()); |
| 258 | 260 |
| 259 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags); | 261 Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags); |
| 260 | 262 |
| 261 ranges.clear(); | 263 ranges.clear(); |
| 262 StatisticsRecorder::GetBucketRanges(&ranges); | 264 StatisticsRecorder::GetBucketRanges(&ranges); |
| 263 EXPECT_EQ(2u, ranges.size()); | 265 EXPECT_EQ(2u, ranges.size()); |
| 264 } | 266 } |
| 265 | 267 |
| 268 TEST_F(StatisticsRecorderTest, ToJSON) { | |
| 269 HISTOGRAM_COUNTS("TestHistogram1", 30); | |
| 270 HISTOGRAM_COUNTS("TestHistogram1", 40); | |
| 271 HISTOGRAM_COUNTS("TestHistogram2", 30); | |
| 272 HISTOGRAM_COUNTS("TestHistogram2", 40); | |
| 273 | |
| 274 std::string query; | |
|
Peter Kasting
2013/11/14 07:04:06
Nit: We can just declare this string where it's fi
grt (UTC plus 2)
2013/11/14 14:44:12
Done.
| |
| 275 std::string json(StatisticsRecorder::ToJSON(query)); | |
| 276 | |
| 277 // Check for valid JSON. | |
| 278 scoped_ptr<Value> root; | |
| 279 root.reset(JSONReader::Read(json)); | |
| 280 ASSERT_TRUE(root.get()); | |
| 281 | |
| 282 DictionaryValue* root_dict = NULL; | |
| 283 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); | |
| 284 | |
| 285 // No query should be set. | |
| 286 ASSERT_FALSE(root_dict->HasKey("query")); | |
| 287 | |
| 288 ListValue* histogram_list = NULL; | |
| 289 ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list)); | |
| 290 ASSERT_EQ(2u, histogram_list->GetSize()); | |
| 291 | |
| 292 // Examine the first histogram. | |
| 293 DictionaryValue* histogram_dict = NULL; | |
| 294 ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict)); | |
| 295 | |
| 296 int sample_count; | |
| 297 ASSERT_TRUE(histogram_dict->GetInteger("count", &sample_count)); | |
| 298 EXPECT_EQ(2, sample_count); | |
| 299 | |
| 300 // Test the query filter. | |
| 301 query = "TestHistogram2"; | |
| 302 json = StatisticsRecorder::ToJSON(query); | |
| 303 | |
| 304 root.reset(JSONReader::Read(json)); | |
| 305 ASSERT_TRUE(root.get()); | |
| 306 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); | |
| 307 | |
| 308 std::string query_value; | |
| 309 ASSERT_TRUE(root_dict->GetString("query", &query_value)); | |
| 310 EXPECT_EQ(query, query_value); | |
| 311 | |
| 312 ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list)); | |
| 313 ASSERT_EQ(1u, histogram_list->GetSize()); | |
| 314 | |
| 315 ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict)); | |
| 316 | |
| 317 std::string histogram_name; | |
| 318 ASSERT_TRUE(histogram_dict->GetString("name", &histogram_name)); | |
| 319 EXPECT_EQ("TestHistogram2", histogram_name); | |
| 320 | |
| 321 json.clear(); | |
| 322 UninitializeStatisticsRecorder(); | |
| 323 | |
| 324 // No data should be returned. | |
| 325 json = StatisticsRecorder::ToJSON(query); | |
| 326 EXPECT_TRUE(json.empty()); | |
| 327 } | |
| 328 | |
| 266 } // namespace base | 329 } // namespace base |
| OLD | NEW |