Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: components/offline_pages/snapshot_controller_unittest.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "components/offline_pages/snapshot_controller.h"
6
7 #include "base/bind.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/test/test_mock_time_task_runner.h"
11 #include "base/threading/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace offline_pages {
16
17 class SnapshotControllerTest
18 : public testing::Test,
19 public SnapshotController::Client {
20 public:
21 SnapshotControllerTest();
22 ~SnapshotControllerTest() override;
23
24 SnapshotController* controller() { return controller_.get(); }
25 int snapshot_count() { return snapshot_count_; }
26
27 // testing::Test
28 void SetUp() override;
29 void TearDown() override;
30
31 // SnapshotController::Client
32 void StartSnapshot() override;
33
34 // Utility methods.
35 // Runs until all of the tasks that are not delayed are gone from the task
36 // queue.
37 void PumpLoop();
38 // Fast-forwards virtual time by |delta|, causing tasks with a remaining
39 // delay less than or equal to |delta| to be executed.
40 void FastForwardBy(base::TimeDelta delta);
41
42 private:
43 std::unique_ptr<SnapshotController> controller_;
44 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
45 bool snapshot_started_;
46 int snapshot_count_;
47 };
48
49 SnapshotControllerTest::SnapshotControllerTest()
50 : task_runner_(new base::TestMockTimeTaskRunner),
51 snapshot_started_(true),
52 snapshot_count_(0) {
53 }
54
55 SnapshotControllerTest::~SnapshotControllerTest() {
56 }
57
58 void SnapshotControllerTest::SetUp() {
59 controller_.reset(new SnapshotController(task_runner_, this));
60 snapshot_started_ = true;
61 }
62
63 void SnapshotControllerTest::TearDown() {
64 controller_.reset();
65 }
66
67 void SnapshotControllerTest::StartSnapshot() {
68 snapshot_count_++;
69 }
70
71 void SnapshotControllerTest::PumpLoop() {
72 task_runner_->RunUntilIdle();
73 }
74
75 void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) {
76 task_runner_->FastForwardBy(delta);
77 }
78
79 TEST_F(SnapshotControllerTest, OnLoad) {
80 // Onload should make snapshot after its delay.
81 controller()->DocumentOnLoadCompletedInMainFrame();
82 PumpLoop();
83 EXPECT_EQ(0, snapshot_count());
84 FastForwardBy(base::TimeDelta::FromMilliseconds(
85 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
86 EXPECT_EQ(1, snapshot_count());
87 }
88
89 TEST_F(SnapshotControllerTest, OnDocumentAvailable) {
90 EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(), 0UL);
91 // OnDOM should make snapshot after a delay.
92 controller()->DocumentAvailableInMainFrame();
93 PumpLoop();
94 EXPECT_EQ(0, snapshot_count());
95 FastForwardBy(base::TimeDelta::FromMilliseconds(
96 controller()->GetDelayAfterDocumentAvailableForTest()));
97 EXPECT_EQ(1, snapshot_count());
98 }
99
100 TEST_F(SnapshotControllerTest, OnLoadSnapshotIsTheLastOne) {
101 // This test assumes DocumentAvailable delay is longer than OnLoadCompleted.
102 EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(),
103 controller()->GetDelayAfterDocumentOnLoadCompletedForTest());
104 // OnDOM should make snapshot after a delay.
105 controller()->DocumentAvailableInMainFrame();
106 PumpLoop();
107 EXPECT_EQ(0, snapshot_count());
108 controller()->DocumentOnLoadCompletedInMainFrame();
109 // Advance time to OnLoadCompleted delay to trigger snapshot.
110 FastForwardBy(base::TimeDelta::FromMilliseconds(
111 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
112 EXPECT_EQ(1, snapshot_count());
113 // Report that snapshot is completed.
114 controller()->PendingSnapshotCompleted();
115 // Even though previous snapshot is completed, new one should not start
116 // when this DocumentAvailable delay expires.
117 FastForwardBy(base::TimeDelta::FromMilliseconds(
118 controller()->GetDelayAfterDocumentAvailableForTest()));
119 EXPECT_EQ(1, snapshot_count());
120 }
121
122 TEST_F(SnapshotControllerTest, OnLoadSnapshotAfterLongDelay) {
123 // OnDOM should make snapshot after a delay.
124 controller()->DocumentAvailableInMainFrame();
125 PumpLoop();
126 EXPECT_EQ(0, snapshot_count());
127 FastForwardBy(base::TimeDelta::FromMilliseconds(
128 controller()->GetDelayAfterDocumentAvailableForTest()));
129 EXPECT_EQ(1, snapshot_count());
130 // Report that snapshot is completed.
131 controller()->PendingSnapshotCompleted();
132 // OnLoad should make 2nd snapshot after its delay.
133 controller()->DocumentOnLoadCompletedInMainFrame();
134 FastForwardBy(base::TimeDelta::FromMilliseconds(
135 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
136 EXPECT_EQ(2, snapshot_count());
137 }
138
139 TEST_F(SnapshotControllerTest, Stop) {
140 // OnDOM should make snapshot after a delay.
141 controller()->DocumentAvailableInMainFrame();
142 PumpLoop();
143 EXPECT_EQ(0, snapshot_count());
144 controller()->Stop();
145 FastForwardBy(base::TimeDelta::FromMilliseconds(
146 controller()->GetDelayAfterDocumentAvailableForTest()));
147 // Should not start snapshots
148 EXPECT_EQ(0, snapshot_count());
149 // Also should not start snapshot.
150 controller()->DocumentOnLoadCompletedInMainFrame();
151 EXPECT_EQ(0, snapshot_count());
152 }
153
154 TEST_F(SnapshotControllerTest, ClientReset) {
155 controller()->DocumentAvailableInMainFrame();
156
157 controller()->Reset();
158 FastForwardBy(base::TimeDelta::FromMilliseconds(
159 controller()->GetDelayAfterDocumentAvailableForTest()));
160 // No snapshot since session was reset.
161 EXPECT_EQ(0, snapshot_count());
162 controller()->DocumentOnLoadCompletedInMainFrame();
163 FastForwardBy(base::TimeDelta::FromMilliseconds(
164 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
165 EXPECT_EQ(1, snapshot_count());
166
167 controller()->Reset();
168 controller()->DocumentAvailableInMainFrame();
169 FastForwardBy(base::TimeDelta::FromMilliseconds(
170 controller()->GetDelayAfterDocumentAvailableForTest()));
171 // No snapshot since session was reset.
172 EXPECT_EQ(2, snapshot_count());
173 }
174
175 // This simulated a Reset while there is ongoing snapshot, which is reported
176 // as done later. That reporting should have no effect nor crash.
177 TEST_F(SnapshotControllerTest, ClientResetWhileSnapshotting) {
178 controller()->DocumentOnLoadCompletedInMainFrame();
179 FastForwardBy(base::TimeDelta::FromMilliseconds(
180 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
181 EXPECT_EQ(1, snapshot_count());
182 // This normally happens when navigation starts.
183 controller()->Reset();
184 controller()->PendingSnapshotCompleted();
185 // Next snapshot should be initiated when new document is loaded.
186 controller()->DocumentAvailableInMainFrame();
187 FastForwardBy(base::TimeDelta::FromMilliseconds(
188 controller()->GetDelayAfterDocumentAvailableForTest()));
189 // No snapshot since session was reset.
190 EXPECT_EQ(2, snapshot_count());
191 }
192
193 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/snapshot_controller.cc ('k') | components/offline_pages/stub_offline_page_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698