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

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

Issue 2818783002: [Offline pages]: Implement background loader to save on last retry, and record last retry success U… (Closed)
Patch Set: request coordinator update 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 "chrome/browser/android/offline_pages/background_loader_offliner.h" 5 #include "chrome/browser/android/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_mock_time_message_loop_task_runner.h" 10 #include "base/test/scoped_mock_time_message_loop_task_runner.h"
11 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
12 #include "chrome/browser/android/offline_pages/offliner_helper.h" 12 #include "chrome/browser/android/offline_pages/offliner_helper.h"
13 #include "chrome/browser/net/prediction_options.h" 13 #include "chrome/browser/net/prediction_options.h"
14 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
16 #include "components/content_settings/core/common/pref_names.h" 16 #include "components/content_settings/core/common/pref_names.h"
17 #include "components/offline_pages/content/background_loader/background_loader_c ontents_stub.h" 17 #include "components/offline_pages/content/background_loader/background_loader_c ontents_stub.h"
18 #include "components/offline_pages/core/background/offliner.h" 18 #include "components/offline_pages/core/background/offliner.h"
19 #include "components/offline_pages/core/background/offliner_policy.h"
19 #include "components/offline_pages/core/background/save_page_request.h" 20 #include "components/offline_pages/core/background/save_page_request.h"
20 #include "components/offline_pages/core/stub_offline_page_model.h" 21 #include "components/offline_pages/core/stub_offline_page_model.h"
21 #include "components/prefs/pref_service.h" 22 #include "components/prefs/pref_service.h"
22 #include "content/public/browser/navigation_handle.h" 23 #include "content/public/browser/navigation_handle.h"
23 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 25 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "content/public/test/web_contents_tester.h" 26 #include "content/public/test/web_contents_tester.h"
26 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 29
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 base::Unretained(this)); 140 base::Unretained(this));
140 } 141 }
141 Profile* profile() { return &profile_; } 142 Profile* profile() { return &profile_; }
142 bool completion_callback_called() { return completion_callback_called_; } 143 bool completion_callback_called() { return completion_callback_called_; }
143 Offliner::RequestStatus request_status() { return request_status_; } 144 Offliner::RequestStatus request_status() { return request_status_; }
144 bool cancel_callback_called() { return cancel_callback_called_; } 145 bool cancel_callback_called() { return cancel_callback_called_; }
145 bool SaveInProgress() const { return model_->mock_saving(); } 146 bool SaveInProgress() const { return model_->mock_saving(); }
146 MockOfflinePageModel* model() const { return model_; } 147 MockOfflinePageModel* model() const { return model_; }
147 const base::HistogramTester& histograms() const { return histogram_tester_; } 148 const base::HistogramTester& histograms() const { return histogram_tester_; }
148 int64_t progress() { return progress_; } 149 int64_t progress() { return progress_; }
150 OfflinerPolicy* policy() const { return policy_.get(); }
149 151
150 void PumpLoop() { base::RunLoop().RunUntilIdle(); } 152 void PumpLoop() { base::RunLoop().RunUntilIdle(); }
151 153
152 void CompleteLoading() { 154 void CompleteLoading() {
153 offliner()->DocumentOnLoadCompletedInMainFrame(); 155 offliner()->DocumentOnLoadCompletedInMainFrame();
154 PumpLoop(); 156 PumpLoop();
155 } 157 }
156 158
157 private: 159 private:
158 void OnCompletion(const SavePageRequest& request, 160 void OnCompletion(const SavePageRequest& request,
159 Offliner::RequestStatus status); 161 Offliner::RequestStatus status);
160 void OnProgress(const SavePageRequest& request, int64_t bytes); 162 void OnProgress(const SavePageRequest& request, int64_t bytes);
161 void OnCancel(int64_t offline_id); 163 void OnCancel(int64_t offline_id);
162 content::TestBrowserThreadBundle thread_bundle_; 164 content::TestBrowserThreadBundle thread_bundle_;
163 TestingProfile profile_; 165 TestingProfile profile_;
166 std::unique_ptr<OfflinerPolicy> policy_;
164 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; 167 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_;
165 MockOfflinePageModel* model_; 168 MockOfflinePageModel* model_;
166 bool completion_callback_called_; 169 bool completion_callback_called_;
167 bool cancel_callback_called_; 170 bool cancel_callback_called_;
168 int64_t progress_; 171 int64_t progress_;
169 Offliner::RequestStatus request_status_; 172 Offliner::RequestStatus request_status_;
170 base::HistogramTester histogram_tester_; 173 base::HistogramTester histogram_tester_;
171 174
172 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); 175 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest);
173 }; 176 };
174 177
175 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() 178 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest()
176 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 179 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
177 completion_callback_called_(false), 180 completion_callback_called_(false),
178 cancel_callback_called_(false), 181 cancel_callback_called_(false),
179 progress_(0LL), 182 progress_(0LL),
180 request_status_(Offliner::RequestStatus::UNKNOWN) {} 183 request_status_(Offliner::RequestStatus::UNKNOWN) {}
181 184
182 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} 185 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {}
183 186
184 void BackgroundLoaderOfflinerTest::SetUp() { 187 void BackgroundLoaderOfflinerTest::SetUp() {
185 model_ = new MockOfflinePageModel(); 188 model_ = new MockOfflinePageModel();
186 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); 189 policy_.reset(new OfflinerPolicy());
190 offliner_.reset(
191 new TestBackgroundLoaderOffliner(profile(), policy_.get(), model_));
187 offliner_->SetPageDelayForTest(0L); 192 offliner_->SetPageDelayForTest(0L);
188 } 193 }
189 194
190 void BackgroundLoaderOfflinerTest::OnCompletion( 195 void BackgroundLoaderOfflinerTest::OnCompletion(
191 const SavePageRequest& request, 196 const SavePageRequest& request,
192 Offliner::RequestStatus status) { 197 Offliner::RequestStatus status) {
193 DCHECK(!completion_callback_called_); // Expect 1 callback per request. 198 DCHECK(!completion_callback_called_); // Expect 1 callback per request.
194 completion_callback_called_ = true; 199 completion_callback_called_ = true;
195 request_status_ = status; 200 request_status_ = status;
196 } 201 }
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 PumpLoop(); 481 PumpLoop();
477 model()->CompleteSavingAsSuccess(); 482 model()->CompleteSavingAsSuccess();
478 PumpLoop(); 483 PumpLoop();
479 484
480 EXPECT_TRUE(completion_callback_called()); 485 EXPECT_TRUE(completion_callback_called());
481 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); 486 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status());
482 EXPECT_FALSE(offliner()->is_loading()); 487 EXPECT_FALSE(offliner()->is_loading());
483 EXPECT_FALSE(SaveInProgress()); 488 EXPECT_FALSE(SaveInProgress());
484 } 489 }
485 490
491 TEST_F(BackgroundLoaderOfflinerTest, HandleTimeoutWithLowBarStartedTriesMet) {
Pete Williamson 2017/04/13 22:57:15 Should we have a test where we get a last timeout,
chili 2017/04/15 00:34:59 That is the situation in HandleTimeoutWithStartedT
492 base::Time creation_time = base::Time::Now();
493 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
494 kUserRequested);
495 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
496 progress_callback()));
497 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1);
498 // Sets lowbar.
499 offliner()->DocumentAvailableInMainFrame();
500 // Timeout
501 EXPECT_TRUE(offliner()->HandleTimeout(request));
502 EXPECT_TRUE(SaveInProgress());
503 model()->CompleteSavingAsSuccess();
504 PumpLoop();
505 EXPECT_EQ(Offliner::RequestStatus::SAVED_ON_LAST_RETRY, request_status());
506 }
507
508 TEST_F(BackgroundLoaderOfflinerTest, HandleTimeoutWithStartedTriesMet) {
Pete Williamson 2017/04/13 22:57:15 Should we test both started and finished attempt c
romax 2017/04/13 23:17:14 I think the test is missing for the prerendering_o
chili 2017/04/15 00:34:59 Done.
509 base::Time creation_time = base::Time::Now();
510 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
511 kUserRequested);
512 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
513 progress_callback()));
514 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1);
515 // Timeout
516 EXPECT_FALSE(offliner()->HandleTimeout(request));
517 EXPECT_FALSE(SaveInProgress());
518 }
519
520 TEST_F(BackgroundLoaderOfflinerTest, HandleTimeoutWithLowBar) {
Pete Williamson 2017/04/13 22:57:15 Nit - this test name might be better as HandleTim
521 base::Time creation_time = base::Time::Now();
522 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
523 kUserRequested);
524 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
525 progress_callback()));
526 // Timeout
527 EXPECT_FALSE(offliner()->HandleTimeout(request));
528 EXPECT_FALSE(SaveInProgress());
529 }
530
486 } // namespace offline_pages 531 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698