background_snapshot_controller_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2018 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/offline_pages/core/background_snapshot_controller.h"
  5. #include "base/bind.h"
  6. #include "base/run_loop.h"
  7. #include "base/task/single_thread_task_runner.h"
  8. #include "base/test/test_mock_time_task_runner.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "base/time/time.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace offline_pages {
  13. class BackgroundSnapshotControllerTest
  14. : public testing::Test,
  15. public BackgroundSnapshotController::Client {
  16. public:
  17. BackgroundSnapshotControllerTest();
  18. ~BackgroundSnapshotControllerTest() override;
  19. BackgroundSnapshotController* controller() { return controller_.get(); }
  20. int snapshot_count() { return snapshot_count_; }
  21. // testing::Test
  22. void SetUp() override;
  23. void TearDown() override;
  24. // BackgroundSnapshotController::Client
  25. void StartSnapshot() override;
  26. // Utility methods.
  27. // Runs until all of the tasks that are not delayed are gone from the task
  28. // queue.
  29. void PumpLoop();
  30. // Fast-forwards virtual time by |delta|, causing tasks with a remaining
  31. // delay less than or equal to |delta| to be executed.
  32. void FastForwardBy(base::TimeDelta delta);
  33. private:
  34. std::unique_ptr<BackgroundSnapshotController> controller_;
  35. scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
  36. bool snapshot_started_;
  37. int snapshot_count_;
  38. };
  39. BackgroundSnapshotControllerTest::BackgroundSnapshotControllerTest()
  40. : task_runner_(new base::TestMockTimeTaskRunner),
  41. snapshot_started_(true),
  42. snapshot_count_(0) {}
  43. BackgroundSnapshotControllerTest::~BackgroundSnapshotControllerTest() {}
  44. void BackgroundSnapshotControllerTest::SetUp() {
  45. controller_ =
  46. std::make_unique<BackgroundSnapshotController>(task_runner_, this, false);
  47. snapshot_started_ = true;
  48. }
  49. void BackgroundSnapshotControllerTest::TearDown() {
  50. controller_.reset();
  51. }
  52. void BackgroundSnapshotControllerTest::StartSnapshot() {
  53. snapshot_count_++;
  54. }
  55. void BackgroundSnapshotControllerTest::PumpLoop() {
  56. task_runner_->RunUntilIdle();
  57. }
  58. void BackgroundSnapshotControllerTest::FastForwardBy(base::TimeDelta delta) {
  59. task_runner_->FastForwardBy(delta);
  60. }
  61. TEST_F(BackgroundSnapshotControllerTest, OnLoad) {
  62. // Onload should make snapshot after its delay.
  63. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  64. PumpLoop();
  65. EXPECT_EQ(0, snapshot_count());
  66. FastForwardBy(base::Milliseconds(
  67. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  68. EXPECT_EQ(1, snapshot_count());
  69. }
  70. TEST_F(BackgroundSnapshotControllerTest, Stop) {
  71. // OnDOM should make snapshot after a delay.
  72. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  73. PumpLoop();
  74. EXPECT_EQ(0, snapshot_count());
  75. controller()->Stop();
  76. FastForwardBy(base::Milliseconds(
  77. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  78. // Should not start snapshots
  79. EXPECT_EQ(0, snapshot_count());
  80. // Also should not start snapshot.
  81. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  82. EXPECT_EQ(0, snapshot_count());
  83. }
  84. // This simulated a Reset while there is ongoing snapshot, which is reported
  85. // as done later. That reporting should have no effect nor crash.
  86. TEST_F(BackgroundSnapshotControllerTest, ClientReset) {
  87. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  88. FastForwardBy(base::Milliseconds(
  89. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  90. EXPECT_EQ(1, snapshot_count());
  91. // This normally happens when navigation starts.
  92. controller()->Reset();
  93. // Next snapshot should be initiated when new document is loaded.
  94. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  95. FastForwardBy(base::Milliseconds(
  96. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  97. // No snapshot since session was reset.
  98. EXPECT_EQ(2, snapshot_count());
  99. }
  100. } // namespace offline_pages