OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/navigation_entry.h" | 5 #include "chrome/browser/navigation_entry.h" |
6 #include "chrome/browser/sessions/session_types.h" | 6 #include "chrome/browser/sessions/session_types.h" |
7 #include "chrome/browser/sessions/tab_restore_service.h" | 7 #include "chrome/browser/sessions/tab_restore_service.h" |
8 #include "chrome/test/test_tab_contents.h" | 8 #include "chrome/test/test_tab_contents.h" |
9 #include "chrome/test/testing_profile.h" | 9 #include "chrome/test/testing_profile.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 controller_->GetPendingEntry()->url()); | 46 controller_->GetPendingEntry()->url()); |
47 } | 47 } |
48 | 48 |
49 void RecreateService() { | 49 void RecreateService() { |
50 // Must set service to null first so that it is destroyed. | 50 // Must set service to null first so that it is destroyed. |
51 service_ = NULL; | 51 service_ = NULL; |
52 service_ = new TabRestoreService(profile_.get()); | 52 service_ = new TabRestoreService(profile_.get()); |
53 service_->LoadTabsFromLastSession(); | 53 service_->LoadTabsFromLastSession(); |
54 } | 54 } |
55 | 55 |
| 56 // Adds a window with one tab and url to the profile's session service. |
| 57 void AddWindowWithOneTabToSessionService() { |
| 58 SessionService* session_service = profile_->GetSessionService(); |
| 59 SessionID tab_id; |
| 60 SessionID window_id; |
| 61 session_service->SetWindowType(window_id, Browser::TYPE_NORMAL); |
| 62 session_service->SetTabWindow(window_id, tab_id); |
| 63 session_service->SetTabIndexInWindow(window_id, tab_id, 0); |
| 64 session_service->SetSelectedTabInWindow(window_id, 0); |
| 65 NavigationEntry entry(tab_contents_factory_->type()); |
| 66 entry.set_url(url1_); |
| 67 session_service->UpdateTabNavigation(window_id, tab_id, 0, entry); |
| 68 } |
| 69 |
| 70 // Creates a SessionService and assigns it to the Profile. The SessionService |
| 71 // is configured with a single window with a single tab pointing at url1_ by |
| 72 // way of AddWindowWithOneTabToSessionService. |
| 73 void CreateSessionServiceWithOneWindow() { |
| 74 // The profile takes ownership of this. |
| 75 SessionService* session_service = new SessionService(profile_.get()); |
| 76 profile_->set_session_service(session_service); |
| 77 |
| 78 AddWindowWithOneTabToSessionService(); |
| 79 |
| 80 // Set this, otherwise previous session won't be loaded. |
| 81 profile_->set_last_session_exited_cleanly(false); |
| 82 } |
| 83 |
56 GURL url1_; | 84 GURL url1_; |
57 GURL url2_; | 85 GURL url2_; |
58 GURL url3_; | 86 GURL url3_; |
59 scoped_ptr<TestTabContentsFactory> tab_contents_factory_; | 87 scoped_ptr<TestTabContentsFactory> tab_contents_factory_; |
60 scoped_ptr<TestingProfile> profile_; | 88 scoped_ptr<TestingProfile> profile_; |
61 scoped_refptr<TabRestoreService> service_; | 89 scoped_refptr<TabRestoreService> service_; |
62 NavigationController* controller_; | 90 NavigationController* controller_; |
63 TestTabContents* test_contents_; | 91 TestTabContents* test_contents_; |
64 }; | 92 }; |
65 | 93 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 ASSERT_EQ(1, service_->entries().size()); | 265 ASSERT_EQ(1, service_->entries().size()); |
238 | 266 |
239 // Recreate the service and have it load the tabs. | 267 // Recreate the service and have it load the tabs. |
240 RecreateService(); | 268 RecreateService(); |
241 | 269 |
242 service_->LoadTabsFromLastSession(); | 270 service_->LoadTabsFromLastSession(); |
243 | 271 |
244 // There should only be one entry. | 272 // There should only be one entry. |
245 ASSERT_EQ(1, service_->entries().size()); | 273 ASSERT_EQ(1, service_->entries().size()); |
246 } | 274 } |
| 275 |
| 276 // Makes sure we load the previous session as necessary. |
| 277 TEST_F(TabRestoreServiceTest, LoadPreviousSession) { |
| 278 CreateSessionServiceWithOneWindow(); |
| 279 |
| 280 profile_->GetSessionService()->MoveCurrentSessionToLastSession(); |
| 281 |
| 282 service_->LoadTabsFromLastSession(); |
| 283 |
| 284 // Make sure we get back one entry with one tab whose url is url1. |
| 285 ASSERT_EQ(1, service_->entries().size()); |
| 286 TabRestoreService::Entry* entry2 = service_->entries().front(); |
| 287 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type); |
| 288 TabRestoreService::Window* window = |
| 289 static_cast<TabRestoreService::Window*>(entry2); |
| 290 ASSERT_EQ(1, window->tabs.size()); |
| 291 EXPECT_EQ(0, window->selected_tab_index); |
| 292 ASSERT_EQ(1, window->tabs[0].navigations.size()); |
| 293 EXPECT_EQ(0, window->tabs[0].current_navigation_index); |
| 294 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].url()); |
| 295 } |
| 296 |
| 297 // Makes sure we don't attempt to load previous sessions after a restore. |
| 298 TEST_F(TabRestoreServiceTest, DontLoadAfterRestore) { |
| 299 CreateSessionServiceWithOneWindow(); |
| 300 |
| 301 profile_->GetSessionService()->MoveCurrentSessionToLastSession(); |
| 302 |
| 303 profile_->set_restored_last_session(true); |
| 304 |
| 305 service_->LoadTabsFromLastSession(); |
| 306 |
| 307 // Because we restored a session TabRestoreService shouldn't load the tabs. |
| 308 ASSERT_EQ(0, service_->entries().size()); |
| 309 } |
| 310 |
| 311 // Makes sure we don't attempt to load previous sessions after a clean exit. |
| 312 TEST_F(TabRestoreServiceTest, DontLoadAfterCleanExit) { |
| 313 CreateSessionServiceWithOneWindow(); |
| 314 |
| 315 profile_->GetSessionService()->MoveCurrentSessionToLastSession(); |
| 316 |
| 317 profile_->set_last_session_exited_cleanly(true); |
| 318 |
| 319 service_->LoadTabsFromLastSession(); |
| 320 |
| 321 ASSERT_EQ(0, service_->entries().size()); |
| 322 } |
| 323 |
| 324 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabs) { |
| 325 CreateSessionServiceWithOneWindow(); |
| 326 |
| 327 profile_->GetSessionService()->MoveCurrentSessionToLastSession(); |
| 328 |
| 329 AddThreeNavigations(); |
| 330 |
| 331 service_->CreateHistoricalTab(controller_); |
| 332 |
| 333 RecreateService(); |
| 334 |
| 335 // We should get back two entries, one from the previous session and one from |
| 336 // the tab restore service. The previous session entry should be first. |
| 337 ASSERT_EQ(2, service_->entries().size()); |
| 338 // The first entry should come from the session service. |
| 339 TabRestoreService::Entry* entry = service_->entries().front(); |
| 340 ASSERT_EQ(TabRestoreService::WINDOW, entry->type); |
| 341 TabRestoreService::Window* window = |
| 342 static_cast<TabRestoreService::Window*>(entry); |
| 343 ASSERT_EQ(1, window->tabs.size()); |
| 344 EXPECT_EQ(0, window->selected_tab_index); |
| 345 ASSERT_EQ(1, window->tabs[0].navigations.size()); |
| 346 EXPECT_EQ(0, window->tabs[0].current_navigation_index); |
| 347 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].url()); |
| 348 |
| 349 // Then the closed tab. |
| 350 entry = *(++service_->entries().begin()); |
| 351 ASSERT_EQ(TabRestoreService::TAB, entry->type); |
| 352 TabRestoreService::Tab* tab = static_cast<TabRestoreService::Tab*>(entry); |
| 353 ASSERT_EQ(3, tab->navigations.size()); |
| 354 EXPECT_EQ(2, tab->current_navigation_index); |
| 355 EXPECT_TRUE(url1_ == tab->navigations[0].url()); |
| 356 EXPECT_TRUE(url2_ == tab->navigations[1].url()); |
| 357 EXPECT_TRUE(url3_ == tab->navigations[2].url()); |
| 358 } |
| 359 |
| 360 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service |
| 361 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. |
| 362 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { |
| 363 CreateSessionServiceWithOneWindow(); |
| 364 |
| 365 for (size_t i = 0; i < TabRestoreService::kMaxEntries; ++i) |
| 366 AddWindowWithOneTabToSessionService(); |
| 367 |
| 368 profile_->GetSessionService()->MoveCurrentSessionToLastSession(); |
| 369 |
| 370 AddThreeNavigations(); |
| 371 |
| 372 service_->CreateHistoricalTab(controller_); |
| 373 |
| 374 RecreateService(); |
| 375 |
| 376 // We should get back kMaxEntries entries. We added more, but |
| 377 // TabRestoreService only allows up to kMaxEntries. |
| 378 ASSERT_EQ(TabRestoreService::kMaxEntries, service_->entries().size()); |
| 379 |
| 380 // The first entry should come from the session service. |
| 381 TabRestoreService::Entry* entry = service_->entries().front(); |
| 382 ASSERT_EQ(TabRestoreService::WINDOW, entry->type); |
| 383 TabRestoreService::Window* window = |
| 384 static_cast<TabRestoreService::Window*>(entry); |
| 385 ASSERT_EQ(1, window->tabs.size()); |
| 386 EXPECT_EQ(0, window->selected_tab_index); |
| 387 ASSERT_EQ(1, window->tabs[0].navigations.size()); |
| 388 EXPECT_EQ(0, window->tabs[0].current_navigation_index); |
| 389 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].url()); |
| 390 } |
OLD | NEW |