| 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 "chrome/browser/sessions/persistent_tab_restore_service.h" | 5 #include "chrome/browser/sessions/persistent_tab_restore_service.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/sessions/session_service.h" | 10 #include "chrome/browser/sessions/session_service.h" |
| 11 #include "chrome/browser/sessions/session_service_factory.h" | 11 #include "chrome/browser/sessions/session_service_factory.h" |
| 12 #include "chrome/browser/sessions/session_types.h" | 12 #include "chrome/browser/sessions/session_types.h" |
| 13 #include "chrome/browser/sessions/session_types_test_helper.h" | |
| 14 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 13 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 15 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 19 #include "chrome/test/base/chrome_render_view_test.h" | 18 #include "chrome/test/base/chrome_render_view_test.h" |
| 20 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/sessions/serialized_navigation_entry_test_helper.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
| 24 #include "content/public/browser/navigation_entry.h" | 24 #include "content/public/browser/navigation_entry.h" |
| 25 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_types.h" | 26 #include "content/public/browser/notification_types.h" |
| 27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/test/render_view_test.h" | 28 #include "content/public/test/render_view_test.h" |
| 29 #include "content/public/test/test_browser_thread.h" | 29 #include "content/public/test/test_browser_thread.h" |
| 30 #include "content/public/test/test_utils.h" | 30 #include "content/public/test/test_utils.h" |
| 31 #include "content/public/test/web_contents_tester.h" | 31 #include "content/public/test/web_contents_tester.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 34 | 34 |
| 35 typedef TabRestoreService::Tab Tab; | 35 typedef TabRestoreService::Tab Tab; |
| 36 typedef TabRestoreService::Window Window; | 36 typedef TabRestoreService::Window Window; |
| 37 |
| 38 using components::SerializedNavigationEntry; |
| 39 using components::SerializedNavigationEntryTestHelper; |
| 40 using content::NavigationEntry; |
| 37 using content::WebContentsTester; | 41 using content::WebContentsTester; |
| 38 | 42 |
| 39 using content::NavigationEntry; | |
| 40 | |
| 41 // Create subclass that overrides TimeNow so that we can control the time used | 43 // Create subclass that overrides TimeNow so that we can control the time used |
| 42 // for closed tabs and windows. | 44 // for closed tabs and windows. |
| 43 class PersistentTabRestoreTimeFactory : public TabRestoreService::TimeFactory { | 45 class PersistentTabRestoreTimeFactory : public TabRestoreService::TimeFactory { |
| 44 public: | 46 public: |
| 45 PersistentTabRestoreTimeFactory() : time_(base::Time::Now()) {} | 47 PersistentTabRestoreTimeFactory() : time_(base::Time::Now()) {} |
| 46 | 48 |
| 47 virtual ~PersistentTabRestoreTimeFactory() {} | 49 virtual ~PersistentTabRestoreTimeFactory() {} |
| 48 | 50 |
| 49 virtual base::Time TimeNow() OVERRIDE { | 51 virtual base::Time TimeNow() OVERRIDE { |
| 50 return time_; | 52 return time_; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 SessionID window_id; | 131 SessionID window_id; |
| 130 session_service->SetWindowType( | 132 session_service->SetWindowType( |
| 131 window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL); | 133 window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL); |
| 132 session_service->SetTabWindow(window_id, tab_id); | 134 session_service->SetTabWindow(window_id, tab_id); |
| 133 session_service->SetTabIndexInWindow(window_id, tab_id, 0); | 135 session_service->SetTabIndexInWindow(window_id, tab_id, 0); |
| 134 session_service->SetSelectedTabInWindow(window_id, 0); | 136 session_service->SetSelectedTabInWindow(window_id, 0); |
| 135 if (pinned) | 137 if (pinned) |
| 136 session_service->SetPinnedState(window_id, tab_id, true); | 138 session_service->SetPinnedState(window_id, tab_id, true); |
| 137 session_service->UpdateTabNavigation( | 139 session_service->UpdateTabNavigation( |
| 138 window_id, tab_id, | 140 window_id, tab_id, |
| 139 SessionTypesTestHelper::CreateNavigation(url1_.spec(), "title")); | 141 SerializedNavigationEntryTestHelper::CreateNavigation( |
| 142 url1_.spec(), "title")); |
| 140 } | 143 } |
| 141 | 144 |
| 142 // Creates a SessionService and assigns it to the Profile. The SessionService | 145 // Creates a SessionService and assigns it to the Profile. The SessionService |
| 143 // is configured with a single window with a single tab pointing at url1_ by | 146 // is configured with a single window with a single tab pointing at url1_ by |
| 144 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the | 147 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the |
| 145 // tab is marked as pinned in the session service. | 148 // tab is marked as pinned in the session service. |
| 146 void CreateSessionServiceWithOneWindow(bool pinned) { | 149 void CreateSessionServiceWithOneWindow(bool pinned) { |
| 147 // The profile takes ownership of this. | 150 // The profile takes ownership of this. |
| 148 SessionService* session_service = new SessionService(profile()); | 151 SessionService* session_service = new SessionService(profile()); |
| 149 SessionServiceFactory::SetForTestProfile(profile(), session_service); | 152 SessionServiceFactory::SetForTestProfile(profile(), session_service); |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 | 541 |
| 539 AddThreeNavigations(); | 542 AddThreeNavigations(); |
| 540 | 543 |
| 541 // Have the service record the tab. | 544 // Have the service record the tab. |
| 542 service_->CreateHistoricalTab(web_contents(), -1); | 545 service_->CreateHistoricalTab(web_contents(), -1); |
| 543 | 546 |
| 544 // Make sure an entry was created. | 547 // Make sure an entry was created. |
| 545 ASSERT_EQ(1U, service_->entries().size()); | 548 ASSERT_EQ(1U, service_->entries().size()); |
| 546 | 549 |
| 547 // Make sure the entry matches. | 550 // Make sure the entry matches. |
| 548 std::vector<TabNavigation> old_navigations; | 551 std::vector<SerializedNavigationEntry> old_navigations; |
| 549 { | 552 { |
| 550 // |entry|/|tab| doesn't survive after RecreateService(). | 553 // |entry|/|tab| doesn't survive after RecreateService(). |
| 551 TabRestoreService::Entry* entry = service_->entries().front(); | 554 TabRestoreService::Entry* entry = service_->entries().front(); |
| 552 ASSERT_EQ(TabRestoreService::TAB, entry->type); | 555 ASSERT_EQ(TabRestoreService::TAB, entry->type); |
| 553 Tab* tab = static_cast<Tab*>(entry); | 556 Tab* tab = static_cast<Tab*>(entry); |
| 554 tab->timestamp = tab_timestamp; | 557 tab->timestamp = tab_timestamp; |
| 555 old_navigations = tab->navigations; | 558 old_navigations = tab->navigations; |
| 556 } | 559 } |
| 557 | 560 |
| 558 EXPECT_EQ(3U, old_navigations.size()); | 561 EXPECT_EQ(3U, old_navigations.size()); |
| 559 for (size_t i = 0; i < old_navigations.size(); ++i) { | 562 for (size_t i = 0; i < old_navigations.size(); ++i) { |
| 560 EXPECT_FALSE( | 563 EXPECT_FALSE(old_navigations[i].timestamp().is_null()); |
| 561 SessionTypesTestHelper::GetTimestamp(old_navigations[i]).is_null()); | |
| 562 } | 564 } |
| 563 | 565 |
| 564 // Set this, otherwise previous session won't be loaded. | 566 // Set this, otherwise previous session won't be loaded. |
| 565 profile()->set_last_session_exited_cleanly(false); | 567 profile()->set_last_session_exited_cleanly(false); |
| 566 | 568 |
| 567 RecreateService(); | 569 RecreateService(); |
| 568 | 570 |
| 569 // One entry should be created. | 571 // One entry should be created. |
| 570 ASSERT_EQ(1U, service_->entries().size()); | 572 ASSERT_EQ(1U, service_->entries().size()); |
| 571 | 573 |
| 572 // And verify the entry. | 574 // And verify the entry. |
| 573 TabRestoreService::Entry* restored_entry = service_->entries().front(); | 575 TabRestoreService::Entry* restored_entry = service_->entries().front(); |
| 574 ASSERT_EQ(TabRestoreService::TAB, restored_entry->type); | 576 ASSERT_EQ(TabRestoreService::TAB, restored_entry->type); |
| 575 Tab* restored_tab = | 577 Tab* restored_tab = |
| 576 static_cast<Tab*>(restored_entry); | 578 static_cast<Tab*>(restored_entry); |
| 577 EXPECT_EQ(tab_timestamp.ToInternalValue(), | 579 EXPECT_EQ(tab_timestamp.ToInternalValue(), |
| 578 restored_tab->timestamp.ToInternalValue()); | 580 restored_tab->timestamp.ToInternalValue()); |
| 579 ASSERT_EQ(old_navigations.size(), restored_tab->navigations.size()); | 581 ASSERT_EQ(old_navigations.size(), restored_tab->navigations.size()); |
| 580 for (size_t i = 0; i < restored_tab->navigations.size(); ++i) { | 582 for (size_t i = 0; i < restored_tab->navigations.size(); ++i) { |
| 581 EXPECT_EQ( | 583 EXPECT_EQ(old_navigations[i].timestamp(), |
| 582 SessionTypesTestHelper::GetTimestamp(old_navigations[i]), | 584 restored_tab->navigations[i].timestamp()); |
| 583 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i])); | |
| 584 } | 585 } |
| 585 } | 586 } |
| 586 | 587 |
| 587 TEST_F(PersistentTabRestoreServiceTest, PruneEntries) { | 588 TEST_F(PersistentTabRestoreServiceTest, PruneEntries) { |
| 588 service_->ClearEntries(); | 589 service_->ClearEntries(); |
| 589 ASSERT_TRUE(service_->entries().empty()); | 590 ASSERT_TRUE(service_->entries().empty()); |
| 590 | 591 |
| 591 const size_t max_entries = kMaxEntries; | 592 const size_t max_entries = kMaxEntries; |
| 592 for (size_t i = 0; i < max_entries + 5; i++) { | 593 for (size_t i = 0; i < max_entries + 5; i++) { |
| 593 TabNavigation navigation = | 594 SerializedNavigationEntry navigation = |
| 594 SessionTypesTestHelper::CreateNavigation( | 595 SerializedNavigationEntryTestHelper::CreateNavigation( |
| 595 base::StringPrintf("http://%d", static_cast<int>(i)), | 596 base::StringPrintf("http://%d", static_cast<int>(i)), |
| 596 base::StringPrintf("%d", static_cast<int>(i))); | 597 base::StringPrintf("%d", static_cast<int>(i))); |
| 597 | 598 |
| 598 Tab* tab = new Tab(); | 599 Tab* tab = new Tab(); |
| 599 tab->navigations.push_back(navigation); | 600 tab->navigations.push_back(navigation); |
| 600 tab->current_navigation_index = 0; | 601 tab->current_navigation_index = 0; |
| 601 | 602 |
| 602 mutable_entries()->push_back(tab); | 603 mutable_entries()->push_back(tab); |
| 603 } | 604 } |
| 604 | 605 |
| 605 // Only keep kMaxEntries around. | 606 // Only keep kMaxEntries around. |
| 606 EXPECT_EQ(max_entries + 5, service_->entries().size()); | 607 EXPECT_EQ(max_entries + 5, service_->entries().size()); |
| 607 PruneEntries(); | 608 PruneEntries(); |
| 608 EXPECT_EQ(max_entries, service_->entries().size()); | 609 EXPECT_EQ(max_entries, service_->entries().size()); |
| 609 // Pruning again does nothing. | 610 // Pruning again does nothing. |
| 610 PruneEntries(); | 611 PruneEntries(); |
| 611 EXPECT_EQ(max_entries, service_->entries().size()); | 612 EXPECT_EQ(max_entries, service_->entries().size()); |
| 612 | 613 |
| 613 // Prune older first. | 614 // Prune older first. |
| 614 const char kRecentUrl[] = "http://recent"; | 615 const char kRecentUrl[] = "http://recent"; |
| 615 TabNavigation navigation = | 616 SerializedNavigationEntry navigation = |
| 616 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent"); | 617 SerializedNavigationEntryTestHelper::CreateNavigation(kRecentUrl, |
| 618 "Most recent"); |
| 617 Tab* tab = new Tab(); | 619 Tab* tab = new Tab(); |
| 618 tab->navigations.push_back(navigation); | 620 tab->navigations.push_back(navigation); |
| 619 tab->current_navigation_index = 0; | 621 tab->current_navigation_index = 0; |
| 620 mutable_entries()->push_front(tab); | 622 mutable_entries()->push_front(tab); |
| 621 EXPECT_EQ(max_entries + 1, service_->entries().size()); | 623 EXPECT_EQ(max_entries + 1, service_->entries().size()); |
| 622 PruneEntries(); | 624 PruneEntries(); |
| 623 EXPECT_EQ(max_entries, service_->entries().size()); | 625 EXPECT_EQ(max_entries, service_->entries().size()); |
| 624 EXPECT_EQ(GURL(kRecentUrl), | 626 EXPECT_EQ(GURL(kRecentUrl), |
| 625 static_cast<Tab*>(service_->entries().front())-> | 627 static_cast<Tab*>(service_->entries().front())-> |
| 626 navigations[0].virtual_url()); | 628 navigations[0].virtual_url()); |
| 627 | 629 |
| 628 // Ignore NTPs. | 630 // Ignore NTPs. |
| 629 navigation = | 631 navigation = SerializedNavigationEntryTestHelper::CreateNavigation( |
| 630 SessionTypesTestHelper::CreateNavigation( | 632 chrome::kChromeUINewTabURL, "New tab"); |
| 631 chrome::kChromeUINewTabURL, "New tab"); | |
| 632 | 633 |
| 633 tab = new Tab(); | 634 tab = new Tab(); |
| 634 tab->navigations.push_back(navigation); | 635 tab->navigations.push_back(navigation); |
| 635 tab->current_navigation_index = 0; | 636 tab->current_navigation_index = 0; |
| 636 mutable_entries()->push_front(tab); | 637 mutable_entries()->push_front(tab); |
| 637 | 638 |
| 638 EXPECT_EQ(max_entries + 1, service_->entries().size()); | 639 EXPECT_EQ(max_entries + 1, service_->entries().size()); |
| 639 PruneEntries(); | 640 PruneEntries(); |
| 640 EXPECT_EQ(max_entries, service_->entries().size()); | 641 EXPECT_EQ(max_entries, service_->entries().size()); |
| 641 EXPECT_EQ(GURL(kRecentUrl), | 642 EXPECT_EQ(GURL(kRecentUrl), |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 NavigateAndCommit( | 687 NavigateAndCommit( |
| 687 GURL(base::StringPrintf("http://%d", static_cast<int>(i)))); | 688 GURL(base::StringPrintf("http://%d", static_cast<int>(i)))); |
| 688 service_->CreateHistoricalTab(web_contents(), -1); | 689 service_->CreateHistoricalTab(web_contents(), -1); |
| 689 } | 690 } |
| 690 | 691 |
| 691 EXPECT_EQ(max_entries, service_->entries().size()); | 692 EXPECT_EQ(max_entries, service_->entries().size()); |
| 692 // This should not crash. | 693 // This should not crash. |
| 693 service_->LoadTabsFromLastSession(); | 694 service_->LoadTabsFromLastSession(); |
| 694 EXPECT_EQ(max_entries, service_->entries().size()); | 695 EXPECT_EQ(max_entries, service_->entries().size()); |
| 695 } | 696 } |
| OLD | NEW |