| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/session_types.h" | 5 #include "chrome/browser/sessions/session_types.h" |
| 6 #include "chrome/browser/sessions/session_service.h" | 6 #include "chrome/browser/sessions/session_service.h" |
| 7 #include "chrome/browser/sessions/session_service_factory.h" |
| 7 #include "chrome/browser/sessions/tab_restore_service.h" | 8 #include "chrome/browser/sessions/tab_restore_service.h" |
| 8 #include "chrome/test/render_view_test.h" | 9 #include "chrome/test/render_view_test.h" |
| 9 #include "chrome/test/testing_profile.h" | 10 #include "chrome/test/testing_profile.h" |
| 10 #include "content/browser/renderer_host/test_render_view_host.h" | 11 #include "content/browser/renderer_host/test_render_view_host.h" |
| 11 #include "content/browser/tab_contents/navigation_controller.h" | 12 #include "content/browser/tab_contents/navigation_controller.h" |
| 12 #include "content/browser/tab_contents/navigation_entry.h" | 13 #include "content/browser/tab_contents/navigation_entry.h" |
| 13 #include "content/browser/tab_contents/test_tab_contents.h" | 14 #include "content/browser/tab_contents/test_tab_contents.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 16 | 17 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // Must set service to null first so that it is destroyed before the new | 76 // Must set service to null first so that it is destroyed before the new |
| 76 // one is created. | 77 // one is created. |
| 77 service_.reset(); | 78 service_.reset(); |
| 78 service_.reset(new TabRestoreService(profile(), time_factory_)); | 79 service_.reset(new TabRestoreService(profile(), time_factory_)); |
| 79 service_->LoadTabsFromLastSession(); | 80 service_->LoadTabsFromLastSession(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 // Adds a window with one tab and url to the profile's session service. | 83 // Adds a window with one tab and url to the profile's session service. |
| 83 // If |pinned| is true, the tab is marked as pinned in the session service. | 84 // If |pinned| is true, the tab is marked as pinned in the session service. |
| 84 void AddWindowWithOneTabToSessionService(bool pinned) { | 85 void AddWindowWithOneTabToSessionService(bool pinned) { |
| 85 SessionService* session_service = profile()->GetSessionService(); | 86 SessionService* session_service = |
| 87 SessionServiceFactory::GetForProfile(profile()); |
| 86 SessionID tab_id; | 88 SessionID tab_id; |
| 87 SessionID window_id; | 89 SessionID window_id; |
| 88 session_service->SetWindowType(window_id, Browser::TYPE_NORMAL); | 90 session_service->SetWindowType(window_id, Browser::TYPE_NORMAL); |
| 89 session_service->SetTabWindow(window_id, tab_id); | 91 session_service->SetTabWindow(window_id, tab_id); |
| 90 session_service->SetTabIndexInWindow(window_id, tab_id, 0); | 92 session_service->SetTabIndexInWindow(window_id, tab_id, 0); |
| 91 session_service->SetSelectedTabInWindow(window_id, 0); | 93 session_service->SetSelectedTabInWindow(window_id, 0); |
| 92 if (pinned) | 94 if (pinned) |
| 93 session_service->SetPinnedState(window_id, tab_id, true); | 95 session_service->SetPinnedState(window_id, tab_id, true); |
| 94 NavigationEntry entry; | 96 NavigationEntry entry; |
| 95 entry.set_url(url1_); | 97 entry.set_url(url1_); |
| 96 session_service->UpdateTabNavigation(window_id, tab_id, 0, entry); | 98 session_service->UpdateTabNavigation(window_id, tab_id, 0, entry); |
| 97 } | 99 } |
| 98 | 100 |
| 99 // Creates a SessionService and assigns it to the Profile. The SessionService | 101 // Creates a SessionService and assigns it to the Profile. The SessionService |
| 100 // is configured with a single window with a single tab pointing at url1_ by | 102 // is configured with a single window with a single tab pointing at url1_ by |
| 101 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the | 103 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the |
| 102 // tab is marked as pinned in the session service. | 104 // tab is marked as pinned in the session service. |
| 103 void CreateSessionServiceWithOneWindow(bool pinned) { | 105 void CreateSessionServiceWithOneWindow(bool pinned) { |
| 104 // The profile takes ownership of this. | 106 // The profile takes ownership of this. |
| 105 SessionService* session_service = new SessionService(profile()); | 107 SessionService* session_service = new SessionService(profile()); |
| 106 profile()->set_session_service(session_service); | 108 SessionServiceFactory::SetForTestProfile(profile(), session_service); |
| 107 | 109 |
| 108 AddWindowWithOneTabToSessionService(pinned); | 110 AddWindowWithOneTabToSessionService(pinned); |
| 109 | 111 |
| 110 // Set this, otherwise previous session won't be loaded. | 112 // Set this, otherwise previous session won't be loaded. |
| 111 profile()->set_last_session_exited_cleanly(false); | 113 profile()->set_last_session_exited_cleanly(false); |
| 112 } | 114 } |
| 113 | 115 |
| 114 GURL url1_; | 116 GURL url1_; |
| 115 GURL url2_; | 117 GURL url2_; |
| 116 GURL url3_; | 118 GURL url3_; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 service_->LoadTabsFromLastSession(); | 282 service_->LoadTabsFromLastSession(); |
| 281 | 283 |
| 282 // There should only be one entry. | 284 // There should only be one entry. |
| 283 ASSERT_EQ(1U, service_->entries().size()); | 285 ASSERT_EQ(1U, service_->entries().size()); |
| 284 } | 286 } |
| 285 | 287 |
| 286 // Makes sure we load the previous session as necessary. | 288 // Makes sure we load the previous session as necessary. |
| 287 TEST_F(TabRestoreServiceTest, LoadPreviousSession) { | 289 TEST_F(TabRestoreServiceTest, LoadPreviousSession) { |
| 288 CreateSessionServiceWithOneWindow(false); | 290 CreateSessionServiceWithOneWindow(false); |
| 289 | 291 |
| 290 profile()->GetSessionService()->MoveCurrentSessionToLastSession(); | 292 SessionServiceFactory::GetForProfile(profile())-> |
| 293 MoveCurrentSessionToLastSession(); |
| 291 | 294 |
| 292 service_->LoadTabsFromLastSession(); | 295 service_->LoadTabsFromLastSession(); |
| 293 | 296 |
| 294 // Make sure we get back one entry with one tab whose url is url1. | 297 // Make sure we get back one entry with one tab whose url is url1. |
| 295 ASSERT_EQ(1U, service_->entries().size()); | 298 ASSERT_EQ(1U, service_->entries().size()); |
| 296 TabRestoreService::Entry* entry2 = service_->entries().front(); | 299 TabRestoreService::Entry* entry2 = service_->entries().front(); |
| 297 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type); | 300 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type); |
| 298 TabRestoreService::Window* window = | 301 TabRestoreService::Window* window = |
| 299 static_cast<TabRestoreService::Window*>(entry2); | 302 static_cast<TabRestoreService::Window*>(entry2); |
| 300 ASSERT_EQ(1U, window->tabs.size()); | 303 ASSERT_EQ(1U, window->tabs.size()); |
| 301 EXPECT_EQ(0, window->timestamp.ToInternalValue()); | 304 EXPECT_EQ(0, window->timestamp.ToInternalValue()); |
| 302 EXPECT_EQ(0, window->selected_tab_index); | 305 EXPECT_EQ(0, window->selected_tab_index); |
| 303 ASSERT_EQ(1U, window->tabs[0].navigations.size()); | 306 ASSERT_EQ(1U, window->tabs[0].navigations.size()); |
| 304 EXPECT_EQ(0, window->tabs[0].current_navigation_index); | 307 EXPECT_EQ(0, window->tabs[0].current_navigation_index); |
| 305 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue()); | 308 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue()); |
| 306 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url()); | 309 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url()); |
| 307 } | 310 } |
| 308 | 311 |
| 309 // Makes sure we don't attempt to load previous sessions after a restore. | 312 // Makes sure we don't attempt to load previous sessions after a restore. |
| 310 TEST_F(TabRestoreServiceTest, DontLoadAfterRestore) { | 313 TEST_F(TabRestoreServiceTest, DontLoadAfterRestore) { |
| 311 CreateSessionServiceWithOneWindow(false); | 314 CreateSessionServiceWithOneWindow(false); |
| 312 | 315 |
| 313 profile()->GetSessionService()->MoveCurrentSessionToLastSession(); | 316 SessionServiceFactory::GetForProfile(profile())-> |
| 317 MoveCurrentSessionToLastSession(); |
| 314 | 318 |
| 315 profile()->set_restored_last_session(true); | 319 profile()->set_restored_last_session(true); |
| 316 | 320 |
| 317 service_->LoadTabsFromLastSession(); | 321 service_->LoadTabsFromLastSession(); |
| 318 | 322 |
| 319 // Because we restored a session TabRestoreService shouldn't load the tabs. | 323 // Because we restored a session TabRestoreService shouldn't load the tabs. |
| 320 ASSERT_EQ(0U, service_->entries().size()); | 324 ASSERT_EQ(0U, service_->entries().size()); |
| 321 } | 325 } |
| 322 | 326 |
| 323 // Makes sure we don't attempt to load previous sessions after a clean exit. | 327 // Makes sure we don't attempt to load previous sessions after a clean exit. |
| 324 TEST_F(TabRestoreServiceTest, DontLoadAfterCleanExit) { | 328 TEST_F(TabRestoreServiceTest, DontLoadAfterCleanExit) { |
| 325 CreateSessionServiceWithOneWindow(false); | 329 CreateSessionServiceWithOneWindow(false); |
| 326 | 330 |
| 327 profile()->GetSessionService()->MoveCurrentSessionToLastSession(); | 331 SessionServiceFactory::GetForProfile(profile())-> |
| 332 MoveCurrentSessionToLastSession(); |
| 328 | 333 |
| 329 profile()->set_last_session_exited_cleanly(true); | 334 profile()->set_last_session_exited_cleanly(true); |
| 330 | 335 |
| 331 service_->LoadTabsFromLastSession(); | 336 service_->LoadTabsFromLastSession(); |
| 332 | 337 |
| 333 ASSERT_EQ(0U, service_->entries().size()); | 338 ASSERT_EQ(0U, service_->entries().size()); |
| 334 } | 339 } |
| 335 | 340 |
| 336 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabs) { | 341 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabs) { |
| 337 CreateSessionServiceWithOneWindow(false); | 342 CreateSessionServiceWithOneWindow(false); |
| 338 | 343 |
| 339 profile()->GetSessionService()->MoveCurrentSessionToLastSession(); | 344 SessionServiceFactory::GetForProfile(profile())-> |
| 345 MoveCurrentSessionToLastSession(); |
| 340 | 346 |
| 341 AddThreeNavigations(); | 347 AddThreeNavigations(); |
| 342 | 348 |
| 343 service_->CreateHistoricalTab(&controller(), -1); | 349 service_->CreateHistoricalTab(&controller(), -1); |
| 344 | 350 |
| 345 RecreateService(); | 351 RecreateService(); |
| 346 | 352 |
| 347 // We should get back two entries, one from the previous session and one from | 353 // We should get back two entries, one from the previous session and one from |
| 348 // the tab restore service. The previous session entry should be first. | 354 // the tab restore service. The previous session entry should be first. |
| 349 ASSERT_EQ(2U, service_->entries().size()); | 355 ASSERT_EQ(2U, service_->entries().size()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 371 tab->timestamp.ToInternalValue()); | 377 tab->timestamp.ToInternalValue()); |
| 372 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); | 378 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); |
| 373 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); | 379 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); |
| 374 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); | 380 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); |
| 375 } | 381 } |
| 376 | 382 |
| 377 // Make sure pinned state is correctly loaded from session service. | 383 // Make sure pinned state is correctly loaded from session service. |
| 378 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabsPinned) { | 384 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabsPinned) { |
| 379 CreateSessionServiceWithOneWindow(true); | 385 CreateSessionServiceWithOneWindow(true); |
| 380 | 386 |
| 381 profile()->GetSessionService()->MoveCurrentSessionToLastSession(); | 387 SessionServiceFactory::GetForProfile(profile())-> |
| 388 MoveCurrentSessionToLastSession(); |
| 382 | 389 |
| 383 AddThreeNavigations(); | 390 AddThreeNavigations(); |
| 384 | 391 |
| 385 service_->CreateHistoricalTab(&controller(), -1); | 392 service_->CreateHistoricalTab(&controller(), -1); |
| 386 | 393 |
| 387 RecreateService(); | 394 RecreateService(); |
| 388 | 395 |
| 389 // We should get back two entries, one from the previous session and one from | 396 // We should get back two entries, one from the previous session and one from |
| 390 // the tab restore service. The previous session entry should be first. | 397 // the tab restore service. The previous session entry should be first. |
| 391 ASSERT_EQ(2U, service_->entries().size()); | 398 ASSERT_EQ(2U, service_->entries().size()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 414 } | 421 } |
| 415 | 422 |
| 416 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service | 423 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service |
| 417 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. | 424 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. |
| 418 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { | 425 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { |
| 419 CreateSessionServiceWithOneWindow(false); | 426 CreateSessionServiceWithOneWindow(false); |
| 420 | 427 |
| 421 for (size_t i = 0; i < TabRestoreService::kMaxEntries; ++i) | 428 for (size_t i = 0; i < TabRestoreService::kMaxEntries; ++i) |
| 422 AddWindowWithOneTabToSessionService(false); | 429 AddWindowWithOneTabToSessionService(false); |
| 423 | 430 |
| 424 profile()->GetSessionService()->MoveCurrentSessionToLastSession(); | 431 SessionServiceFactory::GetForProfile(profile())-> |
| 432 MoveCurrentSessionToLastSession(); |
| 425 | 433 |
| 426 AddThreeNavigations(); | 434 AddThreeNavigations(); |
| 427 | 435 |
| 428 service_->CreateHistoricalTab(&controller(), -1); | 436 service_->CreateHistoricalTab(&controller(), -1); |
| 429 | 437 |
| 430 RecreateService(); | 438 RecreateService(); |
| 431 | 439 |
| 432 // We should get back kMaxEntries entries. We added more, but | 440 // We should get back kMaxEntries entries. We added more, but |
| 433 // TabRestoreService only allows up to kMaxEntries. | 441 // TabRestoreService only allows up to kMaxEntries. |
| 434 ASSERT_EQ(TabRestoreService::kMaxEntries, service_->entries().size()); | 442 ASSERT_EQ(TabRestoreService::kMaxEntries, service_->entries().size()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 ASSERT_EQ(1U, service_->entries().size()); | 482 ASSERT_EQ(1U, service_->entries().size()); |
| 475 | 483 |
| 476 // And verify the entry. | 484 // And verify the entry. |
| 477 TabRestoreService::Entry* restored_entry = service_->entries().front(); | 485 TabRestoreService::Entry* restored_entry = service_->entries().front(); |
| 478 ASSERT_EQ(TabRestoreService::TAB, restored_entry->type); | 486 ASSERT_EQ(TabRestoreService::TAB, restored_entry->type); |
| 479 TabRestoreService::Tab* restored_tab = | 487 TabRestoreService::Tab* restored_tab = |
| 480 static_cast<TabRestoreService::Tab*>(restored_entry); | 488 static_cast<TabRestoreService::Tab*>(restored_entry); |
| 481 EXPECT_EQ(tab_timestamp.ToInternalValue(), | 489 EXPECT_EQ(tab_timestamp.ToInternalValue(), |
| 482 restored_tab->timestamp.ToInternalValue()); | 490 restored_tab->timestamp.ToInternalValue()); |
| 483 } | 491 } |
| OLD | NEW |