| 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" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } | 92 const std::vector<OfflinePageItem>& all_pages() { return all_pages_; } |
| 93 | 93 |
| 94 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { | 94 scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() { |
| 95 return task_runner_; | 95 return task_runner_; |
| 96 } | 96 } |
| 97 | 97 |
| 98 size_t page_added_count() { return page_added_count_; } | 98 size_t page_added_count() { return page_added_count_; } |
| 99 size_t model_removed_count() { return model_removed_count_; } | 99 size_t model_removed_count() { return model_removed_count_; } |
| 100 | 100 |
| 101 // OfflinePageModel::Observer | 101 // OfflinePageModel::Observer |
| 102 void OfflinePageModelLoaded(OfflinePageModel* model) override { } | |
| 103 void OfflinePageAdded(OfflinePageModel* model, | 102 void OfflinePageAdded(OfflinePageModel* model, |
| 104 const OfflinePageItem& added_page) override { | 103 const OfflinePageItem& added_page) override { |
| 105 page_added_count_++; | 104 page_added_count_++; |
| 106 } | 105 } |
| 107 void OfflinePageDeleted(int64_t, const offline_pages::ClientId&) override { | 106 void OfflinePageDeleted(int64_t, const offline_pages::ClientId&) override { |
| 108 model_removed_count_++; | 107 model_removed_count_++; |
| 109 } | 108 } |
| 110 | 109 |
| 111 // OfflinePageTestArchiver::Observer | 110 // OfflinePageTestArchiver::Observer |
| 112 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} | 111 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override {} |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 scoped_feature_list.Init(); | 221 scoped_feature_list.Init(); |
| 223 EXPECT_NE(nullptr, recent_tab_helper()); | 222 EXPECT_NE(nullptr, recent_tab_helper()); |
| 224 } | 223 } |
| 225 | 224 |
| 226 // Loads a page and verifies that a snapshot is created. | 225 // Loads a page and verifies that a snapshot is created. |
| 227 TEST_F(RecentTabHelperTest, SimpleCapture) { | 226 TEST_F(RecentTabHelperTest, SimpleCapture) { |
| 228 NavigateAndCommit(kTestPageUrl); | 227 NavigateAndCommit(kTestPageUrl); |
| 229 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 228 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 230 FastForwardSnapshotController(); | 229 FastForwardSnapshotController(); |
| 231 RunUntilIdle(); | 230 RunUntilIdle(); |
| 232 EXPECT_TRUE(model()->is_loaded()); | |
| 233 GetAllPages(); | 231 GetAllPages(); |
| 234 EXPECT_EQ(1U, all_pages().size()); | 232 EXPECT_EQ(1U, all_pages().size()); |
| 235 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 233 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 236 } | 234 } |
| 237 | 235 |
| 238 // Checks that WebContents with no tab IDs are properly ignored. | 236 // Checks that WebContents with no tab IDs are properly ignored. |
| 239 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) { | 237 TEST_F(RecentTabHelperTest, NoTabIdNoCapture) { |
| 240 // Create delegate that returns 'false' as TabId retrieval result. | 238 // Create delegate that returns 'false' as TabId retrieval result. |
| 241 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>( | 239 recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>( |
| 242 this, task_runner(), kTabId, false)); | 240 this, task_runner(), kTabId, false)); |
| 243 | 241 |
| 244 NavigateAndCommit(kTestPageUrl); | 242 NavigateAndCommit(kTestPageUrl); |
| 245 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 243 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 246 FastForwardSnapshotController(); | 244 FastForwardSnapshotController(); |
| 247 RunUntilIdle(); | 245 RunUntilIdle(); |
| 248 EXPECT_TRUE(model()->is_loaded()); | |
| 249 GetAllPages(); | 246 GetAllPages(); |
| 250 // No page should be captured. | 247 // No page should be captured. |
| 251 EXPECT_EQ(0U, all_pages().size()); | 248 EXPECT_EQ(0U, all_pages().size()); |
| 252 } | 249 } |
| 253 | 250 |
| 254 // Triggers two snapshot captures during a single page load. Should end up with | 251 // Triggers two snapshot captures during a single page load. Should end up with |
| 255 // one offline page (the 2nd snapshot should be kept). | 252 // one offline page (the 2nd snapshot should be kept). |
| 256 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { | 253 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) { |
| 257 NavigateAndCommit(kTestPageUrl); | 254 NavigateAndCommit(kTestPageUrl); |
| 258 // Triggers snapshot after a time delay. | 255 // Triggers snapshot after a time delay. |
| 259 recent_tab_helper()->DocumentAvailableInMainFrame(); | 256 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 260 RunUntilIdle(); | 257 RunUntilIdle(); |
| 261 EXPECT_TRUE(model()->is_loaded()); | |
| 262 EXPECT_EQ(0U, page_added_count()); | 258 EXPECT_EQ(0U, page_added_count()); |
| 263 // Move the snapshot controller's time forward so it gets past timeouts. | 259 // Move the snapshot controller's time forward so it gets past timeouts. |
| 264 FastForwardSnapshotController(); | 260 FastForwardSnapshotController(); |
| 265 RunUntilIdle(); | 261 RunUntilIdle(); |
| 266 EXPECT_EQ(1U, page_added_count()); | 262 EXPECT_EQ(1U, page_added_count()); |
| 267 EXPECT_EQ(0U, model_removed_count()); | 263 EXPECT_EQ(0U, model_removed_count()); |
| 268 GetAllPages(); | 264 GetAllPages(); |
| 269 EXPECT_EQ(1U, all_pages().size()); | 265 EXPECT_EQ(1U, all_pages().size()); |
| 270 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 266 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 271 int64_t first_offline_id = all_pages()[0].offline_id; | 267 int64_t first_offline_id = all_pages()[0].offline_id; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 285 } | 281 } |
| 286 | 282 |
| 287 // Triggers two snapshot captures during a single page load, where the 2nd one | 283 // 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 | 284 // fails. Should end up with one offline page (the 1st, successful snapshot |
| 289 // should be kept). | 285 // should be kept). |
| 290 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoadSecondFails) { | 286 TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoadSecondFails) { |
| 291 NavigateAndCommit(kTestPageUrl); | 287 NavigateAndCommit(kTestPageUrl); |
| 292 // Triggers snapshot after a time delay. | 288 // Triggers snapshot after a time delay. |
| 293 recent_tab_helper()->DocumentAvailableInMainFrame(); | 289 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 294 RunUntilIdle(); | 290 RunUntilIdle(); |
| 295 EXPECT_TRUE(model()->is_loaded()); | |
| 296 EXPECT_EQ(0U, page_added_count()); | 291 EXPECT_EQ(0U, page_added_count()); |
| 297 // Move the snapshot controller's time forward so it gets past timeouts. | 292 // Move the snapshot controller's time forward so it gets past timeouts. |
| 298 FastForwardSnapshotController(); | 293 FastForwardSnapshotController(); |
| 299 RunUntilIdle(); | 294 RunUntilIdle(); |
| 300 EXPECT_EQ(1U, page_added_count()); | 295 EXPECT_EQ(1U, page_added_count()); |
| 301 EXPECT_EQ(0U, model_removed_count()); | 296 EXPECT_EQ(0U, model_removed_count()); |
| 302 GetAllPages(); | 297 GetAllPages(); |
| 303 EXPECT_EQ(1U, all_pages().size()); | 298 EXPECT_EQ(1U, all_pages().size()); |
| 304 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 299 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 305 int64_t first_offline_id = all_pages()[0].offline_id; | 300 int64_t first_offline_id = all_pages()[0].offline_id; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 328 EXPECT_EQ(first_offline_id, all_pages()[0].offline_id); | 323 EXPECT_EQ(first_offline_id, all_pages()[0].offline_id); |
| 329 } | 324 } |
| 330 | 325 |
| 331 // Triggers two snapshot captures for two different page loads for the same URL. | 326 // Triggers two snapshot captures for two different page loads for the same URL. |
| 332 // Should end up with one offline page (snapshot from the 2nd load). | 327 // Should end up with one offline page (snapshot from the 2nd load). |
| 333 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrl) { | 328 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrl) { |
| 334 NavigateAndCommit(kTestPageUrl); | 329 NavigateAndCommit(kTestPageUrl); |
| 335 // Triggers snapshot after a time delay. | 330 // Triggers snapshot after a time delay. |
| 336 recent_tab_helper()->DocumentAvailableInMainFrame(); | 331 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 337 RunUntilIdle(); | 332 RunUntilIdle(); |
| 338 EXPECT_TRUE(model()->is_loaded()); | |
| 339 EXPECT_EQ(0U, page_added_count()); | 333 EXPECT_EQ(0U, page_added_count()); |
| 340 // Move the snapshot controller's time forward so it gets past timeouts. | 334 // Move the snapshot controller's time forward so it gets past timeouts. |
| 341 FastForwardSnapshotController(); | 335 FastForwardSnapshotController(); |
| 342 RunUntilIdle(); | 336 RunUntilIdle(); |
| 343 EXPECT_EQ(1U, page_added_count()); | 337 EXPECT_EQ(1U, page_added_count()); |
| 344 EXPECT_EQ(0U, model_removed_count()); | 338 EXPECT_EQ(0U, model_removed_count()); |
| 345 GetAllPages(); | 339 GetAllPages(); |
| 346 EXPECT_EQ(1U, all_pages().size()); | 340 EXPECT_EQ(1U, all_pages().size()); |
| 347 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 341 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 348 int64_t first_offline_id = all_pages()[0].offline_id; | 342 int64_t first_offline_id = all_pages()[0].offline_id; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 363 } | 357 } |
| 364 | 358 |
| 365 // Triggers two snapshot captures for two different page loads for the same URL, | 359 // Triggers two snapshot captures for two different page loads for the same URL, |
| 366 // where the 2nd one fails. Should end up with no offline pages (privacy driven | 360 // where the 2nd one fails. Should end up with no offline pages (privacy driven |
| 367 // decision). | 361 // decision). |
| 368 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrlSecondFails) { | 362 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrlSecondFails) { |
| 369 NavigateAndCommit(kTestPageUrl); | 363 NavigateAndCommit(kTestPageUrl); |
| 370 // Triggers snapshot after a time delay. | 364 // Triggers snapshot after a time delay. |
| 371 recent_tab_helper()->DocumentAvailableInMainFrame(); | 365 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 372 RunUntilIdle(); | 366 RunUntilIdle(); |
| 373 EXPECT_TRUE(model()->is_loaded()); | |
| 374 EXPECT_EQ(0U, page_added_count()); | 367 EXPECT_EQ(0U, page_added_count()); |
| 375 // Move the snapshot controller's time forward so it gets past timeouts. | 368 // Move the snapshot controller's time forward so it gets past timeouts. |
| 376 FastForwardSnapshotController(); | 369 FastForwardSnapshotController(); |
| 377 RunUntilIdle(); | 370 RunUntilIdle(); |
| 378 EXPECT_EQ(1U, page_added_count()); | 371 EXPECT_EQ(1U, page_added_count()); |
| 379 EXPECT_EQ(0U, model_removed_count()); | 372 EXPECT_EQ(0U, model_removed_count()); |
| 380 GetAllPages(); | 373 GetAllPages(); |
| 381 EXPECT_EQ(1U, all_pages().size()); | 374 EXPECT_EQ(1U, all_pages().size()); |
| 382 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 375 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 383 | 376 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 404 EXPECT_EQ(0U, all_pages().size()); | 397 EXPECT_EQ(0U, all_pages().size()); |
| 405 } | 398 } |
| 406 | 399 |
| 407 // Triggers two snapshot captures for two different page loads and URLs. Should | 400 // Triggers two snapshot captures for two different page loads and URLs. Should |
| 408 // end up with one offline page. | 401 // end up with one offline page. |
| 409 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsAndUrls) { | 402 TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsAndUrls) { |
| 410 NavigateAndCommit(kTestPageUrl); | 403 NavigateAndCommit(kTestPageUrl); |
| 411 // Triggers snapshot after a time delay. | 404 // Triggers snapshot after a time delay. |
| 412 recent_tab_helper()->DocumentAvailableInMainFrame(); | 405 recent_tab_helper()->DocumentAvailableInMainFrame(); |
| 413 RunUntilIdle(); | 406 RunUntilIdle(); |
| 414 EXPECT_TRUE(model()->is_loaded()); | |
| 415 EXPECT_EQ(0U, page_added_count()); | 407 EXPECT_EQ(0U, page_added_count()); |
| 416 // Move the snapshot controller's time forward so it gets past timeouts. | 408 // Move the snapshot controller's time forward so it gets past timeouts. |
| 417 FastForwardSnapshotController(); | 409 FastForwardSnapshotController(); |
| 418 RunUntilIdle(); | 410 RunUntilIdle(); |
| 419 EXPECT_EQ(1U, page_added_count()); | 411 EXPECT_EQ(1U, page_added_count()); |
| 420 EXPECT_EQ(0U, model_removed_count()); | 412 EXPECT_EQ(0U, model_removed_count()); |
| 421 GetAllPages(); | 413 GetAllPages(); |
| 422 EXPECT_EQ(1U, all_pages().size()); | 414 EXPECT_EQ(1U, all_pages().size()); |
| 423 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); | 415 EXPECT_EQ(kTestPageUrl, all_pages()[0].url); |
| 424 | 416 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 436 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); | 428 EXPECT_EQ(kTestPageUrlOther, all_pages()[0].url); |
| 437 } | 429 } |
| 438 | 430 |
| 439 // Simulates an error (disconnection) during the load of a page. Should end up | 431 // Simulates an error (disconnection) during the load of a page. Should end up |
| 440 // with no offline pages. | 432 // with no offline pages. |
| 441 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { | 433 TEST_F(RecentTabHelperTest, NoCaptureOnErrorPage) { |
| 442 FailLoad(kTestPageUrl); | 434 FailLoad(kTestPageUrl); |
| 443 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 435 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 444 FastForwardSnapshotController(); | 436 FastForwardSnapshotController(); |
| 445 RunUntilIdle(); | 437 RunUntilIdle(); |
| 446 EXPECT_TRUE(model()->is_loaded()); | |
| 447 GetAllPages(); | 438 GetAllPages(); |
| 448 EXPECT_EQ(0U, all_pages().size()); | 439 EXPECT_EQ(0U, all_pages().size()); |
| 449 } | 440 } |
| 450 | 441 |
| 451 // Checks that no snapshots are created if the Offline Page Cache feature is | 442 // Checks that no snapshots are created if the Offline Page Cache feature is |
| 452 // disabled. | 443 // disabled. |
| 453 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { | 444 TEST_F(RecentTabHelperTest, FeatureNotEnabled) { |
| 454 base::test::ScopedFeatureList scoped_feature_list; | 445 base::test::ScopedFeatureList scoped_feature_list; |
| 455 scoped_feature_list.Init(); | 446 scoped_feature_list.Init(); |
| 456 NavigateAndCommit(kTestPageUrl); | 447 NavigateAndCommit(kTestPageUrl); |
| 457 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 448 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 458 FastForwardSnapshotController(); | 449 FastForwardSnapshotController(); |
| 459 RunUntilIdle(); | 450 RunUntilIdle(); |
| 460 EXPECT_TRUE(model()->is_loaded()); | |
| 461 GetAllPages(); | 451 GetAllPages(); |
| 462 // No page should be captured. | 452 // No page should be captured. |
| 463 EXPECT_EQ(0U, all_pages().size()); | 453 EXPECT_EQ(0U, all_pages().size()); |
| 464 } | 454 } |
| 465 | 455 |
| 466 // Simulates a download request to offline the current page. Should end up with | 456 // Simulates a download request to offline the current page. Should end up with |
| 467 // no offline pages. | 457 // no offline pages. |
| 468 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { | 458 TEST_F(RecentTabHelperTest, DISABLED_DownloadRequest) { |
| 469 NavigateAndCommit(kTestPageUrl); | 459 NavigateAndCommit(kTestPageUrl); |
| 470 recent_tab_helper()->ObserveAndDownloadCurrentPage( | 460 recent_tab_helper()->ObserveAndDownloadCurrentPage( |
| 471 ClientId("download", "id1"), 153l); | 461 ClientId("download", "id1"), 153l); |
| 472 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); | 462 recent_tab_helper()->DocumentOnLoadCompletedInMainFrame(); |
| 473 RunUntilIdle(); | 463 RunUntilIdle(); |
| 474 EXPECT_TRUE(model()->is_loaded()); | |
| 475 GetAllPages(); | 464 GetAllPages(); |
| 476 EXPECT_EQ(1U, all_pages().size()); | 465 EXPECT_EQ(1U, all_pages().size()); |
| 477 const OfflinePageItem& page = all_pages()[0]; | 466 const OfflinePageItem& page = all_pages()[0]; |
| 478 EXPECT_EQ(kTestPageUrl, page.url); | 467 EXPECT_EQ(kTestPageUrl, page.url); |
| 479 EXPECT_EQ("download", page.client_id.name_space); | 468 EXPECT_EQ("download", page.client_id.name_space); |
| 480 EXPECT_EQ("id1", page.client_id.id); | 469 EXPECT_EQ("id1", page.client_id.id); |
| 481 EXPECT_EQ(153l, page.offline_id); | 470 EXPECT_EQ(153l, page.offline_id); |
| 482 } | 471 } |
| 483 | 472 |
| 484 } // namespace offline_pages | 473 } // namespace offline_pages |
| OLD | NEW |