| 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/run_loop.h" |
| 8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/sessions/session_service.h" | 12 #include "chrome/browser/sessions/session_service.h" |
| 12 #include "chrome/browser/sessions/session_service_factory.h" | 13 #include "chrome/browser/sessions/session_service_factory.h" |
| 13 #include "chrome/browser/sessions/session_types.h" | 14 #include "chrome/browser/sessions/session_types.h" |
| 14 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 15 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 15 #include "chrome/browser/sessions/tab_restore_service_observer.h" | 16 #include "chrome/browser/sessions/tab_restore_service_observer.h" |
| 16 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 20 #include "chrome/test/base/chrome_render_view_test.h" | 21 #include "chrome/test/base/chrome_render_view_test.h" |
| 21 #include "chrome/test/base/in_process_browser_test.h" | 22 #include "chrome/test/base/in_process_browser_test.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/sessions/serialized_navigation_entry_test_helper.h" | 24 #include "components/sessions/serialized_navigation_entry_test_helper.h" |
| 24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/navigation_controller.h" | 26 #include "content/public/browser/navigation_controller.h" |
| 26 #include "content/public/browser/navigation_entry.h" | 27 #include "content/public/browser/navigation_entry.h" |
| 27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/notification_types.h" | 29 #include "content/public/browser/notification_types.h" |
| 29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/test/render_view_test.h" | 31 #include "content/public/test/render_view_test.h" |
| 31 #include "content/public/test/test_browser_thread.h" | |
| 32 #include "content/public/test/test_utils.h" | 32 #include "content/public/test/test_utils.h" |
| 33 #include "content/public/test/web_contents_tester.h" | 33 #include "content/public/test/web_contents_tester.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 36 | 36 |
| 37 typedef TabRestoreService::Tab Tab; | 37 typedef TabRestoreService::Tab Tab; |
| 38 typedef TabRestoreService::Window Window; | 38 typedef TabRestoreService::Window Window; |
| 39 | 39 |
| 40 using content::NavigationEntry; | 40 using content::NavigationEntry; |
| 41 using content::WebContentsTester; | 41 using content::WebContentsTester; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 54 return time_; | 54 return time_; |
| 55 } | 55 } |
| 56 | 56 |
| 57 private: | 57 private: |
| 58 base::Time time_; | 58 base::Time time_; |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 class PersistentTabRestoreServiceTest : public ChromeRenderViewHostTestHarness { | 61 class PersistentTabRestoreServiceTest : public ChromeRenderViewHostTestHarness { |
| 62 public: | 62 public: |
| 63 PersistentTabRestoreServiceTest() | 63 PersistentTabRestoreServiceTest() |
| 64 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 64 : url1_("http://1"), |
| 65 url1_ = GURL("http://1"); | 65 url2_("http://2"), |
| 66 url2_ = GURL("http://2"); | 66 url3_("http://3"), |
| 67 url3_ = GURL("http://3"); | 67 user_agent_override_( |
| 68 user_agent_override_ = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19" | 68 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19" |
| 69 " (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"; | 69 " (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19") { |
| 70 } | 70 } |
| 71 | 71 |
| 72 virtual ~PersistentTabRestoreServiceTest() { | 72 virtual ~PersistentTabRestoreServiceTest() { |
| 73 } | 73 } |
| 74 | 74 |
| 75 protected: | 75 protected: |
| 76 enum { | 76 enum { |
| 77 kMaxEntries = TabRestoreServiceHelper::kMaxEntries, | 77 kMaxEntries = TabRestoreServiceHelper::kMaxEntries, |
| 78 }; | 78 }; |
| 79 | 79 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 controller().GoToIndex(index); | 114 controller().GoToIndex(index); |
| 115 WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 115 WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void RecreateService() { | 118 void RecreateService() { |
| 119 // Must set service to null first so that it is destroyed before the new | 119 // Must set service to null first so that it is destroyed before the new |
| 120 // one is created. | 120 // one is created. |
| 121 service_->Shutdown(); | 121 service_->Shutdown(); |
| 122 service_.reset(); | 122 service_.reset(); |
| 123 service_.reset(new PersistentTabRestoreService(profile(), time_factory_)); | 123 service_.reset(new PersistentTabRestoreService(profile(), time_factory_)); |
| 124 service_->LoadTabsFromLastSession(); | 124 SynchronousLoadTabsFromLastSession(); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Adds a window with one tab and url to the profile's session service. | 127 // Adds a window with one tab and url to the profile's session service. |
| 128 // If |pinned| is true, the tab is marked as pinned in the session service. | 128 // If |pinned| is true, the tab is marked as pinned in the session service. |
| 129 void AddWindowWithOneTabToSessionService(bool pinned) { | 129 void AddWindowWithOneTabToSessionService(bool pinned) { |
| 130 SessionService* session_service = | 130 SessionService* session_service = |
| 131 SessionServiceFactory::GetForProfile(profile()); | 131 SessionServiceFactory::GetForProfile(profile()); |
| 132 SessionID tab_id; | 132 SessionID tab_id; |
| 133 SessionID window_id; | 133 SessionID window_id; |
| 134 session_service->SetWindowType( | 134 session_service->SetWindowType( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 152 // The profile takes ownership of this. | 152 // The profile takes ownership of this. |
| 153 SessionService* session_service = new SessionService(profile()); | 153 SessionService* session_service = new SessionService(profile()); |
| 154 SessionServiceFactory::SetForTestProfile(profile(), session_service); | 154 SessionServiceFactory::SetForTestProfile(profile(), session_service); |
| 155 | 155 |
| 156 AddWindowWithOneTabToSessionService(pinned); | 156 AddWindowWithOneTabToSessionService(pinned); |
| 157 | 157 |
| 158 // Set this, otherwise previous session won't be loaded. | 158 // Set this, otherwise previous session won't be loaded. |
| 159 profile()->set_last_session_exited_cleanly(false); | 159 profile()->set_last_session_exited_cleanly(false); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void SynchronousLoadTabsFromLastSession() { |
| 163 // Ensures that the load is complete before continuing. |
| 164 service_->LoadTabsFromLastSession(); |
| 165 base::RunLoop().RunUntilIdle(); |
| 166 } |
| 167 |
| 162 GURL url1_; | 168 GURL url1_; |
| 163 GURL url2_; | 169 GURL url2_; |
| 164 GURL url3_; | 170 GURL url3_; |
| 165 std::string user_agent_override_; | 171 std::string user_agent_override_; |
| 166 scoped_ptr<PersistentTabRestoreService> service_; | 172 scoped_ptr<PersistentTabRestoreService> service_; |
| 167 PersistentTabRestoreTimeFactory* time_factory_; | 173 PersistentTabRestoreTimeFactory* time_factory_; |
| 168 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox | 174 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox |
| 169 webkit_platform_support_; | 175 webkit_platform_support_; |
| 170 content::TestBrowserThread ui_thread_; | |
| 171 }; | 176 }; |
| 172 | 177 |
| 173 namespace { | 178 namespace { |
| 174 | 179 |
| 175 class TestTabRestoreServiceObserver : public TabRestoreServiceObserver { | 180 class TestTabRestoreServiceObserver : public TabRestoreServiceObserver { |
| 176 public: | 181 public: |
| 177 TestTabRestoreServiceObserver() : got_loaded_(false) {} | 182 TestTabRestoreServiceObserver() : got_loaded_(false) {} |
| 178 | 183 |
| 179 void clear_got_loaded() { got_loaded_ = false; } | 184 void clear_got_loaded() { got_loaded_ = false; } |
| 180 bool got_loaded() const { return got_loaded_; } | 185 bool got_loaded() const { return got_loaded_; } |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 TEST_F(PersistentTabRestoreServiceTest, DontLoadTwice) { | 385 TEST_F(PersistentTabRestoreServiceTest, DontLoadTwice) { |
| 381 AddThreeNavigations(); | 386 AddThreeNavigations(); |
| 382 | 387 |
| 383 // Have the service record the tab. | 388 // Have the service record the tab. |
| 384 service_->CreateHistoricalTab(web_contents(), -1); | 389 service_->CreateHistoricalTab(web_contents(), -1); |
| 385 ASSERT_EQ(1U, service_->entries().size()); | 390 ASSERT_EQ(1U, service_->entries().size()); |
| 386 | 391 |
| 387 // Recreate the service and have it load the tabs. | 392 // Recreate the service and have it load the tabs. |
| 388 RecreateService(); | 393 RecreateService(); |
| 389 | 394 |
| 390 service_->LoadTabsFromLastSession(); | 395 SynchronousLoadTabsFromLastSession(); |
| 391 | 396 |
| 392 // There should only be one entry. | 397 // There should only be one entry. |
| 393 ASSERT_EQ(1U, service_->entries().size()); | 398 ASSERT_EQ(1U, service_->entries().size()); |
| 394 } | 399 } |
| 395 | 400 |
| 396 // Makes sure we load the previous session as necessary. | 401 // Makes sure we load the previous session as necessary. |
| 397 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSession) { | 402 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSession) { |
| 398 CreateSessionServiceWithOneWindow(false); | 403 CreateSessionServiceWithOneWindow(false); |
| 399 | 404 |
| 400 SessionServiceFactory::GetForProfile(profile())-> | 405 SessionServiceFactory::GetForProfile(profile())-> |
| 401 MoveCurrentSessionToLastSession(); | 406 MoveCurrentSessionToLastSession(); |
| 402 | 407 |
| 403 EXPECT_FALSE(service_->IsLoaded()); | 408 EXPECT_FALSE(service_->IsLoaded()); |
| 404 | 409 |
| 405 TestTabRestoreServiceObserver observer; | 410 TestTabRestoreServiceObserver observer; |
| 406 service_->AddObserver(&observer); | 411 service_->AddObserver(&observer); |
| 407 service_->LoadTabsFromLastSession(); | 412 SynchronousLoadTabsFromLastSession(); |
| 408 EXPECT_TRUE(observer.got_loaded()); | 413 EXPECT_TRUE(observer.got_loaded()); |
| 409 service_->RemoveObserver(&observer); | 414 service_->RemoveObserver(&observer); |
| 410 | 415 |
| 411 // Make sure we get back one entry with one tab whose url is url1. | 416 // Make sure we get back one entry with one tab whose url is url1. |
| 412 ASSERT_EQ(1U, service_->entries().size()); | 417 ASSERT_EQ(1U, service_->entries().size()); |
| 413 TabRestoreService::Entry* entry2 = service_->entries().front(); | 418 TabRestoreService::Entry* entry2 = service_->entries().front(); |
| 414 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type); | 419 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type); |
| 415 TabRestoreService::Window* window = | 420 TabRestoreService::Window* window = |
| 416 static_cast<TabRestoreService::Window*>(entry2); | 421 static_cast<TabRestoreService::Window*>(entry2); |
| 417 ASSERT_EQ(1U, window->tabs.size()); | 422 ASSERT_EQ(1U, window->tabs.size()); |
| 418 EXPECT_EQ(0, window->timestamp.ToInternalValue()); | 423 EXPECT_EQ(0, window->timestamp.ToInternalValue()); |
| 419 EXPECT_EQ(0, window->selected_tab_index); | 424 EXPECT_EQ(0, window->selected_tab_index); |
| 420 ASSERT_EQ(1U, window->tabs[0].navigations.size()); | 425 ASSERT_EQ(1U, window->tabs[0].navigations.size()); |
| 421 EXPECT_EQ(0, window->tabs[0].current_navigation_index); | 426 EXPECT_EQ(0, window->tabs[0].current_navigation_index); |
| 422 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue()); | 427 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue()); |
| 423 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url()); | 428 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url()); |
| 424 } | 429 } |
| 425 | 430 |
| 426 // Makes sure we don't attempt to load previous sessions after a restore. | 431 // Makes sure we don't attempt to load previous sessions after a restore. |
| 427 TEST_F(PersistentTabRestoreServiceTest, DontLoadAfterRestore) { | 432 TEST_F(PersistentTabRestoreServiceTest, DontLoadAfterRestore) { |
| 428 CreateSessionServiceWithOneWindow(false); | 433 CreateSessionServiceWithOneWindow(false); |
| 429 | 434 |
| 430 SessionServiceFactory::GetForProfile(profile())-> | 435 SessionServiceFactory::GetForProfile(profile())-> |
| 431 MoveCurrentSessionToLastSession(); | 436 MoveCurrentSessionToLastSession(); |
| 432 | 437 |
| 433 profile()->set_restored_last_session(true); | 438 profile()->set_restored_last_session(true); |
| 434 | 439 |
| 435 service_->LoadTabsFromLastSession(); | 440 SynchronousLoadTabsFromLastSession(); |
| 436 | 441 |
| 437 // Because we restored a session PersistentTabRestoreService shouldn't load | 442 // Because we restored a session PersistentTabRestoreService shouldn't load |
| 438 // the tabs. | 443 // the tabs. |
| 439 ASSERT_EQ(0U, service_->entries().size()); | 444 ASSERT_EQ(0U, service_->entries().size()); |
| 440 } | 445 } |
| 441 | 446 |
| 442 // Makes sure we don't attempt to load previous sessions after a clean exit. | 447 // Makes sure we don't attempt to load previous sessions after a clean exit. |
| 443 TEST_F(PersistentTabRestoreServiceTest, DontLoadAfterCleanExit) { | 448 TEST_F(PersistentTabRestoreServiceTest, DontLoadAfterCleanExit) { |
| 444 CreateSessionServiceWithOneWindow(false); | 449 CreateSessionServiceWithOneWindow(false); |
| 445 | 450 |
| 446 SessionServiceFactory::GetForProfile(profile())-> | 451 SessionServiceFactory::GetForProfile(profile())-> |
| 447 MoveCurrentSessionToLastSession(); | 452 MoveCurrentSessionToLastSession(); |
| 448 | 453 |
| 449 profile()->set_last_session_exited_cleanly(true); | 454 profile()->set_last_session_exited_cleanly(true); |
| 450 | 455 |
| 451 service_->LoadTabsFromLastSession(); | 456 SynchronousLoadTabsFromLastSession(); |
| 452 | 457 |
| 453 ASSERT_EQ(0U, service_->entries().size()); | 458 ASSERT_EQ(0U, service_->entries().size()); |
| 454 } | 459 } |
| 455 | 460 |
| 456 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSessionAndTabs) { | 461 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSessionAndTabs) { |
| 457 CreateSessionServiceWithOneWindow(false); | 462 CreateSessionServiceWithOneWindow(false); |
| 458 | 463 |
| 459 SessionServiceFactory::GetForProfile(profile())-> | 464 SessionServiceFactory::GetForProfile(profile())-> |
| 460 MoveCurrentSessionToLastSession(); | 465 MoveCurrentSessionToLastSession(); |
| 461 | 466 |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 | 724 |
| 720 const size_t max_entries = kMaxEntries; | 725 const size_t max_entries = kMaxEntries; |
| 721 for (size_t i = 0; i < max_entries + 5; i++) { | 726 for (size_t i = 0; i < max_entries + 5; i++) { |
| 722 NavigateAndCommit( | 727 NavigateAndCommit( |
| 723 GURL(base::StringPrintf("http://%d", static_cast<int>(i)))); | 728 GURL(base::StringPrintf("http://%d", static_cast<int>(i)))); |
| 724 service_->CreateHistoricalTab(web_contents(), -1); | 729 service_->CreateHistoricalTab(web_contents(), -1); |
| 725 } | 730 } |
| 726 | 731 |
| 727 EXPECT_EQ(max_entries, service_->entries().size()); | 732 EXPECT_EQ(max_entries, service_->entries().size()); |
| 728 // This should not crash. | 733 // This should not crash. |
| 729 service_->LoadTabsFromLastSession(); | 734 SynchronousLoadTabsFromLastSession(); |
| 730 EXPECT_EQ(max_entries, service_->entries().size()); | 735 EXPECT_EQ(max_entries, service_->entries().size()); |
| 731 } | 736 } |
| 732 | 737 |
| 733 // Makes sure invoking LoadTabsFromLastSession() when the max number of entries | 738 // Makes sure invoking LoadTabsFromLastSession() when the max number of entries |
| 734 // have been added results in IsLoaded() returning true and notifies observers. | 739 // have been added results in IsLoaded() returning true and notifies observers. |
| 735 TEST_F(PersistentTabRestoreServiceTest, GoToLoadedWhenHaveMaxEntries) { | 740 TEST_F(PersistentTabRestoreServiceTest, GoToLoadedWhenHaveMaxEntries) { |
| 736 const size_t max_entries = kMaxEntries; | 741 const size_t max_entries = kMaxEntries; |
| 737 for (size_t i = 0; i < max_entries + 5; i++) { | 742 for (size_t i = 0; i < max_entries + 5; i++) { |
| 738 NavigateAndCommit( | 743 NavigateAndCommit( |
| 739 GURL(base::StringPrintf("http://%d", static_cast<int>(i)))); | 744 GURL(base::StringPrintf("http://%d", static_cast<int>(i)))); |
| 740 service_->CreateHistoricalTab(web_contents(), -1); | 745 service_->CreateHistoricalTab(web_contents(), -1); |
| 741 } | 746 } |
| 742 | 747 |
| 743 EXPECT_FALSE(service_->IsLoaded()); | 748 EXPECT_FALSE(service_->IsLoaded()); |
| 744 TestTabRestoreServiceObserver observer; | 749 TestTabRestoreServiceObserver observer; |
| 745 service_->AddObserver(&observer); | 750 service_->AddObserver(&observer); |
| 746 EXPECT_EQ(max_entries, service_->entries().size()); | 751 EXPECT_EQ(max_entries, service_->entries().size()); |
| 747 service_->LoadTabsFromLastSession(); | 752 SynchronousLoadTabsFromLastSession(); |
| 748 EXPECT_TRUE(observer.got_loaded()); | 753 EXPECT_TRUE(observer.got_loaded()); |
| 749 EXPECT_TRUE(service_->IsLoaded()); | 754 EXPECT_TRUE(service_->IsLoaded()); |
| 750 service_->RemoveObserver(&observer); | 755 service_->RemoveObserver(&observer); |
| 751 } | 756 } |
| OLD | NEW |