routine_log_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/routine_log.h"
  5. #include <string>
  6. #include <vector>
  7. #include "ash/system/diagnostics/log_test_helpers.h"
  8. #include "ash/webui/diagnostics_ui/mojom/system_routine_controller.mojom.h"
  9. #include "base/files/file_util.h"
  10. #include "base/files/scoped_temp_dir.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/time/clock.h"
  14. #include "base/time/time.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace ash {
  17. namespace diagnostics {
  18. namespace {
  19. const char kLogFileName[] = "diagnostic_routine_log";
  20. } // namespace
  21. class RoutineLogTest : public testing::Test {
  22. public:
  23. RoutineLogTest() {
  24. EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
  25. log_path_ = temp_dir_.GetPath().AppendASCII(kLogFileName);
  26. }
  27. ~RoutineLogTest() override { base::RunLoop().RunUntilIdle(); }
  28. protected:
  29. base::test::TaskEnvironment task_environment_{
  30. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  31. base::ScopedTempDir temp_dir_;
  32. base::FilePath log_path_;
  33. };
  34. TEST_F(RoutineLogTest, Empty) {
  35. RoutineLog log(log_path_);
  36. // Ensure pending tasks complete.
  37. task_environment_.RunUntilIdle();
  38. EXPECT_FALSE(base::PathExists(log_path_));
  39. EXPECT_TRUE(
  40. log.GetContentsForCategory(RoutineLog::RoutineCategory::kSystem).empty());
  41. }
  42. TEST_F(RoutineLogTest, Basic) {
  43. RoutineLog log(log_path_);
  44. log.LogRoutineStarted(mojom::RoutineType::kCpuStress);
  45. // Ensure pending tasks complete.
  46. task_environment_.RunUntilIdle();
  47. EXPECT_TRUE(base::PathExists(log_path_));
  48. const std::string contents =
  49. log.GetContentsForCategory(RoutineLog::RoutineCategory::kSystem);
  50. const std::string first_line = GetLogLines(contents)[0];
  51. const std::vector<std::string> first_line_contents =
  52. GetLogLineContents(first_line);
  53. ASSERT_EQ(3u, first_line_contents.size());
  54. EXPECT_EQ("CpuStress", first_line_contents[1]);
  55. EXPECT_EQ("Started", first_line_contents[2]);
  56. }
  57. TEST_F(RoutineLogTest, TwoLine) {
  58. RoutineLog log(log_path_);
  59. log.LogRoutineStarted(mojom::RoutineType::kMemory);
  60. log.LogRoutineCompleted(mojom::RoutineType::kMemory,
  61. mojom::StandardRoutineResult::kTestPassed);
  62. // Ensure pending tasks complete.
  63. task_environment_.RunUntilIdle();
  64. EXPECT_TRUE(base::PathExists(log_path_));
  65. const std::string contents =
  66. log.GetContentsForCategory(RoutineLog::RoutineCategory::kSystem);
  67. const std::vector<std::string> log_lines = GetLogLines(contents);
  68. const std::string first_line = log_lines[0];
  69. const std::vector<std::string> first_line_contents =
  70. GetLogLineContents(first_line);
  71. ASSERT_EQ(3u, first_line_contents.size());
  72. EXPECT_EQ("Memory", first_line_contents[1]);
  73. EXPECT_EQ("Started", first_line_contents[2]);
  74. const std::string second_line = log_lines[1];
  75. const std::vector<std::string> second_line_contents =
  76. GetLogLineContents(second_line);
  77. ASSERT_EQ(3u, second_line_contents.size());
  78. EXPECT_EQ("Memory", second_line_contents[1]);
  79. EXPECT_EQ("Passed", second_line_contents[2]);
  80. }
  81. TEST_F(RoutineLogTest, Cancelled) {
  82. RoutineLog log(log_path_);
  83. log.LogRoutineStarted(mojom::RoutineType::kMemory);
  84. log.LogRoutineCancelled(mojom::RoutineType::kMemory);
  85. // Ensure pending tasks complete.
  86. task_environment_.RunUntilIdle();
  87. EXPECT_TRUE(base::PathExists(log_path_));
  88. const std::string contents =
  89. log.GetContentsForCategory(RoutineLog::RoutineCategory::kSystem);
  90. LOG(ERROR) << contents;
  91. const std::vector<std::string> log_lines = GetLogLines(contents);
  92. ASSERT_EQ(2u, log_lines.size());
  93. const std::string second_line = log_lines[1];
  94. const std::vector<std::string> second_line_contents =
  95. GetLogLineContents(second_line);
  96. ASSERT_EQ(2u, second_line_contents.size());
  97. EXPECT_EQ("Inflight Routine Cancelled", second_line_contents[1]);
  98. }
  99. } // namespace diagnostics
  100. } // namespace ash