Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: chrome/browser/sessions/tab_restore_service_browsertest.cc

Issue 10989027: Split TabRestoreService into InMemoryTRS and PersistentTRS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix some comments Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 static const size_t kMaxEntries;
71 virtual void SetUp() { 72
73 // testing::Test:
74 virtual void SetUp() OVERRIDE {
72 WebKit::initialize(webkit_platform_support_.Get()); 75 WebKit::initialize(webkit_platform_support_.Get());
73 ChromeRenderViewHostTestHarness::SetUp(); 76 ChromeRenderViewHostTestHarness::SetUp();
74 time_factory_ = new TabRestoreTimeFactory(); 77 time_factory_ = new TabRestoreTimeFactory();
75 service_.reset(new TabRestoreService(profile(), time_factory_)); 78 service_.reset(new PersistentTabRestoreService(profile(), time_factory_));
76 } 79 }
77 80
78 virtual void TearDown() { 81 virtual void TearDown() OVERRIDE {
79 service_->Shutdown(); 82 service_->Shutdown();
80 service_.reset(); 83 service_.reset();
81 delete time_factory_; 84 delete time_factory_;
82 ChromeRenderViewHostTestHarness::TearDown(); 85 ChromeRenderViewHostTestHarness::TearDown();
83 WebKit::shutdown(); 86 WebKit::shutdown();
84 } 87 }
85 88
89 TabRestoreService::Entries* mutable_entries() {
90 return service_->mutable_entries();
91 }
92
93 void PruneEntries() {
94 service_->PruneEntries();
95 }
96
86 void AddThreeNavigations() { 97 void AddThreeNavigations() {
87 // Navigate to three URLs. 98 // Navigate to three URLs.
88 NavigateAndCommit(url1_); 99 NavigateAndCommit(url1_);
89 NavigateAndCommit(url2_); 100 NavigateAndCommit(url2_);
90 NavigateAndCommit(url3_); 101 NavigateAndCommit(url3_);
91 } 102 }
92 103
93 void NavigateToIndex(int index) { 104 void NavigateToIndex(int index) {
94 // Navigate back. We have to do this song and dance as NavigationController 105 // Navigate back. We have to do this song and dance as NavigationController
95 // isn't happy if you navigate immediately while going back. 106 // isn't happy if you navigate immediately while going back.
96 controller().GoToIndex(index); 107 controller().GoToIndex(index);
97 WebContentsTester::For(contents())->CommitPendingNavigation(); 108 WebContentsTester::For(contents())->CommitPendingNavigation();
98 } 109 }
99 110
100 void RecreateService() { 111 void RecreateService() {
101 // Must set service to null first so that it is destroyed before the new 112 // Must set service to null first so that it is destroyed before the new
102 // one is created. 113 // one is created.
103 service_->Shutdown(); 114 service_->Shutdown();
104 service_.reset(); 115 service_.reset();
105 service_.reset(new TabRestoreService(profile(), time_factory_)); 116 service_.reset(new PersistentTabRestoreService(profile(), time_factory_));
106 service_->LoadTabsFromLastSession(); 117 service_->LoadTabsFromLastSession();
107 } 118 }
108 119
109 // Adds a window with one tab and url to the profile's session service. 120 // 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. 121 // If |pinned| is true, the tab is marked as pinned in the session service.
111 void AddWindowWithOneTabToSessionService(bool pinned) { 122 void AddWindowWithOneTabToSessionService(bool pinned) {
112 SessionService* session_service = 123 SessionService* session_service =
113 SessionServiceFactory::GetForProfile(profile()); 124 SessionServiceFactory::GetForProfile(profile());
114 SessionID tab_id; 125 SessionID tab_id;
115 SessionID window_id; 126 SessionID window_id;
(...skipping 21 matching lines...) Expand all
137 AddWindowWithOneTabToSessionService(pinned); 148 AddWindowWithOneTabToSessionService(pinned);
138 149
139 // Set this, otherwise previous session won't be loaded. 150 // Set this, otherwise previous session won't be loaded.
140 profile()->set_last_session_exited_cleanly(false); 151 profile()->set_last_session_exited_cleanly(false);
141 } 152 }
142 153
143 GURL url1_; 154 GURL url1_;
144 GURL url2_; 155 GURL url2_;
145 GURL url3_; 156 GURL url3_;
146 std::string user_agent_override_; 157 std::string user_agent_override_;
147 scoped_ptr<TabRestoreService> service_; 158 scoped_ptr<PersistentTabRestoreService> service_;
148 TabRestoreTimeFactory* time_factory_; 159 TabRestoreTimeFactory* time_factory_;
149 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox 160 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox
150 webkit_platform_support_; 161 webkit_platform_support_;
151 content::TestBrowserThread ui_thread_; 162 content::TestBrowserThread ui_thread_;
152 }; 163 };
153 164
165 const size_t TabRestoreServiceTest::kMaxEntries =
166 TabRestoreServiceHelper::kMaxEntries;
167
154 TEST_F(TabRestoreServiceTest, Basic) { 168 TEST_F(TabRestoreServiceTest, Basic) {
155 AddThreeNavigations(); 169 AddThreeNavigations();
156 170
157 // Have the service record the tab. 171 // Have the service record the tab.
158 service_->CreateHistoricalTab(contents(), -1); 172 service_->CreateHistoricalTab(contents(), -1);
159 173
160 // Make sure an entry was created. 174 // Make sure an entry was created.
161 ASSERT_EQ(1U, service_->entries().size()); 175 ASSERT_EQ(1U, service_->entries().size());
162 176
163 // Make sure the entry matches. 177 // Make sure the entry matches.
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); 494 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url());
481 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); 495 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url());
482 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); 496 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url());
483 } 497 }
484 498
485 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service 499 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service
486 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. 500 // and makes sure we only get back TabRestoreService::kMaxEntries on restore.
487 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { 501 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) {
488 CreateSessionServiceWithOneWindow(false); 502 CreateSessionServiceWithOneWindow(false);
489 503
490 for (size_t i = 0; i < TabRestoreService::kMaxEntries; ++i) 504 for (size_t i = 0; i < kMaxEntries; ++i)
491 AddWindowWithOneTabToSessionService(false); 505 AddWindowWithOneTabToSessionService(false);
492 506
493 SessionServiceFactory::GetForProfile(profile())-> 507 SessionServiceFactory::GetForProfile(profile())->
494 MoveCurrentSessionToLastSession(); 508 MoveCurrentSessionToLastSession();
495 509
496 AddThreeNavigations(); 510 AddThreeNavigations();
497 511
498 service_->CreateHistoricalTab(contents(), -1); 512 service_->CreateHistoricalTab(contents(), -1);
499 513
500 RecreateService(); 514 RecreateService();
501 515
502 // We should get back kMaxEntries entries. We added more, but 516 // We should get back kMaxEntries entries. We added more, but
503 // TabRestoreService only allows up to kMaxEntries. 517 // TabRestoreService only allows up to kMaxEntries.
504 ASSERT_EQ(TabRestoreService::kMaxEntries, service_->entries().size()); 518 ASSERT_EQ(kMaxEntries, service_->entries().size());
505 519
506 // The first entry should come from the session service. 520 // The first entry should come from the session service.
507 TabRestoreService::Entry* entry = service_->entries().front(); 521 TabRestoreService::Entry* entry = service_->entries().front();
508 ASSERT_EQ(TabRestoreService::WINDOW, entry->type); 522 ASSERT_EQ(TabRestoreService::WINDOW, entry->type);
509 TabRestoreService::Window* window = 523 TabRestoreService::Window* window =
510 static_cast<TabRestoreService::Window*>(entry); 524 static_cast<TabRestoreService::Window*>(entry);
511 ASSERT_EQ(1U, window->tabs.size()); 525 ASSERT_EQ(1U, window->tabs.size());
512 EXPECT_EQ(0, window->selected_tab_index); 526 EXPECT_EQ(0, window->selected_tab_index);
513 EXPECT_EQ(0, window->timestamp.ToInternalValue()); 527 EXPECT_EQ(0, window->timestamp.ToInternalValue());
514 ASSERT_EQ(1U, window->tabs[0].navigations.size()); 528 ASSERT_EQ(1U, window->tabs[0].navigations.size());
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 EXPECT_EQ( 580 EXPECT_EQ(
567 SessionTypesTestHelper::GetTimestamp(old_navigations[i]), 581 SessionTypesTestHelper::GetTimestamp(old_navigations[i]),
568 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i])); 582 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i]));
569 } 583 }
570 } 584 }
571 585
572 TEST_F(TabRestoreServiceTest, PruneEntries) { 586 TEST_F(TabRestoreServiceTest, PruneEntries) {
573 service_->ClearEntries(); 587 service_->ClearEntries();
574 ASSERT_TRUE(service_->entries().empty()); 588 ASSERT_TRUE(service_->entries().empty());
575 589
576 const size_t max_entries = TabRestoreService::kMaxEntries; 590 const size_t max_entries = kMaxEntries;
577 for (size_t i = 0; i < max_entries + 5; i++) { 591 for (size_t i = 0; i < max_entries + 5; i++) {
578 TabNavigation navigation = 592 TabNavigation navigation =
579 SessionTypesTestHelper::CreateNavigation( 593 SessionTypesTestHelper::CreateNavigation(
580 StringPrintf("http://%d", static_cast<int>(i)), 594 StringPrintf("http://%d", static_cast<int>(i)),
581 StringPrintf("%d", static_cast<int>(i))); 595 StringPrintf("%d", static_cast<int>(i)));
582 596
583 Tab* tab = new Tab(); 597 Tab* tab = new Tab();
584 tab->navigations.push_back(navigation); 598 tab->navigations.push_back(navigation);
585 tab->current_navigation_index = 0; 599 tab->current_navigation_index = 0;
586 600
587 service_->entries_.push_back(tab); 601 mutable_entries()->push_back(tab);
588 } 602 }
589 603
590 // Only keep kMaxEntries around. 604 // Only keep kMaxEntries around.
591 EXPECT_EQ(max_entries + 5, service_->entries_.size()); 605 EXPECT_EQ(max_entries + 5, service_->entries().size());
592 service_->PruneEntries(); 606 PruneEntries();
593 EXPECT_EQ(max_entries, service_->entries_.size()); 607 EXPECT_EQ(max_entries, service_->entries().size());
594 // Pruning again does nothing. 608 // Pruning again does nothing.
595 service_->PruneEntries(); 609 PruneEntries();
596 EXPECT_EQ(max_entries, service_->entries_.size()); 610 EXPECT_EQ(max_entries, service_->entries().size());
597 611
598 // Prune older first. 612 // Prune older first.
599 const char kRecentUrl[] = "http://recent"; 613 const char kRecentUrl[] = "http://recent";
600 TabNavigation navigation = 614 TabNavigation navigation =
601 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent"); 615 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent");
602 Tab* tab = new Tab(); 616 Tab* tab = new Tab();
603 tab->navigations.push_back(navigation); 617 tab->navigations.push_back(navigation);
604 tab->current_navigation_index = 0; 618 tab->current_navigation_index = 0;
605 service_->entries_.push_front(tab); 619 mutable_entries()->push_front(tab);
606 EXPECT_EQ(max_entries + 1, service_->entries_.size()); 620 EXPECT_EQ(max_entries + 1, service_->entries().size());
607 service_->PruneEntries(); 621 PruneEntries();
608 EXPECT_EQ(max_entries, service_->entries_.size()); 622 EXPECT_EQ(max_entries, service_->entries().size());
609 EXPECT_EQ(GURL(kRecentUrl), 623 EXPECT_EQ(GURL(kRecentUrl),
610 static_cast<Tab*>(service_->entries_.front())-> 624 static_cast<Tab*>(service_->entries().front())->
611 navigations[0].virtual_url()); 625 navigations[0].virtual_url());
612 626
613 // Ignore NTPs. 627 // Ignore NTPs.
614 navigation = 628 navigation =
615 SessionTypesTestHelper::CreateNavigation( 629 SessionTypesTestHelper::CreateNavigation(
616 chrome::kChromeUINewTabURL, "New tab"); 630 chrome::kChromeUINewTabURL, "New tab");
617 631
618 tab = new Tab(); 632 tab = new Tab();
619 tab->navigations.push_back(navigation); 633 tab->navigations.push_back(navigation);
620 tab->current_navigation_index = 0; 634 tab->current_navigation_index = 0;
621 service_->entries_.push_front(tab); 635 mutable_entries()->push_front(tab);
622 636
623 EXPECT_EQ(max_entries + 1, service_->entries_.size()); 637 EXPECT_EQ(max_entries + 1, service_->entries().size());
624 service_->PruneEntries(); 638 PruneEntries();
625 EXPECT_EQ(max_entries, service_->entries_.size()); 639 EXPECT_EQ(max_entries, service_->entries().size());
626 EXPECT_EQ(GURL(kRecentUrl), 640 EXPECT_EQ(GURL(kRecentUrl),
627 static_cast<Tab*>(service_->entries_.front())-> 641 static_cast<Tab*>(service_->entries().front())->
628 navigations[0].virtual_url()); 642 navigations[0].virtual_url());
629 643
630 // Don't prune pinned NTPs. 644 // Don't prune pinned NTPs.
631 tab = new Tab(); 645 tab = new Tab();
632 tab->pinned = true; 646 tab->pinned = true;
633 tab->current_navigation_index = 0; 647 tab->current_navigation_index = 0;
634 tab->navigations.push_back(navigation); 648 tab->navigations.push_back(navigation);
635 service_->entries_.push_front(tab); 649 mutable_entries()->push_front(tab);
636 EXPECT_EQ(max_entries + 1, service_->entries_.size()); 650 EXPECT_EQ(max_entries + 1, service_->entries().size());
637 service_->PruneEntries(); 651 PruneEntries();
638 EXPECT_EQ(max_entries, service_->entries_.size()); 652 EXPECT_EQ(max_entries, service_->entries().size());
639 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 653 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
640 static_cast<Tab*>(service_->entries_.front())-> 654 static_cast<Tab*>(service_->entries().front())->
641 navigations[0].virtual_url()); 655 navigations[0].virtual_url());
642 656
643 // Don't prune NTPs that have multiple navigations. 657 // Don't prune NTPs that have multiple navigations.
644 // (Erase the last NTP first.) 658 // (Erase the last NTP first.)
645 delete service_->entries_.front(); 659 delete service_->entries().front();
646 service_->entries_.erase(service_->entries_.begin()); 660 mutable_entries()->erase(mutable_entries()->begin());
647 tab = new Tab(); 661 tab = new Tab();
648 tab->current_navigation_index = 1; 662 tab->current_navigation_index = 1;
649 tab->navigations.push_back(navigation); 663 tab->navigations.push_back(navigation);
650 tab->navigations.push_back(navigation); 664 tab->navigations.push_back(navigation);
651 service_->entries_.push_front(tab); 665 mutable_entries()->push_front(tab);
652 EXPECT_EQ(max_entries, service_->entries_.size()); 666 EXPECT_EQ(max_entries, service_->entries().size());
653 service_->PruneEntries(); 667 PruneEntries();
654 EXPECT_EQ(max_entries, service_->entries_.size()); 668 EXPECT_EQ(max_entries, service_->entries().size());
655 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 669 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
656 static_cast<Tab*>(service_->entries_.front())-> 670 static_cast<Tab*>(service_->entries().front())->
657 navigations[1].virtual_url()); 671 navigations[1].virtual_url());
658 } 672 }
659 673
660 // Regression test for crbug.com/106082 674 // Regression test for crbug.com/106082
661 TEST_F(TabRestoreServiceTest, PruneIsCalled) { 675 TEST_F(TabRestoreServiceTest, PruneIsCalled) {
662 CreateSessionServiceWithOneWindow(false); 676 CreateSessionServiceWithOneWindow(false);
663 677
664 SessionServiceFactory::GetForProfile(profile())-> 678 SessionServiceFactory::GetForProfile(profile())->
665 MoveCurrentSessionToLastSession(); 679 MoveCurrentSessionToLastSession();
666 680
667 profile()->set_restored_last_session(true); 681 profile()->set_restored_last_session(true);
668 682
669 const size_t max_entries = TabRestoreService::kMaxEntries; 683 const size_t max_entries = kMaxEntries;
670 for (size_t i = 0; i < max_entries + 5; i++) { 684 for (size_t i = 0; i < max_entries + 5; i++) {
671 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); 685 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i))));
672 service_->CreateHistoricalTab(contents(), -1); 686 service_->CreateHistoricalTab(contents(), -1);
673 } 687 }
674 688
675 EXPECT_EQ(max_entries, service_->entries_.size()); 689 EXPECT_EQ(max_entries, service_->entries().size());
676 // This should not crash. 690 // This should not crash.
677 service_->LoadTabsFromLastSession(); 691 service_->LoadTabsFromLastSession();
678 EXPECT_EQ(max_entries, service_->entries_.size()); 692 EXPECT_EQ(max_entries, service_->entries().size());
679 } 693 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698