| 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/stringprintf.h" | 5 #include "base/stringprintf.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/sessions/session_service.h" |
| 7 #include "chrome/browser/sessions/session_service_factory.h" | 8 #include "chrome/browser/sessions/session_service_factory.h" |
| 8 #include "chrome/browser/sessions/session_service.h" | |
| 9 #include "chrome/browser/sessions/session_types.h" | 9 #include "chrome/browser/sessions/session_types.h" |
| 10 #include "chrome/browser/sessions/tab_restore_service.h" | 10 #include "chrome/browser/sessions/tab_restore_service.h" |
| 11 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 11 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/chrome_render_view_test.h" | 16 #include "chrome/test/base/chrome_render_view_test.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 SessionServiceFactory::GetForProfile(profile()); | 112 SessionServiceFactory::GetForProfile(profile()); |
| 113 SessionID tab_id; | 113 SessionID tab_id; |
| 114 SessionID window_id; | 114 SessionID window_id; |
| 115 session_service->SetWindowType( | 115 session_service->SetWindowType( |
| 116 window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL); | 116 window_id, Browser::TYPE_TABBED, SessionService::TYPE_NORMAL); |
| 117 session_service->SetTabWindow(window_id, tab_id); | 117 session_service->SetTabWindow(window_id, tab_id); |
| 118 session_service->SetTabIndexInWindow(window_id, tab_id, 0); | 118 session_service->SetTabIndexInWindow(window_id, tab_id, 0); |
| 119 session_service->SetSelectedTabInWindow(window_id, 0); | 119 session_service->SetSelectedTabInWindow(window_id, 0); |
| 120 if (pinned) | 120 if (pinned) |
| 121 session_service->SetPinnedState(window_id, tab_id, true); | 121 session_service->SetPinnedState(window_id, tab_id, true); |
| 122 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); | 122 session_service->UpdateTabNavigation( |
| 123 entry->SetURL(url1_); | 123 window_id, tab_id, |
| 124 session_service->UpdateTabNavigation(window_id, tab_id, 0, *entry.get()); | 124 TabNavigation::CreateForTest(url1_.spec(), "title")); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Creates a SessionService and assigns it to the Profile. The SessionService | 127 // Creates a SessionService and assigns it to the Profile. The SessionService |
| 128 // is configured with a single window with a single tab pointing at url1_ by | 128 // is configured with a single window with a single tab pointing at url1_ by |
| 129 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the | 129 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the |
| 130 // tab is marked as pinned in the session service. | 130 // tab is marked as pinned in the session service. |
| 131 void CreateSessionServiceWithOneWindow(bool pinned) { | 131 void CreateSessionServiceWithOneWindow(bool pinned) { |
| 132 // The profile takes ownership of this. | 132 // The profile takes ownership of this. |
| 133 SessionService* session_service = new SessionService(profile()); | 133 SessionService* session_service = new SessionService(profile()); |
| 134 SessionServiceFactory::SetForTestProfile(profile(), session_service); | 134 SessionServiceFactory::SetForTestProfile(profile(), session_service); |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 EXPECT_EQ(tab_timestamp.ToInternalValue(), | 550 EXPECT_EQ(tab_timestamp.ToInternalValue(), |
| 551 restored_tab->timestamp.ToInternalValue()); | 551 restored_tab->timestamp.ToInternalValue()); |
| 552 } | 552 } |
| 553 | 553 |
| 554 TEST_F(TabRestoreServiceTest, PruneEntries) { | 554 TEST_F(TabRestoreServiceTest, PruneEntries) { |
| 555 service_->ClearEntries(); | 555 service_->ClearEntries(); |
| 556 ASSERT_TRUE(service_->entries().empty()); | 556 ASSERT_TRUE(service_->entries().empty()); |
| 557 | 557 |
| 558 const size_t max_entries = TabRestoreService::kMaxEntries; | 558 const size_t max_entries = TabRestoreService::kMaxEntries; |
| 559 for (size_t i = 0; i < max_entries + 5; i++) { | 559 for (size_t i = 0; i < max_entries + 5; i++) { |
| 560 TabNavigation navigation; | 560 TabNavigation navigation = |
| 561 navigation.set_virtual_url(GURL(StringPrintf("http://%d", | 561 TabNavigation::CreateForTest( |
| 562 static_cast<int>(i)))); | 562 StringPrintf("http://%d", static_cast<int>(i)), |
| 563 navigation.set_title(ASCIIToUTF16(StringPrintf("%d", static_cast<int>(i)))); | 563 StringPrintf("%d", static_cast<int>(i))); |
| 564 | 564 |
| 565 Tab* tab = new Tab(); | 565 Tab* tab = new Tab(); |
| 566 tab->navigations.push_back(navigation); | 566 tab->navigations.push_back(navigation); |
| 567 tab->current_navigation_index = 0; | 567 tab->current_navigation_index = 0; |
| 568 | 568 |
| 569 service_->entries_.push_back(tab); | 569 service_->entries_.push_back(tab); |
| 570 } | 570 } |
| 571 | 571 |
| 572 // Only keep kMaxEntries around. | 572 // Only keep kMaxEntries around. |
| 573 EXPECT_EQ(max_entries + 5, service_->entries_.size()); | 573 EXPECT_EQ(max_entries + 5, service_->entries_.size()); |
| 574 service_->PruneEntries(); | 574 service_->PruneEntries(); |
| 575 EXPECT_EQ(max_entries, service_->entries_.size()); | 575 EXPECT_EQ(max_entries, service_->entries_.size()); |
| 576 // Pruning again does nothing. | 576 // Pruning again does nothing. |
| 577 service_->PruneEntries(); | 577 service_->PruneEntries(); |
| 578 EXPECT_EQ(max_entries, service_->entries_.size()); | 578 EXPECT_EQ(max_entries, service_->entries_.size()); |
| 579 | 579 |
| 580 // Prune older first. | 580 // Prune older first. |
| 581 TabNavigation navigation; | |
| 582 const char kRecentUrl[] = "http://recent"; | 581 const char kRecentUrl[] = "http://recent"; |
| 583 navigation.set_virtual_url(GURL(kRecentUrl)); | 582 TabNavigation navigation = |
| 584 navigation.set_title(ASCIIToUTF16("Most recent")); | 583 TabNavigation::CreateForTest(kRecentUrl, "Most recent"); |
| 585 Tab* tab = new Tab(); | 584 Tab* tab = new Tab(); |
| 586 tab->navigations.push_back(navigation); | 585 tab->navigations.push_back(navigation); |
| 587 tab->current_navigation_index = 0; | 586 tab->current_navigation_index = 0; |
| 588 service_->entries_.push_front(tab); | 587 service_->entries_.push_front(tab); |
| 589 EXPECT_EQ(max_entries + 1, service_->entries_.size()); | 588 EXPECT_EQ(max_entries + 1, service_->entries_.size()); |
| 590 service_->PruneEntries(); | 589 service_->PruneEntries(); |
| 591 EXPECT_EQ(max_entries, service_->entries_.size()); | 590 EXPECT_EQ(max_entries, service_->entries_.size()); |
| 592 EXPECT_EQ(GURL(kRecentUrl), | 591 EXPECT_EQ(GURL(kRecentUrl), |
| 593 static_cast<Tab*>(service_->entries_.front())-> | 592 static_cast<Tab*>(service_->entries_.front())-> |
| 594 navigations[0].virtual_url()); | 593 navigations[0].virtual_url()); |
| 595 | 594 |
| 596 // Ignore NTPs. | 595 // Ignore NTPs. |
| 597 navigation.set_virtual_url(GURL(chrome::kChromeUINewTabURL)); | 596 navigation = |
| 598 navigation.set_title(ASCIIToUTF16("New tab")); | 597 TabNavigation::CreateForTest(chrome::kChromeUINewTabURL, "New tab"); |
| 599 | 598 |
| 600 tab = new Tab(); | 599 tab = new Tab(); |
| 601 tab->navigations.push_back(navigation); | 600 tab->navigations.push_back(navigation); |
| 602 tab->current_navigation_index = 0; | 601 tab->current_navigation_index = 0; |
| 603 service_->entries_.push_front(tab); | 602 service_->entries_.push_front(tab); |
| 604 | 603 |
| 605 EXPECT_EQ(max_entries + 1, service_->entries_.size()); | 604 EXPECT_EQ(max_entries + 1, service_->entries_.size()); |
| 606 service_->PruneEntries(); | 605 service_->PruneEntries(); |
| 607 EXPECT_EQ(max_entries, service_->entries_.size()); | 606 EXPECT_EQ(max_entries, service_->entries_.size()); |
| 608 EXPECT_EQ(GURL(kRecentUrl), | 607 EXPECT_EQ(GURL(kRecentUrl), |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 for (size_t i = 0; i < max_entries + 5; i++) { | 651 for (size_t i = 0; i < max_entries + 5; i++) { |
| 653 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); | 652 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); |
| 654 service_->CreateHistoricalTab(contents(), -1); | 653 service_->CreateHistoricalTab(contents(), -1); |
| 655 } | 654 } |
| 656 | 655 |
| 657 EXPECT_EQ(max_entries, service_->entries_.size()); | 656 EXPECT_EQ(max_entries, service_->entries_.size()); |
| 658 // This should not crash. | 657 // This should not crash. |
| 659 service_->LoadTabsFromLastSession(); | 658 service_->LoadTabsFromLastSession(); |
| 660 EXPECT_EQ(max_entries, service_->entries_.size()); | 659 EXPECT_EQ(max_entries, service_->entries_.size()); |
| 661 } | 660 } |
| OLD | NEW |