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

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

Issue 2637563002: [Offline Pages] Snapshotting on timeout of last retry. (Closed)
Patch Set: merging with master again Created 3 years, 10 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/prerendering_offliner.h" 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/sys_info.h" 11 #include "base/sys_info.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/prerendering_loader.h" 13 #include "chrome/browser/android/offline_pages/prerendering_loader.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/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/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/web_contents_tester.h" 24 #include "content/public/test/web_contents_tester.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 namespace offline_pages { 27 namespace offline_pages {
27 28
28 namespace { 29 namespace {
29 const int64_t kRequestId = 7; 30 const int64_t kRequestId = 7;
30 const GURL kHttpUrl("http://tunafish.com"); 31 const GURL kHttpUrl("http://tunafish.com");
31 const GURL kFileUrl("file://sailfish.png"); 32 const GURL kFileUrl("file://sailfish.png");
32 const ClientId kClientId("AsyncLoading", "88"); 33 const ClientId kClientId("AsyncLoading", "88");
33 const bool kUserRequested = true; 34 const bool kUserRequested = true;
34 35
35 // Mock Loader for testing the Offliner calls. 36 // Mock Loader for testing the Offliner calls.
36 class MockPrerenderingLoader : public PrerenderingLoader { 37 class MockPrerenderingLoader : public PrerenderingLoader {
37 public: 38 public:
38 explicit MockPrerenderingLoader(content::BrowserContext* browser_context) 39 explicit MockPrerenderingLoader(content::BrowserContext* browser_context)
39 : PrerenderingLoader(browser_context), 40 : PrerenderingLoader(browser_context),
40 can_prerender_(true), 41 can_prerender_(true),
41 mock_loading_(false), 42 mock_loading_(false),
42 mock_loaded_(false) {} 43 mock_loaded_(false),
44 mock_is_lowbar_met_(false),
45 start_snapshot_called_(false) {}
43 ~MockPrerenderingLoader() override {} 46 ~MockPrerenderingLoader() override {}
44 47
45 bool LoadPage(const GURL& url, 48 bool LoadPage(const GURL& url,
46 const LoadPageCallback& load_done_callback, 49 const LoadPageCallback& load_done_callback,
47 const ProgressCallback& progress_callback) override { 50 const ProgressCallback& progress_callback) override {
48 mock_loading_ = can_prerender_; 51 mock_loading_ = can_prerender_;
49 load_page_callback_ = load_done_callback; 52 load_page_callback_ = load_done_callback;
50 progress_callback_ = progress_callback; 53 progress_callback_ = progress_callback;
51 return mock_loading_; 54 return mock_loading_;
52 } 55 }
53 56
54 void StopLoading() override { 57 void StopLoading() override {
55 mock_loading_ = false; 58 mock_loading_ = false;
56 mock_loaded_ = false; 59 mock_loaded_ = false;
57 } 60 }
58 61
59 bool IsIdle() override { return !mock_loading_ && !mock_loaded_; } 62 bool IsIdle() override { return !mock_loading_ && !mock_loaded_; }
60 bool IsLoaded() override { return mock_loaded_; } 63 bool IsLoaded() override { return mock_loaded_; }
64 bool IsLowbarMet() override { return mock_is_lowbar_met_; }
65
66 void StartSnapshot() override { start_snapshot_called_ = true; }
61 67
62 void CompleteLoadingAsFailed() { 68 void CompleteLoadingAsFailed() {
63 DCHECK(mock_loading_); 69 DCHECK(mock_loading_);
64 mock_loading_ = false; 70 mock_loading_ = false;
65 mock_loaded_ = false; 71 mock_loaded_ = false;
66 base::ThreadTaskRunnerHandle::Get()->PostTask( 72 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE, 73 FROM_HERE,
68 base::Bind(load_page_callback_, Offliner::RequestStatus::LOADING_FAILED, 74 base::Bind(load_page_callback_, Offliner::RequestStatus::LOADING_FAILED,
69 nullptr /* web_contents */)); 75 nullptr /* web_contents */));
70 } 76 }
(...skipping 14 matching lines...) Expand all
85 mock_loading_ = false; 91 mock_loading_ = false;
86 mock_loaded_ = false; 92 mock_loaded_ = false;
87 base::ThreadTaskRunnerHandle::Get()->PostTask( 93 base::ThreadTaskRunnerHandle::Get()->PostTask(
88 FROM_HERE, base::Bind(load_page_callback_, 94 FROM_HERE, base::Bind(load_page_callback_,
89 Offliner::RequestStatus::LOADING_CANCELED, 95 Offliner::RequestStatus::LOADING_CANCELED,
90 nullptr /* web_contents */)); 96 nullptr /* web_contents */));
91 } 97 }
92 98
93 void DisablePrerendering() { can_prerender_ = false; } 99 void DisablePrerendering() { can_prerender_ = false; }
94 const LoadPageCallback& load_page_callback() { return load_page_callback_; } 100 const LoadPageCallback& load_page_callback() { return load_page_callback_; }
101 void set_is_lowbar_met(bool is_lowbar_met) {
102 mock_is_lowbar_met_ = is_lowbar_met;
103 }
104 bool start_snapshot_called() { return start_snapshot_called_; }
95 105
96 private: 106 private:
97 bool can_prerender_; 107 bool can_prerender_;
98 bool mock_loading_; 108 bool mock_loading_;
99 bool mock_loaded_; 109 bool mock_loaded_;
110 bool mock_is_lowbar_met_;
111 bool start_snapshot_called_;
100 LoadPageCallback load_page_callback_; 112 LoadPageCallback load_page_callback_;
101 ProgressCallback progress_callback_; 113 ProgressCallback progress_callback_;
102 114
103 DISALLOW_COPY_AND_ASSIGN(MockPrerenderingLoader); 115 DISALLOW_COPY_AND_ASSIGN(MockPrerenderingLoader);
104 }; 116 };
105 117
106 // Mock OfflinePageModel for testing the SavePage calls. 118 // Mock OfflinePageModel for testing the SavePage calls.
107 class MockOfflinePageModel : public StubOfflinePageModel { 119 class MockOfflinePageModel : public StubOfflinePageModel {
108 public: 120 public:
109 MockOfflinePageModel() : mock_saving_(false) {} 121 MockOfflinePageModel() : mock_saving_(false) {}
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 Offliner::CompletionCallback const callback() { 171 Offliner::CompletionCallback const callback() {
160 return base::Bind(&PrerenderingOfflinerTest::OnCompletion, 172 return base::Bind(&PrerenderingOfflinerTest::OnCompletion,
161 base::Unretained(this)); 173 base::Unretained(this));
162 } 174 }
163 175
164 bool SaveInProgress() const { return model_->mock_saving(); } 176 bool SaveInProgress() const { return model_->mock_saving(); }
165 MockPrerenderingLoader* loader() { return loader_; } 177 MockPrerenderingLoader* loader() { return loader_; }
166 MockOfflinePageModel* model() { return model_; } 178 MockOfflinePageModel* model() { return model_; }
167 bool completion_callback_called() { return completion_callback_called_; } 179 bool completion_callback_called() { return completion_callback_called_; }
168 Offliner::RequestStatus request_status() { return request_status_; } 180 Offliner::RequestStatus request_status() { return request_status_; }
181 OfflinerPolicy* policy() { return policy_; }
169 182
170 private: 183 private:
171 void OnCompletion(const SavePageRequest& request, 184 void OnCompletion(const SavePageRequest& request,
172 Offliner::RequestStatus status); 185 Offliner::RequestStatus status);
173 186
174 content::TestBrowserThreadBundle thread_bundle_; 187 content::TestBrowserThreadBundle thread_bundle_;
175 TestingProfile profile_; 188 TestingProfile profile_;
176 std::unique_ptr<PrerenderingOffliner> offliner_; 189 std::unique_ptr<PrerenderingOffliner> offliner_;
177 // Not owned. 190 // Not owned.
178 MockPrerenderingLoader* loader_; 191 MockPrerenderingLoader* loader_;
179 MockOfflinePageModel* model_; 192 MockOfflinePageModel* model_;
180 bool completion_callback_called_; 193 bool completion_callback_called_;
181 Offliner::RequestStatus request_status_; 194 Offliner::RequestStatus request_status_;
195 OfflinerPolicy* policy_;
182 196
183 DISALLOW_COPY_AND_ASSIGN(PrerenderingOfflinerTest); 197 DISALLOW_COPY_AND_ASSIGN(PrerenderingOfflinerTest);
184 }; 198 };
185 199
186 PrerenderingOfflinerTest::PrerenderingOfflinerTest() 200 PrerenderingOfflinerTest::PrerenderingOfflinerTest()
187 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 201 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
188 completion_callback_called_(false), 202 completion_callback_called_(false),
189 request_status_(Offliner::RequestStatus::UNKNOWN) {} 203 request_status_(Offliner::RequestStatus::UNKNOWN) {}
190 204
191 PrerenderingOfflinerTest::~PrerenderingOfflinerTest() {} 205 PrerenderingOfflinerTest::~PrerenderingOfflinerTest() {}
192 206
193 void PrerenderingOfflinerTest::SetUp() { 207 void PrerenderingOfflinerTest::SetUp() {
194 model_ = new MockOfflinePageModel(); 208 model_ = new MockOfflinePageModel();
195 offliner_.reset(new PrerenderingOffliner(profile(), nullptr, model_)); 209 policy_ = new OfflinerPolicy();
210 offliner_.reset(new PrerenderingOffliner(profile(), policy_, model_));
196 std::unique_ptr<MockPrerenderingLoader> mock_loader( 211 std::unique_ptr<MockPrerenderingLoader> mock_loader(
197 new MockPrerenderingLoader(nullptr)); 212 new MockPrerenderingLoader(nullptr));
198 loader_ = mock_loader.get(); 213 loader_ = mock_loader.get();
199 offliner_->SetLoaderForTesting(std::move(mock_loader)); 214 offliner_->SetLoaderForTesting(std::move(mock_loader));
200 } 215 }
201 216
202 void PrerenderingOfflinerTest::OnCompletion(const SavePageRequest& request, 217 void PrerenderingOfflinerTest::OnCompletion(const SavePageRequest& request,
203 Offliner::RequestStatus status) { 218 Offliner::RequestStatus status) {
204 DCHECK(!completion_callback_called_); // Expect single callback per request. 219 DCHECK(!completion_callback_called_); // Expect single callback per request.
205 completion_callback_called_ = true; 220 completion_callback_called_ = true;
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 411 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
397 EXPECT_FALSE(loader()->IsIdle()); 412 EXPECT_FALSE(loader()->IsIdle());
398 413
399 offliner()->SetApplicationStateForTesting( 414 offliner()->SetApplicationStateForTesting(
400 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES); 415 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
401 416
402 // Loader still loading since not low-end device. 417 // Loader still loading since not low-end device.
403 EXPECT_FALSE(loader()->IsIdle()); 418 EXPECT_FALSE(loader()->IsIdle());
404 } 419 }
405 420
421 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithLowbarAndCompletedTriesMet) {
422 offliner()->SetLowEndDeviceForTesting(false);
423
424 base::Time creation_time = base::Time::Now();
425 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
426 kUserRequested);
427 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1);
428 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
429 loader()->set_is_lowbar_met(true);
430 EXPECT_TRUE(offliner()->HandleTimeout(request));
431 EXPECT_TRUE(loader()->start_snapshot_called());
432 }
433
434 TEST_F(PrerenderingOfflinerTest,
435 HandleTimeoutWithLowbarAndCompletedTriesMetLowEndDevice) {
436 offliner()->SetLowEndDeviceForTesting(true);
437
438 base::Time creation_time = base::Time::Now();
439 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
440 kUserRequested);
441 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1);
442 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
443 loader()->set_is_lowbar_met(true);
444 EXPECT_TRUE(offliner()->HandleTimeout(request));
445 EXPECT_TRUE(loader()->start_snapshot_called());
446 }
447
448 TEST_F(PrerenderingOfflinerTest,
449 HandleTimeoutCompletedTriesMetWithoutLowbarMet) {
450 offliner()->SetLowEndDeviceForTesting(false);
451
452 base::Time creation_time = base::Time::Now();
453 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
454 kUserRequested);
455 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1);
456 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
457 loader()->set_is_lowbar_met(false);
458 EXPECT_FALSE(offliner()->HandleTimeout(request));
459 EXPECT_FALSE(loader()->start_snapshot_called());
460 }
461
462 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithLowbarAndStartedTriesMet) {
463 offliner()->SetLowEndDeviceForTesting(false);
464
465 base::Time creation_time = base::Time::Now();
466 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
467 kUserRequested);
468 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1);
469 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
470 loader()->set_is_lowbar_met(true);
471 EXPECT_TRUE(offliner()->HandleTimeout(request));
472 EXPECT_TRUE(loader()->start_snapshot_called());
473 }
474
475 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithOnlyLowbarMet) {
476 offliner()->SetLowEndDeviceForTesting(false);
477
478 base::Time creation_time = base::Time::Now();
479 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
480 kUserRequested);
481 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
482 loader()->set_is_lowbar_met(true);
483 EXPECT_FALSE(offliner()->HandleTimeout(request));
484 EXPECT_FALSE(loader()->start_snapshot_called());
485 }
486
406 } // namespace offline_pages 487 } // namespace offline_pages
OLDNEW
« no previous file with comments | « chrome/browser/android/offline_pages/prerendering_offliner.cc ('k') | components/offline_pages/core/background/offliner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698