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