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

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

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

Powered by Google App Engine
This is Rietveld 408576698