| 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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 46 int tab_id, | 46 int tab_id, |
| 47 bool tab_id_result); | 47 bool tab_id_result); |
| 48 ~TestDelegate() override {} | 48 ~TestDelegate() override {} |
| 49 | 49 |
| 50 std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( | 50 std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( |
| 51 content::WebContents* web_contents) override; | 51 content::WebContents* web_contents) override; |
| 52 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override; | 52 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override; |
| 53 // There is no expectations that tab_id is always present. | 53 // There is no expectations that tab_id is always present. |
| 54 bool GetTabId(content::WebContents* web_contents, int* tab_id) override; | 54 bool GetTabId(content::WebContents* web_contents, int* tab_id) override; |
| 55 bool IsLowEndDevice() override { return is_low_end_device_; } |
| 55 | 56 |
| 56 void set_archive_result( | 57 void set_archive_result( |
| 57 offline_pages::OfflinePageArchiver::ArchiverResult result) { | 58 offline_pages::OfflinePageArchiver::ArchiverResult result) { |
| 58 archive_result_ = result; | 59 archive_result_ = result; |
| 59 } | 60 } |
| 60 | 61 |
| 61 void set_archive_size(int64_t size) { archive_size_ = size; } | 62 void set_archive_size(int64_t size) { archive_size_ = size; } |
| 62 | 63 |
| 64 void SetAsLowEndDevice() { is_low_end_device_ = true; } |
| 65 |
| 63 private: | 66 private: |
| 64 OfflinePageTestArchiver::Observer* observer_; // observer owns this. | 67 OfflinePageTestArchiver::Observer* observer_; // observer owns this. |
| 65 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 68 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 66 int tab_id_; | 69 int tab_id_; |
| 67 bool tab_id_result_; | 70 bool tab_id_result_; |
| 68 | 71 |
| 69 // These values can be updated so that new OfflinePageTestArchiver instances | 72 // These values can be updated so that new OfflinePageTestArchiver instances |
| 70 // will return different results. | 73 // will return different results. |
| 71 offline_pages::OfflinePageArchiver::ArchiverResult archive_result_ = | 74 offline_pages::OfflinePageArchiver::ArchiverResult archive_result_ = |
| 72 offline_pages::OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED; | 75 offline_pages::OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED; |
| 73 int64_t archive_size_ = kArchiveSizeToReport; | 76 int64_t archive_size_ = kArchiveSizeToReport; |
| 77 bool is_low_end_device_ = false; |
| 74 }; | 78 }; |
| 75 | 79 |
| 76 class RecentTabHelperTest | 80 class RecentTabHelperTest |
| 77 : public ChromeRenderViewHostTestHarness, | 81 : public ChromeRenderViewHostTestHarness, |
| 78 public OfflinePageModel::Observer, | 82 public OfflinePageModel::Observer, |
| 79 public OfflinePageTestArchiver::Observer { | 83 public OfflinePageTestArchiver::Observer { |
| 80 public: | 84 public: |
| 81 RecentTabHelperTest(); | 85 RecentTabHelperTest(); |
| 82 ~RecentTabHelperTest() override {} | 86 ~RecentTabHelperTest() override {} |
| 83 | 87 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 97 // and when reusing the same URL that will be considered a same page | 101 // and when reusing the same URL that will be considered a same page |
| 98 // navigation. | 102 // navigation. |
| 99 void NavigateAndCommitTyped(const GURL& url); | 103 void NavigateAndCommitTyped(const GURL& url); |
| 100 | 104 |
| 101 ClientId NewDownloadClientId(); | 105 ClientId NewDownloadClientId(); |
| 102 | 106 |
| 103 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } | 107 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } |
| 104 | 108 |
| 105 OfflinePageModel* model() const { return model_; } | 109 OfflinePageModel* model() const { return model_; } |
| 106 | 110 |
| 111 TestDelegate* default_test_delegate() { return default_test_delegate_; } |
| 112 |
| 107 // Returns a OfflinePageItem pointer from |all_pages| that matches the | 113 // Returns a OfflinePageItem pointer from |all_pages| that matches the |
| 108 // provided |offline_id|. If a match is not found returns nullptr. | 114 // provided |offline_id|. If a match is not found returns nullptr. |
| 109 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) { | 115 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) { |
| 110 for (const OfflinePageItem& page : GetAllPages()) { | 116 for (const OfflinePageItem& page : GetAllPages()) { |
| 111 if (page.offline_id == offline_id) | 117 if (page.offline_id == offline_id) |
| 112 return &page; | 118 return &page; |
| 113 } | 119 } |
| 114 return nullptr; | 120 return nullptr; |
| 115 } | 121 } |
| 116 | 122 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 134 model_removed_count_++; | 140 model_removed_count_++; |
| 135 all_pages_needs_updating_ = true; | 141 all_pages_needs_updating_ = true; |
| 136 } | 142 } |
| 137 | 143 |
| 138 // OfflinePageTestArchiver::Observer | 144 // OfflinePageTestArchiver::Observer |
| 139 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} | 145 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} |
| 140 | 146 |
| 141 private: | 147 private: |
| 142 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); | 148 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); |
| 143 | 149 |
| 144 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. | 150 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. |
| 145 OfflinePageModel* model_; // Keyed service | 151 OfflinePageModel* model_; // Keyed service. |
| 152 TestDelegate* default_test_delegate_; // Created at SetUp. |
| 146 size_t page_added_count_; | 153 size_t page_added_count_; |
| 147 size_t model_removed_count_; | 154 size_t model_removed_count_; |
| 148 std::vector<OfflinePageItem> all_pages_; | 155 std::vector<OfflinePageItem> all_pages_; |
| 149 bool all_pages_needs_updating_; | 156 bool all_pages_needs_updating_; |
| 150 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 157 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 151 base::test::ScopedFeatureList scoped_feature_list_; | 158 base::test::ScopedFeatureList scoped_feature_list_; |
| 152 | 159 |
| 153 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; | 160 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; |
| 154 | 161 |
| 155 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); | 162 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 180 } | 187 } |
| 181 // There is no expectations that tab_id is always present. | 188 // There is no expectations that tab_id is always present. |
| 182 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { | 189 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { |
| 183 *tab_id = tab_id_; | 190 *tab_id = tab_id_; |
| 184 return tab_id_result_; | 191 return tab_id_result_; |
| 185 } | 192 } |
| 186 | 193 |
| 187 RecentTabHelperTest::RecentTabHelperTest() | 194 RecentTabHelperTest::RecentTabHelperTest() |
| 188 : recent_tab_helper_(nullptr), | 195 : recent_tab_helper_(nullptr), |
| 189 model_(nullptr), | 196 model_(nullptr), |
| 197 default_test_delegate_(nullptr), |
| 190 page_added_count_(0), | 198 page_added_count_(0), |
| 191 model_removed_count_(0), | 199 model_removed_count_(0), |
| 192 all_pages_needs_updating_(true), | 200 all_pages_needs_updating_(true), |
| 193 task_runner_(new base::TestMockTimeTaskRunner), | 201 task_runner_(new base::TestMockTimeTaskRunner), |
| 194 weak_ptr_factory_(this) {} | 202 weak_ptr_factory_(this) {} |
| 195 | 203 |
| 196 void RecentTabHelperTest::SetUp() { | 204 void RecentTabHelperTest::SetUp() { |
| 197 ChromeRenderViewHostTestHarness::SetUp(); | 205 ChromeRenderViewHostTestHarness::SetUp(); |
| 198 | 206 |
| 199 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); | 207 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); |
| 200 // Sets up the factories for testing. | 208 // Sets up the factories for testing. |
| 201 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | 209 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 202 browser_context(), BuildTestOfflinePageModel); | 210 browser_context(), BuildTestOfflinePageModel); |
| 203 RunUntilIdle(); | 211 RunUntilIdle(); |
| 204 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse( | 212 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse( |
| 205 browser_context(), BuildTestRequestCoordinator); | 213 browser_context(), BuildTestRequestCoordinator); |
| 206 RunUntilIdle(); | 214 RunUntilIdle(); |
| 207 | 215 |
| 208 RecentTabHelper::CreateForWebContents(web_contents()); | 216 RecentTabHelper::CreateForWebContents(web_contents()); |
| 209 recent_tab_helper_ = RecentTabHelper::FromWebContents(web_contents()); | 217 recent_tab_helper_ = RecentTabHelper::FromWebContents(web_contents()); |
| 210 | 218 |
| 211 recent_tab_helper_->SetDelegate(base::MakeUnique<TestDelegate>( | 219 std::unique_ptr<TestDelegate> test_delegate( |
| 212 this, task_runner(), kTabId, true)); | 220 new TestDelegate(this, task_runner(), kTabId, true)); |
| 221 default_test_delegate_ = test_delegate.get(); |
| 222 recent_tab_helper_->SetDelegate(std::move(test_delegate)); |
| 213 | 223 |
| 214 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 224 model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 215 model_->AddObserver(this); | 225 model_->AddObserver(this); |
| 216 } | 226 } |
| 217 | 227 |
| 218 void RecentTabHelperTest::FailLoad(const GURL& url) { | 228 void RecentTabHelperTest::FailLoad(const GURL& url) { |
| 219 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, | 229 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 220 std::string()); | 230 std::string()); |
| 221 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); | 231 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); |
| 222 content::RenderFrameHostTester::For(main_rfh())-> | 232 content::RenderFrameHostTester::For(main_rfh())-> |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 recent_tab_helper()->WasHidden(); | 336 recent_tab_helper()->WasHidden(); |
| 327 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), | 337 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), |
| 328 123L); | 338 123L); |
| 329 RunUntilIdle(); | 339 RunUntilIdle(); |
| 330 EXPECT_TRUE(model()->is_loaded()); | 340 EXPECT_TRUE(model()->is_loaded()); |
| 331 // No page should be captured. | 341 // No page should be captured. |
| 332 EXPECT_EQ(0U, page_added_count()); | 342 EXPECT_EQ(0U, page_added_count()); |
| 333 ASSERT_EQ(0U, GetAllPages().size()); | 343 ASSERT_EQ(0U, GetAllPages().size()); |
| 334 } | 344 } |
| 335 | 345 |
| 346 // Checks that last_n is disabled if the device is low-end (aka svelte) but that |
| 347 // download requests still work. |
| 348 TEST_F(RecentTabHelperTest, LastNDisabledOnSvelte) { |
| 349 // Simulates a low end device. |
| 350 default_test_delegate()->SetAsLowEndDevice(); |
| 351 |
| 352 // Navigate and finish loading then hide the tab. Nothing should be saved. |
| 353 NavigateAndCommit(kTestPageUrl); |
| 354 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 355 FastForwardSnapshotController(); |
| 356 recent_tab_helper()->WasHidden(); |
| 357 RunUntilIdle(); |
| 358 EXPECT_TRUE(model()->is_loaded()); |
| 359 EXPECT_EQ(0U, page_added_count()); |
| 360 ASSERT_EQ(0U, GetAllPages().size()); |
| 361 |
| 362 // But the following download request should work normally |
| 363 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), |
| 364 123L); |
| 365 RunUntilIdle(); |
| 366 EXPECT_EQ(1U, page_added_count()); |
| 367 ASSERT_EQ(1U, GetAllPages().size()); |
| 368 } |
| 369 |
| 336 // Triggers two last_n snapshot captures during a single page load. Should end | 370 // Triggers two last_n snapshot captures during a single page load. Should end |
| 337 // up with one snapshot, the 1st being replaced by the 2nd. | 371 // up with one snapshot, the 1st being replaced by the 2nd. |
| 338 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { | 372 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { |
| 339 NavigateAndCommit(kTestPageUrl); | 373 NavigateAndCommit(kTestPageUrl); |
| 340 // Set page loading state to the 1st snapshot-able stage. No capture so far. | 374 // Set page loading state to the 1st snapshot-able stage. No capture so far. |
| 341 recent_tab_helper()->DocumentAvailableInMainFrame(); | 375 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 342 FastForwardSnapshotController(); | 376 FastForwardSnapshotController(); |
| 343 RunUntilIdle(); | 377 RunUntilIdle(); |
| 344 EXPECT_TRUE(model()->is_loaded()); | 378 EXPECT_TRUE(model()->is_loaded()); |
| 345 EXPECT_EQ(0U, page_added_count()); | 379 EXPECT_EQ(0U, page_added_count()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 recent_tab_helper()->DocumentAvailableInMainFrame(); | 417 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 384 FastForwardSnapshotController(); | 418 FastForwardSnapshotController(); |
| 385 recent_tab_helper()->WasHidden(); | 419 recent_tab_helper()->WasHidden(); |
| 386 RunUntilIdle(); | 420 RunUntilIdle(); |
| 387 EXPECT_EQ(1U, page_added_count()); | 421 EXPECT_EQ(1U, page_added_count()); |
| 388 EXPECT_EQ(0U, model_removed_count()); | 422 EXPECT_EQ(0U, model_removed_count()); |
| 389 ASSERT_EQ(1U, GetAllPages().size()); | 423 ASSERT_EQ(1U, GetAllPages().size()); |
| 390 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); | 424 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 391 int64_t first_offline_id = GetAllPages()[0].offline_id; | 425 int64_t first_offline_id = GetAllPages()[0].offline_id; |
| 392 | 426 |
| 393 // Sets a new delegate that will make the second snapshot fail. | 427 // Updates the delegate so that will make the second snapshot fail. |
| 394 TestDelegate* failing_delegate = | 428 default_test_delegate()->set_archive_size(-1); |
| 395 new TestDelegate(this, task_runner(), kTabId, true); | 429 default_test_delegate()->set_archive_result( |
| 396 failing_delegate->set_archive_size(-1); | |
| 397 failing_delegate->set_archive_result( | |
| 398 offline_pages::OfflinePageArchiver::ArchiverResult:: | 430 offline_pages::OfflinePageArchiver::ArchiverResult:: |
| 399 ERROR_ARCHIVE_CREATION_FAILED); | 431 ERROR_ARCHIVE_CREATION_FAILED); |
| 400 recent_tab_helper()->SetDelegate( | |
| 401 std::unique_ptr<TestDelegate>(failing_delegate)); | |
| 402 | 432 |
| 403 // Advance loading to the 2nd and final stage and then hide the tab. A new | 433 // Advance loading to the 2nd and final stage and then hide the tab. A new |
| 404 // capture is requested but its creation will fail. The exact same snapshot | 434 // capture is requested but its creation will fail. The exact same snapshot |
| 405 // from before should still be available. | 435 // from before should still be available. |
| 406 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 436 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 407 FastForwardSnapshotController(); | 437 FastForwardSnapshotController(); |
| 408 recent_tab_helper()->WasHidden(); | 438 recent_tab_helper()->WasHidden(); |
| 409 RunUntilIdle(); | 439 RunUntilIdle(); |
| 410 EXPECT_EQ(1U, page_added_count()); | 440 EXPECT_EQ(1U, page_added_count()); |
| 411 EXPECT_EQ(0U, model_removed_count()); | 441 EXPECT_EQ(0U, model_removed_count()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 NavigateAndCommit(kTestPageUrl); | 486 NavigateAndCommit(kTestPageUrl); |
| 457 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 487 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 458 FastForwardSnapshotController(); | 488 FastForwardSnapshotController(); |
| 459 recent_tab_helper()->WasHidden(); | 489 recent_tab_helper()->WasHidden(); |
| 460 RunUntilIdle(); | 490 RunUntilIdle(); |
| 461 EXPECT_EQ(1U, page_added_count()); | 491 EXPECT_EQ(1U, page_added_count()); |
| 462 EXPECT_EQ(0U, model_removed_count()); | 492 EXPECT_EQ(0U, model_removed_count()); |
| 463 ASSERT_EQ(1U, GetAllPages().size()); | 493 ASSERT_EQ(1U, GetAllPages().size()); |
| 464 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); | 494 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 465 | 495 |
| 466 // Sets a new delegate that will make the second snapshot fail. | 496 // Updates the delegate so that will make the second snapshot fail. |
| 467 TestDelegate* failing_delegate = | 497 default_test_delegate()->set_archive_size(-1); |
| 468 new TestDelegate(this, task_runner(), kTabId, true); | 498 default_test_delegate()->set_archive_result( |
| 469 failing_delegate->set_archive_size(-1); | |
| 470 failing_delegate->set_archive_result( | |
| 471 offline_pages::OfflinePageArchiver::ArchiverResult:: | 499 offline_pages::OfflinePageArchiver::ArchiverResult:: |
| 472 ERROR_ARCHIVE_CREATION_FAILED); | 500 ERROR_ARCHIVE_CREATION_FAILED); |
| 473 recent_tab_helper()->SetDelegate( | |
| 474 std::unique_ptr<TestDelegate>(failing_delegate)); | |
| 475 | 501 |
| 476 // Fully load the page once more then hide the tab again. A capture happens | 502 // Fully load the page once more then hide the tab again. A capture happens |
| 477 // and fails but no snapshot should remain. | 503 // and fails but no snapshot should remain. |
| 478 NavigateAndCommitTyped(kTestPageUrl); | 504 NavigateAndCommitTyped(kTestPageUrl); |
| 479 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 505 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 480 FastForwardSnapshotController(); | 506 FastForwardSnapshotController(); |
| 481 recent_tab_helper()->WasHidden(); | 507 recent_tab_helper()->WasHidden(); |
| 482 RunUntilIdle(); | 508 RunUntilIdle(); |
| 483 EXPECT_EQ(1U, page_added_count()); | 509 EXPECT_EQ(1U, page_added_count()); |
| 484 EXPECT_EQ(1U, model_removed_count()); | 510 EXPECT_EQ(1U, model_removed_count()); |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 EXPECT_EQ(2U, page_added_count()); | 810 EXPECT_EQ(2U, page_added_count()); |
| 785 EXPECT_EQ(0U, model_removed_count()); | 811 EXPECT_EQ(0U, model_removed_count()); |
| 786 ASSERT_EQ(2U, GetAllPages().size()); | 812 ASSERT_EQ(2U, GetAllPages().size()); |
| 787 const OfflinePageItem* second_page = FindPageForOfflineId(offline_id_3); | 813 const OfflinePageItem* second_page = FindPageForOfflineId(offline_id_3); |
| 788 ASSERT_TRUE(second_page); | 814 ASSERT_TRUE(second_page); |
| 789 EXPECT_EQ(client_id_3, second_page->client_id); | 815 EXPECT_EQ(client_id_3, second_page->client_id); |
| 790 EXPECT_EQ(offline_id_3, second_page->offline_id); | 816 EXPECT_EQ(offline_id_3, second_page->offline_id); |
| 791 } | 817 } |
| 792 | 818 |
| 793 } // namespace offline_pages | 819 } // namespace offline_pages |
| OLD | NEW |