| 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 "chrome/common/metrics/metrics_log_base.h" | 5 #include "chrome/common/metrics/metrics_log_base.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/metrics/bucket_ranges.h" | 10 #include "base/metrics/bucket_ranges.h" |
| 12 #include "base/metrics/sample_vector.h" | 11 #include "base/metrics/sample_vector.h" |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h" | 12 #include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 14 |
| 17 namespace { | 15 namespace { |
| 18 | 16 |
| 19 class TestMetricsLogBase : public MetricsLogBase { | 17 class TestMetricsLogBase : public MetricsLogBase { |
| 20 public: | 18 public: |
| 21 TestMetricsLogBase() : MetricsLogBase("client_id", 1, "1.2.3.4") {} | 19 TestMetricsLogBase() : MetricsLogBase("client_id", 1, "1.2.3.4") {} |
| 22 virtual ~TestMetricsLogBase() {} | 20 virtual ~TestMetricsLogBase() {} |
| 23 | 21 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 parsed.system_profile().build_timestamp()); | 47 parsed.system_profile().build_timestamp()); |
| 50 expected.mutable_system_profile()->set_app_version("bogus version"); | 48 expected.mutable_system_profile()->set_app_version("bogus version"); |
| 51 expected.mutable_system_profile()->set_channel( | 49 expected.mutable_system_profile()->set_channel( |
| 52 parsed.system_profile().channel()); | 50 parsed.system_profile().channel()); |
| 53 expected.mutable_system_profile()->mutable_hardware()->set_hardware_class( | 51 expected.mutable_system_profile()->mutable_hardware()->set_hardware_class( |
| 54 "sample-class"); | 52 "sample-class"); |
| 55 | 53 |
| 56 EXPECT_EQ(expected.SerializeAsString(), encoded); | 54 EXPECT_EQ(expected.SerializeAsString(), encoded); |
| 57 } | 55 } |
| 58 | 56 |
| 59 // Make sure our ID hashes are the same as what we see on the server side. | |
| 60 TEST(MetricsLogBaseTest, Hashes) { | |
| 61 static const struct { | |
| 62 std::string input; | |
| 63 std::string output; | |
| 64 } cases[] = { | |
| 65 {"Back", "0x0557fa923dcee4d0"}, | |
| 66 {"Forward", "0x67d2f6740a8eaebf"}, | |
| 67 {"NewTab", "0x290eb683f96572f1"}, | |
| 68 }; | |
| 69 | |
| 70 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 71 uint64 hash = MetricsLogBase::Hash(cases[i].input); | |
| 72 std::string hash_hex = base::StringPrintf("0x%016" PRIx64, hash); | |
| 73 EXPECT_EQ(cases[i].output, hash_hex); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 TEST(MetricsLogBaseTest, HistogramBucketFields) { | 57 TEST(MetricsLogBaseTest, HistogramBucketFields) { |
| 78 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. | 58 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. |
| 79 base::BucketRanges ranges(8); | 59 base::BucketRanges ranges(8); |
| 80 ranges.set_range(0, 1); | 60 ranges.set_range(0, 1); |
| 81 ranges.set_range(1, 5); | 61 ranges.set_range(1, 5); |
| 82 ranges.set_range(2, 7); | 62 ranges.set_range(2, 7); |
| 83 ranges.set_range(3, 8); | 63 ranges.set_range(3, 8); |
| 84 ranges.set_range(4, 9); | 64 ranges.set_range(4, 9); |
| 85 ranges.set_range(5, 10); | 65 ranges.set_range(5, 10); |
| 86 ranges.set_range(6, 11); | 66 ranges.set_range(6, 11); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). | 103 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). |
| 124 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); | 104 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); |
| 125 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); | 105 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); |
| 126 EXPECT_EQ(10, histogram_proto.bucket(3).min()); | 106 EXPECT_EQ(10, histogram_proto.bucket(3).min()); |
| 127 | 107 |
| 128 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). | 108 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). |
| 129 EXPECT_FALSE(histogram_proto.bucket(4).has_min()); | 109 EXPECT_FALSE(histogram_proto.bucket(4).has_min()); |
| 130 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); | 110 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); |
| 131 EXPECT_EQ(12, histogram_proto.bucket(4).max()); | 111 EXPECT_EQ(12, histogram_proto.bucket(4).max()); |
| 132 } | 112 } |
| OLD | NEW |