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

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

Issue 2380093002: [Offline Pages] SnapshotController support for delay after onLoadCompleted and also parameterized c… (Closed)
Patch Set: Fixed a comment Created 4 years, 2 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
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"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 70
71 void SnapshotControllerTest::PumpLoop() { 71 void SnapshotControllerTest::PumpLoop() {
72 task_runner_->RunUntilIdle(); 72 task_runner_->RunUntilIdle();
73 } 73 }
74 74
75 void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) { 75 void SnapshotControllerTest::FastForwardBy(base::TimeDelta delta) {
76 task_runner_->FastForwardBy(delta); 76 task_runner_->FastForwardBy(delta);
77 } 77 }
78 78
79 TEST_F(SnapshotControllerTest, OnLoad) { 79 TEST_F(SnapshotControllerTest, OnLoad) {
80 // Onload should make snapshot right away. 80 // Onload should make snapshot after its delay.
81 EXPECT_EQ(0, snapshot_count());
82 controller()->DocumentOnLoadCompletedInMainFrame(); 81 controller()->DocumentOnLoadCompletedInMainFrame();
83 PumpLoop(); 82 PumpLoop();
83 EXPECT_EQ(0, snapshot_count());
84 FastForwardBy(base::TimeDelta::FromMilliseconds(
85 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
84 EXPECT_EQ(1, snapshot_count()); 86 EXPECT_EQ(1, snapshot_count());
85 } 87 }
86 88
87 TEST_F(SnapshotControllerTest, OnDocumentAvailable) { 89 TEST_F(SnapshotControllerTest, OnDocumentAvailable) {
88 EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(), 0UL); 90 EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(), 0UL);
89 // OnDOM should make snapshot after a delay. 91 // OnDOM should make snapshot after a delay.
90 controller()->DocumentAvailableInMainFrame(); 92 controller()->DocumentAvailableInMainFrame();
91 PumpLoop(); 93 PumpLoop();
92 EXPECT_EQ(0, snapshot_count()); 94 EXPECT_EQ(0, snapshot_count());
93 FastForwardBy(base::TimeDelta::FromMilliseconds( 95 FastForwardBy(base::TimeDelta::FromMilliseconds(
94 controller()->GetDelayAfterDocumentAvailableForTest())); 96 controller()->GetDelayAfterDocumentAvailableForTest()));
95 EXPECT_EQ(1, snapshot_count()); 97 EXPECT_EQ(1, snapshot_count());
96 } 98 }
97 99
98 TEST_F(SnapshotControllerTest, OnLoadSnapshotIsTheLastOne) { 100 TEST_F(SnapshotControllerTest, OnLoadSnapshotIsTheLastOne) {
101 // This test assumes DocumentAvailable delay is longer than OnLoadCompleted.
102 EXPECT_GT(controller()->GetDelayAfterDocumentAvailableForTest(),
103 controller()->GetDelayAfterDocumentOnLoadCompletedForTest());
99 // OnDOM should make snapshot after a delay. 104 // OnDOM should make snapshot after a delay.
100 controller()->DocumentAvailableInMainFrame(); 105 controller()->DocumentAvailableInMainFrame();
101 PumpLoop(); 106 PumpLoop();
102 EXPECT_EQ(0, snapshot_count()); 107 EXPECT_EQ(0, snapshot_count());
103 // This should start snapshot immediately.
104 controller()->DocumentOnLoadCompletedInMainFrame(); 108 controller()->DocumentOnLoadCompletedInMainFrame();
109 // Advance time to OnLoadCompleted delay to trigger snapshot.
110 FastForwardBy(base::TimeDelta::FromMilliseconds(
111 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
105 EXPECT_EQ(1, snapshot_count()); 112 EXPECT_EQ(1, snapshot_count());
106 // Report that snapshot is completed. 113 // Report that snapshot is completed.
107 controller()->PendingSnapshotCompleted(); 114 controller()->PendingSnapshotCompleted();
108 // Even though previous snapshot is completed, new one should not start 115 // Even though previous snapshot is completed, new one should not start
109 // when this delay expires. 116 // when this DocumentAvailable delay expires.
110 FastForwardBy(base::TimeDelta::FromMilliseconds( 117 FastForwardBy(base::TimeDelta::FromMilliseconds(
111 controller()->GetDelayAfterDocumentAvailableForTest())); 118 controller()->GetDelayAfterDocumentAvailableForTest()));
112 EXPECT_EQ(1, snapshot_count()); 119 EXPECT_EQ(1, snapshot_count());
113 } 120 }
114 121
115 TEST_F(SnapshotControllerTest, OnLoadSnapshotAfterLongDelay) { 122 TEST_F(SnapshotControllerTest, OnLoadSnapshotAfterLongDelay) {
116 // OnDOM should make snapshot after a delay. 123 // OnDOM should make snapshot after a delay.
117 controller()->DocumentAvailableInMainFrame(); 124 controller()->DocumentAvailableInMainFrame();
118 PumpLoop(); 125 PumpLoop();
119 EXPECT_EQ(0, snapshot_count()); 126 EXPECT_EQ(0, snapshot_count());
120 FastForwardBy(base::TimeDelta::FromMilliseconds( 127 FastForwardBy(base::TimeDelta::FromMilliseconds(
121 controller()->GetDelayAfterDocumentAvailableForTest())); 128 controller()->GetDelayAfterDocumentAvailableForTest()));
122 EXPECT_EQ(1, snapshot_count()); 129 EXPECT_EQ(1, snapshot_count());
123 // Report that snapshot is completed. 130 // Report that snapshot is completed.
124 controller()->PendingSnapshotCompleted(); 131 controller()->PendingSnapshotCompleted();
125 // This should start snapshot immediately. 132 // OnLoad should make 2nd snapshot after its delay.
126 controller()->DocumentOnLoadCompletedInMainFrame(); 133 controller()->DocumentOnLoadCompletedInMainFrame();
134 FastForwardBy(base::TimeDelta::FromMilliseconds(
135 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
127 EXPECT_EQ(2, snapshot_count()); 136 EXPECT_EQ(2, snapshot_count());
128 } 137 }
129 138
130 TEST_F(SnapshotControllerTest, Stop) { 139 TEST_F(SnapshotControllerTest, Stop) {
131 // OnDOM should make snapshot after a delay. 140 // OnDOM should make snapshot after a delay.
132 controller()->DocumentAvailableInMainFrame(); 141 controller()->DocumentAvailableInMainFrame();
133 PumpLoop(); 142 PumpLoop();
134 EXPECT_EQ(0, snapshot_count()); 143 EXPECT_EQ(0, snapshot_count());
135 controller()->Stop(); 144 controller()->Stop();
136 FastForwardBy(base::TimeDelta::FromMilliseconds( 145 FastForwardBy(base::TimeDelta::FromMilliseconds(
137 controller()->GetDelayAfterDocumentAvailableForTest())); 146 controller()->GetDelayAfterDocumentAvailableForTest()));
138 // Should not start snapshots 147 // Should not start snapshots
139 EXPECT_EQ(0, snapshot_count()); 148 EXPECT_EQ(0, snapshot_count());
140 // Also should not start snapshot. 149 // Also should not start snapshot.
141 controller()->DocumentOnLoadCompletedInMainFrame(); 150 controller()->DocumentOnLoadCompletedInMainFrame();
142 EXPECT_EQ(0, snapshot_count()); 151 EXPECT_EQ(0, snapshot_count());
143 } 152 }
144 153
145 TEST_F(SnapshotControllerTest, ClientReset) { 154 TEST_F(SnapshotControllerTest, ClientReset) {
146 controller()->DocumentAvailableInMainFrame(); 155 controller()->DocumentAvailableInMainFrame();
147 156
148 controller()->Reset(); 157 controller()->Reset();
149 FastForwardBy(base::TimeDelta::FromMilliseconds( 158 FastForwardBy(base::TimeDelta::FromMilliseconds(
150 controller()->GetDelayAfterDocumentAvailableForTest())); 159 controller()->GetDelayAfterDocumentAvailableForTest()));
151 // No snapshot since session was reset. 160 // No snapshot since session was reset.
152 EXPECT_EQ(0, snapshot_count()); 161 EXPECT_EQ(0, snapshot_count());
153 controller()->DocumentOnLoadCompletedInMainFrame(); 162 controller()->DocumentOnLoadCompletedInMainFrame();
163 FastForwardBy(base::TimeDelta::FromMilliseconds(
164 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
154 EXPECT_EQ(1, snapshot_count()); 165 EXPECT_EQ(1, snapshot_count());
155 166
156 controller()->Reset(); 167 controller()->Reset();
157 controller()->DocumentAvailableInMainFrame(); 168 controller()->DocumentAvailableInMainFrame();
158 FastForwardBy(base::TimeDelta::FromMilliseconds( 169 FastForwardBy(base::TimeDelta::FromMilliseconds(
159 controller()->GetDelayAfterDocumentAvailableForTest())); 170 controller()->GetDelayAfterDocumentAvailableForTest()));
160 // No snapshot since session was reset. 171 // No snapshot since session was reset.
161 EXPECT_EQ(2, snapshot_count()); 172 EXPECT_EQ(2, snapshot_count());
162 } 173 }
163 174
164 // This simulated a Reset while there is ongoing snapshot, which is reported 175 // This simulated a Reset while there is ongoing snapshot, which is reported
165 // as done later. That reporting should have no effect nor crash. 176 // as done later. That reporting should have no effect nor crash.
166 TEST_F(SnapshotControllerTest, ClientResetWhileSnapshotting) { 177 TEST_F(SnapshotControllerTest, ClientResetWhileSnapshotting) {
167 controller()->DocumentOnLoadCompletedInMainFrame(); 178 controller()->DocumentOnLoadCompletedInMainFrame();
179 FastForwardBy(base::TimeDelta::FromMilliseconds(
180 controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
168 EXPECT_EQ(1, snapshot_count()); 181 EXPECT_EQ(1, snapshot_count());
169 // This normally happens when navigation starts. 182 // This normally happens when navigation starts.
170 controller()->Reset(); 183 controller()->Reset();
171 controller()->PendingSnapshotCompleted(); 184 controller()->PendingSnapshotCompleted();
172 // Next snapshot should be initiated when new document is loaded. 185 // Next snapshot should be initiated when new document is loaded.
173 controller()->DocumentAvailableInMainFrame(); 186 controller()->DocumentAvailableInMainFrame();
174 FastForwardBy(base::TimeDelta::FromMilliseconds( 187 FastForwardBy(base::TimeDelta::FromMilliseconds(
175 controller()->GetDelayAfterDocumentAvailableForTest())); 188 controller()->GetDelayAfterDocumentAvailableForTest()));
176 // No snapshot since session was reset. 189 // No snapshot since session was reset.
177 EXPECT_EQ(2, snapshot_count()); 190 EXPECT_EQ(2, snapshot_count());
178 } 191 }
179 192
180 } // namespace offline_pages 193 } // 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