telemetry_log_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2021 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 "ash/system/diagnostics/telemetry_log.h"
  5. #include "ash/system/diagnostics/log_test_helpers.h"
  6. #include "ash/webui/diagnostics_ui/mojom/system_data_provider.mojom.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/string_util.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace ash {
  11. namespace diagnostics {
  12. namespace {
  13. mojom::SystemInfoPtr CreateSystemInfoPtr(const std::string& board_name,
  14. const std::string& marketing_name,
  15. const std::string& cpu_model,
  16. uint32_t total_memory_kib,
  17. uint16_t cpu_threads_count,
  18. uint32_t cpu_max_clock_speed_khz,
  19. bool has_battery,
  20. const std::string& milestone_version,
  21. const std::string& full_version) {
  22. auto version_info = mojom::VersionInfo::New(milestone_version, full_version);
  23. auto device_capabilities = mojom::DeviceCapabilities::New(has_battery);
  24. auto system_info = mojom::SystemInfo::New(
  25. board_name, marketing_name, cpu_model, total_memory_kib,
  26. cpu_threads_count, cpu_max_clock_speed_khz, std::move(version_info),
  27. std::move(device_capabilities));
  28. return system_info;
  29. }
  30. } // namespace
  31. class TelemetryLogTest : public testing::Test {
  32. public:
  33. TelemetryLogTest() = default;
  34. ~TelemetryLogTest() override = default;
  35. };
  36. TEST_F(TelemetryLogTest, DetailedLogContents) {
  37. const std::string expected_board_name = "board_name";
  38. const std::string expected_marketing_name = "marketing_name";
  39. const std::string expected_cpu_model = "cpu_model";
  40. const uint32_t expected_total_memory_kib = 1234;
  41. const uint16_t expected_cpu_threads_count = 5678;
  42. const uint32_t expected_cpu_max_clock_speed_khz = 91011;
  43. const bool expected_has_battery = true;
  44. const std::string expected_milestone_version = "M99";
  45. const std::string expected_full_version = "M99.1234.5.6";
  46. mojom::SystemInfoPtr test_info = CreateSystemInfoPtr(
  47. expected_board_name, expected_marketing_name, expected_cpu_model,
  48. expected_total_memory_kib, expected_cpu_threads_count,
  49. expected_cpu_max_clock_speed_khz, expected_has_battery,
  50. expected_milestone_version, expected_full_version);
  51. TelemetryLog log;
  52. log.UpdateSystemInfo(test_info.Clone());
  53. const std::string log_as_string = log.GetContents();
  54. const std::vector<std::string> log_lines = GetLogLines(log_as_string);
  55. const std::string expected_snapshot_time_prefix = "Snapshot Time: ";
  56. // Expect one title line and 9 content lines.
  57. EXPECT_EQ(10u, log_lines.size());
  58. EXPECT_GT(log_lines[1].size(), expected_snapshot_time_prefix.size());
  59. EXPECT_TRUE(base::StartsWith(log_lines[1], expected_snapshot_time_prefix));
  60. EXPECT_EQ("Board Name: " + expected_board_name, log_lines[2]);
  61. EXPECT_EQ("Marketing Name: " + expected_marketing_name, log_lines[3]);
  62. EXPECT_EQ("CpuModel Name: " + expected_cpu_model, log_lines[4]);
  63. EXPECT_EQ(
  64. "Total Memory (kib): " + base::NumberToString(expected_total_memory_kib),
  65. log_lines[5]);
  66. EXPECT_EQ(
  67. "Thread Count: " + base::NumberToString(expected_cpu_threads_count),
  68. log_lines[6]);
  69. EXPECT_EQ("Cpu Max Clock Speed (kHz): " +
  70. base::NumberToString(expected_cpu_max_clock_speed_khz),
  71. log_lines[7]);
  72. EXPECT_EQ("Version: " + expected_full_version, log_lines[8]);
  73. EXPECT_EQ("Has Battery: true", log_lines[9]);
  74. }
  75. TEST_F(TelemetryLogTest, ChangeContents) {
  76. const std::string expected_board_name = "board_name";
  77. const std::string expected_marketing_name = "marketing_name";
  78. const std::string expected_cpu_model = "cpu_model";
  79. const uint32_t expected_total_memory_kib = 1234;
  80. const uint16_t expected_cpu_threads_count = 5678;
  81. const uint32_t expected_cpu_max_clock_speed_khz = 91011;
  82. const bool expected_has_battery = true;
  83. const std::string expected_milestone_version = "M99";
  84. const std::string expected_full_version = "M99.1234.5.6";
  85. mojom::SystemInfoPtr test_info = CreateSystemInfoPtr(
  86. expected_board_name, expected_marketing_name, expected_cpu_model,
  87. expected_total_memory_kib, expected_cpu_threads_count,
  88. expected_cpu_max_clock_speed_khz, expected_has_battery,
  89. expected_milestone_version, expected_full_version);
  90. TelemetryLog log;
  91. log.UpdateSystemInfo(test_info.Clone());
  92. test_info->board_name = "new board_name";
  93. log.UpdateSystemInfo(test_info.Clone());
  94. const std::string log_as_string = log.GetContents();
  95. const std::vector<std::string> log_lines = GetLogLines(log_as_string);
  96. }
  97. TEST_F(TelemetryLogTest, CpuUsageUint8) {
  98. const std::string expected_board_name = "board_name";
  99. const std::string expected_marketing_name = "marketing_name";
  100. const std::string expected_cpu_model = "cpu_model";
  101. const uint32_t expected_total_memory_kib = 1234;
  102. const uint16_t expected_cpu_threads_count = 5678;
  103. const uint32_t expected_cpu_max_clock_speed_khz = 91011;
  104. const bool expected_has_battery = true;
  105. const std::string expected_milestone_version = "M99";
  106. const std::string expected_full_version = "M99.1234.5.6";
  107. const uint8_t percent_usage_user = 10;
  108. const uint8_t percent_usage_system = 20;
  109. const uint8_t percent_usage_free = 80;
  110. const uint16_t average_cpu_temp_celsius = 31;
  111. const uint32_t scaling_current_frequency_khz = 500;
  112. mojom::SystemInfoPtr test_info = CreateSystemInfoPtr(
  113. expected_board_name, expected_marketing_name, expected_cpu_model,
  114. expected_total_memory_kib, expected_cpu_threads_count,
  115. expected_cpu_max_clock_speed_khz, expected_has_battery,
  116. expected_milestone_version, expected_full_version);
  117. mojom::CpuUsagePtr cpu_usage = mojom::CpuUsage::New(
  118. percent_usage_user, percent_usage_system, percent_usage_free,
  119. average_cpu_temp_celsius, scaling_current_frequency_khz);
  120. TelemetryLog log;
  121. log.UpdateSystemInfo(test_info.Clone());
  122. log.UpdateCpuUsage(cpu_usage.Clone());
  123. const std::string log_as_string = log.GetContents();
  124. const std::vector<std::string> log_lines = GetLogLines(log_as_string);
  125. EXPECT_EQ("Usage User (%): " + base::NumberToString(percent_usage_user),
  126. log_lines[11]);
  127. EXPECT_EQ("Usage System (%): " + base::NumberToString(percent_usage_system),
  128. log_lines[12]);
  129. EXPECT_EQ("Usage Free (%): " + base::NumberToString(percent_usage_free),
  130. log_lines[13]);
  131. }
  132. } // namespace diagnostics
  133. } // namespace ash