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

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

Issue 1920603002: SnapshotController implementation. It will be used in WebContentsObservers for Offline Pages - to d… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: renaming constant Created 4 years, 7 months 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
« no previous file with comments | « components/offline_pages/snapshot_controller.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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 void set_snapshot_started(bool started) { snapshot_started_ = started; }
26 int snapshot_count() { return snapshot_count_; }
27
28 // testing::Test
29 void SetUp() override;
30 void TearDown() override;
31
32 // SnapshotController::Client
33 bool StartSnapshot() override;
34
35 // Utility methods.
36 // Runs until all of the tasks that are not delayed are gone from the task
37 // queue.
38 void PumpLoop();
39 // Fast-forwards virtual time by |delta|, causing tasks with a remaining
40 // delay less than or equal to |delta| to be executed.
41 void FastForwardBy(base::TimeDelta delta);
42
43 private:
44 std::unique_ptr<SnapshotController> controller_;
45 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
46 bool snapshot_started_;
47 int snapshot_count_;
48 };
49
50 SnapshotControllerTest::SnapshotControllerTest()
51 : task_runner_(new base::TestMockTimeTaskRunner),
52 snapshot_started_(true),
53 snapshot_count_(0) {
54 }
55
56 SnapshotControllerTest::~SnapshotControllerTest() {
57 }
58
59 void SnapshotControllerTest::SetUp() {
60 controller_.reset(new SnapshotController(task_runner_, this));
61 snapshot_started_ = true;
62 }
63
64 void SnapshotControllerTest::TearDown() {
65 controller_.reset();
66 }
67
68 bool SnapshotControllerTest::StartSnapshot() {
69 snapshot_count_++;
70 return snapshot_started_;
71 }
72
73 void SnapshotControllerTest::PumpLoop() {
74 task_runner_->RunUntilIdle();
75 }
76
77 void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) {
78 task_runner_->FastForwardBy(delta);
79 }
80
81 TEST_F(SnapshotControllerTest, OnLoad) {
82 // Onload should make snapshot right away.
83 EXPECT_EQ(0, snapshot_count());
84 controller()->DocumentOnLoadCompletedInMainFrame();
85 PumpLoop();
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 // OnDOM should make snapshot after a delay.
102 controller()->DocumentAvailableInMainFrame();
103 PumpLoop();
104 EXPECT_EQ(0, snapshot_count());
105 // This should start snapshot immediately.
106 controller()->DocumentOnLoadCompletedInMainFrame();
107 EXPECT_EQ(1, snapshot_count());
108 // Report that snapshot is completed.
109 controller()->PendingSnapshotCompleted();
110 // Even though previous snapshot is completed, new one should not start
111 // when this delay expires.
112 FastForwardBy(base::TimeDelta::FromMilliseconds(
113 controller()->GetDelayAfterDocumentAvailableForTest()));
114 EXPECT_EQ(1, snapshot_count());
115 }
116
117 TEST_F(SnapshotControllerTest, OnLoadSnapshotAfterLongDelay) {
118 // OnDOM should make snapshot after a delay.
119 controller()->DocumentAvailableInMainFrame();
120 PumpLoop();
121 EXPECT_EQ(0, snapshot_count());
122 FastForwardBy(base::TimeDelta::FromMilliseconds(
123 controller()->GetDelayAfterDocumentAvailableForTest()));
124 EXPECT_EQ(1, snapshot_count());
125 // Report that snapshot is completed.
126 controller()->PendingSnapshotCompleted();
127 // This should start snapshot immediately.
128 controller()->DocumentOnLoadCompletedInMainFrame();
129 EXPECT_EQ(2, snapshot_count());
130 }
131
132 TEST_F(SnapshotControllerTest, Stop) {
133 // OnDOM should make snapshot after a delay.
134 controller()->DocumentAvailableInMainFrame();
135 PumpLoop();
136 EXPECT_EQ(0, snapshot_count());
137 controller()->Stop();
138 FastForwardBy(base::TimeDelta::FromMilliseconds(
139 controller()->GetDelayAfterDocumentAvailableForTest()));
140 // Should not start snapshots
141 EXPECT_EQ(0, snapshot_count());
142 // Also should not start snapshot.
143 controller()->DocumentOnLoadCompletedInMainFrame();
144 EXPECT_EQ(0, snapshot_count());
145 }
146
147 TEST_F(SnapshotControllerTest, ClientDidntStartSnapshot) {
148 // This will tell that Client did not start the snapshot
149 set_snapshot_started(false);
150 controller()->DocumentAvailableInMainFrame();
151 FastForwardBy(base::TimeDelta::FromMilliseconds(
152 controller()->GetDelayAfterDocumentAvailableForTest()));
153 // Should have one snapshot requested, but not reported started.
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());
158 }
159
160 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/snapshot_controller.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698