Chromium Code Reviews| Index: chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc |
| diff --git a/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc b/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc |
| index 922d80756a23a81039a59057ff7f2fc5ce512634..55ba97f332920e6173ed8e298f081697fe61d0ac 100644 |
| --- a/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc |
| +++ b/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/test/test_mock_time_task_runner.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| +#include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h" |
| #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| #include "components/offline_pages/core/offline_page_feature.h" |
| @@ -28,7 +29,7 @@ namespace { |
| const GURL kTestPageUrl("http://mystery.site/foo.html"); |
| const GURL kTestPageUrlOther("http://crazy.site/foo_other.html"); |
| const int kTabId = 153; |
| -} |
| +} // namespace |
| class TestDelegate: public RecentTabHelper::Delegate { |
| public: |
| @@ -76,6 +77,7 @@ class RecentTabHelperTest |
| ~RecentTabHelperTest() override {} |
| void SetUp() override; |
| + void TearDown() override; |
| const std::vector<OfflinePageItem>& GetAllPages(); |
| void FailLoad(const GURL& url); |
| @@ -91,6 +93,16 @@ class RecentTabHelperTest |
| const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } |
| + // Returns the index in |all_pages| of the OfflinePageItem that matches the |
| + // provided |offline_id|. If a match is not found returns -1. |
| + int FindPageIndexForOfflineId(int64_t offline_id) { |
| + for (size_t i = 0; i < all_pages_.size(); ++i) { |
| + if (all_pages_[i].offline_id == offline_id) |
| + return i; |
| + } |
| + return -1; |
| + } |
| + |
| scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { |
| return task_runner_; |
| } |
| @@ -165,7 +177,7 @@ RecentTabHelperTest::RecentTabHelperTest() |
| weak_ptr_factory_(this) {} |
| void RecentTabHelperTest::SetUp() { |
| - content::RenderViewHostTestHarness::SetUp(); |
| + ChromeRenderViewHostTestHarness::SetUp(); |
| scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); |
| // Sets up the factory for testing. |
| @@ -182,6 +194,12 @@ void RecentTabHelperTest::SetUp() { |
| model_ = OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| model_->AddObserver(this); |
| + RequestCoordinatorFactory::GetInstance()->ReturnNullptrForTesting(true); |
| +} |
| + |
| +void RecentTabHelperTest::TearDown() { |
| + 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.
|
| + RequestCoordinatorFactory::GetInstance()->ReturnNullptrForTesting(false); |
| } |
| void RecentTabHelperTest::FailLoad(const GURL& url) { |
| @@ -287,7 +305,8 @@ TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { |
| // Triggers two snapshot captures during a single page load, where the 2nd one |
| // fails. Should end up with one offline page (the 1st, successful snapshot |
| // should be kept). |
| -TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoadSecondFails) { |
| +// TODO(carlosk): re-enable once https://crbug.com/655697 is fixed, again. |
| +TEST_F(RecentTabHelperTest, DISABLED_TwoCapturesSamePageLoadSecondFails) { |
| NavigateAndCommit(kTestPageUrl); |
| // Triggers snapshot after a time delay. |
| recent_tab_helper()->DocumentAvailableInMainFrame(); |
| @@ -436,6 +455,67 @@ TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsAndUrls) { |
| EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); |
| } |
| +// Triggers two snapshot captures via the download after a page was loaded page |
| +// and saved twice by last_n. Should end up with three offline pages: two from |
| +// downloads (which shouldn't replace each other) and one from last_n. |
| +TEST_F(RecentTabHelperTest, TwoDownloadCapturesInARowSamePage) { |
| + NavigateAndCommit(kTestPageUrl); |
| + // Executes a regular load with snapshots taken by last_n. |
| + recent_tab_helper()->DocumentAvailableInMainFrame(); |
| + RunUntilIdle(); |
| + FastForwardSnapshotController(); |
| + RunUntilIdle(); |
| + recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| + FastForwardSnapshotController(); |
| + RunUntilIdle(); |
| + // Checks that two last_n snapshots were created but only one was kept. |
| + EXPECT_EQ(2U, page_added_count()); |
| + EXPECT_EQ(1U, model_removed_count()); |
| + GetAllPages(); |
| + EXPECT_EQ(1U, all_pages().size()); |
| + EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| + int64_t first_offline_id = all_pages()[0].offline_id; |
| + |
| + // Request the 1st offline download. |
| + 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
|
| + 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
|
| + recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| + ClientId("download", "id2"), second_offline_id); |
| + RunUntilIdle(); |
| + GetAllPages(); |
| + // Checks that both the previous last_n and download snapshots are present. |
| + ASSERT_EQ(2U, all_pages().size()); |
| + EXPECT_NE(-1, FindPageIndexForOfflineId(first_offline_id)); |
| + { |
|
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
|
| + int second_index = FindPageIndexForOfflineId(second_offline_id); |
| + EXPECT_NE(-1, second_index); |
| + EXPECT_EQ(kTestPageUrl, all_pages()[second_index].url); |
| + EXPECT_EQ("download", all_pages()[second_index].client_id.name_space); |
| + EXPECT_EQ("id2", all_pages()[second_index].client_id.id); |
| + } |
| + |
| + // Request the 2nd offline download. |
| + const int64_t third_offline_id = 456l; |
|
fgorski
2016/12/13 17:17:38
ditto
carlosk
2016/12/13 20:04:20
Done.
|
| + ASSERT_NE(third_offline_id, first_offline_id); |
| + 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.
|
| + recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| + ClientId("download", "id3"), third_offline_id); |
| + RunUntilIdle(); |
| + GetAllPages(); |
| + ASSERT_EQ(3U, all_pages().size()); |
| + // Checks that the previous last_n and download snapshots are still present |
| + // and the new downloaded one was added. |
| + EXPECT_NE(-1, FindPageIndexForOfflineId(first_offline_id)); |
| + EXPECT_NE(-1, FindPageIndexForOfflineId(second_offline_id)); |
| + { |
| + 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
|
| + EXPECT_NE(-1, third_index); |
| + EXPECT_EQ(kTestPageUrl, all_pages()[third_index].url); |
| + EXPECT_EQ("download", all_pages()[third_index].client_id.name_space); |
| + EXPECT_EQ("id3", all_pages()[third_index].client_id.id); |
| + } |
| +} |
| + |
| // Simulates an error (disconnection) during the load of a page. Should end up |
| // with no offline pages. |
| TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { |
| @@ -464,7 +544,7 @@ TEST_F(RecentTabHelperTest, FeatureNotEnabled) { |
| } |
| // Simulates a download request to offline the current page. Should end up with |
| -// no offline pages. |
| +// one offline pages. |
| TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { |
| NavigateAndCommit(kTestPageUrl); |
| recent_tab_helper()->ObserveAndDownloadCurrentPage( |