Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / common / metrics / metrics_log_base_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/common/metrics/metrics_log_base.h"
6
7 #include <string>
8
9 #include "base/base64.h"
10 #include "base/metrics/bucket_ranges.h"
11 #include "base/metrics/sample_vector.h"
12 #include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 class TestMetricsLogBase : public MetricsLogBase {
18  public:
19   TestMetricsLogBase() : MetricsLogBase("client_id", 1, "1.2.3.4") {}
20   virtual ~TestMetricsLogBase() {}
21
22   using MetricsLogBase::uma_proto;
23
24  private:
25   DISALLOW_COPY_AND_ASSIGN(TestMetricsLogBase);
26 };
27
28 }  // namespace
29
30 TEST(MetricsLogBaseTest, EmptyRecord) {
31   MetricsLogBase log("totally bogus client ID", 137, "bogus version");
32   log.set_hardware_class("sample-class");
33   log.CloseLog();
34
35   std::string encoded;
36   log.GetEncodedLog(&encoded);
37
38   // A couple of fields are hard to mock, so these will be copied over directly
39   // for the expected output.
40   metrics::ChromeUserMetricsExtension parsed;
41   ASSERT_TRUE(parsed.ParseFromString(encoded));
42
43   metrics::ChromeUserMetricsExtension expected;
44   expected.set_client_id(5217101509553811875);  // Hashed bogus client ID
45   expected.set_session_id(137);
46   expected.mutable_system_profile()->set_build_timestamp(
47       parsed.system_profile().build_timestamp());
48   expected.mutable_system_profile()->set_app_version("bogus version");
49   expected.mutable_system_profile()->set_channel(
50       parsed.system_profile().channel());
51   expected.mutable_system_profile()->mutable_hardware()->set_hardware_class(
52       "sample-class");
53
54   EXPECT_EQ(expected.SerializeAsString(), encoded);
55 }
56
57 TEST(MetricsLogBaseTest, HistogramBucketFields) {
58   // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
59   base::BucketRanges ranges(8);
60   ranges.set_range(0, 1);
61   ranges.set_range(1, 5);
62   ranges.set_range(2, 7);
63   ranges.set_range(3, 8);
64   ranges.set_range(4, 9);
65   ranges.set_range(5, 10);
66   ranges.set_range(6, 11);
67   ranges.set_range(7, 12);
68
69   base::SampleVector samples(&ranges);
70   samples.Accumulate(3, 1);  // Bucket 1-5.
71   samples.Accumulate(6, 1);  // Bucket 5-7.
72   samples.Accumulate(8, 1);  // Bucket 8-9. (7-8 skipped)
73   samples.Accumulate(10, 1);  // Bucket 10-11. (9-10 skipped)
74   samples.Accumulate(11, 1);  // Bucket 11-12.
75
76   TestMetricsLogBase log;
77   log.RecordHistogramDelta("Test", samples);
78
79   const metrics::ChromeUserMetricsExtension* uma_proto = log.uma_proto();
80   const metrics::HistogramEventProto& histogram_proto =
81       uma_proto->histogram_event(uma_proto->histogram_event_size() - 1);
82
83   // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
84   // Should become: 1-/, 5-7, /-9, 10-/, /-12.
85   ASSERT_EQ(5, histogram_proto.bucket_size());
86
87   // 1-5 becomes 1-/ (max is same as next min).
88   EXPECT_TRUE(histogram_proto.bucket(0).has_min());
89   EXPECT_FALSE(histogram_proto.bucket(0).has_max());
90   EXPECT_EQ(1, histogram_proto.bucket(0).min());
91
92   // 5-7 stays 5-7 (no optimization possible).
93   EXPECT_TRUE(histogram_proto.bucket(1).has_min());
94   EXPECT_TRUE(histogram_proto.bucket(1).has_max());
95   EXPECT_EQ(5, histogram_proto.bucket(1).min());
96   EXPECT_EQ(7, histogram_proto.bucket(1).max());
97
98   // 8-9 becomes /-9 (min is same as max - 1).
99   EXPECT_FALSE(histogram_proto.bucket(2).has_min());
100   EXPECT_TRUE(histogram_proto.bucket(2).has_max());
101   EXPECT_EQ(9, histogram_proto.bucket(2).max());
102
103   // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized).
104   EXPECT_TRUE(histogram_proto.bucket(3).has_min());
105   EXPECT_FALSE(histogram_proto.bucket(3).has_max());
106   EXPECT_EQ(10, histogram_proto.bucket(3).min());
107
108   // 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
109   EXPECT_FALSE(histogram_proto.bucket(4).has_min());
110   EXPECT_TRUE(histogram_proto.bucket(4).has_max());
111   EXPECT_EQ(12, histogram_proto.bucket(4).max());
112 }