| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/offline_pages/snapshot_controller.h" | 5 #include "components/offline_pages/snapshot_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/test/test_mock_time_task_runner.h" | 10 #include "base/test/test_mock_time_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace offline_pages { | 15 namespace offline_pages { |
| 16 | 16 |
| 17 class SnapshotControllerTest | 17 class SnapshotControllerTest |
| 18 : public testing::Test, | 18 : public testing::Test, |
| 19 public SnapshotController::Client { | 19 public SnapshotController::Client { |
| 20 public: | 20 public: |
| 21 SnapshotControllerTest(); | 21 SnapshotControllerTest(); |
| 22 ~SnapshotControllerTest() override; | 22 ~SnapshotControllerTest() override; |
| 23 | 23 |
| 24 SnapshotController* controller() { return controller_.get(); } | 24 SnapshotController* controller() { return controller_.get(); } |
| 25 void set_snapshot_started(bool started) { snapshot_started_ = started; } | |
| 26 int snapshot_count() { return snapshot_count_; } | 25 int snapshot_count() { return snapshot_count_; } |
| 27 | 26 |
| 28 // testing::Test | 27 // testing::Test |
| 29 void SetUp() override; | 28 void SetUp() override; |
| 30 void TearDown() override; | 29 void TearDown() override; |
| 31 | 30 |
| 32 // SnapshotController::Client | 31 // SnapshotController::Client |
| 33 bool StartSnapshot() override; | 32 void StartSnapshot() override; |
| 34 | 33 |
| 35 // Utility methods. | 34 // Utility methods. |
| 36 // Runs until all of the tasks that are not delayed are gone from the task | 35 // Runs until all of the tasks that are not delayed are gone from the task |
| 37 // queue. | 36 // queue. |
| 38 void PumpLoop(); | 37 void PumpLoop(); |
| 39 // Fast-forwards virtual time by |delta|, causing tasks with a remaining | 38 // Fast-forwards virtual time by |delta|, causing tasks with a remaining |
| 40 // delay less than or equal to |delta| to be executed. | 39 // delay less than or equal to |delta| to be executed. |
| 41 void FastForwardBy(base::TimeDelta delta); | 40 void FastForwardBy(base::TimeDelta delta); |
| 42 | 41 |
| 43 private: | 42 private: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 58 | 57 |
| 59 void SnapshotControllerTest::SetUp() { | 58 void SnapshotControllerTest::SetUp() { |
| 60 controller_.reset(new SnapshotController(task_runner_, this)); | 59 controller_.reset(new SnapshotController(task_runner_, this)); |
| 61 snapshot_started_ = true; | 60 snapshot_started_ = true; |
| 62 } | 61 } |
| 63 | 62 |
| 64 void SnapshotControllerTest::TearDown() { | 63 void SnapshotControllerTest::TearDown() { |
| 65 controller_.reset(); | 64 controller_.reset(); |
| 66 } | 65 } |
| 67 | 66 |
| 68 bool SnapshotControllerTest::StartSnapshot() { | 67 void SnapshotControllerTest::StartSnapshot() { |
| 69 snapshot_count_++; | 68 snapshot_count_++; |
| 70 return snapshot_started_; | |
| 71 } | 69 } |
| 72 | 70 |
| 73 void SnapshotControllerTest::PumpLoop() { | 71 void SnapshotControllerTest::PumpLoop() { |
| 74 task_runner_->RunUntilIdle(); | 72 task_runner_->RunUntilIdle(); |
| 75 } | 73 } |
| 76 | 74 |
| 77 void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) { | 75 void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) { |
| 78 task_runner_->FastForwardBy(delta); | 76 task_runner_->FastForwardBy(delta); |
| 79 } | 77 } |
| 80 | 78 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 controller()->Stop(); | 135 controller()->Stop(); |
| 138 FastForwardBy(base::TimeDelta::FromMilliseconds( | 136 FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 139 controller()->GetDelayAfterDocumentAvailableForTest())); | 137 controller()->GetDelayAfterDocumentAvailableForTest())); |
| 140 // Should not start snapshots | 138 // Should not start snapshots |
| 141 EXPECT_EQ(0, snapshot_count()); | 139 EXPECT_EQ(0, snapshot_count()); |
| 142 // Also should not start snapshot. | 140 // Also should not start snapshot. |
| 143 controller()->DocumentOnLoadCompletedInMainFrame(); | 141 controller()->DocumentOnLoadCompletedInMainFrame(); |
| 144 EXPECT_EQ(0, snapshot_count()); | 142 EXPECT_EQ(0, snapshot_count()); |
| 145 } | 143 } |
| 146 | 144 |
| 147 TEST_F(SnapshotControllerTest, ClientDidntStartSnapshot) { | 145 TEST_F(SnapshotControllerTest, ClientReset) { |
| 148 // This will tell that Client did not start the snapshot | 146 controller()->DocumentAvailableInMainFrame(); |
| 149 set_snapshot_started(false); | 147 |
| 148 controller()->Reset(); |
| 149 FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 150 controller()->GetDelayAfterDocumentAvailableForTest())); |
| 151 // No snapshot since session was reset. |
| 152 EXPECT_EQ(0, snapshot_count()); |
| 153 controller()->DocumentOnLoadCompletedInMainFrame(); |
| 154 EXPECT_EQ(1, snapshot_count()); |
| 155 |
| 156 controller()->Reset(); |
| 150 controller()->DocumentAvailableInMainFrame(); | 157 controller()->DocumentAvailableInMainFrame(); |
| 151 FastForwardBy(base::TimeDelta::FromMilliseconds( | 158 FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 152 controller()->GetDelayAfterDocumentAvailableForTest())); | 159 controller()->GetDelayAfterDocumentAvailableForTest())); |
| 153 // Should have one snapshot requested, but not reported started. | 160 // No snapshot since session was reset. |
| 154 EXPECT_EQ(1, snapshot_count()); | |
| 155 // Should start another snapshot since previous did not start | |
| 156 controller()->DocumentOnLoadCompletedInMainFrame(); | |
| 157 EXPECT_EQ(2, snapshot_count()); | 161 EXPECT_EQ(2, snapshot_count()); |
| 158 } | 162 } |
| 159 | 163 |
| 160 } // namespace offline_pages | 164 } // namespace offline_pages |
| OLD | NEW |