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

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: Minor test simplification. 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
« no previous file with comments | « chrome/browser/android/offline_pages/recent_tab_helper.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "
16 #include "chrome/browser/android/offline_pages/test_request_coordinator_builder. h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "components/offline_pages/core/offline_page_feature.h" 18 #include "components/offline_pages/core/offline_page_feature.h"
17 #include "components/offline_pages/core/offline_page_item.h" 19 #include "components/offline_pages/core/offline_page_item.h"
18 #include "components/offline_pages/core/offline_page_model.h" 20 #include "components/offline_pages/core/offline_page_model.h"
19 #include "components/offline_pages/core/offline_page_test_archiver.h" 21 #include "components/offline_pages/core/offline_page_test_archiver.h"
20 #include "content/public/browser/navigation_entry.h" 22 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/navigation_handle.h" 23 #include "content/public/browser/navigation_handle.h"
22 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
23 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
24 26
25 namespace offline_pages { 27 namespace offline_pages {
26 28
27 namespace { 29 namespace {
28 const GURL kTestPageUrl("http://mystery.site/foo.html"); 30 const GURL kTestPageUrl("http://mystery.site/foo.html");
29 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html"); 31 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html");
30 const int kTabId = 153; 32 const int kTabId = 153;
31 } 33 } // namespace
32 34
33 class TestDelegate: public RecentTabHelper::Delegate { 35 class TestDelegate: public RecentTabHelper::Delegate {
34 public: 36 public:
35 const size_t kArchiveSizeToReport = 1234; 37 const size_t kArchiveSizeToReport = 1234;
36 38
37 explicit TestDelegate( 39 explicit TestDelegate(
38 OfflinePageTestArchiver::Observer* observer, 40 OfflinePageTestArchiver::Observer* observer,
39 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 41 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
40 int tab_id, 42 int tab_id,
41 bool tab_id_result); 43 bool tab_id_result);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 a OfflinePageItem pointer from |all_pages| that matches the
97 // provided |offline_id|. If a match is not found returns nullptr.
98 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) {
99 for (const OfflinePageItem& page : all_pages_) {
100 if (page.offline_id == offline_id)
101 return &page;
102 }
103 return nullptr;
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 factories for testing.
172 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( 184 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
173 browser_context(), BuildTestOfflinePageModel); 185 browser_context(), BuildTestOfflinePageModel);
174 RunUntilIdle(); 186 RunUntilIdle();
187 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse(
188 browser_context(), BuildTestRequestCoordinator);
189 RunUntilIdle();
175 190
176 RecentTabHelper::CreateForWebContents(web_contents()); 191 RecentTabHelper::CreateForWebContents(web_contents());
177 recent_tab_helper_ = 192 recent_tab_helper_ =
178 RecentTabHelper::FromWebContents(web_contents()); 193 RecentTabHelper::FromWebContents(web_contents());
179 194
180 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>( 195 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>(
181 this, task_runner(), kTabId, true)); 196 this, task_runner(), kTabId, true));
182 197
183 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context()); 198 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context());
184 model_->AddObserver(this); 199 model_->AddObserver(this);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 // the same page should be simply overridden. 295 // the same page should be simply overridden.
281 GetAllPages(); 296 GetAllPages();
282 EXPECT_EQ(1U, all_pages().size()); 297 EXPECT_EQ(1U, all_pages().size());
283 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); 298 EXPECT_EQ(kTestPageUrl, all_pages()[0].url);
284 EXPECT_NE(first_offline_id, all_pages()[0].offline_id); 299 EXPECT_NE(first_offline_id, all_pages()[0].offline_id);
285 } 300 }
286 301
287 // Triggers two snapshot captures during a single page load, where the 2nd one 302 // 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 303 // fails. Should end up with one offline page (the 1st, successful snapshot
289 // should be kept). 304 // should be kept).
290 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoadSecondFails) { 305 // TODO(carlosk): re-enable once https://crbug.com/655697 is fixed, again.
306 TEST_F(RecentTabHelperTest, DISABLED_TwoCapturesSamePageLoadSecondFails) {
291 NavigateAndCommit(kTestPageUrl); 307 NavigateAndCommit(kTestPageUrl);
292 // Triggers snapshot after a time delay. 308 // Triggers snapshot after a time delay.
293 recent_tab_helper()->DocumentAvailableInMainFrame(); 309 recent_tab_helper()->DocumentAvailableInMainFrame();
294 RunUntilIdle(); 310 RunUntilIdle();
295 EXPECT_TRUE(model()->is_loaded()); 311 EXPECT_TRUE(model()->is_loaded());
296 EXPECT_EQ(0U, page_added_count()); 312 EXPECT_EQ(0U, page_added_count());
297 // Move the snapshot controller's time forward so it gets past timeouts. 313 // Move the snapshot controller's time forward so it gets past timeouts.
298 FastForwardSnapshotController(); 314 FastForwardSnapshotController();
299 RunUntilIdle(); 315 RunUntilIdle();
300 EXPECT_EQ(1U, page_added_count()); 316 EXPECT_EQ(1U, page_added_count());
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 FastForwardSnapshotController(); 445 FastForwardSnapshotController();
430 RunUntilIdle(); 446 RunUntilIdle();
431 EXPECT_EQ(2U, page_added_count()); 447 EXPECT_EQ(2U, page_added_count());
432 EXPECT_EQ(1U, model_removed_count()); 448 EXPECT_EQ(1U, model_removed_count());
433 // the same page should be simply overridden. 449 // the same page should be simply overridden.
434 GetAllPages(); 450 GetAllPages();
435 EXPECT_EQ(1U, all_pages().size()); 451 EXPECT_EQ(1U, all_pages().size());
436 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); 452 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url);
437 } 453 }
438 454
455 // Triggers two snapshot captures via the download after a page was loaded page
456 // and saved twice by last_n. Should end up with three offline pages: two from
457 // downloads (which shouldn't replace each other) and one from last_n.
458 TEST_F(RecentTabHelperTest, TwoDownloadCapturesInARowSamePage) {
459 NavigateAndCommit(kTestPageUrl);
460 // Executes a regular load with snapshots taken by last_n.
461 recent_tab_helper()->DocumentAvailableInMainFrame();
462 RunUntilIdle();
463 FastForwardSnapshotController();
464 RunUntilIdle();
465 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
466 FastForwardSnapshotController();
467 RunUntilIdle();
468 // Checks that two last_n snapshots were created but only one was kept.
469 EXPECT_EQ(2U, page_added_count());
470 EXPECT_EQ(1U, model_removed_count());
471 GetAllPages();
472 EXPECT_EQ(1U, all_pages().size());
473 EXPECT_EQ(kTestPageUrl, all_pages()[0].url);
474 int64_t first_offline_id = all_pages()[0].offline_id;
475
476 // Request the 1st offline download.
477 const int64_t second_offline_id = first_offline_id + 1;
478 recent_tab_helper()->ObserveAndDownloadCurrentPage(
479 ClientId("download", "id2"), second_offline_id);
480 RunUntilIdle();
481 GetAllPages();
482 // Checks that both the previous last_n and download snapshots are present.
483 ASSERT_EQ(2U, all_pages().size());
484 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id));
485 const OfflinePageItem* second_page = FindPageForOfflineId(second_offline_id);
486 ASSERT_NE(nullptr, second_page);
487 EXPECT_EQ(kTestPageUrl, second_page->url);
488 EXPECT_EQ("download", second_page->client_id.name_space);
489 EXPECT_EQ("id2", second_page->client_id.id);
490
491 // Request the 2nd offline download.
492 const int64_t third_offline_id = first_offline_id + 2;
493 recent_tab_helper()->ObserveAndDownloadCurrentPage(
494 ClientId("download", "id3"), third_offline_id);
495 RunUntilIdle();
496 GetAllPages();
497 ASSERT_EQ(3U, all_pages().size());
498 // Checks that the previous last_n and download snapshots are still present
499 // and the new downloaded one was added.
500 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id));
501 EXPECT_NE(nullptr, FindPageForOfflineId(second_offline_id));
502 const OfflinePageItem* third_page = FindPageForOfflineId(third_offline_id);
503 ASSERT_NE(nullptr, third_page);
504 EXPECT_EQ(kTestPageUrl, third_page->url);
505 EXPECT_EQ("download", third_page->client_id.name_space);
506 EXPECT_EQ("id3", third_page->client_id.id);
507 }
508
439 // Simulates an error (disconnection) during the load of a page. Should end up 509 // Simulates an error (disconnection) during the load of a page. Should end up
440 // with no offline pages. 510 // with no offline pages.
441 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { 511 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) {
442 FailLoad(kTestPageUrl); 512 FailLoad(kTestPageUrl);
443 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 513 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
444 FastForwardSnapshotController(); 514 FastForwardSnapshotController();
445 RunUntilIdle(); 515 RunUntilIdle();
446 EXPECT_TRUE(model()->is_loaded()); 516 EXPECT_TRUE(model()->is_loaded());
447 GetAllPages(); 517 GetAllPages();
448 EXPECT_EQ(0U, all_pages().size()); 518 EXPECT_EQ(0U, all_pages().size());
449 } 519 }
450 520
451 // Checks that no snapshots are created if the Offline Page Cache feature is 521 // Checks that no snapshots are created if the Offline Page Cache feature is
452 // disabled. 522 // disabled.
453 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { 523 TEST_F(RecentTabHelperTest, FeatureNotEnabled) {
454 base::test::ScopedFeatureList scoped_feature_list; 524 base::test::ScopedFeatureList scoped_feature_list;
455 scoped_feature_list.Init(); 525 scoped_feature_list.Init();
456 NavigateAndCommit(kTestPageUrl); 526 NavigateAndCommit(kTestPageUrl);
457 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 527 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
458 FastForwardSnapshotController(); 528 FastForwardSnapshotController();
459 RunUntilIdle(); 529 RunUntilIdle();
460 EXPECT_TRUE(model()->is_loaded()); 530 EXPECT_TRUE(model()->is_loaded());
461 GetAllPages(); 531 GetAllPages();
462 // No page should be captured. 532 // No page should be captured.
463 EXPECT_EQ(0U, all_pages().size()); 533 EXPECT_EQ(0U, all_pages().size());
464 } 534 }
465 535
466 // Simulates a download request to offline the current page. Should end up with 536 // Simulates a download request to offline the current page. Should end up with
467 // no offline pages. 537 // one offline pages.
468 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { 538 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) {
469 NavigateAndCommit(kTestPageUrl); 539 NavigateAndCommit(kTestPageUrl);
470 recent_tab_helper()->ObserveAndDownloadCurrentPage( 540 recent_tab_helper()->ObserveAndDownloadCurrentPage(
471 ClientId("download", "id1"), 153l); 541 ClientId("download", "id1"), 153L);
472 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); 542 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
473 RunUntilIdle(); 543 RunUntilIdle();
474 EXPECT_TRUE(model()->is_loaded()); 544 EXPECT_TRUE(model()->is_loaded());
475 GetAllPages(); 545 GetAllPages();
476 EXPECT_EQ(1U, all_pages().size()); 546 EXPECT_EQ(1U, all_pages().size());
477 const OfflinePageItem& page = all_pages()[0]; 547 const OfflinePageItem& page = all_pages()[0];
478 EXPECT_EQ(kTestPageUrl, page.url); 548 EXPECT_EQ(kTestPageUrl, page.url);
479 EXPECT_EQ("download", page.client_id.name_space); 549 EXPECT_EQ("download", page.client_id.name_space);
480 EXPECT_EQ("id1", page.client_id.id); 550 EXPECT_EQ("id1", page.client_id.id);
481 EXPECT_EQ(153l, page.offline_id); 551 EXPECT_EQ(153L, page.offline_id);
482 } 552 }
483 553
484 } // namespace offline_pages 554 } // namespace offline_pages
OLDNEW
« no previous file with comments | « chrome/browser/android/offline_pages/recent_tab_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698