• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/library_support/histogram_manager.h"
6 
7 #include <stdint.h>
8 
9 #include <string>
10 
11 #include "base/metrics/histogram_macros.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace metrics {
15 
TEST(HistogramManager,HistogramBucketFields)16 TEST(HistogramManager, HistogramBucketFields) {
17   // Capture histograms at the start of the test to avoid later GetDeltas()
18   // calls picking them up.
19   std::vector<uint8_t> data_init;
20   HistogramManager::GetInstance()->GetDeltas(&data_init);
21 
22   // kNoFlags filter should record all histograms.
23   UMA_HISTOGRAM_ENUMERATION("UmaHistogramManager", 1, 2);
24 
25   std::vector<uint8_t> data;
26   EXPECT_TRUE(HistogramManager::GetInstance()->GetDeltas(&data));
27   EXPECT_FALSE(data.empty());
28   ChromeUserMetricsExtension uma_proto;
29   EXPECT_TRUE(uma_proto.ParseFromArray(reinterpret_cast<const char*>(&data[0]),
30                                        data.size()));
31   EXPECT_FALSE(data.empty());
32 
33   const HistogramEventProto& histogram_proto =
34       uma_proto.histogram_event(uma_proto.histogram_event_size() - 1);
35   ASSERT_EQ(1, histogram_proto.bucket_size());
36   EXPECT_LE(0, histogram_proto.bucket(0).min());
37   EXPECT_LE(2, histogram_proto.bucket(0).max());
38   EXPECT_EQ(1, histogram_proto.bucket(0).count());
39 
40   UMA_HISTOGRAM_ENUMERATION("UmaHistogramManager2", 2, 3);
41   std::vector<uint8_t> data2;
42   EXPECT_TRUE(HistogramManager::GetInstance()->GetDeltas(&data2));
43   EXPECT_FALSE(data2.empty());
44   ChromeUserMetricsExtension uma_proto2;
45   EXPECT_TRUE(uma_proto2.ParseFromArray(
46       reinterpret_cast<const char*>(&data2[0]), data2.size()));
47   EXPECT_FALSE(data2.empty());
48 
49   const HistogramEventProto& histogram_proto2 =
50       uma_proto2.histogram_event(uma_proto2.histogram_event_size() - 1);
51   ASSERT_EQ(1, histogram_proto2.bucket_size());
52   EXPECT_LE(0, histogram_proto2.bucket(0).min());
53   EXPECT_LE(3, histogram_proto2.bucket(0).max());
54   EXPECT_EQ(1, histogram_proto2.bucket(0).count());
55 }
56 
57 }  // namespace metrics
58