Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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(); |
|
carlosk
2016/12/13 20:04:20
I left this change because it makes the code more
| |
| 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 Loading... | |
| 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 Loading... | |
| 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 = 123L; | |
| 478 ASSERT_NE(second_offline_id, first_offline_id); | |
|
fgorski
2016/12/13 21:24:17
Since you know the first_offline_id, and you can s
carlosk
2016/12/13 21:51:05
Done.
| |
| 479 recent_tab_helper()->ObserveAndDownloadCurrentPage( | |
| 480 ClientId("download", "id2"), second_offline_id); | |
| 481 RunUntilIdle(); | |
| 482 GetAllPages(); | |
| 483 // Checks that both the previous last_n and download snapshots are present. | |
| 484 ASSERT_EQ(2U, all_pages().size()); | |
| 485 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id)); | |
| 486 const OfflinePageItem* second_page = FindPageForOfflineId(second_offline_id); | |
| 487 ASSERT_NE(nullptr, second_page); | |
| 488 EXPECT_EQ(kTestPageUrl, second_page->url); | |
| 489 EXPECT_EQ("download", second_page->client_id.name_space); | |
| 490 EXPECT_EQ("id2", second_page->client_id.id); | |
| 491 | |
| 492 // Request the 2nd offline download. | |
| 493 const int64_t third_offline_id = 456L; | |
| 494 ASSERT_NE(third_offline_id, first_offline_id); | |
| 495 recent_tab_helper()->ObserveAndDownloadCurrentPage( | |
| 496 ClientId("download", "id3"), third_offline_id); | |
| 497 RunUntilIdle(); | |
| 498 GetAllPages(); | |
| 499 ASSERT_EQ(3U, all_pages().size()); | |
| 500 // Checks that the previous last_n and download snapshots are still present | |
| 501 // and the new downloaded one was added. | |
| 502 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id)); | |
| 503 EXPECT_NE(nullptr, FindPageForOfflineId(second_offline_id)); | |
| 504 const OfflinePageItem* third_page = FindPageForOfflineId(third_offline_id); | |
| 505 ASSERT_NE(nullptr, third_page); | |
| 506 EXPECT_EQ(kTestPageUrl, third_page->url); | |
| 507 EXPECT_EQ("download", third_page->client_id.name_space); | |
| 508 EXPECT_EQ("id3", third_page->client_id.id); | |
| 509 } | |
| 510 | |
| 439 // Simulates an error (disconnection) during the load of a page. Should end up | 511 // Simulates an error (disconnection) during the load of a page. Should end up |
| 440 // with no offline pages. | 512 // with no offline pages. |
| 441 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { | 513 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { |
| 442 FailLoad(kTestPageUrl); | 514 FailLoad(kTestPageUrl); |
| 443 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 515 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 444 FastForwardSnapshotController(); | 516 FastForwardSnapshotController(); |
| 445 RunUntilIdle(); | 517 RunUntilIdle(); |
| 446 EXPECT_TRUE(model()->is_loaded()); | 518 EXPECT_TRUE(model()->is_loaded()); |
| 447 GetAllPages(); | 519 GetAllPages(); |
| 448 EXPECT_EQ(0U, all_pages().size()); | 520 EXPECT_EQ(0U, all_pages().size()); |
| 449 } | 521 } |
| 450 | 522 |
| 451 // Checks that no snapshots are created if the Offline Page Cache feature is | 523 // Checks that no snapshots are created if the Offline Page Cache feature is |
| 452 // disabled. | 524 // disabled. |
| 453 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { | 525 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { |
| 454 base::test::ScopedFeatureList scoped_feature_list; | 526 base::test::ScopedFeatureList scoped_feature_list; |
| 455 scoped_feature_list.Init(); | 527 scoped_feature_list.Init(); |
| 456 NavigateAndCommit(kTestPageUrl); | 528 NavigateAndCommit(kTestPageUrl); |
| 457 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 529 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 458 FastForwardSnapshotController(); | 530 FastForwardSnapshotController(); |
| 459 RunUntilIdle(); | 531 RunUntilIdle(); |
| 460 EXPECT_TRUE(model()->is_loaded()); | 532 EXPECT_TRUE(model()->is_loaded()); |
| 461 GetAllPages(); | 533 GetAllPages(); |
| 462 // No page should be captured. | 534 // No page should be captured. |
| 463 EXPECT_EQ(0U, all_pages().size()); | 535 EXPECT_EQ(0U, all_pages().size()); |
| 464 } | 536 } |
| 465 | 537 |
| 466 // Simulates a download request to offline the current page. Should end up with | 538 // Simulates a download request to offline the current page. Should end up with |
| 467 // no offline pages. | 539 // one offline pages. |
| 468 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { | 540 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { |
| 469 NavigateAndCommit(kTestPageUrl); | 541 NavigateAndCommit(kTestPageUrl); |
| 470 recent_tab_helper()->ObserveAndDownloadCurrentPage( | 542 recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| 471 ClientId("download", "id1"), 153l); | 543 ClientId("download", "id1"), 153L); |
| 472 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 544 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 473 RunUntilIdle(); | 545 RunUntilIdle(); |
| 474 EXPECT_TRUE(model()->is_loaded()); | 546 EXPECT_TRUE(model()->is_loaded()); |
| 475 GetAllPages(); | 547 GetAllPages(); |
| 476 EXPECT_EQ(1U, all_pages().size()); | 548 EXPECT_EQ(1U, all_pages().size()); |
| 477 const OfflinePageItem& page = all_pages()[0]; | 549 const OfflinePageItem& page = all_pages()[0]; |
| 478 EXPECT_EQ(kTestPageUrl, page.url); | 550 EXPECT_EQ(kTestPageUrl, page.url); |
| 479 EXPECT_EQ("download", page.client_id.name_space); | 551 EXPECT_EQ("download", page.client_id.name_space); |
| 480 EXPECT_EQ("id1", page.client_id.id); | 552 EXPECT_EQ("id1", page.client_id.id); |
| 481 EXPECT_EQ(153l, page.offline_id); | 553 EXPECT_EQ(153L, page.offline_id); |
| 482 } | 554 } |
| 483 | 555 |
| 484 } // namespace offline_pages | 556 } // namespace offline_pages |
| OLD | NEW |