| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/compiler_specific.h" |
| 5 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
| 6 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/sessions/persistent_tab_restore_service.h" |
| 7 #include "chrome/browser/sessions/session_service.h" | 9 #include "chrome/browser/sessions/session_service.h" |
| 8 #include "chrome/browser/sessions/session_service_factory.h" | 10 #include "chrome/browser/sessions/session_service_factory.h" |
| 9 #include "chrome/browser/sessions/session_types.h" | 11 #include "chrome/browser/sessions/session_types.h" |
| 10 #include "chrome/browser/sessions/session_types_test_helper.h" | 12 #include "chrome/browser/sessions/session_types_test_helper.h" |
| 11 #include "chrome/browser/sessions/tab_restore_service.h" | |
| 12 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 13 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 13 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/chrome_render_view_test.h" | 18 #include "chrome/test/base/chrome_render_view_test.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/navigation_controller.h" | 22 #include "content/public/browser/navigation_controller.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 url2_ = GURL("http://2"); | 61 url2_ = GURL("http://2"); |
| 61 url3_ = GURL("http://3"); | 62 url3_ = GURL("http://3"); |
| 62 user_agent_override_ = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19" | 63 user_agent_override_ = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19" |
| 63 " (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"; | 64 " (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"; |
| 64 } | 65 } |
| 65 | 66 |
| 66 ~TabRestoreServiceTest() { | 67 ~TabRestoreServiceTest() { |
| 67 } | 68 } |
| 68 | 69 |
| 69 protected: | 70 protected: |
| 70 // testing::Test overrides | 71 enum { |
| 71 virtual void SetUp() { | 72 kMaxEntries = InMemoryTabRestoreService::kMaxEntries, |
| 73 }; |
| 74 |
| 75 // testing::Test: |
| 76 virtual void SetUp() OVERRIDE { |
| 72 WebKit::initialize(webkit_platform_support_.Get()); | 77 WebKit::initialize(webkit_platform_support_.Get()); |
| 73 ChromeRenderViewHostTestHarness::SetUp(); | 78 ChromeRenderViewHostTestHarness::SetUp(); |
| 74 time_factory_ = new TabRestoreTimeFactory(); | 79 time_factory_ = new TabRestoreTimeFactory(); |
| 75 service_.reset(new TabRestoreService(profile(), time_factory_)); | 80 service_.reset(new PersistentTabRestoreService(profile(), time_factory_)); |
| 76 } | 81 } |
| 77 | 82 |
| 78 virtual void TearDown() { | 83 virtual void TearDown() OVERRIDE { |
| 79 service_->Shutdown(); | 84 service_->Shutdown(); |
| 80 service_.reset(); | 85 service_.reset(); |
| 81 delete time_factory_; | 86 delete time_factory_; |
| 82 ChromeRenderViewHostTestHarness::TearDown(); | 87 ChromeRenderViewHostTestHarness::TearDown(); |
| 83 WebKit::shutdown(); | 88 WebKit::shutdown(); |
| 84 } | 89 } |
| 85 | 90 |
| 91 TabRestoreService::Entries* mutable_entries() { |
| 92 return &service_->entries_; |
| 93 } |
| 94 |
| 95 void PruneEntries() { |
| 96 service_->PruneEntries(); |
| 97 } |
| 98 |
| 86 void AddThreeNavigations() { | 99 void AddThreeNavigations() { |
| 87 // Navigate to three URLs. | 100 // Navigate to three URLs. |
| 88 NavigateAndCommit(url1_); | 101 NavigateAndCommit(url1_); |
| 89 NavigateAndCommit(url2_); | 102 NavigateAndCommit(url2_); |
| 90 NavigateAndCommit(url3_); | 103 NavigateAndCommit(url3_); |
| 91 } | 104 } |
| 92 | 105 |
| 93 void NavigateToIndex(int index) { | 106 void NavigateToIndex(int index) { |
| 94 // Navigate back. We have to do this song and dance as NavigationController | 107 // Navigate back. We have to do this song and dance as NavigationController |
| 95 // isn't happy if you navigate immediately while going back. | 108 // isn't happy if you navigate immediately while going back. |
| 96 controller().GoToIndex(index); | 109 controller().GoToIndex(index); |
| 97 WebContentsTester::For(contents())->CommitPendingNavigation(); | 110 WebContentsTester::For(contents())->CommitPendingNavigation(); |
| 98 } | 111 } |
| 99 | 112 |
| 100 void RecreateService() { | 113 void RecreateService() { |
| 101 // Must set service to null first so that it is destroyed before the new | 114 // Must set service to null first so that it is destroyed before the new |
| 102 // one is created. | 115 // one is created. |
| 103 service_->Shutdown(); | 116 service_->Shutdown(); |
| 104 service_.reset(); | 117 service_.reset(); |
| 105 service_.reset(new TabRestoreService(profile(), time_factory_)); | 118 service_.reset(new PersistentTabRestoreService(profile(), time_factory_)); |
| 106 service_->LoadTabsFromLastSession(); | 119 service_->LoadTabsFromLastSession(); |
| 107 } | 120 } |
| 108 | 121 |
| 109 // Adds a window with one tab and url to the profile's session service. | 122 // Adds a window with one tab and url to the profile's session service. |
| 110 // If |pinned| is true, the tab is marked as pinned in the session service. | 123 // If |pinned| is true, the tab is marked as pinned in the session service. |
| 111 void AddWindowWithOneTabToSessionService(bool pinned) { | 124 void AddWindowWithOneTabToSessionService(bool pinned) { |
| 112 SessionService* session_service = | 125 SessionService* session_service = |
| 113 SessionServiceFactory::GetForProfile(profile()); | 126 SessionServiceFactory::GetForProfile(profile()); |
| 114 SessionID tab_id; | 127 SessionID tab_id; |
| 115 SessionID window_id; | 128 SessionID window_id; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 137 AddWindowWithOneTabToSessionService(pinned); | 150 AddWindowWithOneTabToSessionService(pinned); |
| 138 | 151 |
| 139 // Set this, otherwise previous session won't be loaded. | 152 // Set this, otherwise previous session won't be loaded. |
| 140 profile()->set_last_session_exited_cleanly(false); | 153 profile()->set_last_session_exited_cleanly(false); |
| 141 } | 154 } |
| 142 | 155 |
| 143 GURL url1_; | 156 GURL url1_; |
| 144 GURL url2_; | 157 GURL url2_; |
| 145 GURL url3_; | 158 GURL url3_; |
| 146 std::string user_agent_override_; | 159 std::string user_agent_override_; |
| 147 scoped_ptr<TabRestoreService> service_; | 160 scoped_ptr<PersistentTabRestoreService> service_; |
| 148 TabRestoreTimeFactory* time_factory_; | 161 TabRestoreTimeFactory* time_factory_; |
| 149 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox | 162 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox |
| 150 webkit_platform_support_; | 163 webkit_platform_support_; |
| 151 content::TestBrowserThread ui_thread_; | 164 content::TestBrowserThread ui_thread_; |
| 152 }; | 165 }; |
| 153 | 166 |
| 154 TEST_F(TabRestoreServiceTest, Basic) { | 167 TEST_F(TabRestoreServiceTest, Basic) { |
| 155 AddThreeNavigations(); | 168 AddThreeNavigations(); |
| 156 | 169 |
| 157 // Have the service record the tab. | 170 // Have the service record the tab. |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); | 493 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); |
| 481 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); | 494 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); |
| 482 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); | 495 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); |
| 483 } | 496 } |
| 484 | 497 |
| 485 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service | 498 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service |
| 486 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. | 499 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. |
| 487 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { | 500 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { |
| 488 CreateSessionServiceWithOneWindow(false); | 501 CreateSessionServiceWithOneWindow(false); |
| 489 | 502 |
| 490 for (size_t i = 0; i < TabRestoreService::kMaxEntries; ++i) | 503 for (size_t i = 0; i < kMaxEntries; ++i) |
| 491 AddWindowWithOneTabToSessionService(false); | 504 AddWindowWithOneTabToSessionService(false); |
| 492 | 505 |
| 493 SessionServiceFactory::GetForProfile(profile())-> | 506 SessionServiceFactory::GetForProfile(profile())-> |
| 494 MoveCurrentSessionToLastSession(); | 507 MoveCurrentSessionToLastSession(); |
| 495 | 508 |
| 496 AddThreeNavigations(); | 509 AddThreeNavigations(); |
| 497 | 510 |
| 498 service_->CreateHistoricalTab(contents(), -1); | 511 service_->CreateHistoricalTab(contents(), -1); |
| 499 | 512 |
| 500 RecreateService(); | 513 RecreateService(); |
| 501 | 514 |
| 502 // We should get back kMaxEntries entries. We added more, but | 515 // We should get back kMaxEntries entries. We added more, but |
| 503 // TabRestoreService only allows up to kMaxEntries. | 516 // TabRestoreService only allows up to kMaxEntries. |
| 504 ASSERT_EQ(TabRestoreService::kMaxEntries, service_->entries().size()); | 517 ASSERT_EQ(kMaxEntries, service_->entries().size()); |
| 505 | 518 |
| 506 // The first entry should come from the session service. | 519 // The first entry should come from the session service. |
| 507 TabRestoreService::Entry* entry = service_->entries().front(); | 520 TabRestoreService::Entry* entry = service_->entries().front(); |
| 508 ASSERT_EQ(TabRestoreService::WINDOW, entry->type); | 521 ASSERT_EQ(TabRestoreService::WINDOW, entry->type); |
| 509 TabRestoreService::Window* window = | 522 TabRestoreService::Window* window = |
| 510 static_cast<TabRestoreService::Window*>(entry); | 523 static_cast<TabRestoreService::Window*>(entry); |
| 511 ASSERT_EQ(1U, window->tabs.size()); | 524 ASSERT_EQ(1U, window->tabs.size()); |
| 512 EXPECT_EQ(0, window->selected_tab_index); | 525 EXPECT_EQ(0, window->selected_tab_index); |
| 513 EXPECT_EQ(0, window->timestamp.ToInternalValue()); | 526 EXPECT_EQ(0, window->timestamp.ToInternalValue()); |
| 514 ASSERT_EQ(1U, window->tabs[0].navigations.size()); | 527 ASSERT_EQ(1U, window->tabs[0].navigations.size()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 EXPECT_EQ( | 579 EXPECT_EQ( |
| 567 SessionTypesTestHelper::GetTimestamp(old_navigations[i]), | 580 SessionTypesTestHelper::GetTimestamp(old_navigations[i]), |
| 568 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i])); | 581 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i])); |
| 569 } | 582 } |
| 570 } | 583 } |
| 571 | 584 |
| 572 TEST_F(TabRestoreServiceTest, PruneEntries) { | 585 TEST_F(TabRestoreServiceTest, PruneEntries) { |
| 573 service_->ClearEntries(); | 586 service_->ClearEntries(); |
| 574 ASSERT_TRUE(service_->entries().empty()); | 587 ASSERT_TRUE(service_->entries().empty()); |
| 575 | 588 |
| 576 const size_t max_entries = TabRestoreService::kMaxEntries; | 589 const size_t max_entries = kMaxEntries; |
| 577 for (size_t i = 0; i < max_entries + 5; i++) { | 590 for (size_t i = 0; i < max_entries + 5; i++) { |
| 578 TabNavigation navigation = | 591 TabNavigation navigation = |
| 579 SessionTypesTestHelper::CreateNavigation( | 592 SessionTypesTestHelper::CreateNavigation( |
| 580 StringPrintf("http://%d", static_cast<int>(i)), | 593 StringPrintf("http://%d", static_cast<int>(i)), |
| 581 StringPrintf("%d", static_cast<int>(i))); | 594 StringPrintf("%d", static_cast<int>(i))); |
| 582 | 595 |
| 583 Tab* tab = new Tab(); | 596 Tab* tab = new Tab(); |
| 584 tab->navigations.push_back(navigation); | 597 tab->navigations.push_back(navigation); |
| 585 tab->current_navigation_index = 0; | 598 tab->current_navigation_index = 0; |
| 586 | 599 |
| 587 service_->entries_.push_back(tab); | 600 mutable_entries()->push_back(tab); |
| 588 } | 601 } |
| 589 | 602 |
| 590 // Only keep kMaxEntries around. | 603 // Only keep kMaxEntries around. |
| 591 EXPECT_EQ(max_entries + 5, service_->entries_.size()); | 604 EXPECT_EQ(max_entries + 5, service_->entries().size()); |
| 592 service_->PruneEntries(); | 605 PruneEntries(); |
| 593 EXPECT_EQ(max_entries, service_->entries_.size()); | 606 EXPECT_EQ(max_entries, service_->entries().size()); |
| 594 // Pruning again does nothing. | 607 // Pruning again does nothing. |
| 595 service_->PruneEntries(); | 608 PruneEntries(); |
| 596 EXPECT_EQ(max_entries, service_->entries_.size()); | 609 EXPECT_EQ(max_entries, service_->entries().size()); |
| 597 | 610 |
| 598 // Prune older first. | 611 // Prune older first. |
| 599 const char kRecentUrl[] = "http://recent"; | 612 const char kRecentUrl[] = "http://recent"; |
| 600 TabNavigation navigation = | 613 TabNavigation navigation = |
| 601 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent"); | 614 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent"); |
| 602 Tab* tab = new Tab(); | 615 Tab* tab = new Tab(); |
| 603 tab->navigations.push_back(navigation); | 616 tab->navigations.push_back(navigation); |
| 604 tab->current_navigation_index = 0; | 617 tab->current_navigation_index = 0; |
| 605 service_->entries_.push_front(tab); | 618 mutable_entries()->push_front(tab); |
| 606 EXPECT_EQ(max_entries + 1, service_->entries_.size()); | 619 EXPECT_EQ(max_entries + 1, service_->entries().size()); |
| 607 service_->PruneEntries(); | 620 PruneEntries(); |
| 608 EXPECT_EQ(max_entries, service_->entries_.size()); | 621 EXPECT_EQ(max_entries, service_->entries().size()); |
| 609 EXPECT_EQ(GURL(kRecentUrl), | 622 EXPECT_EQ(GURL(kRecentUrl), |
| 610 static_cast<Tab*>(service_->entries_.front())-> | 623 static_cast<Tab*>(service_->entries().front())-> |
| 611 navigations[0].virtual_url()); | 624 navigations[0].virtual_url()); |
| 612 | 625 |
| 613 // Ignore NTPs. | 626 // Ignore NTPs. |
| 614 navigation = | 627 navigation = |
| 615 SessionTypesTestHelper::CreateNavigation( | 628 SessionTypesTestHelper::CreateNavigation( |
| 616 chrome::kChromeUINewTabURL, "New tab"); | 629 chrome::kChromeUINewTabURL, "New tab"); |
| 617 | 630 |
| 618 tab = new Tab(); | 631 tab = new Tab(); |
| 619 tab->navigations.push_back(navigation); | 632 tab->navigations.push_back(navigation); |
| 620 tab->current_navigation_index = 0; | 633 tab->current_navigation_index = 0; |
| 621 service_->entries_.push_front(tab); | 634 mutable_entries()->push_front(tab); |
| 622 | 635 |
| 623 EXPECT_EQ(max_entries + 1, service_->entries_.size()); | 636 EXPECT_EQ(max_entries + 1, service_->entries().size()); |
| 624 service_->PruneEntries(); | 637 PruneEntries(); |
| 625 EXPECT_EQ(max_entries, service_->entries_.size()); | 638 EXPECT_EQ(max_entries, service_->entries().size()); |
| 626 EXPECT_EQ(GURL(kRecentUrl), | 639 EXPECT_EQ(GURL(kRecentUrl), |
| 627 static_cast<Tab*>(service_->entries_.front())-> | 640 static_cast<Tab*>(service_->entries().front())-> |
| 628 navigations[0].virtual_url()); | 641 navigations[0].virtual_url()); |
| 629 | 642 |
| 630 // Don't prune pinned NTPs. | 643 // Don't prune pinned NTPs. |
| 631 tab = new Tab(); | 644 tab = new Tab(); |
| 632 tab->pinned = true; | 645 tab->pinned = true; |
| 633 tab->current_navigation_index = 0; | 646 tab->current_navigation_index = 0; |
| 634 tab->navigations.push_back(navigation); | 647 tab->navigations.push_back(navigation); |
| 635 service_->entries_.push_front(tab); | 648 mutable_entries()->push_front(tab); |
| 636 EXPECT_EQ(max_entries + 1, service_->entries_.size()); | 649 EXPECT_EQ(max_entries + 1, service_->entries().size()); |
| 637 service_->PruneEntries(); | 650 PruneEntries(); |
| 638 EXPECT_EQ(max_entries, service_->entries_.size()); | 651 EXPECT_EQ(max_entries, service_->entries().size()); |
| 639 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), | 652 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), |
| 640 static_cast<Tab*>(service_->entries_.front())-> | 653 static_cast<Tab*>(service_->entries().front())-> |
| 641 navigations[0].virtual_url()); | 654 navigations[0].virtual_url()); |
| 642 | 655 |
| 643 // Don't prune NTPs that have multiple navigations. | 656 // Don't prune NTPs that have multiple navigations. |
| 644 // (Erase the last NTP first.) | 657 // (Erase the last NTP first.) |
| 645 delete service_->entries_.front(); | 658 delete service_->entries().front(); |
| 646 service_->entries_.erase(service_->entries_.begin()); | 659 mutable_entries()->erase(mutable_entries()->begin()); |
| 647 tab = new Tab(); | 660 tab = new Tab(); |
| 648 tab->current_navigation_index = 1; | 661 tab->current_navigation_index = 1; |
| 649 tab->navigations.push_back(navigation); | 662 tab->navigations.push_back(navigation); |
| 650 tab->navigations.push_back(navigation); | 663 tab->navigations.push_back(navigation); |
| 651 service_->entries_.push_front(tab); | 664 mutable_entries()->push_front(tab); |
| 652 EXPECT_EQ(max_entries, service_->entries_.size()); | 665 EXPECT_EQ(max_entries, service_->entries().size()); |
| 653 service_->PruneEntries(); | 666 PruneEntries(); |
| 654 EXPECT_EQ(max_entries, service_->entries_.size()); | 667 EXPECT_EQ(max_entries, service_->entries().size()); |
| 655 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), | 668 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), |
| 656 static_cast<Tab*>(service_->entries_.front())-> | 669 static_cast<Tab*>(service_->entries().front())-> |
| 657 navigations[1].virtual_url()); | 670 navigations[1].virtual_url()); |
| 658 } | 671 } |
| 659 | 672 |
| 660 // Regression test for crbug.com/106082 | 673 // Regression test for crbug.com/106082 |
| 661 TEST_F(TabRestoreServiceTest, PruneIsCalled) { | 674 TEST_F(TabRestoreServiceTest, PruneIsCalled) { |
| 662 CreateSessionServiceWithOneWindow(false); | 675 CreateSessionServiceWithOneWindow(false); |
| 663 | 676 |
| 664 SessionServiceFactory::GetForProfile(profile())-> | 677 SessionServiceFactory::GetForProfile(profile())-> |
| 665 MoveCurrentSessionToLastSession(); | 678 MoveCurrentSessionToLastSession(); |
| 666 | 679 |
| 667 profile()->set_restored_last_session(true); | 680 profile()->set_restored_last_session(true); |
| 668 | 681 |
| 669 const size_t max_entries = TabRestoreService::kMaxEntries; | 682 const size_t max_entries = kMaxEntries; |
| 670 for (size_t i = 0; i < max_entries + 5; i++) { | 683 for (size_t i = 0; i < max_entries + 5; i++) { |
| 671 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); | 684 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); |
| 672 service_->CreateHistoricalTab(contents(), -1); | 685 service_->CreateHistoricalTab(contents(), -1); |
| 673 } | 686 } |
| 674 | 687 |
| 675 EXPECT_EQ(max_entries, service_->entries_.size()); | 688 EXPECT_EQ(max_entries, service_->entries().size()); |
| 676 // This should not crash. | 689 // This should not crash. |
| 677 service_->LoadTabsFromLastSession(); | 690 service_->LoadTabsFromLastSession(); |
| 678 EXPECT_EQ(max_entries, service_->entries_.size()); | 691 EXPECT_EQ(max_entries, service_->entries().size()); |
| 679 } | 692 } |
| OLD | NEW |