metrics_log_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // Copyright 2014 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. #include "components/metrics/metrics_log.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/base64.h"
  9. #include "base/command_line.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "base/metrics/bucket_ranges.h"
  12. #include "base/metrics/sample_vector.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/strings/string_util.h"
  15. #include "base/system/sys_info.h"
  16. #include "base/test/simple_test_clock.h"
  17. #include "base/test/simple_test_tick_clock.h"
  18. #include "base/test/task_environment.h"
  19. #include "base/time/default_clock.h"
  20. #include "base/time/default_tick_clock.h"
  21. #include "base/time/time.h"
  22. #include "build/build_config.h"
  23. #include "build/chromeos_buildflags.h"
  24. #include "components/metrics/cpu_metrics_provider.h"
  25. #include "components/metrics/delegating_provider.h"
  26. #include "components/metrics/environment_recorder.h"
  27. #include "components/metrics/metrics_pref_names.h"
  28. #include "components/metrics/metrics_state_manager.h"
  29. #include "components/metrics/test/test_metrics_provider.h"
  30. #include "components/metrics/test/test_metrics_service_client.h"
  31. #include "components/prefs/pref_service.h"
  32. #include "components/prefs/testing_pref_service.h"
  33. #include "components/variations/active_field_trials.h"
  34. #include "services/network/test/test_shared_url_loader_factory.h"
  35. #include "testing/gtest/include/gtest/gtest.h"
  36. #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
  37. #if BUILDFLAG(IS_ANDROID)
  38. #include "base/android/build_info.h"
  39. #endif
  40. #if BUILDFLAG(IS_WIN)
  41. #include "base/win/current_module.h"
  42. #endif
  43. namespace metrics {
  44. namespace {
  45. const char kClientId[] = "bogus client ID";
  46. const int kSessionId = 127;
  47. class TestMetricsLog : public MetricsLog {
  48. public:
  49. TestMetricsLog(const std::string& client_id,
  50. int session_id,
  51. LogType log_type,
  52. MetricsServiceClient* client)
  53. : MetricsLog(client_id, session_id, log_type, client) {}
  54. TestMetricsLog(const TestMetricsLog&) = delete;
  55. TestMetricsLog& operator=(const TestMetricsLog&) = delete;
  56. ~TestMetricsLog() override {}
  57. const ChromeUserMetricsExtension& uma_proto() const {
  58. return *MetricsLog::uma_proto();
  59. }
  60. ChromeUserMetricsExtension* mutable_uma_proto() {
  61. return MetricsLog::uma_proto();
  62. }
  63. const SystemProfileProto& system_profile() const {
  64. return uma_proto().system_profile();
  65. }
  66. };
  67. // Returns the expected hardware class for a metrics log.
  68. std::string GetExpectedHardwareClass() {
  69. #if BUILDFLAG(IS_CHROMEOS_ASH)
  70. // Currently, we are relying on base/ implementation for functionality on our
  71. // side which can be fragile if in the future someone decides to change that.
  72. // This replicates the logic to get the hardware class for ChromeOS and this
  73. // result should match with the result by calling
  74. // base::SysInfo::HardwareModelName().
  75. std::string board = base::SysInfo::GetLsbReleaseBoard();
  76. const size_t index = board.find("-signed-");
  77. if (index != std::string::npos)
  78. board.resize(index);
  79. return base::ToUpperASCII(board);
  80. #else
  81. return base::SysInfo::HardwareModelName();
  82. #endif
  83. }
  84. } // namespace
  85. class MetricsLogTest : public testing::Test {
  86. public:
  87. MetricsLogTest() {}
  88. MetricsLogTest(const MetricsLogTest&) = delete;
  89. MetricsLogTest& operator=(const MetricsLogTest&) = delete;
  90. ~MetricsLogTest() override {}
  91. protected:
  92. // Check that the values in |system_values| are filled in and expected ones
  93. // correspond to the test data defined at the top of this file.
  94. void CheckSystemProfile(const SystemProfileProto& system_profile) {
  95. // Check for presence of core system profile fields.
  96. EXPECT_TRUE(system_profile.has_build_timestamp());
  97. EXPECT_TRUE(system_profile.has_app_version());
  98. EXPECT_TRUE(system_profile.has_channel());
  99. EXPECT_FALSE(system_profile.has_is_extended_stable_channel());
  100. EXPECT_TRUE(system_profile.has_application_locale());
  101. const SystemProfileProto::OS& os = system_profile.os();
  102. EXPECT_TRUE(os.has_name());
  103. EXPECT_TRUE(os.has_version());
  104. // Check matching test brand code.
  105. EXPECT_EQ(TestMetricsServiceClient::kBrandForTesting,
  106. system_profile.brand_code());
  107. // Check for presence of fields set by a metrics provider.
  108. const SystemProfileProto::Hardware& hardware = system_profile.hardware();
  109. EXPECT_EQ(hardware.hardware_class(), GetExpectedHardwareClass());
  110. EXPECT_TRUE(hardware.has_cpu());
  111. EXPECT_TRUE(hardware.cpu().has_vendor_name());
  112. EXPECT_TRUE(hardware.cpu().has_signature());
  113. EXPECT_TRUE(hardware.cpu().has_num_cores());
  114. // TODO(isherman): Verify other data written into the protobuf as a result
  115. // of this call.
  116. }
  117. };
  118. TEST_F(MetricsLogTest, LogType) {
  119. TestMetricsServiceClient client;
  120. TestingPrefServiceSimple prefs;
  121. MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client);
  122. EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type());
  123. MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client);
  124. EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type());
  125. }
  126. TEST_F(MetricsLogTest, BasicRecord) {
  127. TestMetricsServiceClient client;
  128. client.set_version_string("bogus version");
  129. const std::string kOtherClientId = "totally bogus client ID";
  130. TestingPrefServiceSimple prefs;
  131. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  132. // Clears existing command line flags and sets mock flags:
  133. // "--mock-flag-1 --mock-flag-2=unused_value"
  134. // Hashes of these flags should be populated on the system_profile field.
  135. command_line->InitFromArgv(0, nullptr);
  136. command_line->AppendSwitch("mock-flag-1");
  137. command_line->AppendSwitchASCII("mock-flag-2", "unused_value");
  138. MetricsLog log(kOtherClientId, 137, MetricsLog::ONGOING_LOG, &client);
  139. log.CloseLog();
  140. std::string encoded;
  141. log.GetEncodedLog(&encoded);
  142. // A couple of fields are hard to mock, so these will be copied over directly
  143. // for the expected output.
  144. ChromeUserMetricsExtension parsed;
  145. ASSERT_TRUE(parsed.ParseFromString(encoded));
  146. ChromeUserMetricsExtension expected;
  147. expected.set_client_id(5217101509553811875); // Hashed bogus client ID
  148. expected.set_session_id(137);
  149. SystemProfileProto* system_profile = expected.mutable_system_profile();
  150. system_profile->set_app_version("bogus version");
  151. // Make sure |client_uuid| in the system profile is the unhashed client id
  152. // and is the same as the client id in |local_prefs|.
  153. system_profile->set_client_uuid(kOtherClientId);
  154. system_profile->set_channel(client.GetChannel());
  155. system_profile->set_application_locale(client.GetApplicationLocale());
  156. system_profile->set_brand_code(TestMetricsServiceClient::kBrandForTesting);
  157. // Hashes of "mock-flag-1" and "mock-flag-2" from SetUpCommandLine.
  158. system_profile->add_command_line_key_hash(2578836236);
  159. system_profile->add_command_line_key_hash(2867288449);
  160. #if defined(ADDRESS_SANITIZER) || DCHECK_IS_ON()
  161. system_profile->set_is_instrumented_build(true);
  162. #endif
  163. metrics::SystemProfileProto::Hardware* hardware =
  164. system_profile->mutable_hardware();
  165. hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
  166. auto app_os_arch = base::SysInfo::ProcessCPUArchitecture();
  167. if (!app_os_arch.empty())
  168. hardware->set_app_cpu_architecture(app_os_arch);
  169. hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
  170. hardware->set_hardware_class(GetExpectedHardwareClass());
  171. #if BUILDFLAG(IS_WIN)
  172. hardware->set_dll_base(reinterpret_cast<uint64_t>(CURRENT_MODULE()));
  173. #endif
  174. #if BUILDFLAG(IS_CHROMEOS_LACROS)
  175. system_profile->mutable_os()->set_name("Lacros");
  176. #elif BUILDFLAG(IS_CHROMEOS_ASH)
  177. system_profile->mutable_os()->set_name("CrOS");
  178. #else
  179. system_profile->mutable_os()->set_name(base::SysInfo::OperatingSystemName());
  180. #endif
  181. system_profile->mutable_os()->set_version(
  182. base::SysInfo::OperatingSystemVersion());
  183. #if BUILDFLAG(IS_CHROMEOS_ASH)
  184. system_profile->mutable_os()->set_kernel_version(
  185. base::SysInfo::KernelVersion());
  186. #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  187. system_profile->mutable_os()->set_kernel_version(
  188. base::SysInfo::OperatingSystemVersion());
  189. #elif BUILDFLAG(IS_ANDROID)
  190. system_profile->mutable_os()->set_build_fingerprint(
  191. base::android::BuildInfo::GetInstance()->android_build_fp());
  192. system_profile->set_app_package_name("test app");
  193. #elif BUILDFLAG(IS_IOS)
  194. system_profile->mutable_os()->set_build_number(
  195. base::SysInfo::GetIOSBuildNumber());
  196. #endif
  197. // Hard to mock.
  198. system_profile->set_build_timestamp(
  199. parsed.system_profile().build_timestamp());
  200. #if BUILDFLAG(IS_ANDROID)
  201. system_profile->set_installer_package(
  202. parsed.system_profile().installer_package());
  203. #endif
  204. // Not tested here; instead tested in Timestamps_* tests below.
  205. expected.mutable_time_log_created()->CopyFrom(parsed.time_log_created());
  206. expected.mutable_time_log_closed()->CopyFrom(parsed.time_log_closed());
  207. EXPECT_EQ(expected.SerializeAsString(), encoded);
  208. }
  209. TEST_F(MetricsLogTest, Timestamps_InitialStabilityLog) {
  210. TestMetricsServiceClient client;
  211. std::unique_ptr<base::SimpleTestClock> clock =
  212. std::make_unique<base::SimpleTestClock>();
  213. // Should not have times from initial stability logs.
  214. clock->SetNow(base::Time::FromTimeT(1));
  215. MetricsLog log("id", 0, MetricsLog::INITIAL_STABILITY_LOG, clock.get(),
  216. nullptr, &client);
  217. clock->SetNow(base::Time::FromTimeT(2));
  218. log.CloseLog();
  219. std::string encoded;
  220. log.GetEncodedLog(&encoded);
  221. ChromeUserMetricsExtension parsed;
  222. ASSERT_TRUE(parsed.ParseFromString(encoded));
  223. EXPECT_FALSE(parsed.has_time_log_created());
  224. EXPECT_FALSE(parsed.has_time_log_closed());
  225. }
  226. TEST_F(MetricsLogTest, Timestamps_IndependentLog) {
  227. TestMetricsServiceClient client;
  228. std::unique_ptr<base::SimpleTestClock> clock =
  229. std::make_unique<base::SimpleTestClock>();
  230. // Should not have times from independent logs.
  231. clock->SetNow(base::Time::FromTimeT(1));
  232. MetricsLog log("id", 0, MetricsLog::INDEPENDENT_LOG, clock.get(), nullptr,
  233. &client);
  234. clock->SetNow(base::Time::FromTimeT(2));
  235. log.CloseLog();
  236. std::string encoded;
  237. log.GetEncodedLog(&encoded);
  238. ChromeUserMetricsExtension parsed;
  239. ASSERT_TRUE(parsed.ParseFromString(encoded));
  240. EXPECT_FALSE(parsed.has_time_log_created());
  241. EXPECT_FALSE(parsed.has_time_log_closed());
  242. }
  243. TEST_F(MetricsLogTest, Timestamps_OngoingLog) {
  244. TestMetricsServiceClient client;
  245. std::unique_ptr<base::SimpleTestClock> clock =
  246. std::make_unique<base::SimpleTestClock>();
  247. // Should have times from regular (ongoing) logs.
  248. clock->SetNow(base::Time::FromTimeT(1));
  249. MetricsLog log("id", 0, MetricsLog::ONGOING_LOG, clock.get(), nullptr,
  250. &client);
  251. clock->SetNow(base::Time::FromTimeT(2));
  252. log.CloseLog();
  253. std::string encoded;
  254. log.GetEncodedLog(&encoded);
  255. ChromeUserMetricsExtension parsed;
  256. ASSERT_TRUE(parsed.ParseFromString(encoded));
  257. EXPECT_TRUE(parsed.has_time_log_created());
  258. EXPECT_EQ(parsed.time_log_created().time_sec(), 1);
  259. EXPECT_EQ(parsed.time_log_created().time_source(),
  260. ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
  261. // The timezone should not be set in the time_log_created field.
  262. EXPECT_FALSE(parsed.time_log_created().has_time_zone_offset_from_gmt_sec());
  263. EXPECT_TRUE(parsed.has_time_log_closed());
  264. EXPECT_EQ(parsed.time_log_closed().time_sec(), 2);
  265. EXPECT_EQ(parsed.time_log_closed().time_source(),
  266. ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
  267. // The timezone should be set, but we don't check what it is.
  268. EXPECT_TRUE(parsed.time_log_closed().has_time_zone_offset_from_gmt_sec());
  269. }
  270. TEST_F(MetricsLogTest, HistogramBucketFields) {
  271. // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
  272. base::BucketRanges ranges(8);
  273. ranges.set_range(0, 1);
  274. ranges.set_range(1, 5);
  275. ranges.set_range(2, 7);
  276. ranges.set_range(3, 8);
  277. ranges.set_range(4, 9);
  278. ranges.set_range(5, 10);
  279. ranges.set_range(6, 11);
  280. ranges.set_range(7, 12);
  281. base::SampleVector samples(1, &ranges);
  282. samples.Accumulate(3, 1); // Bucket 1-5.
  283. samples.Accumulate(6, 1); // Bucket 5-7.
  284. samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped)
  285. samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
  286. samples.Accumulate(11, 1); // Bucket 11-12.
  287. TestMetricsServiceClient client;
  288. TestingPrefServiceSimple prefs;
  289. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  290. log.RecordHistogramDelta("Test", samples);
  291. const ChromeUserMetricsExtension& uma_proto = log.uma_proto();
  292. const HistogramEventProto& histogram_proto =
  293. uma_proto.histogram_event(uma_proto.histogram_event_size() - 1);
  294. // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
  295. // Should become: 1-/, 5-7, /-9, 10-/, /-12.
  296. ASSERT_EQ(5, histogram_proto.bucket_size());
  297. // 1-5 becomes 1-/ (max is same as next min).
  298. EXPECT_TRUE(histogram_proto.bucket(0).has_min());
  299. EXPECT_FALSE(histogram_proto.bucket(0).has_max());
  300. EXPECT_EQ(1, histogram_proto.bucket(0).min());
  301. // 5-7 stays 5-7 (no optimization possible).
  302. EXPECT_TRUE(histogram_proto.bucket(1).has_min());
  303. EXPECT_TRUE(histogram_proto.bucket(1).has_max());
  304. EXPECT_EQ(5, histogram_proto.bucket(1).min());
  305. EXPECT_EQ(7, histogram_proto.bucket(1).max());
  306. // 8-9 becomes /-9 (min is same as max - 1).
  307. EXPECT_FALSE(histogram_proto.bucket(2).has_min());
  308. EXPECT_TRUE(histogram_proto.bucket(2).has_max());
  309. EXPECT_EQ(9, histogram_proto.bucket(2).max());
  310. // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized).
  311. EXPECT_TRUE(histogram_proto.bucket(3).has_min());
  312. EXPECT_FALSE(histogram_proto.bucket(3).has_max());
  313. EXPECT_EQ(10, histogram_proto.bucket(3).min());
  314. // 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
  315. EXPECT_FALSE(histogram_proto.bucket(4).has_min());
  316. EXPECT_TRUE(histogram_proto.bucket(4).has_max());
  317. EXPECT_EQ(12, histogram_proto.bucket(4).max());
  318. }
  319. TEST_F(MetricsLogTest, HistogramSamplesCount) {
  320. const std::string histogram_name = "test";
  321. TestMetricsServiceClient client;
  322. TestingPrefServiceSimple prefs;
  323. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  324. // Create buckets: 1-5.
  325. base::BucketRanges ranges(2);
  326. ranges.set_range(0, 1);
  327. ranges.set_range(1, 5);
  328. // Add two samples.
  329. base::SampleVector samples(1, &ranges);
  330. samples.Accumulate(3, 2);
  331. log.RecordHistogramDelta(histogram_name, samples);
  332. EXPECT_EQ(2, log.log_metadata().samples_count.value());
  333. // Add two more samples.
  334. log.RecordHistogramDelta(histogram_name, samples);
  335. EXPECT_EQ(4, log.log_metadata().samples_count.value());
  336. }
  337. TEST_F(MetricsLogTest, RecordEnvironment) {
  338. TestMetricsServiceClient client;
  339. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  340. DelegatingProvider delegating_provider;
  341. auto cpu_provider = std::make_unique<metrics::CPUMetricsProvider>();
  342. delegating_provider.RegisterMetricsProvider(std::move(cpu_provider));
  343. log.RecordEnvironment(&delegating_provider);
  344. // Check non-system profile values.
  345. EXPECT_EQ(MetricsLog::Hash(kClientId), log.uma_proto().client_id());
  346. EXPECT_EQ(kSessionId, log.uma_proto().session_id());
  347. // Check that the system profile on the log has the correct values set.
  348. CheckSystemProfile(log.system_profile());
  349. // Call RecordEnvironment() again and verify things are are still filled in.
  350. log.RecordEnvironment(&delegating_provider);
  351. // Check non-system profile values.
  352. EXPECT_EQ(MetricsLog::Hash(kClientId), log.uma_proto().client_id());
  353. EXPECT_EQ(kSessionId, log.uma_proto().session_id());
  354. // Check that the system profile on the log has the correct values set.
  355. CheckSystemProfile(log.system_profile());
  356. }
  357. TEST_F(MetricsLogTest, RecordEnvironmentExtendedStable) {
  358. TestMetricsServiceClient client;
  359. client.set_is_extended_stable_channel(true);
  360. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  361. DelegatingProvider delegating_provider;
  362. auto cpu_provider = std::make_unique<metrics::CPUMetricsProvider>();
  363. delegating_provider.RegisterMetricsProvider(std::move(cpu_provider));
  364. log.RecordEnvironment(&delegating_provider);
  365. EXPECT_TRUE(log.system_profile().has_is_extended_stable_channel());
  366. EXPECT_TRUE(log.system_profile().is_extended_stable_channel());
  367. }
  368. TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) {
  369. TestMetricsServiceClient client;
  370. TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
  371. &client);
  372. DelegatingProvider delegating_provider;
  373. log_unknown.RecordEnvironment(&delegating_provider);
  374. EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state());
  375. client.set_enable_default(EnableMetricsDefault::OPT_IN);
  376. TestMetricsLog log_opt_in(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
  377. &client);
  378. log_opt_in.RecordEnvironment(&delegating_provider);
  379. EXPECT_TRUE(log_opt_in.system_profile().has_uma_default_state());
  380. EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_IN,
  381. log_opt_in.system_profile().uma_default_state());
  382. client.set_enable_default(EnableMetricsDefault::OPT_OUT);
  383. TestMetricsLog log_opt_out(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
  384. &client);
  385. log_opt_out.RecordEnvironment(&delegating_provider);
  386. EXPECT_TRUE(log_opt_out.system_profile().has_uma_default_state());
  387. EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_OUT,
  388. log_opt_out.system_profile().uma_default_state());
  389. client.set_reporting_is_managed(true);
  390. TestMetricsLog log_managed(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
  391. &client);
  392. log_managed.RecordEnvironment(&delegating_provider);
  393. EXPECT_TRUE(log_managed.system_profile().has_uma_default_state());
  394. EXPECT_EQ(SystemProfileProto_UmaDefaultState_POLICY_FORCED_ENABLED,
  395. log_managed.system_profile().uma_default_state());
  396. }
  397. TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
  398. TestMetricsServiceClient client;
  399. TestMetricsLog log(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG,
  400. &client);
  401. TestMetricsProvider* test_provider = new TestMetricsProvider();
  402. DelegatingProvider delegating_provider;
  403. delegating_provider.RegisterMetricsProvider(
  404. base::WrapUnique<MetricsProvider>(test_provider));
  405. log.RecordEnvironment(&delegating_provider);
  406. TestingPrefServiceSimple prefs;
  407. log.RecordPreviousSessionData(&delegating_provider, &prefs);
  408. // The test provider should have been called upon to provide initial
  409. // stability and regular stability metrics.
  410. EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called());
  411. EXPECT_TRUE(test_provider->provide_stability_metrics_called());
  412. }
  413. TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
  414. TestMetricsServiceClient client;
  415. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  416. TestMetricsProvider* test_provider = new TestMetricsProvider();
  417. DelegatingProvider delegating_provider;
  418. delegating_provider.RegisterMetricsProvider(
  419. base::WrapUnique<MetricsProvider>(test_provider));
  420. log.RecordEnvironment(&delegating_provider);
  421. TestingPrefServiceSimple prefs;
  422. log.RecordCurrentSessionData(base::TimeDelta(), base::TimeDelta(),
  423. &delegating_provider, &prefs);
  424. // The test provider should have been called upon to provide regular but not
  425. // initial stability metrics.
  426. EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called());
  427. EXPECT_TRUE(test_provider->provide_stability_metrics_called());
  428. }
  429. TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) {
  430. TestMetricsServiceClient client;
  431. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  432. EXPECT_TRUE(log.uma_proto().system_profile().has_channel());
  433. }
  434. TEST_F(MetricsLogTest, ProductNotSetIfDefault) {
  435. TestMetricsServiceClient client;
  436. EXPECT_EQ(ChromeUserMetricsExtension::CHROME, client.GetProduct());
  437. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  438. // Check that the product isn't set, since it's default and also verify the
  439. // default value is indeed equal to Chrome.
  440. EXPECT_FALSE(log.uma_proto().has_product());
  441. EXPECT_EQ(ChromeUserMetricsExtension::CHROME, log.uma_proto().product());
  442. }
  443. TEST_F(MetricsLogTest, ProductSetIfNotDefault) {
  444. const int32_t kTestProduct = 100;
  445. EXPECT_NE(ChromeUserMetricsExtension::CHROME, kTestProduct);
  446. TestMetricsServiceClient client;
  447. client.set_product(kTestProduct);
  448. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  449. // Check that the product is set to |kTestProduct|.
  450. EXPECT_TRUE(log.uma_proto().has_product());
  451. EXPECT_EQ(kTestProduct, log.uma_proto().product());
  452. }
  453. TEST_F(MetricsLogTest, TruncateEvents) {
  454. TestMetricsServiceClient client;
  455. TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client);
  456. for (int i = 0; i < internal::kUserActionEventLimit * 2; ++i) {
  457. log.RecordUserAction("BasicAction", base::TimeTicks::Now());
  458. EXPECT_EQ(i + 1, log.uma_proto().user_action_event_size());
  459. }
  460. for (int i = 0; i < internal::kOmniboxEventLimit * 2; ++i) {
  461. // Add an empty omnibox event. Not fully realistic since these are normally
  462. // supplied by a metrics provider.
  463. log.mutable_uma_proto()->add_omnibox_event();
  464. EXPECT_EQ(i + 1, log.uma_proto().omnibox_event_size());
  465. }
  466. // Truncate, and check that the current size is the limit.
  467. log.TruncateEvents();
  468. EXPECT_EQ(internal::kUserActionEventLimit,
  469. log.uma_proto().user_action_event_size());
  470. EXPECT_EQ(internal::kOmniboxEventLimit, log.uma_proto().omnibox_event_size());
  471. }
  472. TEST_F(MetricsLogTest, ToInstallerPackage) {
  473. using internal::ToInstallerPackage;
  474. EXPECT_EQ(SystemProfileProto::INSTALLER_PACKAGE_NONE, ToInstallerPackage(""));
  475. EXPECT_EQ(SystemProfileProto::INSTALLER_PACKAGE_GOOGLE_PLAY_STORE,
  476. ToInstallerPackage("com.android.vending"));
  477. EXPECT_EQ(SystemProfileProto::INSTALLER_PACKAGE_OTHER,
  478. ToInstallerPackage("foo"));
  479. }
  480. } // namespace metrics