session_restore_browsertest.cc revision dc0f95d653279beabeb9817299e2902918ba123e
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 3// found in the LICENSE file. 4 5#include "base/file_path.h" 6#include "chrome/browser/defaults.h" 7#include "chrome/browser/profiles/profile.h" 8#include "chrome/browser/sessions/tab_restore_service.h" 9#include "chrome/browser/tabs/tab_strip_model.h" 10#include "chrome/browser/ui/browser.h" 11#include "chrome/browser/ui/browser_window.h" 12#include "chrome/common/page_transition_types.h" 13#include "chrome/test/in_process_browser_test.h" 14#include "chrome/test/ui_test_utils.h" 15#include "content/browser/tab_contents/tab_contents.h" 16 17typedef InProcessBrowserTest SessionRestoreTest; 18 19#if defined(OS_LINUX) && defined(TOOLKIT_VIEWS) 20// Crashes on Linux Views: http://crbug.com/39476 21#define MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers \ 22 DISABLED_RestoreOnNewWindowWithNoTabbedBrowsers 23#else 24#define MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers \ 25 RestoreOnNewWindowWithNoTabbedBrowsers 26#endif 27 28// Makes sure when session restore is triggered in the same process we don't end 29// up with an extra tab. 30IN_PROC_BROWSER_TEST_F(SessionRestoreTest, 31 MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers) { 32 if (browser_defaults::kRestorePopups) 33 return; 34 35 const FilePath::CharType* kTitle1File = FILE_PATH_LITERAL("title1.html"); 36 GURL url(ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), 37 FilePath(kTitle1File))); 38 ui_test_utils::NavigateToURL(browser(), url); 39 40 // Turn on session restore. 41 SessionStartupPref::SetStartupPref( 42 browser()->profile(), 43 SessionStartupPref(SessionStartupPref::LAST)); 44 45 // Create a new popup. 46 Profile* profile = browser()->profile(); 47 Browser* popup = Browser::CreateForType(Browser::TYPE_POPUP, profile); 48 popup->window()->Show(); 49 50 // Close the browser. 51 browser()->window()->Close(); 52 53 // Create a new window, which should trigger session restore. 54 popup->NewWindow(); 55 56 Browser* new_browser = ui_test_utils::WaitForNewBrowser(); 57 58 ASSERT_TRUE(new_browser != NULL); 59 60 // The browser should only have one tab. 61 ASSERT_EQ(1, new_browser->tab_count()); 62 63 // And the first url should be url. 64 EXPECT_EQ(url, new_browser->GetTabContentsAt(0)->GetURL()); 65} 66 67 68IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreIndividualTabFromWindow) { 69 GURL url1(ui_test_utils::GetTestUrl( 70 FilePath(FilePath::kCurrentDirectory), 71 FilePath(FILE_PATH_LITERAL("title1.html")))); 72 GURL url2(ui_test_utils::GetTestUrl( 73 FilePath(FilePath::kCurrentDirectory), 74 FilePath(FILE_PATH_LITERAL("title2.html")))); 75 GURL url3(ui_test_utils::GetTestUrl( 76 FilePath(FilePath::kCurrentDirectory), 77 FilePath(FILE_PATH_LITERAL("title3.html")))); 78 79 // Add and navigate three tabs. 80 ui_test_utils::NavigateToURL(browser(), url1); 81 browser()->AddSelectedTabWithURL(url2, PageTransition::LINK); 82 ui_test_utils::WaitForNavigationInCurrentTab(browser()); 83 84 browser()->AddSelectedTabWithURL(url3, PageTransition::LINK); 85 ui_test_utils::WaitForNavigationInCurrentTab(browser()); 86 87 TabRestoreService* service = browser()->profile()->GetTabRestoreService(); 88 service->ClearEntries(); 89 90 browser()->window()->Close(); 91 92 // Expect a window with three tabs. 93 EXPECT_EQ(1U, service->entries().size()); 94 ASSERT_EQ(TabRestoreService::WINDOW, service->entries().front()->type); 95 const TabRestoreService::Window* window = 96 static_cast<TabRestoreService::Window*>(service->entries().front()); 97 EXPECT_EQ(3U, window->tabs.size()); 98 99 // Find the SessionID for entry2. Since the session service was destroyed, 100 // there is no guarantee that the SessionID for the tab has remained the same. 101 std::vector<TabRestoreService::Tab>::const_iterator it = window->tabs.begin(); 102 for ( ; it != window->tabs.end(); ++it) { 103 const TabRestoreService::Tab& tab = *it; 104 // If this tab held url2, then restore this single tab. 105 if (tab.navigations[0].virtual_url() == url2) { 106 service->RestoreEntryById(NULL, tab.id, false); 107 break; 108 } 109 } 110 111 // Make sure that the Window got updated. 112 EXPECT_EQ(1U, service->entries().size()); 113 ASSERT_EQ(TabRestoreService::WINDOW, service->entries().front()->type); 114 window = static_cast<TabRestoreService::Window*>(service->entries().front()); 115 EXPECT_EQ(2U, window->tabs.size()); 116} 117 118IN_PROC_BROWSER_TEST_F(SessionRestoreTest, WindowWithOneTab) { 119 GURL url(ui_test_utils::GetTestUrl( 120 FilePath(FilePath::kCurrentDirectory), 121 FilePath(FILE_PATH_LITERAL("title1.html")))); 122 123 // Add a single tab. 124 ui_test_utils::NavigateToURL(browser(), url); 125 126 TabRestoreService* service = browser()->profile()->GetTabRestoreService(); 127 service->ClearEntries(); 128 EXPECT_EQ(0U, service->entries().size()); 129 130 // Close the window. 131 browser()->window()->Close(); 132 133 // Expect the window to be converted to a tab by the TRS. 134 EXPECT_EQ(1U, service->entries().size()); 135 ASSERT_EQ(TabRestoreService::TAB, service->entries().front()->type); 136 const TabRestoreService::Tab* tab = 137 static_cast<TabRestoreService::Tab*>(service->entries().front()); 138 139 // Restore the tab. 140 service->RestoreEntryById(NULL, tab->id, false); 141 142 // Make sure the restore was successful. 143 EXPECT_EQ(0U, service->entries().size()); 144} 145