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 json(StatisticsRecorder::ToJSON(std::string())); |
| 275 |
| 276 // Check for valid JSON. |
| 277 scoped_ptr<Value> root; |
| 278 root.reset(JSONReader::Read(json)); |
| 279 ASSERT_TRUE(root.get()); |
| 280 |
| 281 DictionaryValue* root_dict = NULL; |
| 282 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); |
| 283 |
| 284 // No query should be set. |
| 285 ASSERT_FALSE(root_dict->HasKey("query")); |
| 286 |
| 287 ListValue* histogram_list = NULL; |
| 288 ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list)); |
| 289 ASSERT_EQ(2u, histogram_list->GetSize()); |
| 290 |
| 291 // Examine the first histogram. |
| 292 DictionaryValue* histogram_dict = NULL; |
| 293 ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict)); |
| 294 |
| 295 int sample_count; |
| 296 ASSERT_TRUE(histogram_dict->GetInteger("count", &sample_count)); |
| 297 EXPECT_EQ(2, sample_count); |
| 298 |
| 299 // Test the query filter. |
| 300 std::string query("TestHistogram2"); |
| 301 json = StatisticsRecorder::ToJSON(query); |
| 302 |
| 303 root.reset(JSONReader::Read(json)); |
| 304 ASSERT_TRUE(root.get()); |
| 305 ASSERT_TRUE(root->GetAsDictionary(&root_dict)); |
| 306 |
| 307 std::string query_value; |
| 308 ASSERT_TRUE(root_dict->GetString("query", &query_value)); |
| 309 EXPECT_EQ(query, query_value); |
| 310 |
| 311 ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list)); |
| 312 ASSERT_EQ(1u, histogram_list->GetSize()); |
| 313 |
| 314 ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict)); |
| 315 |
| 316 std::string histogram_name; |
| 317 ASSERT_TRUE(histogram_dict->GetString("name", &histogram_name)); |
| 318 EXPECT_EQ("TestHistogram2", histogram_name); |
| 319 |
| 320 json.clear(); |
| 321 UninitializeStatisticsRecorder(); |
| 322 |
| 323 // No data should be returned. |
| 324 json = StatisticsRecorder::ToJSON(query); |
| 325 EXPECT_TRUE(json.empty()); |
| 326 } |
| 327 |
266 } // namespace base | 328 } // namespace base |
OLD | NEW |