| 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/request_coordinator_factory.h" |
| 15 #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" | 16 #include "chrome/browser/android/offline_pages/test_request_coordinator_builder.
h" |
| 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 18 #include "components/offline_pages/core/client_namespace_constants.h" |
| 18 #include "components/offline_pages/core/offline_page_feature.h" | 19 #include "components/offline_pages/core/offline_page_feature.h" |
| 19 #include "components/offline_pages/core/offline_page_item.h" | 20 #include "components/offline_pages/core/offline_page_item.h" |
| 20 #include "components/offline_pages/core/offline_page_model.h" | 21 #include "components/offline_pages/core/offline_page_model.h" |
| 21 #include "components/offline_pages/core/offline_page_test_archiver.h" | 22 #include "components/offline_pages/core/offline_page_test_archiver.h" |
| 22 #include "content/public/browser/navigation_entry.h" | 23 #include "content/public/browser/navigation_entry.h" |
| 23 #include "content/public/browser/navigation_handle.h" | 24 #include "content/public/browser/navigation_handle.h" |
| 24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/common/referrer.h" |
| 27 #include "content/public/test/web_contents_tester.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 29 |
| 27 namespace offline_pages { | 30 namespace offline_pages { |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 const GURL kTestPageUrl("http://mystery.site/foo.html"); | 33 const GURL kTestPageUrl("http://mystery.site/foo.html"); |
| 31 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html"); | 34 const GURL kTestPageUrlOther("http://crazy.site/foo_other.html"); |
| 32 const int kTabId = 153; | 35 const int kTabId = 153; |
| 33 } // namespace | 36 } // namespace |
| 34 | 37 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 void SetUp() override; | 83 void SetUp() override; |
| 81 const std::vector<OfflinePageItem>& GetAllPages(); | 84 const std::vector<OfflinePageItem>& GetAllPages(); |
| 82 | 85 |
| 83 void FailLoad(const GURL& url); | 86 void FailLoad(const GURL& url); |
| 84 | 87 |
| 85 // Runs default thread. | 88 // Runs default thread. |
| 86 void RunUntilIdle(); | 89 void RunUntilIdle(); |
| 87 // Moves forward the snapshot controller's task runner. | 90 // Moves forward the snapshot controller's task runner. |
| 88 void FastForwardSnapshotController(); | 91 void FastForwardSnapshotController(); |
| 89 | 92 |
| 93 // Navigates to the URL and commit as if it has been typed in the address bar. |
| 94 // Note: we need this to simulate navigations to the same URL that more like a |
| 95 // reload and not same page. NavigateAndCommit simulates a click on a link |
| 96 // and when reusing the same URL that will be considered a same page |
| 97 // navigation. |
| 98 void NavigateAndCommitTyped(const GURL& url); |
| 99 |
| 90 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } | 100 RecentTabHelper* recent_tab_helper() const { return recent_tab_helper_; } |
| 91 | 101 |
| 92 OfflinePageModel* model() const { return model_; } | 102 OfflinePageModel* model() const { return model_; } |
| 93 | 103 |
| 94 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } | |
| 95 | |
| 96 // Returns a OfflinePageItem pointer from |all_pages| that matches the | 104 // Returns a OfflinePageItem pointer from |all_pages| that matches the |
| 97 // provided |offline_id|. If a match is not found returns nullptr. | 105 // provided |offline_id|. If a match is not found returns nullptr. |
| 98 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) { | 106 const OfflinePageItem* FindPageForOfflineId(int64_t offline_id) { |
| 99 for (const OfflinePageItem& page : all_pages_) { | 107 for (const OfflinePageItem& page : GetAllPages()) { |
| 100 if (page.offline_id == offline_id) | 108 if (page.offline_id == offline_id) |
| 101 return &page; | 109 return &page; |
| 102 } | 110 } |
| 103 return nullptr; | 111 return nullptr; |
| 104 } | 112 } |
| 105 | 113 |
| 106 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { | 114 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { |
| 107 return task_runner_; | 115 return task_runner_; |
| 108 } | 116 } |
| 109 | 117 |
| 110 size_t page_added_count() { return page_added_count_; } | 118 size_t page_added_count() { return page_added_count_; } |
| 111 size_t model_removed_count() { return model_removed_count_; } | 119 size_t model_removed_count() { return model_removed_count_; } |
| 112 | 120 |
| 113 // OfflinePageModel::Observer | 121 // OfflinePageModel::Observer |
| 114 void OfflinePageModelLoaded(OfflinePageModel* model) override { } | 122 void OfflinePageModelLoaded(OfflinePageModel* model) override { |
| 123 all_pages_needs_updating_ = true; |
| 124 } |
| 115 void OfflinePageAdded(OfflinePageModel* model, | 125 void OfflinePageAdded(OfflinePageModel* model, |
| 116 const OfflinePageItem& added_page) override { | 126 const OfflinePageItem& added_page) override { |
| 117 page_added_count_++; | 127 page_added_count_++; |
| 128 all_pages_needs_updating_ = true; |
| 118 } | 129 } |
| 119 void OfflinePageDeleted(int64_t, const offline_pages::ClientId&) override { | 130 void OfflinePageDeleted(int64_t, const offline_pages::ClientId&) override { |
| 120 model_removed_count_++; | 131 model_removed_count_++; |
| 132 all_pages_needs_updating_ = true; |
| 121 } | 133 } |
| 122 | 134 |
| 123 // OfflinePageTestArchiver::Observer | 135 // OfflinePageTestArchiver::Observer |
| 124 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} | 136 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} |
| 125 | 137 |
| 126 private: | 138 private: |
| 127 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); | 139 void OnGetAllPagesDone(const std::vector<OfflinePageItem>& result); |
| 128 | 140 |
| 129 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. | 141 RecentTabHelper* recent_tab_helper_; // Owned by WebContents. |
| 130 OfflinePageModel* model_; // Keyed service | 142 OfflinePageModel* model_; // Keyed service |
| 131 size_t page_added_count_; | 143 size_t page_added_count_; |
| 132 size_t model_removed_count_; | 144 size_t model_removed_count_; |
| 133 std::vector<OfflinePageItem> all_pages_; | 145 std::vector<OfflinePageItem> all_pages_; |
| 146 bool all_pages_needs_updating_; |
| 134 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 147 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 135 base::test::ScopedFeatureList scoped_feature_list_; | 148 base::test::ScopedFeatureList scoped_feature_list_; |
| 136 | 149 |
| 137 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; | 150 base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_; |
| 138 | 151 |
| 139 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); | 152 DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest); |
| 140 }; | 153 }; |
| 141 | 154 |
| 142 TestDelegate::TestDelegate( | 155 TestDelegate::TestDelegate( |
| 143 OfflinePageTestArchiver::Observer* observer, | 156 OfflinePageTestArchiver::Observer* observer, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 166 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { | 179 bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) { |
| 167 *tab_id = tab_id_; | 180 *tab_id = tab_id_; |
| 168 return tab_id_result_; | 181 return tab_id_result_; |
| 169 } | 182 } |
| 170 | 183 |
| 171 RecentTabHelperTest::RecentTabHelperTest() | 184 RecentTabHelperTest::RecentTabHelperTest() |
| 172 : recent_tab_helper_(nullptr), | 185 : recent_tab_helper_(nullptr), |
| 173 model_(nullptr), | 186 model_(nullptr), |
| 174 page_added_count_(0), | 187 page_added_count_(0), |
| 175 model_removed_count_(0), | 188 model_removed_count_(0), |
| 189 all_pages_needs_updating_(true), |
| 176 task_runner_(new base::TestMockTimeTaskRunner), | 190 task_runner_(new base::TestMockTimeTaskRunner), |
| 177 weak_ptr_factory_(this) {} | 191 weak_ptr_factory_(this) {} |
| 178 | 192 |
| 179 void RecentTabHelperTest::SetUp() { | 193 void RecentTabHelperTest::SetUp() { |
| 180 ChromeRenderViewHostTestHarness::SetUp(); | 194 ChromeRenderViewHostTestHarness::SetUp(); |
| 181 | 195 |
| 182 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); | 196 scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature); |
| 183 // Sets up the factories for testing. | 197 // Sets up the factories for testing. |
| 184 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | 198 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 185 browser_context(), BuildTestOfflinePageModel); | 199 browser_context(), BuildTestOfflinePageModel); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 202 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, | 216 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 203 std::string()); | 217 std::string()); |
| 204 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); | 218 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); |
| 205 content::RenderFrameHostTester::For(main_rfh())-> | 219 content::RenderFrameHostTester::For(main_rfh())-> |
| 206 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); | 220 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); |
| 207 content::RenderFrameHostTester::For(main_rfh())-> | 221 content::RenderFrameHostTester::For(main_rfh())-> |
| 208 SimulateNavigationErrorPageCommit(); | 222 SimulateNavigationErrorPageCommit(); |
| 209 } | 223 } |
| 210 | 224 |
| 211 const std::vector<OfflinePageItem>& RecentTabHelperTest::GetAllPages() { | 225 const std::vector<OfflinePageItem>& RecentTabHelperTest::GetAllPages() { |
| 212 model()->GetAllPages( | 226 if (all_pages_needs_updating_) { |
| 213 base::Bind(&RecentTabHelperTest::OnGetAllPagesDone, | 227 model()->GetAllPages(base::Bind(&RecentTabHelperTest::OnGetAllPagesDone, |
| 214 weak_ptr_factory_.GetWeakPtr())); | 228 weak_ptr_factory_.GetWeakPtr())); |
| 215 RunUntilIdle(); | 229 RunUntilIdle(); |
| 230 all_pages_needs_updating_ = false; |
| 231 } |
| 216 return all_pages_; | 232 return all_pages_; |
| 217 } | 233 } |
| 218 | 234 |
| 219 void RecentTabHelperTest::OnGetAllPagesDone( | 235 void RecentTabHelperTest::OnGetAllPagesDone( |
| 220 const std::vector<OfflinePageItem>& result) { | 236 const std::vector<OfflinePageItem>& result) { |
| 221 all_pages_ = result; | 237 all_pages_ = result; |
| 222 } | 238 } |
| 223 | 239 |
| 224 void RecentTabHelperTest::RunUntilIdle() { | 240 void RecentTabHelperTest::RunUntilIdle() { |
| 225 base::RunLoop().RunUntilIdle(); | 241 base::RunLoop().RunUntilIdle(); |
| 226 } | 242 } |
| 227 | 243 |
| 228 void RecentTabHelperTest::FastForwardSnapshotController() { | 244 void RecentTabHelperTest::FastForwardSnapshotController() { |
| 229 const size_t kLongDelayMs = 100*1000; | 245 const size_t kLongDelayMs = 100*1000; |
| 230 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(kLongDelayMs)); | 246 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(kLongDelayMs)); |
| 231 } | 247 } |
| 232 | 248 |
| 249 void RecentTabHelperTest::NavigateAndCommitTyped(const GURL& url) { |
| 250 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 251 std::string()); |
| 252 content::WebContentsTester* web_contents_tester = |
| 253 content::WebContentsTester::For(web_contents()); |
| 254 web_contents_tester->CommitPendingNavigation(); |
| 255 } |
| 256 |
| 233 // Checks the test setup. | 257 // Checks the test setup. |
| 234 TEST_F(RecentTabHelperTest, Basic) { | 258 TEST_F(RecentTabHelperTest, RecentTabHelperInstanceExists) { |
| 235 base::test::ScopedFeatureList scoped_feature_list; | 259 base::test::ScopedFeatureList scoped_feature_list; |
| 236 scoped_feature_list.Init(); | 260 scoped_feature_list.Init(); |
| 237 EXPECT_NE(nullptr, recent_tab_helper()); | 261 EXPECT_NE(nullptr, recent_tab_helper()); |
| 238 } | 262 } |
| 239 | 263 |
| 240 // Loads a page and verifies that a snapshot is created. | 264 // Fully loads a page then simulates the tab being hidden. Verifies that a |
| 241 TEST_F(RecentTabHelperTest, SimpleCapture) { | 265 // snapshot is created only when the latter happens. |
| 266 TEST_F(RecentTabHelperTest, LastNCaptureAfterLoad) { |
| 267 // Navigate and finish loading. Nothing should be saved. |
| 242 NavigateAndCommit(kTestPageUrl); | 268 NavigateAndCommit(kTestPageUrl); |
| 243 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 269 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 270 // Move the snapshot controller's time forward so it gets past timeouts. |
| 244 FastForwardSnapshotController(); | 271 FastForwardSnapshotController(); |
| 245 RunUntilIdle(); | 272 RunUntilIdle(); |
| 246 EXPECT_TRUE(model()->is_loaded()); | 273 EXPECT_TRUE(model()->is_loaded()); |
| 247 GetAllPages(); | 274 EXPECT_EQ(0U, page_added_count()); |
| 248 ASSERT_EQ(1U, all_pages().size()); | 275 ASSERT_EQ(0U, GetAllPages().size()); |
| 249 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 276 |
| 277 // Tab is hidden with a fully loaded page. A snapshot save should happen. |
| 278 recent_tab_helper()->WasHidden(); |
| 279 RunUntilIdle(); |
| 280 EXPECT_TRUE(model()->is_loaded()); |
| 281 EXPECT_EQ(1U, page_added_count()); |
| 282 ASSERT_EQ(1U, GetAllPages().size()); |
| 283 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 284 EXPECT_EQ(kLastNNamespace, GetAllPages()[0].client_id.name_space); |
| 250 } | 285 } |
| 251 | 286 |
| 252 // Checks that WebContents with no tab IDs are properly ignored. | 287 // Simulates the tab being hidden too early in the page loading so that a |
| 288 // snapshot should not be created. |
| 289 TEST_F(RecentTabHelperTest, NoLastNCaptureIfTabHiddenTooEarlyInPageLoad) { |
| 290 // Commit the navigation and hide the tab. Nothing should be saved. |
| 291 NavigateAndCommit(kTestPageUrl); |
| 292 recent_tab_helper()->WasHidden(); |
| 293 RunUntilIdle(); |
| 294 EXPECT_TRUE(model()->is_loaded()); |
| 295 EXPECT_EQ(0U, page_added_count()); |
| 296 ASSERT_EQ(0U, GetAllPages().size()); |
| 297 |
| 298 // Then allow the page to fully load. Nothing should be saved. |
| 299 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 300 // Move the snapshot controller's time forward so it gets past timeouts. |
| 301 FastForwardSnapshotController(); |
| 302 RunUntilIdle(); |
| 303 EXPECT_EQ(0U, page_added_count()); |
| 304 ASSERT_EQ(0U, GetAllPages().size()); |
| 305 } |
| 306 |
| 307 // Checks that WebContents with no tab IDs have snapshot requests properly |
| 308 // ignored from both last_n and downloads. |
| 253 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) { | 309 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) { |
| 254 // Create delegate that returns 'false' as TabId retrieval result. | 310 // Create delegate that returns 'false' as TabId retrieval result. |
| 255 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>( | 311 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>( |
| 256 this, task_runner(), kTabId, false)); | 312 this, task_runner(), kTabId, false)); |
| 257 | 313 |
| 258 NavigateAndCommit(kTestPageUrl); | 314 NavigateAndCommit(kTestPageUrl); |
| 259 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 315 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 260 FastForwardSnapshotController(); | 316 FastForwardSnapshotController(); |
| 317 recent_tab_helper()->WasHidden(); |
| 318 recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| 319 ClientId("download", "id2"), 123L); |
| 261 RunUntilIdle(); | 320 RunUntilIdle(); |
| 262 EXPECT_TRUE(model()->is_loaded()); | 321 EXPECT_TRUE(model()->is_loaded()); |
| 263 GetAllPages(); | |
| 264 // No page should be captured. | 322 // No page should be captured. |
| 265 ASSERT_EQ(0U, all_pages().size()); | 323 EXPECT_EQ(0U, page_added_count()); |
| 324 ASSERT_EQ(0U, GetAllPages().size()); |
| 266 } | 325 } |
| 267 | 326 |
| 268 // Triggers two snapshot captures during a single page load. Should end up with | 327 // Triggers two last_n snapshot captures during a single page load. Should end |
| 269 // one offline page (the 2nd snapshot should be kept). | 328 // up with one snapshot, the 1st being replaced by the 2nd. |
| 270 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { | 329 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { |
| 271 NavigateAndCommit(kTestPageUrl); | 330 NavigateAndCommit(kTestPageUrl); |
| 272 // Triggers snapshot after a time delay. | 331 // Set page loading state to the 1st snapshot-able stage. No capture so far. |
| 273 recent_tab_helper()->DocumentAvailableInMainFrame(); | 332 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 333 FastForwardSnapshotController(); |
| 274 RunUntilIdle(); | 334 RunUntilIdle(); |
| 275 EXPECT_TRUE(model()->is_loaded()); | 335 EXPECT_TRUE(model()->is_loaded()); |
| 276 EXPECT_EQ(0U, page_added_count()); | 336 EXPECT_EQ(0U, page_added_count()); |
| 277 // Move the snapshot controller's time forward so it gets past timeouts. | 337 |
| 338 // Tab is hidden and a snapshot should be saved. |
| 339 recent_tab_helper()->WasHidden(); |
| 340 RunUntilIdle(); |
| 341 EXPECT_EQ(1U, page_added_count()); |
| 342 EXPECT_EQ(0U, model_removed_count()); |
| 343 ASSERT_EQ(1U, GetAllPages().size()); |
| 344 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 345 int64_t first_offline_id = GetAllPages()[0].offline_id; |
| 346 |
| 347 // Set page loading state to the 2nd and last snapshot-able stage. No new |
| 348 // capture should happen. |
| 349 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 278 FastForwardSnapshotController(); | 350 FastForwardSnapshotController(); |
| 279 RunUntilIdle(); | 351 RunUntilIdle(); |
| 280 EXPECT_EQ(1U, page_added_count()); | 352 EXPECT_EQ(1U, page_added_count()); |
| 281 EXPECT_EQ(0U, model_removed_count()); | 353 EXPECT_EQ(0U, model_removed_count()); |
| 282 GetAllPages(); | 354 ASSERT_EQ(1U, GetAllPages().size()); |
| 283 ASSERT_EQ(1U, all_pages().size()); | |
| 284 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | |
| 285 int64_t first_offline_id = all_pages()[0].offline_id; | |
| 286 | 355 |
| 287 // Triggers snapshot after a time delay. | 356 // Tab is hidden again. At this point a higher quality snapshot is expected so |
| 288 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 357 // a new one should be captured and replace the previous one. |
| 289 // Move the snapshot controller's time forward so it gets past timeouts. | 358 recent_tab_helper()->WasHidden(); |
| 290 FastForwardSnapshotController(); | |
| 291 RunUntilIdle(); | 359 RunUntilIdle(); |
| 292 EXPECT_EQ(2U, page_added_count()); | 360 EXPECT_EQ(2U, page_added_count()); |
| 293 EXPECT_EQ(1U, model_removed_count()); | 361 EXPECT_EQ(1U, model_removed_count()); |
| 294 // the same page should be simply overridden. | 362 ASSERT_EQ(1U, GetAllPages().size()); |
| 295 GetAllPages(); | 363 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 296 ASSERT_EQ(1U, all_pages().size()); | 364 EXPECT_NE(first_offline_id, GetAllPages()[0].offline_id); |
| 297 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | |
| 298 EXPECT_NE(first_offline_id, all_pages()[0].offline_id); | |
| 299 } | 365 } |
| 300 | 366 |
| 301 // Triggers two snapshot captures during a single page load, where the 2nd one | 367 // Triggers two last_n captures during a single page load, where the 2nd capture |
| 302 // fails. Should end up with one offline page (the 1st, successful snapshot | 368 // fails. Should end up with one offline page (the 1st, successful snapshot |
| 303 // should be kept). | 369 // should be kept). |
| 304 // TODO(carlosk): re-enable once https://crbug.com/655697 is fixed, again. | 370 // TODO(carlosk): re-enable once https://crbug.com/655697 is fixed, again. |
| 305 TEST_F(RecentTabHelperTest, DISABLED_TwoCapturesSamePageLoadSecondFails) { | 371 TEST_F(RecentTabHelperTest, DISABLED_TwoCapturesWhere2ndFailsSamePageLoad) { |
| 372 // Navigate and load until the 1st stage. Tab hidden should trigger a capture. |
| 306 NavigateAndCommit(kTestPageUrl); | 373 NavigateAndCommit(kTestPageUrl); |
| 307 // Triggers snapshot after a time delay. | |
| 308 recent_tab_helper()->DocumentAvailableInMainFrame(); | 374 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 309 RunUntilIdle(); | |
| 310 EXPECT_TRUE(model()->is_loaded()); | |
| 311 EXPECT_EQ(0U, page_added_count()); | |
| 312 // Move the snapshot controller's time forward so it gets past timeouts. | |
| 313 FastForwardSnapshotController(); | 375 FastForwardSnapshotController(); |
| 376 recent_tab_helper()->WasHidden(); |
| 314 RunUntilIdle(); | 377 RunUntilIdle(); |
| 315 EXPECT_EQ(1U, page_added_count()); | 378 EXPECT_EQ(1U, page_added_count()); |
| 316 EXPECT_EQ(0U, model_removed_count()); | 379 EXPECT_EQ(0U, model_removed_count()); |
| 317 GetAllPages(); | 380 ASSERT_EQ(1U, GetAllPages().size()); |
| 318 ASSERT_EQ(1U, all_pages().size()); | 381 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 319 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 382 int64_t first_offline_id = GetAllPages()[0].offline_id; |
| 320 int64_t first_offline_id = all_pages()[0].offline_id; | |
| 321 | 383 |
| 322 // Sets a new delegate that will make the second snapshot fail. | 384 // Sets a new delegate that will make the second snapshot fail. |
| 323 TestDelegate* failing_delegate = | 385 TestDelegate* failing_delegate = |
| 324 new TestDelegate(this, task_runner(), kTabId, true); | 386 new TestDelegate(this, task_runner(), kTabId, true); |
| 325 failing_delegate->set_archive_size(-1); | 387 failing_delegate->set_archive_size(-1); |
| 326 failing_delegate->set_archive_result( | 388 failing_delegate->set_archive_result( |
| 327 offline_pages::OfflinePageArchiver::ArchiverResult:: | 389 offline_pages::OfflinePageArchiver::ArchiverResult:: |
| 328 ERROR_ARCHIVE_CREATION_FAILED); | 390 ERROR_ARCHIVE_CREATION_FAILED); |
| 329 recent_tab_helper()->SetDelegate( | 391 recent_tab_helper()->SetDelegate( |
| 330 std::unique_ptr<TestDelegate>(failing_delegate)); | 392 std::unique_ptr<TestDelegate>(failing_delegate)); |
| 331 | 393 |
| 332 // Triggers snapshot after a time delay. | 394 // Advance loading to the 2nd and final stage and then hide the tab. A new |
| 395 // capture is requested but its creation will fail. The exact same snapshot |
| 396 // from before should still be available. |
| 333 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 397 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 334 // Move the snapshot controller's time forward so it gets past timeouts. | 398 FastForwardSnapshotController(); |
| 399 recent_tab_helper()->WasHidden(); |
| 400 RunUntilIdle(); |
| 401 EXPECT_EQ(1U, page_added_count()); |
| 402 EXPECT_EQ(0U, model_removed_count()); |
| 403 ASSERT_EQ(1U, GetAllPages().size()); |
| 404 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 405 EXPECT_EQ(first_offline_id, GetAllPages()[0].offline_id); |
| 406 } |
| 407 |
| 408 // Triggers two last_n captures for two different loads of the same URL (aka |
| 409 // reload). Should end up with a single snapshot (from the 2nd load). |
| 410 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrl) { |
| 411 // Fully load the page. Hide the tab and check for a snapshot. |
| 412 NavigateAndCommit(kTestPageUrl); |
| 413 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 414 FastForwardSnapshotController(); |
| 415 recent_tab_helper()->WasHidden(); |
| 416 RunUntilIdle(); |
| 417 EXPECT_EQ(1U, page_added_count()); |
| 418 EXPECT_EQ(0U, model_removed_count()); |
| 419 ASSERT_EQ(1U, GetAllPages().size()); |
| 420 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 421 int64_t first_offline_id = GetAllPages()[0].offline_id; |
| 422 |
| 423 // Navigate with the same URL until the page is minimally loaded then hide the |
| 424 // tab. The previous snapshot should be removed and a new one taken. |
| 425 NavigateAndCommitTyped(kTestPageUrl); |
| 426 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 335 FastForwardSnapshotController(); | 427 FastForwardSnapshotController(); |
| 336 RunUntilIdle(); | 428 RunUntilIdle(); |
| 337 EXPECT_EQ(1U, page_added_count()); | 429 EXPECT_EQ(1U, page_added_count()); |
| 338 EXPECT_EQ(0U, model_removed_count()); | 430 EXPECT_EQ(0U, model_removed_count()); |
| 339 // The exact same page should still be available. | 431 ASSERT_EQ(1U, GetAllPages().size()); |
| 340 GetAllPages(); | 432 |
| 341 ASSERT_EQ(1U, all_pages().size()); | 433 recent_tab_helper()->WasHidden(); |
| 342 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 434 RunUntilIdle(); |
| 343 EXPECT_EQ(first_offline_id, all_pages()[0].offline_id); | 435 EXPECT_EQ(2U, page_added_count()); |
| 436 EXPECT_EQ(1U, model_removed_count()); |
| 437 ASSERT_EQ(1U, GetAllPages().size()); |
| 438 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 439 EXPECT_NE(first_offline_id, GetAllPages()[0].offline_id); |
| 344 } | 440 } |
| 345 | 441 |
| 346 // Triggers two snapshot captures for two different page loads for the same URL. | 442 // Triggers two last_n captures for two different page loads of the same URL |
| 347 // Should end up with one offline page (snapshot from the 2nd load). | 443 // (aka reload), where the 2nd capture fails. Should end up with no offline |
| 348 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrl) { | 444 // pages (a privacy driven decision). |
| 445 TEST_F(RecentTabHelperTest, TwoCapturesWhere2ndFailsDifferentPageLoadsSameUrl) { |
| 446 // Fully load the page then hide the tab. A capture is expected. |
| 349 NavigateAndCommit(kTestPageUrl); | 447 NavigateAndCommit(kTestPageUrl); |
| 350 // Triggers snapshot after a time delay. | 448 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 351 recent_tab_helper()->DocumentAvailableInMainFrame(); | |
| 352 RunUntilIdle(); | |
| 353 EXPECT_TRUE(model()->is_loaded()); | |
| 354 EXPECT_EQ(0U, page_added_count()); | |
| 355 // Move the snapshot controller's time forward so it gets past timeouts. | |
| 356 FastForwardSnapshotController(); | 449 FastForwardSnapshotController(); |
| 450 recent_tab_helper()->WasHidden(); |
| 357 RunUntilIdle(); | 451 RunUntilIdle(); |
| 358 EXPECT_EQ(1U, page_added_count()); | 452 EXPECT_EQ(1U, page_added_count()); |
| 359 EXPECT_EQ(0U, model_removed_count()); | 453 EXPECT_EQ(0U, model_removed_count()); |
| 360 GetAllPages(); | 454 ASSERT_EQ(1U, GetAllPages().size()); |
| 361 ASSERT_EQ(1U, all_pages().size()); | 455 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 362 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | |
| 363 int64_t first_offline_id = all_pages()[0].offline_id; | |
| 364 | |
| 365 NavigateAndCommit(kTestPageUrl); | |
| 366 // Triggers snapshot after a time delay. | |
| 367 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | |
| 368 // Move the snapshot controller's time forward so it gets past timeouts. | |
| 369 FastForwardSnapshotController(); | |
| 370 RunUntilIdle(); | |
| 371 EXPECT_EQ(2U, page_added_count()); | |
| 372 EXPECT_EQ(1U, model_removed_count()); | |
| 373 // the same page should be simply overridden. | |
| 374 GetAllPages(); | |
| 375 ASSERT_EQ(1U, all_pages().size()); | |
| 376 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | |
| 377 EXPECT_NE(first_offline_id, all_pages()[0].offline_id); | |
| 378 } | |
| 379 | |
| 380 // Triggers two snapshot captures for two different page loads for the same URL, | |
| 381 // where the 2nd one fails. Should end up with no offline pages (privacy driven | |
| 382 // decision). | |
| 383 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrlSecondFails) { | |
| 384 NavigateAndCommit(kTestPageUrl); | |
| 385 // Triggers snapshot after a time delay. | |
| 386 recent_tab_helper()->DocumentAvailableInMainFrame(); | |
| 387 RunUntilIdle(); | |
| 388 EXPECT_TRUE(model()->is_loaded()); | |
| 389 EXPECT_EQ(0U, page_added_count()); | |
| 390 // Move the snapshot controller's time forward so it gets past timeouts. | |
| 391 FastForwardSnapshotController(); | |
| 392 RunUntilIdle(); | |
| 393 EXPECT_EQ(1U, page_added_count()); | |
| 394 EXPECT_EQ(0U, model_removed_count()); | |
| 395 GetAllPages(); | |
| 396 ASSERT_EQ(1U, all_pages().size()); | |
| 397 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | |
| 398 | 456 |
| 399 // Sets a new delegate that will make the second snapshot fail. | 457 // Sets a new delegate that will make the second snapshot fail. |
| 400 TestDelegate* failing_delegate = | 458 TestDelegate* failing_delegate = |
| 401 new TestDelegate(this, task_runner(), kTabId, true); | 459 new TestDelegate(this, task_runner(), kTabId, true); |
| 402 failing_delegate->set_archive_size(-1); | 460 failing_delegate->set_archive_size(-1); |
| 403 failing_delegate->set_archive_result( | 461 failing_delegate->set_archive_result( |
| 404 offline_pages::OfflinePageArchiver::ArchiverResult:: | 462 offline_pages::OfflinePageArchiver::ArchiverResult:: |
| 405 ERROR_ARCHIVE_CREATION_FAILED); | 463 ERROR_ARCHIVE_CREATION_FAILED); |
| 406 recent_tab_helper()->SetDelegate( | 464 recent_tab_helper()->SetDelegate( |
| 407 std::unique_ptr<TestDelegate>(failing_delegate)); | 465 std::unique_ptr<TestDelegate>(failing_delegate)); |
| 408 | 466 |
| 409 NavigateAndCommit(kTestPageUrl); | 467 // Fully load the page once more then hide the tab again. A capture happens |
| 410 // Triggers snapshot after a time delay. | 468 // and fails but no snapshot should remain. |
| 411 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 469 NavigateAndCommitTyped(kTestPageUrl); |
| 412 // Move the snapshot controller's time forward so it gets past timeouts. | 470 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 413 FastForwardSnapshotController(); | 471 FastForwardSnapshotController(); |
| 414 RunUntilIdle(); | 472 recent_tab_helper()->WasHidden(); |
| 415 EXPECT_EQ(1U, page_added_count()); | 473 RunUntilIdle(); |
| 416 EXPECT_EQ(1U, model_removed_count()); | 474 EXPECT_EQ(1U, page_added_count()); |
| 417 // the same page should be simply overridden. | 475 EXPECT_EQ(1U, model_removed_count()); |
| 418 GetAllPages(); | 476 ASSERT_EQ(0U, GetAllPages().size()); |
| 419 ASSERT_EQ(0U, all_pages().size()); | 477 } |
| 420 } | 478 |
| 421 | 479 // Triggers two last_n captures for two different page loads of different URLs. |
| 422 // Triggers two snapshot captures for two different page loads and URLs. Should | 480 // Should end up with a single snapshot of the last page. |
| 423 // end up with one offline page. | 481 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsDifferentUrls) { |
| 424 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsAndUrls) { | 482 // Fully load the first URL then hide the tab and check for a snapshot. |
| 425 NavigateAndCommit(kTestPageUrl); | 483 NavigateAndCommit(kTestPageUrl); |
| 426 // Triggers snapshot after a time delay. | 484 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 427 recent_tab_helper()->DocumentAvailableInMainFrame(); | 485 FastForwardSnapshotController(); |
| 428 RunUntilIdle(); | 486 recent_tab_helper()->WasHidden(); |
| 429 EXPECT_TRUE(model()->is_loaded()); | 487 RunUntilIdle(); |
| 430 EXPECT_EQ(0U, page_added_count()); | 488 EXPECT_EQ(1U, page_added_count()); |
| 431 // Move the snapshot controller's time forward so it gets past timeouts. | 489 EXPECT_EQ(0U, model_removed_count()); |
| 432 FastForwardSnapshotController(); | 490 ASSERT_EQ(1U, GetAllPages().size()); |
| 433 RunUntilIdle(); | 491 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 434 EXPECT_EQ(1U, page_added_count()); | 492 |
| 435 EXPECT_EQ(0U, model_removed_count()); | 493 // Fully load the second URL then hide the tab and check for a single snapshot |
| 436 GetAllPages(); | 494 // of the new page. |
| 437 ASSERT_EQ(1U, all_pages().size()); | 495 NavigateAndCommitTyped(kTestPageUrlOther); |
| 438 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 496 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 439 | 497 FastForwardSnapshotController(); |
| 440 NavigateAndCommit(kTestPageUrlOther); | 498 RunUntilIdle(); |
| 441 // Triggers snapshot after a time delay. | 499 EXPECT_EQ(1U, page_added_count()); |
| 442 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 500 EXPECT_EQ(0U, model_removed_count()); |
| 443 // Move the snapshot controller's time forward so it gets past timeouts. | 501 ASSERT_EQ(1U, GetAllPages().size()); |
| 444 FastForwardSnapshotController(); | 502 |
| 445 RunUntilIdle(); | 503 recent_tab_helper()->WasHidden(); |
| 446 EXPECT_EQ(2U, page_added_count()); | 504 RunUntilIdle(); |
| 447 EXPECT_EQ(1U, model_removed_count()); | 505 EXPECT_EQ(2U, page_added_count()); |
| 448 // the same page should be simply overridden. | 506 EXPECT_EQ(1U, model_removed_count()); |
| 449 GetAllPages(); | 507 ASSERT_EQ(1U, GetAllPages().size()); |
| 450 ASSERT_EQ(1U, all_pages().size()); | 508 EXPECT_EQ(kTestPageUrlOther, GetAllPages()[0].url); |
| 451 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); | 509 } |
| 452 } | 510 |
| 453 | 511 // Fully loads a page where last_n captures two snapshots. Then triggers two |
| 454 // Triggers two snapshot captures via the download after a page was loaded page | 512 // snapshot requests by downloads. Should end up with three offline pages: one |
| 455 // and saved twice by last_n. Should end up with three offline pages: two from | 513 // from last_n (2nd replaces the 1st) and two from downloads (which shouldn't |
| 456 // downloads (which shouldn't replace each other) and one from last_n. | 514 // replace each other). |
| 457 TEST_F(RecentTabHelperTest, TwoDownloadCapturesInARowSamePage) { | 515 TEST_F(RecentTabHelperTest, TwoLastNAndTwoDownloadCapturesSamePage) { |
| 458 NavigateAndCommit(kTestPageUrl); | 516 // Fully loads the page with intermediary steps where the tab is hidden. Then |
| 459 // Executes a regular load with snapshots taken by last_n. | 517 // check that two last_n snapshots were created but only one was kept. |
| 460 recent_tab_helper()->DocumentAvailableInMainFrame(); | 518 NavigateAndCommit(kTestPageUrl); |
| 461 RunUntilIdle(); | 519 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 462 FastForwardSnapshotController(); | 520 FastForwardSnapshotController(); |
| 463 RunUntilIdle(); | 521 recent_tab_helper()->WasHidden(); |
| 464 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 522 RunUntilIdle(); |
| 465 FastForwardSnapshotController(); | 523 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 466 RunUntilIdle(); | 524 FastForwardSnapshotController(); |
| 467 // Checks that two last_n snapshots were created but only one was kept. | 525 recent_tab_helper()->WasHidden(); |
| 468 EXPECT_EQ(2U, page_added_count()); | 526 RunUntilIdle(); |
| 469 EXPECT_EQ(1U, model_removed_count()); | 527 EXPECT_EQ(2U, page_added_count()); |
| 470 GetAllPages(); | 528 EXPECT_EQ(1U, model_removed_count()); |
| 471 ASSERT_EQ(1U, all_pages().size()); | 529 ASSERT_EQ(1U, GetAllPages().size()); |
| 472 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 530 EXPECT_EQ(kTestPageUrl, GetAllPages()[0].url); |
| 473 int64_t first_offline_id = all_pages()[0].offline_id; | 531 int64_t first_offline_id = GetAllPages()[0].offline_id; |
| 474 | 532 |
| 475 // Request the 1st offline download. | 533 // First snapshot request by downloads. Two offline pages are expected. |
| 476 const int64_t second_offline_id = first_offline_id + 1; | 534 const int64_t second_offline_id = first_offline_id + 1; |
| 477 recent_tab_helper()->ObserveAndDownloadCurrentPage( | 535 const ClientId second_client_id("download", "id2"); |
| 478 ClientId("download", "id2"), second_offline_id); | 536 recent_tab_helper()->ObserveAndDownloadCurrentPage(second_client_id, |
| 479 RunUntilIdle(); | 537 second_offline_id); |
| 480 GetAllPages(); | 538 RunUntilIdle(); |
| 481 // Checks that both the previous last_n and download snapshots are present. | 539 EXPECT_EQ(3U, page_added_count()); |
| 482 ASSERT_EQ(2U, all_pages().size()); | 540 EXPECT_EQ(1U, model_removed_count()); |
| 541 ASSERT_EQ(2U, GetAllPages().size()); |
| 483 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id)); | 542 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id)); |
| 484 const OfflinePageItem* second_page = FindPageForOfflineId(second_offline_id); | 543 const OfflinePageItem* second_page = FindPageForOfflineId(second_offline_id); |
| 485 ASSERT_NE(nullptr, second_page); | 544 ASSERT_NE(nullptr, second_page); |
| 486 EXPECT_EQ(kTestPageUrl, second_page->url); | 545 EXPECT_EQ(kTestPageUrl, second_page->url); |
| 487 EXPECT_EQ("download", second_page->client_id.name_space); | 546 EXPECT_EQ(second_client_id, second_page->client_id); |
| 488 EXPECT_EQ("id2", second_page->client_id.id); | 547 |
| 489 | 548 // Second snapshot request by downloads. Three offline pages are expected. |
| 490 // Request the 2nd offline download. | |
| 491 const int64_t third_offline_id = first_offline_id + 2; | 549 const int64_t third_offline_id = first_offline_id + 2; |
| 492 recent_tab_helper()->ObserveAndDownloadCurrentPage( | 550 const ClientId third_client_id("download", "id2"); |
| 493 ClientId("download", "id3"), third_offline_id); | 551 recent_tab_helper()->ObserveAndDownloadCurrentPage(third_client_id, |
| 494 RunUntilIdle(); | 552 third_offline_id); |
| 495 GetAllPages(); | 553 RunUntilIdle(); |
| 496 ASSERT_EQ(3U, all_pages().size()); | 554 EXPECT_EQ(4U, page_added_count()); |
| 497 // Checks that the previous last_n and download snapshots are still present | 555 EXPECT_EQ(1U, model_removed_count()); |
| 498 // and the new downloaded one was added. | 556 ASSERT_EQ(3U, GetAllPages().size()); |
| 499 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id)); | 557 EXPECT_NE(nullptr, FindPageForOfflineId(first_offline_id)); |
| 500 EXPECT_NE(nullptr, FindPageForOfflineId(second_offline_id)); | 558 EXPECT_NE(nullptr, FindPageForOfflineId(second_offline_id)); |
| 501 const OfflinePageItem* third_page = FindPageForOfflineId(third_offline_id); | 559 const OfflinePageItem* third_page = FindPageForOfflineId(third_offline_id); |
| 502 ASSERT_NE(nullptr, third_page); | 560 ASSERT_NE(nullptr, third_page); |
| 503 EXPECT_EQ(kTestPageUrl, third_page->url); | 561 EXPECT_EQ(kTestPageUrl, third_page->url); |
| 504 EXPECT_EQ("download", third_page->client_id.name_space); | 562 EXPECT_EQ(third_client_id, third_page->client_id); |
| 505 EXPECT_EQ("id3", third_page->client_id.id); | |
| 506 } | 563 } |
| 507 | 564 |
| 508 // Simulates an error (disconnection) during the load of a page. Should end up | 565 // Simulates an error (disconnection) during the load of a page. Should end up |
| 509 // with no offline pages. | 566 // with no offline pages for any requester. |
| 510 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { | 567 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { |
| 511 FailLoad(kTestPageUrl); | 568 FailLoad(kTestPageUrl); |
| 512 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 569 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 513 FastForwardSnapshotController(); | 570 FastForwardSnapshotController(); |
| 514 RunUntilIdle(); | 571 recent_tab_helper()->WasHidden(); |
| 515 EXPECT_TRUE(model()->is_loaded()); | 572 recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| 516 GetAllPages(); | 573 ClientId("download", "id1"), 123L); |
| 517 ASSERT_EQ(0U, all_pages().size()); | 574 RunUntilIdle(); |
| 518 } | 575 EXPECT_TRUE(model()->is_loaded()); |
| 519 | 576 ASSERT_EQ(0U, GetAllPages().size()); |
| 520 // Checks that no snapshots are created if the Offline Page Cache feature is | 577 } |
| 521 // disabled. | 578 |
| 522 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { | 579 // Checks that last_n snapshots are not created if the feature is disabled. |
| 580 // Download requests should still work. |
| 581 TEST_F(RecentTabHelperTest, LastNFeatureNotEnabled) { |
| 523 base::test::ScopedFeatureList scoped_feature_list; | 582 base::test::ScopedFeatureList scoped_feature_list; |
| 524 scoped_feature_list.Init(); | 583 scoped_feature_list.Init(); |
| 525 NavigateAndCommit(kTestPageUrl); | 584 NavigateAndCommit(kTestPageUrl); |
| 526 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 585 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 527 FastForwardSnapshotController(); | 586 FastForwardSnapshotController(); |
| 528 RunUntilIdle(); | 587 recent_tab_helper()->WasHidden(); |
| 529 EXPECT_TRUE(model()->is_loaded()); | 588 RunUntilIdle(); |
| 530 GetAllPages(); | 589 EXPECT_TRUE(model()->is_loaded()); |
| 531 // No page should be captured. | 590 // No page should be captured. |
| 532 ASSERT_EQ(0U, all_pages().size()); | 591 ASSERT_EQ(0U, GetAllPages().size()); |
| 533 } | 592 |
| 534 | |
| 535 // Simulates a download request to offline the current page. Should end up with | |
| 536 // one offline pages. | |
| 537 TEST_F(RecentTabHelperTest, DownloadRequest) { | |
| 538 NavigateAndCommit(kTestPageUrl); | |
| 539 recent_tab_helper()->ObserveAndDownloadCurrentPage( | 593 recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| 540 ClientId("download", "id1"), 153L); | 594 ClientId("download", "id1"), 123L); |
| 541 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 595 RunUntilIdle(); |
| 542 FastForwardSnapshotController(); | 596 // No page should be captured. |
| 543 RunUntilIdle(); | 597 ASSERT_EQ(1U, GetAllPages().size()); |
| 544 EXPECT_TRUE(model()->is_loaded()); | 598 } |
| 545 GetAllPages(); | 599 |
| 546 ASSERT_EQ(1U, all_pages().size()); | 600 // Simulates a download request to offline the current page made early during |
| 547 const OfflinePageItem& page = all_pages()[0]; | 601 // loading. Should execute two captures but only the final one is kept. |
| 602 TEST_F(RecentTabHelperTest, DownloadRequestEarlyInLoad) { |
| 603 // Commit the navigation and request the snapshot from downloads. No captures |
| 604 // so far. |
| 605 NavigateAndCommit(kTestPageUrl); |
| 606 const ClientId client_id("download", "id1"); |
| 607 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L); |
| 608 FastForwardSnapshotController(); |
| 609 RunUntilIdle(); |
| 610 EXPECT_TRUE(model()->is_loaded()); |
| 611 ASSERT_EQ(0U, GetAllPages().size()); |
| 612 |
| 613 // Minimally load the page. First capture should occur. |
| 614 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 615 FastForwardSnapshotController(); |
| 616 RunUntilIdle(); |
| 617 ASSERT_EQ(1U, GetAllPages().size()); |
| 618 const OfflinePageItem& early_page = GetAllPages()[0]; |
| 619 EXPECT_EQ(kTestPageUrl, early_page.url); |
| 620 EXPECT_EQ(client_id, early_page.client_id); |
| 621 EXPECT_EQ(153L, early_page.offline_id); |
| 622 |
| 623 // Fully load the page. A second capture should replace the first one. |
| 624 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 625 FastForwardSnapshotController(); |
| 626 RunUntilIdle(); |
| 627 EXPECT_EQ(2U, page_added_count()); |
| 628 EXPECT_EQ(1U, model_removed_count()); |
| 629 ASSERT_EQ(1U, GetAllPages().size()); |
| 630 const OfflinePageItem& later_page = GetAllPages()[0]; |
| 631 EXPECT_EQ(kTestPageUrl, later_page.url); |
| 632 EXPECT_EQ(client_id, later_page.client_id); |
| 633 EXPECT_EQ(153L, later_page.offline_id); |
| 634 } |
| 635 |
| 636 // Simulates a download request to offline the current page made when the page |
| 637 // is minimally loaded. Should execute two captures but only the final one is |
| 638 // kept. |
| 639 TEST_F(RecentTabHelperTest, DownloadRequestLaterInLoad) { |
| 640 NavigateAndCommit(kTestPageUrl); |
| 641 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 642 FastForwardSnapshotController(); |
| 643 RunUntilIdle(); |
| 644 EXPECT_TRUE(model()->is_loaded()); |
| 645 ASSERT_EQ(0U, GetAllPages().size()); |
| 646 |
| 647 const ClientId client_id("download", "id1"); |
| 648 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L); |
| 649 RunUntilIdle(); |
| 650 ASSERT_EQ(1U, GetAllPages().size()); |
| 651 const OfflinePageItem& page = GetAllPages()[0]; |
| 548 EXPECT_EQ(kTestPageUrl, page.url); | 652 EXPECT_EQ(kTestPageUrl, page.url); |
| 549 EXPECT_EQ("download", page.client_id.name_space); | 653 EXPECT_EQ(client_id, page.client_id); |
| 550 EXPECT_EQ("id1", page.client_id.id); | |
| 551 EXPECT_EQ(153L, page.offline_id); | 654 EXPECT_EQ(153L, page.offline_id); |
| 655 |
| 656 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 657 FastForwardSnapshotController(); |
| 658 RunUntilIdle(); |
| 659 EXPECT_EQ(2U, page_added_count()); |
| 660 EXPECT_EQ(1U, model_removed_count()); |
| 661 ASSERT_EQ(1U, GetAllPages().size()); |
| 662 } |
| 663 |
| 664 // Simulates a download request to offline the current page made after loading |
| 665 // is completed. Should end up with one offline pages. |
| 666 TEST_F(RecentTabHelperTest, DownloadRequestAfterFullyLoad) { |
| 667 NavigateAndCommit(kTestPageUrl); |
| 668 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 669 FastForwardSnapshotController(); |
| 670 RunUntilIdle(); |
| 671 EXPECT_TRUE(model()->is_loaded()); |
| 672 ASSERT_EQ(0U, GetAllPages().size()); |
| 673 |
| 674 const ClientId client_id("download", "id1"); |
| 675 recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L); |
| 676 RunUntilIdle(); |
| 677 ASSERT_EQ(1U, GetAllPages().size()); |
| 678 const OfflinePageItem& page = GetAllPages()[0]; |
| 679 EXPECT_EQ(kTestPageUrl, page.url); |
| 680 EXPECT_EQ(client_id, page.client_id); |
| 681 EXPECT_EQ(153L, page.offline_id); |
| 682 } |
| 683 |
| 684 // Simulates requests coming from last_n and downloads at the same time for a |
| 685 // fully loaded page. |
| 686 TEST_F(RecentTabHelperTest, SimultaneousCapturesFromLastNAndDownloads) { |
| 687 NavigateAndCommit(kTestPageUrl); |
| 688 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 689 FastForwardSnapshotController(); |
| 690 recent_tab_helper()->WasHidden(); |
| 691 const int64_t download_offline_id = 153L; |
| 692 const ClientId download_client_id("download", "id1"); |
| 693 recent_tab_helper()->ObserveAndDownloadCurrentPage(download_client_id, |
| 694 download_offline_id); |
| 695 RunUntilIdle(); |
| 696 ASSERT_EQ(2U, GetAllPages().size()); |
| 697 |
| 698 const OfflinePageItem* downloads_page = |
| 699 FindPageForOfflineId(download_offline_id); |
| 700 ASSERT_TRUE(downloads_page); |
| 701 EXPECT_EQ(kTestPageUrl, downloads_page->url); |
| 702 EXPECT_EQ(download_client_id, downloads_page->client_id); |
| 703 |
| 704 const OfflinePageItem& last_n_page = |
| 705 GetAllPages()[0].offline_id != download_offline_id ? GetAllPages()[0] |
| 706 : GetAllPages()[1]; |
| 707 EXPECT_EQ(kTestPageUrl, last_n_page.url); |
| 708 EXPECT_EQ(kLastNNamespace, last_n_page.client_id.name_space); |
| 709 } |
| 710 |
| 711 // Simulates multiple tab hidden events -- triggers for last_n snapshots -- |
| 712 // happening at the same loading stages. The duplicate events should not cause |
| 713 // new snapshots to be saved. |
| 714 TEST_F(RecentTabHelperTest, DuplicateTabHiddenEventsShouldNotTriggerSnapshots) { |
| 715 NavigateAndCommit(kTestPageUrl); |
| 716 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 717 FastForwardSnapshotController(); |
| 718 recent_tab_helper()->WasHidden(); |
| 719 RunUntilIdle(); |
| 720 EXPECT_EQ(1U, page_added_count()); |
| 721 EXPECT_EQ(0U, model_removed_count()); |
| 722 ASSERT_EQ(1U, GetAllPages().size()); |
| 723 |
| 724 recent_tab_helper()->WasHidden(); |
| 725 RunUntilIdle(); |
| 726 EXPECT_EQ(1U, page_added_count()); |
| 727 EXPECT_EQ(0U, model_removed_count()); |
| 728 ASSERT_EQ(1U, GetAllPages().size()); |
| 729 |
| 730 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 731 FastForwardSnapshotController(); |
| 732 recent_tab_helper()->WasHidden(); |
| 733 RunUntilIdle(); |
| 734 EXPECT_EQ(2U, page_added_count()); |
| 735 EXPECT_EQ(1U, model_removed_count()); |
| 736 ASSERT_EQ(1U, GetAllPages().size()); |
| 737 |
| 738 recent_tab_helper()->WasHidden(); |
| 739 RunUntilIdle(); |
| 740 EXPECT_EQ(2U, page_added_count()); |
| 741 EXPECT_EQ(1U, model_removed_count()); |
| 742 ASSERT_EQ(1U, GetAllPages().size()); |
| 552 } | 743 } |
| 553 | 744 |
| 554 } // namespace offline_pages | 745 } // namespace offline_pages |
| OLD | NEW |