bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 5 | #include "components/metrics/metrics_log.h" |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include "base/base64.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 13 | #include "base/macros.h" |
ke.he | a9da80c | 2017-02-16 12:45:00 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 15 | #include "base/metrics/bucket_ranges.h" |
| 16 | #include "base/metrics/sample_vector.h" |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
Jonghyun Ahn | c54f59f | 2018-07-19 17:41:50 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 19 | #include "base/system/sys_info.h" |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 20 | #include "base/time/time.h" |
Peter K. Lee | 09746e8 | 2019-01-16 02:38:56 | [diff] [blame] | 21 | #include "build/build_config.h" |
Nikunj Bhagat | 19b85930 | 2018-12-20 00:39:56 | [diff] [blame] | 22 | #include "components/metrics/cpu_metrics_provider.h" |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 23 | #include "components/metrics/delegating_provider.h" |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 24 | #include "components/metrics/environment_recorder.h" |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 25 | #include "components/metrics/metrics_pref_names.h" |
| 26 | #include "components/metrics/metrics_state_manager.h" |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 27 | #include "components/metrics/test_metrics_provider.h" |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 28 | #include "components/metrics/test_metrics_service_client.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 29 | #include "components/prefs/pref_service.h" |
| 30 | #include "components/prefs/testing_pref_service.h" |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 31 | #include "components/variations/active_field_trials.h" |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 32 | #include "testing/gtest/include/gtest/gtest.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 33 | #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h" |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 34 | |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 35 | #if defined(OS_ANDROID) |
| 36 | #include "base/android/build_info.h" |
| 37 | #endif |
| 38 | |
| 39 | #if defined(OS_WIN) |
| 40 | #include "base/win/current_module.h" |
| 41 | #endif |
| 42 | |
bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 43 | namespace metrics { |
| 44 | |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 45 | namespace { |
| 46 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 47 | const char kClientId[] = "bogus client ID"; |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 48 | const int kSessionId = 127; |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 49 | |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 50 | class TestMetricsLog : public MetricsLog { |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 51 | public: |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 52 | TestMetricsLog(const std::string& client_id, |
| 53 | int session_id, |
| 54 | LogType log_type, |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 55 | MetricsServiceClient* client) |
| 56 | : MetricsLog(client_id, session_id, log_type, client) {} |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 57 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 58 | ~TestMetricsLog() override {} |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 59 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 60 | const ChromeUserMetricsExtension& uma_proto() const { |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 61 | return *MetricsLog::uma_proto(); |
| 62 | } |
| 63 | |
rkaplow | 78985a0 | 2017-06-05 16:13:19 | [diff] [blame] | 64 | ChromeUserMetricsExtension* mutable_uma_proto() { |
| 65 | return MetricsLog::uma_proto(); |
| 66 | } |
| 67 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 68 | const SystemProfileProto& system_profile() const { |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 69 | return uma_proto().system_profile(); |
| 70 | } |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 71 | |
| 72 | private: |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | } // namespace |
| 77 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 78 | class MetricsLogTest : public testing::Test { |
| 79 | public: |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 80 | MetricsLogTest() {} |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 81 | ~MetricsLogTest() override {} |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 82 | |
| 83 | protected: |
Alexei Svitkine | 70c9502 | 2019-08-21 18:13:24 | [diff] [blame] | 84 | // Check that the values in |system_values| are filled in and expected ones |
| 85 | // correspond to the test data defined at the top of this file. |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 86 | void CheckSystemProfile(const SystemProfileProto& system_profile) { |
Alexei Svitkine | 70c9502 | 2019-08-21 18:13:24 | [diff] [blame] | 87 | // Check for presence of core system profile fields. |
| 88 | EXPECT_TRUE(system_profile.has_build_timestamp()); |
| 89 | EXPECT_TRUE(system_profile.has_app_version()); |
| 90 | EXPECT_TRUE(system_profile.has_channel()); |
| 91 | EXPECT_TRUE(system_profile.has_application_locale()); |
| 92 | |
| 93 | const SystemProfileProto::OS& os = system_profile.os(); |
| 94 | EXPECT_TRUE(os.has_name()); |
| 95 | EXPECT_TRUE(os.has_version()); |
| 96 | |
| 97 | // Check matching test brand code. |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 98 | EXPECT_EQ(TestMetricsServiceClient::kBrandForTesting, |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 99 | system_profile.brand_code()); |
| 100 | |
Alexei Svitkine | 70c9502 | 2019-08-21 18:13:24 | [diff] [blame] | 101 | // Check for presence of fields set by a metrics provider. |
| 102 | const SystemProfileProto::Hardware& hardware = system_profile.hardware(); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 103 | EXPECT_TRUE(hardware.has_cpu()); |
| 104 | EXPECT_TRUE(hardware.cpu().has_vendor_name()); |
| 105 | EXPECT_TRUE(hardware.cpu().has_signature()); |
gayane | f550f50f | 2015-03-11 00:44:59 | [diff] [blame] | 106 | EXPECT_TRUE(hardware.cpu().has_num_cores()); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 107 | |
| 108 | // TODO(isherman): Verify other data written into the protobuf as a result |
| 109 | // of this call. |
| 110 | } |
| 111 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 112 | private: |
| 113 | DISALLOW_COPY_AND_ASSIGN(MetricsLogTest); |
| 114 | }; |
| 115 | |
| 116 | TEST_F(MetricsLogTest, LogType) { |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 117 | TestMetricsServiceClient client; |
| 118 | TestingPrefServiceSimple prefs; |
asvitkine@chromium.org | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 119 | |
Steven Holte | d0429a70 | 2017-08-03 08:56:17 | [diff] [blame] | 120 | MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client); |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 121 | EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type()); |
| 122 | |
Steven Holte | d0429a70 | 2017-08-03 08:56:17 | [diff] [blame] | 123 | MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client); |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 124 | EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type()); |
asvitkine@chromium.org | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 125 | } |
| 126 | |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 127 | TEST_F(MetricsLogTest, BasicRecord) { |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 128 | TestMetricsServiceClient client; |
| 129 | client.set_version_string("bogus version"); |
| 130 | TestingPrefServiceSimple prefs; |
| 131 | MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG, |
Steven Holte | d0429a70 | 2017-08-03 08:56:17 | [diff] [blame] | 132 | &client); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 133 | log.CloseLog(); |
| 134 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 135 | std::string encoded; |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 136 | log.GetEncodedLog(&encoded); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 137 | |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 138 | // A couple of fields are hard to mock, so these will be copied over directly |
| 139 | // for the expected output. |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 140 | ChromeUserMetricsExtension parsed; |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 141 | ASSERT_TRUE(parsed.ParseFromString(encoded)); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 142 | |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 143 | ChromeUserMetricsExtension expected; |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 144 | expected.set_client_id(5217101509553811875); // Hashed bogus client ID |
| 145 | expected.set_session_id(137); |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 146 | |
| 147 | SystemProfileProto* system_profile = expected.mutable_system_profile(); |
| 148 | system_profile->set_app_version("bogus version"); |
| 149 | system_profile->set_channel(client.GetChannel()); |
| 150 | system_profile->set_application_locale(client.GetApplicationLocale()); |
| 151 | |
Wez | 720413e | 2018-10-04 17:04:31 | [diff] [blame] | 152 | #if defined(ADDRESS_SANITIZER) || DCHECK_IS_ON() |
Wez | e7b1b61 | 2018-10-22 18:17:22 | [diff] [blame] | 153 | system_profile->set_is_instrumented_build(true); |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 154 | #endif |
| 155 | metrics::SystemProfileProto::Hardware* hardware = |
| 156 | system_profile->mutable_hardware(); |
asvitkine | b2932361 | 2017-05-05 14:02:47 | [diff] [blame] | 157 | #if !defined(OS_IOS) |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 158 | hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture()); |
asvitkine | b2932361 | 2017-05-05 14:02:47 | [diff] [blame] | 159 | #endif |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 160 | hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB()); |
| 161 | hardware->set_hardware_class(base::SysInfo::HardwareModelName()); |
| 162 | #if defined(OS_WIN) |
| 163 | hardware->set_dll_base(reinterpret_cast<uint64_t>(CURRENT_MODULE())); |
| 164 | #endif |
| 165 | |
| 166 | system_profile->mutable_os()->set_name(base::SysInfo::OperatingSystemName()); |
| 167 | system_profile->mutable_os()->set_version( |
| 168 | base::SysInfo::OperatingSystemVersion()); |
Jonghyun Ahn | c54f59f | 2018-07-19 17:41:50 | [diff] [blame] | 169 | #if defined(OS_CHROMEOS) |
| 170 | system_profile->mutable_os()->set_kernel_version( |
| 171 | base::SysInfo::KernelVersion()); |
Jonghyun Ahn | e4b9336 | 2018-10-30 01:43:35 | [diff] [blame] | 172 | #elif defined(OS_LINUX) |
| 173 | system_profile->mutable_os()->set_kernel_version( |
| 174 | base::SysInfo::OperatingSystemVersion()); |
Jonghyun Ahn | c54f59f | 2018-07-19 17:41:50 | [diff] [blame] | 175 | #elif defined(OS_ANDROID) |
Steven Holte | d7d6931f | 2017-08-25 22:53:15 | [diff] [blame] | 176 | system_profile->mutable_os()->set_build_fingerprint( |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 177 | base::android::BuildInfo::GetInstance()->android_build_fp()); |
Shuo Weng | 3720265 | 2018-04-19 02:54:11 | [diff] [blame] | 178 | system_profile->set_app_package_name("test app"); |
Peter K. Lee | 09746e8 | 2019-01-16 02:38:56 | [diff] [blame] | 179 | #elif defined(OS_IOS) |
| 180 | system_profile->mutable_os()->set_build_number( |
| 181 | base::SysInfo::GetIOSBuildNumber()); |
rkaplow | 2b85c62 | 2017-01-30 16:57:17 | [diff] [blame] | 182 | #endif |
| 183 | |
| 184 | // Hard to mock. |
| 185 | system_profile->set_build_timestamp( |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 186 | parsed.system_profile().build_timestamp()); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 187 | |
isherman@chromium.org | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 188 | EXPECT_EQ(expected.SerializeAsString(), encoded); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 189 | } |
| 190 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 191 | TEST_F(MetricsLogTest, HistogramBucketFields) { |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 192 | // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. |
| 193 | base::BucketRanges ranges(8); |
| 194 | ranges.set_range(0, 1); |
| 195 | ranges.set_range(1, 5); |
| 196 | ranges.set_range(2, 7); |
| 197 | ranges.set_range(3, 8); |
| 198 | ranges.set_range(4, 9); |
| 199 | ranges.set_range(5, 10); |
| 200 | ranges.set_range(6, 11); |
| 201 | ranges.set_range(7, 12); |
| 202 | |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 203 | base::SampleVector samples(1, &ranges); |
bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 204 | samples.Accumulate(3, 1); // Bucket 1-5. |
| 205 | samples.Accumulate(6, 1); // Bucket 5-7. |
| 206 | samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 207 | samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) |
| 208 | samples.Accumulate(11, 1); // Bucket 11-12. |
| 209 | |
asvitkine@chromium.org | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 210 | TestMetricsServiceClient client; |
| 211 | TestingPrefServiceSimple prefs; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 212 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 213 | log.RecordHistogramDelta("Test", samples); |
| 214 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 215 | const ChromeUserMetricsExtension& uma_proto = log.uma_proto(); |
| 216 | const HistogramEventProto& histogram_proto = |
| 217 | uma_proto.histogram_event(uma_proto.histogram_event_size() - 1); |
asvitkine@chromium.org | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 218 | |
| 219 | // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12. |
| 220 | // Should become: 1-/, 5-7, /-9, 10-/, /-12. |
| 221 | ASSERT_EQ(5, histogram_proto.bucket_size()); |
| 222 | |
| 223 | // 1-5 becomes 1-/ (max is same as next min). |
| 224 | EXPECT_TRUE(histogram_proto.bucket(0).has_min()); |
| 225 | EXPECT_FALSE(histogram_proto.bucket(0).has_max()); |
| 226 | EXPECT_EQ(1, histogram_proto.bucket(0).min()); |
| 227 | |
| 228 | // 5-7 stays 5-7 (no optimization possible). |
| 229 | EXPECT_TRUE(histogram_proto.bucket(1).has_min()); |
| 230 | EXPECT_TRUE(histogram_proto.bucket(1).has_max()); |
| 231 | EXPECT_EQ(5, histogram_proto.bucket(1).min()); |
| 232 | EXPECT_EQ(7, histogram_proto.bucket(1).max()); |
| 233 | |
| 234 | // 8-9 becomes /-9 (min is same as max - 1). |
| 235 | EXPECT_FALSE(histogram_proto.bucket(2).has_min()); |
| 236 | EXPECT_TRUE(histogram_proto.bucket(2).has_max()); |
| 237 | EXPECT_EQ(9, histogram_proto.bucket(2).max()); |
| 238 | |
| 239 | // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). |
| 240 | EXPECT_TRUE(histogram_proto.bucket(3).has_min()); |
| 241 | EXPECT_FALSE(histogram_proto.bucket(3).has_max()); |
| 242 | EXPECT_EQ(10, histogram_proto.bucket(3).min()); |
| 243 | |
| 244 | // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). |
| 245 | EXPECT_FALSE(histogram_proto.bucket(4).has_min()); |
| 246 | EXPECT_TRUE(histogram_proto.bucket(4).has_max()); |
| 247 | EXPECT_EQ(12, histogram_proto.bucket(4).max()); |
| 248 | } |
bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 249 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 250 | TEST_F(MetricsLogTest, RecordEnvironment) { |
| 251 | TestMetricsServiceClient client; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 252 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 253 | |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 254 | DelegatingProvider delegating_provider; |
Nikunj Bhagat | 19b85930 | 2018-12-20 00:39:56 | [diff] [blame] | 255 | auto cpu_provider = std::make_unique<metrics::CPUMetricsProvider>(); |
| 256 | delegating_provider.RegisterMetricsProvider(std::move(cpu_provider)); |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 257 | log.RecordEnvironment(&delegating_provider); |
Alexei Svitkine | 70c9502 | 2019-08-21 18:13:24 | [diff] [blame] | 258 | |
| 259 | // Check non-system profile values. |
| 260 | EXPECT_EQ(MetricsLog::Hash(kClientId), log.uma_proto().client_id()); |
| 261 | EXPECT_EQ(kSessionId, log.uma_proto().session_id()); |
| 262 | // Check that the system profile on the log has the correct values set. |
| 263 | CheckSystemProfile(log.system_profile()); |
| 264 | |
| 265 | // Call RecordEnvironment() again and verify things are are still filled in. |
| 266 | log.RecordEnvironment(&delegating_provider); |
| 267 | |
| 268 | // Check non-system profile values. |
| 269 | EXPECT_EQ(MetricsLog::Hash(kClientId), log.uma_proto().client_id()); |
| 270 | EXPECT_EQ(kSessionId, log.uma_proto().session_id()); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 271 | // Check that the system profile on the log has the correct values set. |
| 272 | CheckSystemProfile(log.system_profile()); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 273 | } |
| 274 | |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 275 | TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) { |
| 276 | TestMetricsServiceClient client; |
| 277 | TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 278 | &client); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 279 | |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 280 | DelegatingProvider delegating_provider; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 281 | log_unknown.RecordEnvironment(&delegating_provider); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 282 | EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state()); |
| 283 | |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 284 | client.set_enable_default(EnableMetricsDefault::OPT_IN); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 285 | TestMetricsLog log_opt_in(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 286 | &client); |
| 287 | log_opt_in.RecordEnvironment(&delegating_provider); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 288 | EXPECT_TRUE(log_opt_in.system_profile().has_uma_default_state()); |
| 289 | EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_IN, |
| 290 | log_opt_in.system_profile().uma_default_state()); |
| 291 | |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 292 | client.set_enable_default(EnableMetricsDefault::OPT_OUT); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 293 | TestMetricsLog log_opt_out(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 294 | &client); |
| 295 | log_opt_out.RecordEnvironment(&delegating_provider); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 296 | EXPECT_TRUE(log_opt_out.system_profile().has_uma_default_state()); |
| 297 | EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_OUT, |
| 298 | log_opt_out.system_profile().uma_default_state()); |
| 299 | |
| 300 | client.set_reporting_is_managed(true); |
| 301 | TestMetricsLog log_managed(kClientId, kSessionId, MetricsLog::ONGOING_LOG, |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 302 | &client); |
| 303 | log_managed.RecordEnvironment(&delegating_provider); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 304 | EXPECT_TRUE(log_managed.system_profile().has_uma_default_state()); |
| 305 | EXPECT_EQ(SystemProfileProto_UmaDefaultState_POLICY_FORCED_ENABLED, |
| 306 | log_managed.system_profile().uma_default_state()); |
| 307 | } |
| 308 | |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 309 | TEST_F(MetricsLogTest, InitialLogStabilityMetrics) { |
| 310 | TestMetricsServiceClient client; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 311 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG, |
| 312 | &client); |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 313 | TestMetricsProvider* test_provider = new TestMetricsProvider(); |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 314 | DelegatingProvider delegating_provider; |
| 315 | delegating_provider.RegisterMetricsProvider( |
| 316 | base::WrapUnique<MetricsProvider>(test_provider)); |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 317 | log.RecordEnvironment(&delegating_provider); |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 318 | log.RecordPreviousSessionData(&delegating_provider); |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 319 | |
| 320 | // The test provider should have been called upon to provide initial |
| 321 | // stability and regular stability metrics. |
| 322 | EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); |
| 323 | EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) { |
| 327 | TestMetricsServiceClient client; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 328 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 329 | TestMetricsProvider* test_provider = new TestMetricsProvider(); |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 330 | DelegatingProvider delegating_provider; |
| 331 | delegating_provider.RegisterMetricsProvider( |
| 332 | base::WrapUnique<MetricsProvider>(test_provider)); |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 333 | log.RecordEnvironment(&delegating_provider); |
Steven Holte | c35411dc | 2017-07-28 04:22:14 | [diff] [blame] | 334 | log.RecordCurrentSessionData(&delegating_provider, base::TimeDelta(), |
Steven Holte | 631803df | 2017-07-24 23:40:34 | [diff] [blame] | 335 | base::TimeDelta()); |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 336 | |
| 337 | // The test provider should have been called upon to provide regular but not |
| 338 | // initial stability metrics. |
| 339 | EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called()); |
| 340 | EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) { |
| 344 | TestMetricsServiceClient client; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 345 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
blundell@chromium.org | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 346 | EXPECT_TRUE(log.uma_proto().system_profile().has_channel()); |
| 347 | } |
| 348 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 349 | TEST_F(MetricsLogTest, ProductNotSetIfDefault) { |
| 350 | TestMetricsServiceClient client; |
| 351 | EXPECT_EQ(ChromeUserMetricsExtension::CHROME, client.GetProduct()); |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 352 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 353 | // Check that the product isn't set, since it's default and also verify the |
| 354 | // default value is indeed equal to Chrome. |
| 355 | EXPECT_FALSE(log.uma_proto().has_product()); |
| 356 | EXPECT_EQ(ChromeUserMetricsExtension::CHROME, log.uma_proto().product()); |
| 357 | } |
| 358 | |
| 359 | TEST_F(MetricsLogTest, ProductSetIfNotDefault) { |
| 360 | const int32_t kTestProduct = 100; |
| 361 | EXPECT_NE(ChromeUserMetricsExtension::CHROME, kTestProduct); |
| 362 | |
| 363 | TestMetricsServiceClient client; |
| 364 | client.set_product(kTestProduct); |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 365 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 366 | // Check that the product is set to |kTestProduct|. |
| 367 | EXPECT_TRUE(log.uma_proto().has_product()); |
| 368 | EXPECT_EQ(kTestProduct, log.uma_proto().product()); |
| 369 | } |
| 370 | |
rkaplow | 78985a0 | 2017-06-05 16:13:19 | [diff] [blame] | 371 | TEST_F(MetricsLogTest, TruncateEvents) { |
| 372 | TestMetricsServiceClient client; |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 373 | TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client); |
rkaplow | 78985a0 | 2017-06-05 16:13:19 | [diff] [blame] | 374 | |
| 375 | for (int i = 0; i < internal::kUserActionEventLimit * 2; ++i) { |
| 376 | log.RecordUserAction("BasicAction"); |
| 377 | EXPECT_EQ(i + 1, log.uma_proto().user_action_event_size()); |
| 378 | } |
| 379 | for (int i = 0; i < internal::kOmniboxEventLimit * 2; ++i) { |
| 380 | // Add an empty omnibox event. Not fully realistic since these are normally |
| 381 | // supplied by a metrics provider. |
| 382 | log.mutable_uma_proto()->add_omnibox_event(); |
| 383 | EXPECT_EQ(i + 1, log.uma_proto().omnibox_event_size()); |
| 384 | } |
| 385 | |
| 386 | // Truncate, and check that the current size is the limit. |
| 387 | log.TruncateEvents(); |
| 388 | EXPECT_EQ(internal::kUserActionEventLimit, |
| 389 | log.uma_proto().user_action_event_size()); |
| 390 | EXPECT_EQ(internal::kOmniboxEventLimit, log.uma_proto().omnibox_event_size()); |
| 391 | } |
| 392 | |
bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 393 | } // namespace metrics |