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

Side by Side Diff: chrome/browser/offline_pages/background_loader_offliner_unittest.cc

Issue 2898393002: Split Android-specific dependency from BackgroundLoaderOffliner. Create a subfolder of c/b/offline_… (Closed)
Patch Set: more build fixes Created 3 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
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 "chrome/browser/offline_pages/background_loader_offliner.h" 5 #include "chrome/browser/offline_pages/background_loader_offliner.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/test/histogram_tester.h" 9 #include "base/test/histogram_tester.h"
10 #include "base/test/scoped_feature_list.h" 10 #include "base/test/scoped_feature_list.h"
11 #include "base/test/scoped_mock_time_message_loop_task_runner.h" 11 #include "base/test/scoped_mock_time_message_loop_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
13 #include "chrome/browser/android/offline_pages/offliner_helper.h" 13 #include "chrome/browser/android/offline_pages/offliner_helper.h"
14 #include "chrome/browser/net/prediction_options.h" 14 #include "chrome/browser/net/prediction_options.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "components/content_settings/core/common/pref_names.h" 17 #include "components/content_settings/core/common/pref_names.h"
18 #include "components/offline_pages/content/background_loader/background_loader_c ontents_stub.h" 18 #include "components/offline_pages/content/background_loader/background_loader_c ontents_stub.h"
19 #include "components/offline_pages/core/background/load_termination_listener.h"
19 #include "components/offline_pages/core/background/offliner.h" 20 #include "components/offline_pages/core/background/offliner.h"
20 #include "components/offline_pages/core/background/offliner_policy.h" 21 #include "components/offline_pages/core/background/offliner_policy.h"
21 #include "components/offline_pages/core/background/save_page_request.h" 22 #include "components/offline_pages/core/background/save_page_request.h"
22 #include "components/offline_pages/core/offline_page_feature.h" 23 #include "components/offline_pages/core/offline_page_feature.h"
23 #include "components/offline_pages/core/stub_offline_page_model.h" 24 #include "components/offline_pages/core/stub_offline_page_model.h"
24 #include "components/prefs/pref_service.h" 25 #include "components/prefs/pref_service.h"
25 #include "content/public/browser/mhtml_extra_parts.h" 26 #include "content/public/browser/mhtml_extra_parts.h"
26 #include "content/public/browser/navigation_handle.h" 27 #include "content/public/browser/navigation_handle.h"
27 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/test_browser_thread_bundle.h" 29 #include "content/public/test/test_browser_thread_bundle.h"
29 #include "content/public/test/web_contents_tester.h" 30 #include "content/public/test/web_contents_tester.h"
30 #include "net/base/net_errors.h" 31 #include "net/base/net_errors.h"
31 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
32 33
33 namespace offline_pages { 34 namespace offline_pages {
34 35
35 namespace { 36 namespace {
36 37
37 const int64_t kRequestId = 7; 38 const int64_t kRequestId = 7;
38 const GURL kHttpUrl("http://www.tunafish.com"); 39 const GURL kHttpUrl("http://www.tunafish.com");
39 const GURL kFileUrl("file://salmon.png"); 40 const GURL kFileUrl("file://salmon.png");
40 const ClientId kClientId("async_loading", "88"); 41 const ClientId kClientId("async_loading", "88");
41 const bool kUserRequested = true; 42 const bool kUserRequested = true;
42 43
44 class TestLoadTerminationListener : public LoadTerminationListener {
45 public:
46 TestLoadTerminationListener() = default;
47 ~TestLoadTerminationListener() override = default;
48
49 void TerminateLoad() { offliner()->TerminateLoadIfInProgress(); }
50
51 Offliner* offliner() { return offliner_; }
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(TestLoadTerminationListener);
55 };
56
43 // Mock OfflinePageModel for testing the SavePage calls 57 // Mock OfflinePageModel for testing the SavePage calls
44 class MockOfflinePageModel : public StubOfflinePageModel { 58 class MockOfflinePageModel : public StubOfflinePageModel {
45 public: 59 public:
46 MockOfflinePageModel() : mock_saving_(false), mock_deleting_(false) {} 60 MockOfflinePageModel() : mock_saving_(false), mock_deleting_(false) {}
47 ~MockOfflinePageModel() override {} 61 ~MockOfflinePageModel() override {}
48 62
49 void SavePage(const SavePageParams& save_page_params, 63 void SavePage(const SavePageParams& save_page_params,
50 std::unique_ptr<OfflinePageArchiver> archiver, 64 std::unique_ptr<OfflinePageArchiver> archiver,
51 const SavePageCallback& callback) override { 65 const SavePageCallback& callback) override {
52 mock_saving_ = true; 66 mock_saving_ = true;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // A BackgroundLoader that we can run tests on. 113 // A BackgroundLoader that we can run tests on.
100 // Overrides the ResetState so we don't actually try to create any web contents. 114 // Overrides the ResetState so we don't actually try to create any web contents.
101 // This is a temporary solution to test core BackgroundLoaderOffliner 115 // This is a temporary solution to test core BackgroundLoaderOffliner
102 // functionality until we straighten out assumptions made by RequestCoordinator 116 // functionality until we straighten out assumptions made by RequestCoordinator
103 // so that the ResetState method is no longer needed. 117 // so that the ResetState method is no longer needed.
104 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner { 118 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner {
105 public: 119 public:
106 explicit TestBackgroundLoaderOffliner( 120 explicit TestBackgroundLoaderOffliner(
107 content::BrowserContext* browser_context, 121 content::BrowserContext* browser_context,
108 const OfflinerPolicy* policy, 122 const OfflinerPolicy* policy,
109 OfflinePageModel* offline_page_model); 123 OfflinePageModel* offline_page_model,
124 std::unique_ptr<LoadTerminationListener> load_termination_listener);
110 ~TestBackgroundLoaderOffliner() override; 125 ~TestBackgroundLoaderOffliner() override;
111 content::WebContentsTester* web_contents_tester() { 126 content::WebContentsTester* web_contents_tester() {
112 return content::WebContentsTester::For(stub_->web_contents()); 127 return content::WebContentsTester::For(stub_->web_contents());
113 } 128 }
114 129
115 content::WebContents* web_contents() { return stub_->web_contents(); } 130 content::WebContents* web_contents() { return stub_->web_contents(); }
116 131
117 bool is_loading() { return loader_ && stub_->is_loading(); } 132 bool is_loading() { return loader_ && stub_->is_loading(); }
118 133
119 protected: 134 protected:
120 void ResetLoader() override; 135 void ResetLoader() override;
121 136
122 private: 137 private:
123 background_loader::BackgroundLoaderContentsStub* stub_; 138 background_loader::BackgroundLoaderContentsStub* stub_;
124 }; 139 };
125 140
126 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner( 141 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner(
127 content::BrowserContext* browser_context, 142 content::BrowserContext* browser_context,
128 const OfflinerPolicy* policy, 143 const OfflinerPolicy* policy,
129 OfflinePageModel* offline_page_model) 144 OfflinePageModel* offline_page_model,
130 : BackgroundLoaderOffliner(browser_context, policy, offline_page_model) {} 145 std::unique_ptr<LoadTerminationListener> load_termination_listener)
146 : BackgroundLoaderOffliner(browser_context,
147 policy,
148 offline_page_model,
149 std::move(load_termination_listener)) {}
131 150
132 TestBackgroundLoaderOffliner::~TestBackgroundLoaderOffliner() {} 151 TestBackgroundLoaderOffliner::~TestBackgroundLoaderOffliner() {}
133 152
134 void TestBackgroundLoaderOffliner::ResetLoader() { 153 void TestBackgroundLoaderOffliner::ResetLoader() {
135 stub_ = new background_loader::BackgroundLoaderContentsStub(browser_context_); 154 stub_ = new background_loader::BackgroundLoaderContentsStub(browser_context_);
136 loader_.reset(stub_); 155 loader_.reset(stub_);
137 } 156 }
138 157
139 class BackgroundLoaderOfflinerTest : public testing::Test { 158 class BackgroundLoaderOfflinerTest : public testing::Test {
140 public: 159 public:
(...skipping 18 matching lines...) Expand all
159 Profile* profile() { return &profile_; } 178 Profile* profile() { return &profile_; }
160 bool completion_callback_called() { return completion_callback_called_; } 179 bool completion_callback_called() { return completion_callback_called_; }
161 Offliner::RequestStatus request_status() { return request_status_; } 180 Offliner::RequestStatus request_status() { return request_status_; }
162 bool cancel_callback_called() { return cancel_callback_called_; } 181 bool cancel_callback_called() { return cancel_callback_called_; }
163 bool SaveInProgress() const { return model_->mock_saving(); } 182 bool SaveInProgress() const { return model_->mock_saving(); }
164 bool DeleteCalled() const { return model_->mock_deleting(); } 183 bool DeleteCalled() const { return model_->mock_deleting(); }
165 MockOfflinePageModel* model() const { return model_; } 184 MockOfflinePageModel* model() const { return model_; }
166 const base::HistogramTester& histograms() const { return histogram_tester_; } 185 const base::HistogramTester& histograms() const { return histogram_tester_; }
167 int64_t progress() { return progress_; } 186 int64_t progress() { return progress_; }
168 OfflinerPolicy* policy() const { return policy_.get(); } 187 OfflinerPolicy* policy() const { return policy_.get(); }
188 TestLoadTerminationListener* load_termination_listener() {
189 return load_termination_listener_;
190 }
169 191
170 void PumpLoop() { base::RunLoop().RunUntilIdle(); } 192 void PumpLoop() { base::RunLoop().RunUntilIdle(); }
171 193
172 void CompleteLoading() { 194 void CompleteLoading() {
173 // Reset snapshot controller. 195 // Reset snapshot controller.
174 std::unique_ptr<SnapshotController> snapshot_controller( 196 std::unique_ptr<SnapshotController> snapshot_controller(
175 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), 197 new SnapshotController(base::ThreadTaskRunnerHandle::Get(),
176 offliner_.get(), 198 offliner_.get(),
177 0L /* DelayAfterDocumentAvailable */, 199 0L /* DelayAfterDocumentAvailable */,
178 0L /* DelayAfterDocumentOnLoad */, 200 0L /* DelayAfterDocumentOnLoad */,
179 false /* DocumentAvailableTriggersSnapshot */)); 201 false /* DocumentAvailableTriggersSnapshot */));
180 offliner_->SetSnapshotControllerForTest(std::move(snapshot_controller)); 202 offliner_->SetSnapshotControllerForTest(std::move(snapshot_controller));
181 // Call complete loading. 203 // Call complete loading.
182 offliner()->DocumentOnLoadCompletedInMainFrame(); 204 offliner()->DocumentOnLoadCompletedInMainFrame();
183 PumpLoop(); 205 PumpLoop();
184 } 206 }
185 207
186 private: 208 private:
187 void OnCompletion(const SavePageRequest& request, 209 void OnCompletion(const SavePageRequest& request,
188 Offliner::RequestStatus status); 210 Offliner::RequestStatus status);
189 void OnProgress(const SavePageRequest& request, int64_t bytes); 211 void OnProgress(const SavePageRequest& request, int64_t bytes);
190 void OnCancel(const SavePageRequest& request); 212 void OnCancel(const SavePageRequest& request);
191 content::TestBrowserThreadBundle thread_bundle_; 213 content::TestBrowserThreadBundle thread_bundle_;
192 TestingProfile profile_; 214 TestingProfile profile_;
193 std::unique_ptr<OfflinerPolicy> policy_; 215 std::unique_ptr<OfflinerPolicy> policy_;
216 TestLoadTerminationListener* load_termination_listener_;
194 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; 217 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_;
195 MockOfflinePageModel* model_; 218 MockOfflinePageModel* model_;
196 bool completion_callback_called_; 219 bool completion_callback_called_;
197 bool cancel_callback_called_; 220 bool cancel_callback_called_;
198 int64_t progress_; 221 int64_t progress_;
199 Offliner::RequestStatus request_status_; 222 Offliner::RequestStatus request_status_;
200 base::HistogramTester histogram_tester_; 223 base::HistogramTester histogram_tester_;
201 224
202 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); 225 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest);
203 }; 226 };
204 227
205 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() 228 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest()
206 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 229 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
230 load_termination_listener_(nullptr),
231 model_(nullptr),
207 completion_callback_called_(false), 232 completion_callback_called_(false),
208 cancel_callback_called_(false), 233 cancel_callback_called_(false),
209 progress_(0LL), 234 progress_(0LL),
210 request_status_(Offliner::RequestStatus::UNKNOWN) {} 235 request_status_(Offliner::RequestStatus::UNKNOWN) {}
211 236
212 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} 237 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {}
213 238
214 void BackgroundLoaderOfflinerTest::SetUp() { 239 void BackgroundLoaderOfflinerTest::SetUp() {
240 std::unique_ptr<TestLoadTerminationListener> listener =
241 base::MakeUnique<TestLoadTerminationListener>();
242 load_termination_listener_ = listener.get();
215 model_ = new MockOfflinePageModel(); 243 model_ = new MockOfflinePageModel();
216 policy_.reset(new OfflinerPolicy()); 244 policy_.reset(new OfflinerPolicy());
217 offliner_.reset( 245 offliner_.reset(new TestBackgroundLoaderOffliner(
218 new TestBackgroundLoaderOffliner(profile(), policy_.get(), model_)); 246 profile(), policy_.get(), model_, std::move(listener)));
219 } 247 }
220 248
221 void BackgroundLoaderOfflinerTest::OnCompletion( 249 void BackgroundLoaderOfflinerTest::OnCompletion(
222 const SavePageRequest& request, 250 const SavePageRequest& request,
223 Offliner::RequestStatus status) { 251 Offliner::RequestStatus status) {
224 DCHECK(!completion_callback_called_); // Expect 1 callback per request. 252 DCHECK(!completion_callback_called_); // Expect 1 callback per request.
225 completion_callback_called_ = true; 253 completion_callback_called_ = true;
226 request_status_ = status; 254 request_status_ = status;
227 } 255 }
228 256
229 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request, 257 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request,
230 int64_t bytes) { 258 int64_t bytes) {
231 progress_ = bytes; 259 progress_ = bytes;
232 } 260 }
233 261
234 void BackgroundLoaderOfflinerTest::OnCancel(const SavePageRequest& request) { 262 void BackgroundLoaderOfflinerTest::OnCancel(const SavePageRequest& request) {
235 DCHECK(!cancel_callback_called_); 263 DCHECK(!cancel_callback_called_);
236 cancel_callback_called_ = true; 264 cancel_callback_called_ = true;
237 } 265 }
238 266
239 // Two tests crash roughly 20% of runs on Android. http://crbug.com/722556. 267 // Two tests crash roughly 20% of runs on Android. http://crbug.com/722556.
240 #if defined(OS_ANDROID) 268 #if defined(OS_ANDROID)
241 #define MAYBE_FailsOnErrorPage DISABLED_FailsOnErrorPage 269 #define MAYBE_FailsOnErrorPage DISABLED_FailsOnErrorPage
242 #define MAYBE_NoNextOnInternetDisconnected DISABLED_NoNextOnInternetDisconnected 270 #define MAYBE_NoNextOnInternetDisconnected DISABLED_NoNextOnInternetDisconnected
243 #else 271 #else
244 #define MAYBE_FailsOnErrorPage FailsOnErrorPage 272 #define MAYBE_FailsOnErrorPage FailsOnErrorPage
245 #define MAYBE_NoNextOnInternetDisconnected NoNextOnInternetDisconnected 273 #define MAYBE_NoNextOnInternetDisconnected NoNextOnInternetDisconnected
246 #endif 274 #endif
247 275
276 TEST_F(BackgroundLoaderOfflinerTest, LoadTerminationListenerSetup) {
277 // Verify that back pointer to offliner is set up in the listener.
278 Offliner* base_offliner = offliner();
279 EXPECT_NE(base_offliner, nullptr);
280 EXPECT_EQ(base_offliner, load_termination_listener()->offliner());
281 }
282
248 TEST_F(BackgroundLoaderOfflinerTest, 283 TEST_F(BackgroundLoaderOfflinerTest,
249 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { 284 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) {
250 base::Time creation_time = base::Time::Now(); 285 base::Time creation_time = base::Time::Now();
251 ClientId custom_tabs_client_id("custom_tabs", "88"); 286 ClientId custom_tabs_client_id("custom_tabs", "88");
252 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, 287 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id,
253 creation_time, kUserRequested); 288 creation_time, kUserRequested);
254 289
255 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); 290 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
256 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), 291 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(),
257 progress_callback())); 292 progress_callback()));
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), 367 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
333 progress_callback())); 368 progress_callback()));
334 offliner()->Cancel(cancel_callback()); 369 offliner()->Cancel(cancel_callback());
335 PumpLoop(); 370 PumpLoop();
336 offliner()->OnNetworkBytesChanged(15LL); 371 offliner()->OnNetworkBytesChanged(15LL);
337 EXPECT_TRUE(cancel_callback_called()); 372 EXPECT_TRUE(cancel_callback_called());
338 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. 373 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset.
339 EXPECT_EQ(progress(), 0LL); // network bytes not recorded when not busy. 374 EXPECT_EQ(progress(), 0LL); // network bytes not recorded when not busy.
340 } 375 }
341 376
377 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoadTerminated) {
378 base::Time creation_time = base::Time::Now();
379 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
380 kUserRequested);
381 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
382 progress_callback()));
383 load_termination_listener()->TerminateLoad();
384 PumpLoop();
385 EXPECT_TRUE(completion_callback_called());
386 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset.
387 EXPECT_EQ(Offliner::RequestStatus::FOREGROUND_CANCELED, request_status());
388 }
389
342 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { 390 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) {
343 base::Time creation_time = base::Time::Now(); 391 base::Time creation_time = base::Time::Now();
344 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 392 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
345 kUserRequested); 393 kUserRequested);
346 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), 394 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
347 progress_callback())); 395 progress_callback()));
348 CompleteLoading(); 396 CompleteLoading();
349 PumpLoop(); 397 PumpLoop();
350 offliner()->Cancel(cancel_callback()); 398 offliner()->Cancel(cancel_callback());
351 PumpLoop(); 399 PumpLoop();
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 CompleteLoading(); 697 CompleteLoading();
650 PumpLoop(); 698 PumpLoop();
651 699
652 // One extra part should be added if the flag is on. 700 // One extra part should be added if the flag is on.
653 content::MHTMLExtraParts* extra_parts = 701 content::MHTMLExtraParts* extra_parts =
654 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); 702 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents());
655 EXPECT_EQ(extra_parts->size(), 1); 703 EXPECT_EQ(extra_parts->size(), 1);
656 } 704 }
657 705
658 } // namespace offline_pages 706 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698