[go: nahoru, domu]

blob: 0032e5b7b6bb9f17ca38c7d33d2e3404d2c856d1 [file] [log] [blame]
bsimonnet@chromium.org064107e2014-05-02 00:59:061// Copyright 2014 The Chromium Authors. All rights reserved.
isherman@chromium.orgfe58acc22012-02-29 01:29:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
asvitkine@chromium.orgbfb77b52014-06-07 01:54:015#include "components/metrics/metrics_log.h"
isherman@chromium.org46a0efc2013-07-17 15:40:476
avi26062922015-12-26 00:14:187#include <stddef.h>
8#include <stdint.h>
9
isherman@chromium.orgfe58acc22012-02-29 01:29:5810#include <string>
11
12#include "base/base64.h"
avi26062922015-12-26 00:14:1813#include "base/macros.h"
ke.hea9da80c2017-02-16 12:45:0014#include "base/memory/ptr_util.h"
asvitkine@chromium.org838f7702013-10-04 22:20:5115#include "base/metrics/bucket_ranges.h"
16#include "base/metrics/sample_vector.h"
blundell@chromium.orgb0122eb2014-06-11 18:58:3217#include "base/strings/string_number_conversions.h"
Jonghyun Ahnc54f59f2018-07-19 17:41:5018#include "base/strings/stringprintf.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0319#include "base/system/sys_info.h"
blundell@chromium.orgb0122eb2014-06-11 18:58:3220#include "base/time/time.h"
Peter K. Lee09746e82019-01-16 02:38:5621#include "build/build_config.h"
Nikunj Bhagat19b859302018-12-20 00:39:5622#include "components/metrics/cpu_metrics_provider.h"
Steven Holtec35411dc2017-07-28 04:22:1423#include "components/metrics/delegating_provider.h"
holte1bf273c2017-02-23 00:22:2824#include "components/metrics/environment_recorder.h"
blundell@chromium.orgb0122eb2014-06-11 18:58:3225#include "components/metrics/metrics_pref_names.h"
26#include "components/metrics/metrics_state_manager.h"
lpromeroca8cb6f2015-04-30 18:16:5327#include "components/metrics/test_metrics_provider.h"
asvitkine@chromium.orgbfb77b52014-06-07 01:54:0128#include "components/metrics/test_metrics_service_client.h"
brettwf00b9b42016-02-01 22:11:3829#include "components/prefs/pref_service.h"
30#include "components/prefs/testing_pref_service.h"
blundell@chromium.orgb0122eb2014-06-11 18:58:3231#include "components/variations/active_field_trials.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:5832#include "testing/gtest/include/gtest/gtest.h"
Steven Holtef9d5ed62017-10-21 02:02:3033#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:5834
rkaplow2b85c622017-01-30 16:57:1735#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.org064107e2014-05-02 00:59:0643namespace metrics {
44
asvitkine@chromium.org838f7702013-10-04 22:20:5145namespace {
46
blundell@chromium.orgb0122eb2014-06-11 18:58:3247const char kClientId[] = "bogus client ID";
blundell@chromium.orgb0122eb2014-06-11 18:58:3248const int kSessionId = 127;
blundell@chromium.orgb0122eb2014-06-11 18:58:3249
asvitkine@chromium.orgbfb77b52014-06-07 01:54:0150class TestMetricsLog : public MetricsLog {
asvitkine@chromium.org838f7702013-10-04 22:20:5151 public:
blundell@chromium.orgb0122eb2014-06-11 18:58:3252 TestMetricsLog(const std::string& client_id,
53 int session_id,
54 LogType log_type,
Steven Holte8e9db0ca2017-08-11 01:20:0855 MetricsServiceClient* client)
56 : MetricsLog(client_id, session_id, log_type, client) {}
blundell@chromium.orgb0122eb2014-06-11 18:58:3257
dcheng00ea022b2014-10-21 11:24:5658 ~TestMetricsLog() override {}
asvitkine@chromium.org838f7702013-10-04 22:20:5159
asvitkine4c1d1ef2014-09-29 20:57:3260 const ChromeUserMetricsExtension& uma_proto() const {
blundell@chromium.orgb0122eb2014-06-11 18:58:3261 return *MetricsLog::uma_proto();
62 }
63
rkaplow78985a02017-06-05 16:13:1964 ChromeUserMetricsExtension* mutable_uma_proto() {
65 return MetricsLog::uma_proto();
66 }
67
asvitkine4c1d1ef2014-09-29 20:57:3268 const SystemProfileProto& system_profile() const {
blundell@chromium.orgb0122eb2014-06-11 18:58:3269 return uma_proto().system_profile();
70 }
asvitkine@chromium.org838f7702013-10-04 22:20:5171
72 private:
asvitkine@chromium.orgbfb77b52014-06-07 01:54:0173 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
asvitkine@chromium.org838f7702013-10-04 22:20:5174};
75
76} // namespace
77
blundell@chromium.orgb0122eb2014-06-11 18:58:3278class MetricsLogTest : public testing::Test {
79 public:
Steven Holte8e9db0ca2017-08-11 01:20:0880 MetricsLogTest() {}
dcheng30a1b1542014-10-29 21:27:5081 ~MetricsLogTest() override {}
blundell@chromium.orgb0122eb2014-06-11 18:58:3282
83 protected:
Alexei Svitkine70c95022019-08-21 18:13:2484 // 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.
asvitkine4c1d1ef2014-09-29 20:57:3286 void CheckSystemProfile(const SystemProfileProto& system_profile) {
Alexei Svitkine70c95022019-08-21 18:13:2487 // 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.
asvitkine4c1d1ef2014-09-29 20:57:3298 EXPECT_EQ(TestMetricsServiceClient::kBrandForTesting,
blundell@chromium.orgb0122eb2014-06-11 18:58:3299 system_profile.brand_code());
100
Alexei Svitkine70c95022019-08-21 18:13:24101 // Check for presence of fields set by a metrics provider.
102 const SystemProfileProto::Hardware& hardware = system_profile.hardware();
blundell@chromium.orgb0122eb2014-06-11 18:58:32103 EXPECT_TRUE(hardware.has_cpu());
104 EXPECT_TRUE(hardware.cpu().has_vendor_name());
105 EXPECT_TRUE(hardware.cpu().has_signature());
gayanef550f50f2015-03-11 00:44:59106 EXPECT_TRUE(hardware.cpu().has_num_cores());
blundell@chromium.orgb0122eb2014-06-11 18:58:32107
108 // TODO(isherman): Verify other data written into the protobuf as a result
109 // of this call.
110 }
111
blundell@chromium.orgb0122eb2014-06-11 18:58:32112 private:
113 DISALLOW_COPY_AND_ASSIGN(MetricsLogTest);
114};
115
116TEST_F(MetricsLogTest, LogType) {
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01117 TestMetricsServiceClient client;
118 TestingPrefServiceSimple prefs;
asvitkine@chromium.org9eae4032014-04-09 19:15:19119
Steven Holted0429a702017-08-03 08:56:17120 MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client);
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01121 EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type());
122
Steven Holted0429a702017-08-03 08:56:17123 MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client);
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01124 EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type());
asvitkine@chromium.org9eae4032014-04-09 19:15:19125}
126
rkaplow2b85c622017-01-30 16:57:17127TEST_F(MetricsLogTest, BasicRecord) {
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01128 TestMetricsServiceClient client;
129 client.set_version_string("bogus version");
130 TestingPrefServiceSimple prefs;
131 MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG,
Steven Holted0429a702017-08-03 08:56:17132 &client);
isherman@chromium.orgfe58acc22012-02-29 01:29:58133 log.CloseLog();
134
isherman@chromium.orgfe58acc22012-02-29 01:29:58135 std::string encoded;
isherman@chromium.org46a0efc2013-07-17 15:40:47136 log.GetEncodedLog(&encoded);
isherman@chromium.orgfe58acc22012-02-29 01:29:58137
isherman@chromium.org46a0efc2013-07-17 15:40:47138 // A couple of fields are hard to mock, so these will be copied over directly
139 // for the expected output.
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01140 ChromeUserMetricsExtension parsed;
isherman@chromium.org46a0efc2013-07-17 15:40:47141 ASSERT_TRUE(parsed.ParseFromString(encoded));
isherman@chromium.orgfe58acc22012-02-29 01:29:58142
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01143 ChromeUserMetricsExtension expected;
isherman@chromium.org46a0efc2013-07-17 15:40:47144 expected.set_client_id(5217101509553811875); // Hashed bogus client ID
145 expected.set_session_id(137);
rkaplow2b85c622017-01-30 16:57:17146
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
Wez720413e2018-10-04 17:04:31152#if defined(ADDRESS_SANITIZER) || DCHECK_IS_ON()
Weze7b1b612018-10-22 18:17:22153 system_profile->set_is_instrumented_build(true);
rkaplow2b85c622017-01-30 16:57:17154#endif
155 metrics::SystemProfileProto::Hardware* hardware =
156 system_profile->mutable_hardware();
asvitkineb29323612017-05-05 14:02:47157#if !defined(OS_IOS)
rkaplow2b85c622017-01-30 16:57:17158 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
asvitkineb29323612017-05-05 14:02:47159#endif
rkaplow2b85c622017-01-30 16:57:17160 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 Ahnc54f59f2018-07-19 17:41:50169#if defined(OS_CHROMEOS)
170 system_profile->mutable_os()->set_kernel_version(
171 base::SysInfo::KernelVersion());
Jonghyun Ahne4b93362018-10-30 01:43:35172#elif defined(OS_LINUX)
173 system_profile->mutable_os()->set_kernel_version(
174 base::SysInfo::OperatingSystemVersion());
Jonghyun Ahnc54f59f2018-07-19 17:41:50175#elif defined(OS_ANDROID)
Steven Holted7d6931f2017-08-25 22:53:15176 system_profile->mutable_os()->set_build_fingerprint(
rkaplow2b85c622017-01-30 16:57:17177 base::android::BuildInfo::GetInstance()->android_build_fp());
Shuo Weng37202652018-04-19 02:54:11178 system_profile->set_app_package_name("test app");
Peter K. Lee09746e82019-01-16 02:38:56179#elif defined(OS_IOS)
180 system_profile->mutable_os()->set_build_number(
181 base::SysInfo::GetIOSBuildNumber());
rkaplow2b85c622017-01-30 16:57:17182#endif
183
184 // Hard to mock.
185 system_profile->set_build_timestamp(
isherman@chromium.org46a0efc2013-07-17 15:40:47186 parsed.system_profile().build_timestamp());
isherman@chromium.orgfe58acc22012-02-29 01:29:58187
isherman@chromium.org46a0efc2013-07-17 15:40:47188 EXPECT_EQ(expected.SerializeAsString(), encoded);
isherman@chromium.orgfe58acc22012-02-29 01:29:58189}
190
blundell@chromium.orgb0122eb2014-06-11 18:58:32191TEST_F(MetricsLogTest, HistogramBucketFields) {
asvitkine@chromium.org838f7702013-10-04 22:20:51192 // 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
bcwhiteb036e4322015-12-10 18:36:34203 base::SampleVector samples(1, &ranges);
bsimonnet@chromium.org064107e2014-05-02 00:59:06204 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.org838f7702013-10-04 22:20:51207 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
208 samples.Accumulate(11, 1); // Bucket 11-12.
209
asvitkine@chromium.orgbfb77b52014-06-07 01:54:01210 TestMetricsServiceClient client;
211 TestingPrefServiceSimple prefs;
Steven Holte8e9db0ca2017-08-11 01:20:08212 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
asvitkine@chromium.org838f7702013-10-04 22:20:51213 log.RecordHistogramDelta("Test", samples);
214
blundell@chromium.orgb0122eb2014-06-11 18:58:32215 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.org838f7702013-10-04 22:20:51218
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.org064107e2014-05-02 00:59:06249
blundell@chromium.orgb0122eb2014-06-11 18:58:32250TEST_F(MetricsLogTest, RecordEnvironment) {
251 TestMetricsServiceClient client;
Steven Holte8e9db0ca2017-08-11 01:20:08252 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
blundell@chromium.orgb0122eb2014-06-11 18:58:32253
Steven Holtec35411dc2017-07-28 04:22:14254 DelegatingProvider delegating_provider;
Nikunj Bhagat19b859302018-12-20 00:39:56255 auto cpu_provider = std::make_unique<metrics::CPUMetricsProvider>();
256 delegating_provider.RegisterMetricsProvider(std::move(cpu_provider));
Steven Holte8e9db0ca2017-08-11 01:20:08257 log.RecordEnvironment(&delegating_provider);
Alexei Svitkine70c95022019-08-21 18:13:24258
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.orgb0122eb2014-06-11 18:58:32271 // Check that the system profile on the log has the correct values set.
272 CheckSystemProfile(log.system_profile());
blundell@chromium.orgb0122eb2014-06-11 18:58:32273}
274
jwd421086f2016-03-21 14:40:42275TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) {
276 TestMetricsServiceClient client;
277 TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
Steven Holte8e9db0ca2017-08-11 01:20:08278 &client);
jwd421086f2016-03-21 14:40:42279
Steven Holtec35411dc2017-07-28 04:22:14280 DelegatingProvider delegating_provider;
Steven Holte8e9db0ca2017-08-11 01:20:08281 log_unknown.RecordEnvironment(&delegating_provider);
jwd421086f2016-03-21 14:40:42282 EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state());
283
gayanedaaf3a02016-06-15 16:30:21284 client.set_enable_default(EnableMetricsDefault::OPT_IN);
jwd421086f2016-03-21 14:40:42285 TestMetricsLog log_opt_in(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
Steven Holte8e9db0ca2017-08-11 01:20:08286 &client);
287 log_opt_in.RecordEnvironment(&delegating_provider);
jwd421086f2016-03-21 14:40:42288 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
gayanedaaf3a02016-06-15 16:30:21292 client.set_enable_default(EnableMetricsDefault::OPT_OUT);
jwd421086f2016-03-21 14:40:42293 TestMetricsLog log_opt_out(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
Steven Holte8e9db0ca2017-08-11 01:20:08294 &client);
295 log_opt_out.RecordEnvironment(&delegating_provider);
jwd421086f2016-03-21 14:40:42296 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 Holte8e9db0ca2017-08-11 01:20:08302 &client);
303 log_managed.RecordEnvironment(&delegating_provider);
jwd421086f2016-03-21 14:40:42304 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.orgb0122eb2014-06-11 18:58:32309TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
310 TestMetricsServiceClient client;
Steven Holte8e9db0ca2017-08-11 01:20:08311 TestMetricsLog log(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG,
312 &client);
lpromeroca8cb6f2015-04-30 18:16:53313 TestMetricsProvider* test_provider = new TestMetricsProvider();
Steven Holtec35411dc2017-07-28 04:22:14314 DelegatingProvider delegating_provider;
315 delegating_provider.RegisterMetricsProvider(
316 base::WrapUnique<MetricsProvider>(test_provider));
Steven Holte8e9db0ca2017-08-11 01:20:08317 log.RecordEnvironment(&delegating_provider);
Steven Holtec35411dc2017-07-28 04:22:14318 log.RecordPreviousSessionData(&delegating_provider);
lpromeroca8cb6f2015-04-30 18:16:53319
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.orgb0122eb2014-06-11 18:58:32324}
325
326TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
327 TestMetricsServiceClient client;
Steven Holte8e9db0ca2017-08-11 01:20:08328 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
lpromeroca8cb6f2015-04-30 18:16:53329 TestMetricsProvider* test_provider = new TestMetricsProvider();
Steven Holtec35411dc2017-07-28 04:22:14330 DelegatingProvider delegating_provider;
331 delegating_provider.RegisterMetricsProvider(
332 base::WrapUnique<MetricsProvider>(test_provider));
Steven Holte8e9db0ca2017-08-11 01:20:08333 log.RecordEnvironment(&delegating_provider);
Steven Holtec35411dc2017-07-28 04:22:14334 log.RecordCurrentSessionData(&delegating_provider, base::TimeDelta(),
Steven Holte631803df2017-07-24 23:40:34335 base::TimeDelta());
lpromeroca8cb6f2015-04-30 18:16:53336
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.orgb0122eb2014-06-11 18:58:32341}
342
343TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) {
344 TestMetricsServiceClient client;
Steven Holte8e9db0ca2017-08-11 01:20:08345 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
blundell@chromium.orgb0122eb2014-06-11 18:58:32346 EXPECT_TRUE(log.uma_proto().system_profile().has_channel());
347}
348
asvitkine4c1d1ef2014-09-29 20:57:32349TEST_F(MetricsLogTest, ProductNotSetIfDefault) {
350 TestMetricsServiceClient client;
351 EXPECT_EQ(ChromeUserMetricsExtension::CHROME, client.GetProduct());
Steven Holte8e9db0ca2017-08-11 01:20:08352 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
asvitkine4c1d1ef2014-09-29 20:57:32353 // 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
359TEST_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 Holte8e9db0ca2017-08-11 01:20:08365 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
asvitkine4c1d1ef2014-09-29 20:57:32366 // 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
rkaplow78985a02017-06-05 16:13:19371TEST_F(MetricsLogTest, TruncateEvents) {
372 TestMetricsServiceClient client;
Steven Holte8e9db0ca2017-08-11 01:20:08373 TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
rkaplow78985a02017-06-05 16:13:19374
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.org064107e2014-05-02 00:59:06393} // namespace metrics