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

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

Issue 2561163002: Fix snapshots from Downloads being deleted by last_n. (Closed)
Patch Set: Disabled RequestCoordinator factory to fix SQL errors Created 4 years 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/recent_tab_helper.h" 5 #include "chrome/browser/android/offline_pages/recent_tab_helper.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/test/scoped_feature_list.h" 10 #include "base/test/scoped_feature_list.h"
11 #include "base/test/test_mock_time_task_runner.h" 11 #include "base/test/test_mock_time_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/offline_page_model_factory.h" 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
14 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h"
14 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 15 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "components/offline_pages/core/offline_page_feature.h" 17 #include "components/offline_pages/core/offline_page_feature.h"
17 #include "components/offline_pages/core/offline_page_item.h" 18 #include "components/offline_pages/core/offline_page_item.h"
18 #include "components/offline_pages/core/offline_page_model.h" 19 #include "components/offline_pages/core/offline_page_model.h"
19 #include "components/offline_pages/core/offline_page_test_archiver.h" 20 #include "components/offline_pages/core/offline_page_test_archiver.h"
20 #include "content/public/browser/navigation_entry.h" 21 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/navigation_handle.h" 22 #include "content/public/browser/navigation_handle.h"
22 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 namespace offline_pages { 26 namespace offline_pages {
26 27
27 namespace { 28 namespace {
28 const GURL kTestPageUrl("http://mystery.site/foo.html"); 29 const GURL kTestPageUrl("http://mystery.site/foo.html");
29 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html"); 30 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html");
30 const int kTabId = 153; 31 const int kTabId = 153;
31 } 32 } // namespace
32 33
33 class TestDelegate: public RecentTabHelper::Delegate { 34 class TestDelegate: public RecentTabHelper::Delegate {
34 public: 35 public:
35 const size_t kArchiveSizeToReport = 1234; 36 const size_t kArchiveSizeToReport = 1234;
36 37
37 explicit TestDelegate( 38 explicit TestDelegate(
38 OfflinePageTestArchiver::Observer* observer, 39 OfflinePageTestArchiver::Observer* observer,
39 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 40 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
40 int tab_id, 41 int tab_id,
41 bool tab_id_result); 42 bool tab_id_result);
(...skipping 27 matching lines...) Expand all
69 70
70 class RecentTabHelperTest 71 class RecentTabHelperTest
71 : public ChromeRenderViewHostTestHarness, 72 : public ChromeRenderViewHostTestHarness,
72 public OfflinePageModel::Observer, 73 public OfflinePageModel::Observer,
73 public OfflinePageTestArchiver::Observer { 74 public OfflinePageTestArchiver::Observer {
74 public: 75 public:
75 RecentTabHelperTest(); 76 RecentTabHelperTest();
76 ~RecentTabHelperTest() override {} 77 ~RecentTabHelperTest() override {}
77 78
78 void SetUp() override; 79 void SetUp() override;
80 void TearDown() override;
79 const std::vector<OfflinePageItem>& GetAllPages(); 81 const std::vector<OfflinePageItem>& GetAllPages();
80 82
81 void FailLoad(const GURL& url); 83 void FailLoad(const GURL& url);
82 84
83 // Runs default thread. 85 // Runs default thread.
84 void RunUntilIdle(); 86 void RunUntilIdle();
85 // Moves forward the snapshot controller's task runner. 87 // Moves forward the snapshot controller's task runner.
86 void FastForwardSnapshotController(); 88 void FastForwardSnapshotController();
87 89
88 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } 90 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; }
89 91
90 OfflinePageModel* model() const { return model_; } 92 OfflinePageModel* model() const { return model_; }
91 93
92 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } 94 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; }
93 95
96 // Returns the index in |all_pages| of the OfflinePageItem that matches the
97 // provided |offline_id|. If a match is not found returns -1.
98 int FindPageIndexForOfflineId(int64_t offline_id) {
99 for (size_t i = 0; i < all_pages_.size(); ++i) {
100 if (all_pages_[i].offline_id == offline_id)
101 return i;
102 }
103 return -1;
104 }
105
94 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { 106 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() {
95 return task_runner_; 107 return task_runner_;
96 } 108 }
97 109
98 size_t page_added_count() { return page_added_count_; } 110 size_t page_added_count() { return page_added_count_; }
99 size_t model_removed_count() { return model_removed_count_; } 111 size_t model_removed_count() { return model_removed_count_; }
100 112
101 // OfflinePageModel::Observer 113 // OfflinePageModel::Observer
102 void OfflinePageModelLoaded(OfflinePageModel* model) override { } 114 void OfflinePageModelLoaded(OfflinePageModel* model) override { }
103 void OfflinePageAdded(OfflinePageModel* model, 115 void OfflinePageAdded(OfflinePageModel* model,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 170
159 RecentTabHelperTest::RecentTabHelperTest() 171 RecentTabHelperTest::RecentTabHelperTest()
160 : recent_tab_helper_(nullptr), 172 : recent_tab_helper_(nullptr),
161 model_(nullptr), 173 model_(nullptr),
162 page_added_count_(0), 174 page_added_count_(0),
163 model_removed_count_(0), 175 model_removed_count_(0),
164 task_runner_(new base::TestMockTimeTaskRunner), 176 task_runner_(new base::TestMockTimeTaskRunner),
165 weak_ptr_factory_(this) {} 177 weak_ptr_factory_(this) {}
166 178
167 void RecentTabHelperTest::SetUp() { 179 void RecentTabHelperTest::SetUp() {
168 content::RenderViewHostTestHarness::SetUp(); 180 ChromeRenderViewHostTestHarness::SetUp();
169 181
170 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); 182 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature);
171 // Sets up the factory for testing. 183 // Sets up the factory for testing.
172 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( 184 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
173 browser_context(), BuildTestOfflinePageModel); 185 browser_context(), BuildTestOfflinePageModel);
174 RunUntilIdle(); 186 RunUntilIdle();
175 187
176 RecentTabHelper::CreateForWebContents(web_contents()); 188 RecentTabHelper::CreateForWebContents(web_contents());
177 recent_tab_helper_ = 189 recent_tab_helper_ =
178 RecentTabHelper::FromWebContents(web_contents()); 190 RecentTabHelper::FromWebContents(web_contents());
179 191
180 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>( 192 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>(
181 this, task_runner(), kTabId, true)); 193 this, task_runner(), kTabId, true));
182 194
183 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context()); 195 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context());
184 model_->AddObserver(this); 196 model_->AddObserver(this);
197 RequestCoordinatorFactory::GetInstance()->ReturnNullptrForTesting(true);
198 }
199
200 void RecentTabHelperTest::TearDown() {
201 ChromeRenderViewHostTestHarness::TearDown();
fgorski 2016/12/13 17:17:38 per my other comment you don't need this code, but
carlosk 2016/12/13 20:04:19 Removed.
202 RequestCoordinatorFactory::GetInstance()->ReturnNullptrForTesting(false);
185 } 203 }
186 204
187 void RecentTabHelperTest::FailLoad(const GURL& url) { 205 void RecentTabHelperTest::FailLoad(const GURL& url) {
188 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, 206 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
189 std::string()); 207 std::string());
190 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); 208 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url);
191 content::RenderFrameHostTester::For(main_rfh())-> 209 content::RenderFrameHostTester::For(main_rfh())->
192 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); 210 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED);
193 content::RenderFrameHostTester::For(main_rfh())-> 211 content::RenderFrameHostTester::For(main_rfh())->
194 SimulateNavigationErrorPageCommit(); 212 SimulateNavigationErrorPageCommit();
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 // the same page should be simply overridden. 298 // the same page should be simply overridden.
281 GetAllPages(); 299 GetAllPages();
282 EXPECT_EQ(1U, all_pages().size()); 300 EXPECT_EQ(1U, all_pages().size());
283 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); 301 EXPECT_EQ(kTestPageUrl, all_pages()[0].url);
284 EXPECT_NE(first_offline_id, all_pages()[0].offline_id); 302 EXPECT_NE(first_offline_id, all_pages()[0].offline_id);
285 } 303 }
286 304
287 // Triggers two snapshot captures during a single page load, where the 2nd one 305 // Triggers two snapshot captures during a single page load, where the 2nd one
288 // fails. Should end up with one offline page (the 1st, successful snapshot 306 // fails. Should end up with one offline page (the 1st, successful snapshot
289 // should be kept). 307 // should be kept).
290 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoadSecondFails) { 308 // TODO(carlosk): re-enable once https://crbug.com/655697 is fixed, again.
309 TEST_F(RecentTabHelperTest, DISABLED_TwoCapturesSamePageLoadSecondFails) {
291 NavigateAndCommit(kTestPageUrl); 310 NavigateAndCommit(kTestPageUrl);
292 // Triggers snapshot after a time delay. 311 // Triggers snapshot after a time delay.
293 recent_tab_helper()->DocumentAvailableInMainFrame(); 312 recent_tab_helper()->DocumentAvailableInMainFrame();
294 RunUntilIdle(); 313 RunUntilIdle();
295 EXPECT_TRUE(model()->is_loaded()); 314 EXPECT_TRUE(model()->is_loaded());
296 EXPECT_EQ(0U, page_added_count()); 315 EXPECT_EQ(0U, page_added_count());
297 // Move the snapshot controller's time forward so it gets past timeouts. 316 // Move the snapshot controller's time forward so it gets past timeouts.
298 FastForwardSnapshotController(); 317 FastForwardSnapshotController();
299 RunUntilIdle(); 318 RunUntilIdle();
300 EXPECT_EQ(1U, page_added_count()); 319 EXPECT_EQ(1U, page_added_count());
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 FastForwardSnapshotController(); 448 FastForwardSnapshotController();
430 RunUntilIdle(); 449 RunUntilIdle();
431 EXPECT_EQ(2U, page_added_count()); 450 EXPECT_EQ(2U, page_added_count());
432 EXPECT_EQ(1U, model_removed_count()); 451 EXPECT_EQ(1U, model_removed_count());
433 // the same page should be simply overridden. 452 // the same page should be simply overridden.
434 GetAllPages(); 453 GetAllPages();
435 EXPECT_EQ(1U, all_pages().size()); 454 EXPECT_EQ(1U, all_pages().size());
436 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); 455 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url);
437 } 456 }
438 457
458 // Triggers two snapshot captures via the download after a page was loaded page
459 // and saved twice by last_n. Should end up with three offline pages: two from
460 // downloads (which shouldn't replace each other) and one from last_n.
461 TEST_F(RecentTabHelperTest, TwoDownloadCapturesInARowSamePage) {
462 NavigateAndCommit(kTestPageUrl);
463 // Executes a regular load with snapshots taken by last_n.
464 recent_tab_helper()->DocumentAvailableInMainFrame();
465 RunUntilIdle();
466 FastForwardSnapshotController();
467 RunUntilIdle();
468 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
469 FastForwardSnapshotController();
470 RunUntilIdle();
471 // Checks that two last_n snapshots were created but only one was kept.
472 EXPECT_EQ(2U, page_added_count());
473 EXPECT_EQ(1U, model_removed_count());
474 GetAllPages();
475 EXPECT_EQ(1U, all_pages().size());
476 EXPECT_EQ(kTestPageUrl, all_pages()[0].url);
477 int64_t first_offline_id = all_pages()[0].offline_id;
478
479 // Request the 1st offline download.
480 const int64_t second_offline_id = 123l;
fgorski 2016/12/13 17:17:38 nit: 123L
carlosk 2016/12/13 20:04:19 Done here and in the last test in this file where
481 ASSERT_NE(second_offline_id, first_offline_id);
fgorski 2016/12/13 17:17:38 how is the first offline id assigned?
carlosk 2016/12/13 20:04:19 The first entry is a last_n one and its offline ID
482 recent_tab_helper()->ObserveAndDownloadCurrentPage(
483 ClientId("download", "id2"), second_offline_id);
484 RunUntilIdle();
485 GetAllPages();
486 // Checks that both the previous last_n and download snapshots are present.
487 ASSERT_EQ(2U, all_pages().size());
488 EXPECT_NE(-1, FindPageIndexForOfflineId(first_offline_id));
489 {
fgorski 2016/12/13 17:17:38 nit: I am not sure why you are using {} here. Tha
carlosk 2016/12/13 20:04:20 I use them to isolate the internal scope and avoid
490 int second_index = FindPageIndexForOfflineId(second_offline_id);
491 EXPECT_NE(-1, second_index);
492 EXPECT_EQ(kTestPageUrl, all_pages()[second_index].url);
493 EXPECT_EQ("download", all_pages()[second_index].client_id.name_space);
494 EXPECT_EQ("id2", all_pages()[second_index].client_id.id);
495 }
496
497 // Request the 2nd offline download.
498 const int64_t third_offline_id = 456l;
fgorski 2016/12/13 17:17:38 ditto
carlosk 2016/12/13 20:04:20 Done.
499 ASSERT_NE(third_offline_id, first_offline_id);
500 ASSERT_NE(third_offline_id, second_offline_id);
fgorski 2016/12/13 17:17:38 you control both IDs. I am not sure this line make
carlosk 2016/12/13 20:04:19 Done.
501 recent_tab_helper()->ObserveAndDownloadCurrentPage(
502 ClientId("download", "id3"), third_offline_id);
503 RunUntilIdle();
504 GetAllPages();
505 ASSERT_EQ(3U, all_pages().size());
506 // Checks that the previous last_n and download snapshots are still present
507 // and the new downloaded one was added.
508 EXPECT_NE(-1, FindPageIndexForOfflineId(first_offline_id));
509 EXPECT_NE(-1, FindPageIndexForOfflineId(second_offline_id));
510 {
511 int third_index = FindPageIndexForOfflineId(third_offline_id);
fgorski 2016/12/13 17:17:38 given how you use the index below, wouldn't it mak
carlosk 2016/12/13 20:04:19 I agree with what you're saying given the usage be
512 EXPECT_NE(-1, third_index);
513 EXPECT_EQ(kTestPageUrl, all_pages()[third_index].url);
514 EXPECT_EQ("download", all_pages()[third_index].client_id.name_space);
515 EXPECT_EQ("id3", all_pages()[third_index].client_id.id);
516 }
517 }
518
439 // Simulates an error (disconnection) during the load of a page. Should end up 519 // Simulates an error (disconnection) during the load of a page. Should end up
440 // with no offline pages. 520 // with no offline pages.
441 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { 521 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) {
442 FailLoad(kTestPageUrl); 522 FailLoad(kTestPageUrl);
443 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 523 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
444 FastForwardSnapshotController(); 524 FastForwardSnapshotController();
445 RunUntilIdle(); 525 RunUntilIdle();
446 EXPECT_TRUE(model()->is_loaded()); 526 EXPECT_TRUE(model()->is_loaded());
447 GetAllPages(); 527 GetAllPages();
448 EXPECT_EQ(0U, all_pages().size()); 528 EXPECT_EQ(0U, all_pages().size());
449 } 529 }
450 530
451 // Checks that no snapshots are created if the Offline Page Cache feature is 531 // Checks that no snapshots are created if the Offline Page Cache feature is
452 // disabled. 532 // disabled.
453 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { 533 TEST_F(RecentTabHelperTest, FeatureNotEnabled) {
454 base::test::ScopedFeatureList scoped_feature_list; 534 base::test::ScopedFeatureList scoped_feature_list;
455 scoped_feature_list.Init(); 535 scoped_feature_list.Init();
456 NavigateAndCommit(kTestPageUrl); 536 NavigateAndCommit(kTestPageUrl);
457 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 537 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
458 FastForwardSnapshotController(); 538 FastForwardSnapshotController();
459 RunUntilIdle(); 539 RunUntilIdle();
460 EXPECT_TRUE(model()->is_loaded()); 540 EXPECT_TRUE(model()->is_loaded());
461 GetAllPages(); 541 GetAllPages();
462 // No page should be captured. 542 // No page should be captured.
463 EXPECT_EQ(0U, all_pages().size()); 543 EXPECT_EQ(0U, all_pages().size());
464 } 544 }
465 545
466 // Simulates a download request to offline the current page. Should end up with 546 // Simulates a download request to offline the current page. Should end up with
467 // no offline pages. 547 // one offline pages.
468 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { 548 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) {
469 NavigateAndCommit(kTestPageUrl); 549 NavigateAndCommit(kTestPageUrl);
470 recent_tab_helper()->ObserveAndDownloadCurrentPage( 550 recent_tab_helper()->ObserveAndDownloadCurrentPage(
471 ClientId("download", "id1"), 153l); 551 ClientId("download", "id1"), 153l);
472 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 552 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
473 RunUntilIdle(); 553 RunUntilIdle();
474 EXPECT_TRUE(model()->is_loaded()); 554 EXPECT_TRUE(model()->is_loaded());
475 GetAllPages(); 555 GetAllPages();
476 EXPECT_EQ(1U, all_pages().size()); 556 EXPECT_EQ(1U, all_pages().size());
477 const OfflinePageItem& page = all_pages()[0]; 557 const OfflinePageItem& page = all_pages()[0];
478 EXPECT_EQ(kTestPageUrl, page.url); 558 EXPECT_EQ(kTestPageUrl, page.url);
479 EXPECT_EQ("download", page.client_id.name_space); 559 EXPECT_EQ("download", page.client_id.name_space);
480 EXPECT_EQ("id1", page.client_id.id); 560 EXPECT_EQ("id1", page.client_id.id);
481 EXPECT_EQ(153l, page.offline_id); 561 EXPECT_EQ(153l, page.offline_id);
482 } 562 }
483 563
484 } // namespace offline_pages 564 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698