snapshot_controller_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2016 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/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 "components/offline_pages/core/offline_page_feature.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace offline_pages {
  14. class SnapshotControllerTest : public testing::Test,
  15. public SnapshotController::Client {
  16. public:
  17. SnapshotControllerTest();
  18. ~SnapshotControllerTest() override;
  19. SnapshotController* controller() { return controller_.get(); }
  20. int snapshot_count() { return snapshot_count_; }
  21. // testing::Test
  22. void SetUp() override;
  23. void TearDown() override;
  24. // SnapshotController::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<SnapshotController> controller_;
  35. scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
  36. bool snapshot_started_;
  37. int snapshot_count_;
  38. };
  39. SnapshotControllerTest::SnapshotControllerTest()
  40. : task_runner_(new base::TestMockTimeTaskRunner),
  41. snapshot_started_(true),
  42. snapshot_count_(0) {}
  43. SnapshotControllerTest::~SnapshotControllerTest() {}
  44. void SnapshotControllerTest::SetUp() {
  45. controller_ = std::make_unique<SnapshotController>(task_runner_, this);
  46. snapshot_started_ = true;
  47. }
  48. void SnapshotControllerTest::TearDown() {
  49. controller_.reset();
  50. }
  51. void SnapshotControllerTest::StartSnapshot() {
  52. snapshot_count_++;
  53. }
  54. void SnapshotControllerTest::PumpLoop() {
  55. task_runner_->RunUntilIdle();
  56. }
  57. void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) {
  58. task_runner_->FastForwardBy(delta);
  59. }
  60. TEST_F(SnapshotControllerTest, OnLoad) {
  61. // Onload should make snapshot after its delay.
  62. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  63. PumpLoop();
  64. EXPECT_EQ(0, snapshot_count());
  65. FastForwardBy(base::Milliseconds(
  66. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  67. EXPECT_EQ(1, snapshot_count());
  68. }
  69. TEST_F(SnapshotControllerTest, OnDocumentAvailable) {
  70. EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(), 0LL);
  71. // OnDOM should make snapshot after a delay.
  72. controller()->PrimaryMainDocumentElementAvailable();
  73. PumpLoop();
  74. EXPECT_EQ(0, snapshot_count());
  75. FastForwardBy(base::Milliseconds(
  76. controller()->GetDelayAfterDocumentAvailableForTest()));
  77. EXPECT_EQ(1, snapshot_count());
  78. }
  79. TEST_F(SnapshotControllerTest, OnLoadSnapshotIsTheLastOne) {
  80. // This test assumes DocumentAvailable delay is longer than OnLoadCompleted.
  81. EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(),
  82. controller()->GetDelayAfterDocumentOnLoadCompletedForTest());
  83. // OnDOM should make snapshot after a delay.
  84. controller()->PrimaryMainDocumentElementAvailable();
  85. PumpLoop();
  86. EXPECT_EQ(0, snapshot_count());
  87. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  88. // Advance time to OnLoadCompleted delay to trigger snapshot.
  89. FastForwardBy(base::Milliseconds(
  90. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  91. EXPECT_EQ(1, snapshot_count());
  92. // Report that snapshot is completed.
  93. controller()->PendingSnapshotCompleted();
  94. // Even though previous snapshot is completed, new one should not start
  95. // when this DocumentAvailable delay expires.
  96. FastForwardBy(base::Milliseconds(
  97. controller()->GetDelayAfterDocumentAvailableForTest()));
  98. EXPECT_EQ(1, snapshot_count());
  99. }
  100. TEST_F(SnapshotControllerTest, OnLoadSnapshotAfterLongDelay) {
  101. // OnDOM should make snapshot after a delay.
  102. controller()->PrimaryMainDocumentElementAvailable();
  103. PumpLoop();
  104. EXPECT_EQ(0, snapshot_count());
  105. FastForwardBy(base::Milliseconds(
  106. controller()->GetDelayAfterDocumentAvailableForTest()));
  107. EXPECT_EQ(1, snapshot_count());
  108. // Report that snapshot is completed.
  109. controller()->PendingSnapshotCompleted();
  110. // OnLoad should make 2nd snapshot after its delay.
  111. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  112. FastForwardBy(base::Milliseconds(
  113. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  114. EXPECT_EQ(2, snapshot_count());
  115. }
  116. TEST_F(SnapshotControllerTest, Stop) {
  117. // OnDOM should make snapshot after a delay.
  118. controller()->PrimaryMainDocumentElementAvailable();
  119. PumpLoop();
  120. EXPECT_EQ(0, snapshot_count());
  121. controller()->Stop();
  122. FastForwardBy(base::Milliseconds(
  123. controller()->GetDelayAfterDocumentAvailableForTest()));
  124. // Should not start snapshots
  125. EXPECT_EQ(0, snapshot_count());
  126. // Also should not start snapshot.
  127. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  128. EXPECT_EQ(0, snapshot_count());
  129. }
  130. TEST_F(SnapshotControllerTest, ClientReset) {
  131. controller()->PrimaryMainDocumentElementAvailable();
  132. controller()->Reset();
  133. FastForwardBy(base::Milliseconds(
  134. controller()->GetDelayAfterDocumentAvailableForTest()));
  135. // No snapshot since session was reset.
  136. EXPECT_EQ(0, snapshot_count());
  137. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  138. FastForwardBy(base::Milliseconds(
  139. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  140. EXPECT_EQ(1, snapshot_count());
  141. controller()->Reset();
  142. controller()->PrimaryMainDocumentElementAvailable();
  143. FastForwardBy(base::Milliseconds(
  144. controller()->GetDelayAfterDocumentAvailableForTest()));
  145. // No snapshot since session was reset.
  146. EXPECT_EQ(2, snapshot_count());
  147. }
  148. // This simulated a Reset while there is ongoing snapshot, which is reported
  149. // as done later. That reporting should have no effect nor crash.
  150. TEST_F(SnapshotControllerTest, ClientResetWhileSnapshotting) {
  151. controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
  152. FastForwardBy(base::Milliseconds(
  153. controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
  154. EXPECT_EQ(1, snapshot_count());
  155. // This normally happens when navigation starts.
  156. controller()->Reset();
  157. controller()->PendingSnapshotCompleted();
  158. // Next snapshot should be initiated when new document is loaded.
  159. controller()->PrimaryMainDocumentElementAvailable();
  160. FastForwardBy(base::Milliseconds(
  161. controller()->GetDelayAfterDocumentAvailableForTest()));
  162. // No snapshot since session was reset.
  163. EXPECT_EQ(2, snapshot_count());
  164. }
  165. } // namespace offline_pages