| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 OfflinePageTestArchiver::Observer* observer, | 46 OfflinePageTestArchiver::Observer* observer, |
| 47 int tab_id, | 47 int tab_id, |
| 48 bool tab_id_result); | 48 bool tab_id_result); |
| 49 ~TestDelegate() override {} | 49 ~TestDelegate() override {} |
| 50 | 50 |
| 51 std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( | 51 std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( |
| 52 content::WebContents* web_contents) override; | 52 content::WebContents* web_contents) 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 bool IsLowEndDevice() override { return is_low_end_device_; } |
| 56 bool IsCustomTab(content::WebContents* web_contents) override { |
| 57 return is_custom_tab_; |
| 58 } |
| 56 | 59 |
| 57 void set_archive_result( | 60 void set_archive_result( |
| 58 offline_pages::OfflinePageArchiver::ArchiverResult result) { | 61 offline_pages::OfflinePageArchiver::ArchiverResult result) { |
| 59 archive_result_ = result; | 62 archive_result_ = result; |
| 60 } | 63 } |
| 61 | 64 |
| 62 void set_archive_size(int64_t size) { archive_size_ = size; } | 65 void set_archive_size(int64_t size) { archive_size_ = size; } |
| 63 | 66 |
| 64 void SetAsLowEndDevice() { is_low_end_device_ = true; } | 67 void SetAsLowEndDevice() { is_low_end_device_ = true; } |
| 65 | 68 |
| 69 void set_is_custom_tab(bool is_custom_tab) { is_custom_tab_ = is_custom_tab; } |
| 70 |
| 66 private: | 71 private: |
| 67 OfflinePageTestArchiver::Observer* observer_; // observer owns this. | 72 OfflinePageTestArchiver::Observer* observer_; // observer owns this. |
| 68 int tab_id_; | 73 int tab_id_; |
| 69 bool tab_id_result_; | 74 bool tab_id_result_; |
| 70 | 75 |
| 71 // These values can be updated so that new OfflinePageTestArchiver instances | 76 // These values can be updated so that new OfflinePageTestArchiver instances |
| 72 // will return different results. | 77 // will return different results. |
| 73 offline_pages::OfflinePageArchiver::ArchiverResult archive_result_ = | 78 offline_pages::OfflinePageArchiver::ArchiverResult archive_result_ = |
| 74 offline_pages::OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED; | 79 offline_pages::OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED; |
| 75 int64_t archive_size_ = kArchiveSizeToReport; | 80 int64_t archive_size_ = kArchiveSizeToReport; |
| 76 bool is_low_end_device_ = false; | 81 bool is_low_end_device_ = false; |
| 82 bool is_custom_tab_ = false; |
| 77 }; | 83 }; |
| 78 | 84 |
| 79 class RecentTabHelperTest | 85 class RecentTabHelperTest |
| 80 : public ChromeRenderViewHostTestHarness, | 86 : public ChromeRenderViewHostTestHarness, |
| 81 public OfflinePageModel::Observer, | 87 public OfflinePageModel::Observer, |
| 82 public OfflinePageTestArchiver::Observer { | 88 public OfflinePageTestArchiver::Observer { |
| 83 public: | 89 public: |
| 84 RecentTabHelperTest(); | 90 RecentTabHelperTest(); |
| 85 ~RecentTabHelperTest() override {} | 91 ~RecentTabHelperTest() override {} |
| 86 | 92 |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 ASSERT_EQ(0U, GetAllPages().size()); | 368 ASSERT_EQ(0U, GetAllPages().size()); |
| 363 | 369 |
| 364 // But the following download request should work normally | 370 // But the following download request should work normally |
| 365 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), | 371 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), |
| 366 123L); | 372 123L); |
| 367 RunUntilIdle(); | 373 RunUntilIdle(); |
| 368 EXPECT_EQ(1U, page_added_count()); | 374 EXPECT_EQ(1U, page_added_count()); |
| 369 ASSERT_EQ(1U, GetAllPages().size()); | 375 ASSERT_EQ(1U, GetAllPages().size()); |
| 370 } | 376 } |
| 371 | 377 |
| 378 // Checks that last_n will not save a snapshot if the tab while the tab is |
| 379 // presented as a custom tab. Download requests should be unaffected though. |
| 380 TEST_F(RecentTabHelperTest, LastNWontSaveCustomTab) { |
| 381 // Simulates the tab running as a custom tab. |
| 382 default_test_delegate()->set_is_custom_tab(true); |
| 383 |
| 384 // Navigate and finish loading then hide the tab. Nothing should be saved. |
| 385 NavigateAndCommit(kTestPageUrl); |
| 386 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 387 FastForwardSnapshotController(); |
| 388 recent_tab_helper()->WasHidden(); |
| 389 RunUntilIdle(); |
| 390 EXPECT_TRUE(model()->is_loaded()); |
| 391 EXPECT_EQ(0U, page_added_count()); |
| 392 ASSERT_EQ(0U, GetAllPages().size()); |
| 393 |
| 394 // But the following download request should work normally |
| 395 recent_tab_helper()->ObserveAndDownloadCurrentPage(NewDownloadClientId(), |
| 396 123L); |
| 397 RunUntilIdle(); |
| 398 EXPECT_EQ(1U, page_added_count()); |
| 399 ASSERT_EQ(1U, GetAllPages().size()); |
| 400 |
| 401 // Simulates the tab being transfered from the CustomTabActivity back to a |
| 402 // ChromeActivity. |
| 403 default_test_delegate()->set_is_custom_tab(false); |
| 404 |
| 405 // Upon the next hide a last_n snapshot should be saved. |
| 406 recent_tab_helper()->WasHidden(); |
| 407 RunUntilIdle(); |
| 408 EXPECT_TRUE(model()->is_loaded()); |
| 409 EXPECT_EQ(2U, page_added_count()); |
| 410 ASSERT_EQ(2U, GetAllPages().size()); |
| 411 } |
| 412 |
| 372 // Triggers two last_n snapshot captures during a single page load. Should end | 413 // Triggers two last_n snapshot captures during a single page load. Should end |
| 373 // up with one snapshot, the 1st being replaced by the 2nd. | 414 // up with one snapshot, the 1st being replaced by the 2nd. |
| 374 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { | 415 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { |
| 375 NavigateAndCommit(kTestPageUrl); | 416 NavigateAndCommit(kTestPageUrl); |
| 376 // Set page loading state to the 1st snapshot-able stage. No capture so far. | 417 // Set page loading state to the 1st snapshot-able stage. No capture so far. |
| 377 recent_tab_helper()->DocumentAvailableInMainFrame(); | 418 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 378 FastForwardSnapshotController(); | 419 FastForwardSnapshotController(); |
| 379 EXPECT_TRUE(model()->is_loaded()); | 420 EXPECT_TRUE(model()->is_loaded()); |
| 380 EXPECT_EQ(0U, page_added_count()); | 421 EXPECT_EQ(0U, page_added_count()); |
| 381 | 422 |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 // moment a snapshot should be created. | 939 // moment a snapshot should be created. |
| 899 recent_tab_helper()->WasShown(); | 940 recent_tab_helper()->WasShown(); |
| 900 recent_tab_helper()->WasHidden(); | 941 recent_tab_helper()->WasHidden(); |
| 901 RunUntilIdle(); | 942 RunUntilIdle(); |
| 902 EXPECT_EQ(1U, page_added_count()); | 943 EXPECT_EQ(1U, page_added_count()); |
| 903 EXPECT_EQ(0U, model_removed_count()); | 944 EXPECT_EQ(0U, model_removed_count()); |
| 904 ASSERT_EQ(1U, GetAllPages().size()); | 945 ASSERT_EQ(1U, GetAllPages().size()); |
| 905 } | 946 } |
| 906 | 947 |
| 907 } // namespace offline_pages | 948 } // namespace offline_pages |
| OLD | NEW |